00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef _PASSENGER_SPAWN_OPTIONS_H_
00026 #define _PASSENGER_SPAWN_OPTIONS_H_
00027
00028 #include <string>
00029 #include <vector>
00030 #include "Account.h"
00031 #include "Logging.h"
00032 #include "Constants.h"
00033 #include "StringListCreator.h"
00034
00035 namespace Passenger {
00036
00037 using namespace std;
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 struct PoolOptions {
00068
00069
00070
00071
00072
00073 string appRoot;
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 string appGroupName;
00085
00086
00087 string appType;
00088
00089
00090
00091
00092
00093 string environment;
00094
00095
00096
00097
00098
00099
00100
00101 string spawnMethod;
00102
00103
00104 string user;
00105
00106 string group;
00107
00108 string defaultUser;
00109
00110 string defaultGroup;
00111
00112
00113
00114
00115
00116
00117
00118
00119 long frameworkSpawnerTimeout;
00120
00121
00122
00123
00124
00125
00126
00127
00128 long appSpawnerTimeout;
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144 StringListCreatorPtr environmentVariables;
00145
00146
00147
00148
00149
00150
00151
00152 string baseURI;
00153
00154
00155
00156
00157
00158 unsigned long maxRequests;
00159
00160
00161
00162
00163
00164 unsigned long minProcesses;
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176 bool useGlobalQueue;
00177
00178
00179
00180
00181
00182
00183 unsigned long statThrottleRate;
00184
00185
00186
00187
00188
00189 string restartDir;
00190
00191
00192
00193
00194
00195
00196 Account::Rights rights;
00197
00198
00199 bool debugger;
00200
00201
00202
00203
00204
00205 AnalyticsLogPtr log;
00206
00207
00208
00209
00210
00211 bool initiateSession;
00212
00213
00214
00215
00216
00217 bool printExceptions;
00218
00219
00220
00221
00222
00223
00224
00225 PoolOptions() {
00226 appType = "rails";
00227 environment = "production";
00228 spawnMethod = "smart-lv2";
00229 frameworkSpawnerTimeout = -1;
00230 appSpawnerTimeout = -1;
00231 baseURI = "/";
00232 maxRequests = 0;
00233 minProcesses = 0;
00234 useGlobalQueue = false;
00235 statThrottleRate = 0;
00236 rights = DEFAULT_BACKEND_ACCOUNT_RIGHTS;
00237 debugger = false;
00238 initiateSession = true;
00239 printExceptions = true;
00240
00241
00242 }
00243
00244
00245
00246
00247 PoolOptions(const string &appRoot,
00248 string appGroupName = "",
00249 const string &appType = "rails",
00250 const string &environment = "production",
00251 const string &spawnMethod = "smart-lv2",
00252 const string &user = "",
00253 const string &group = "",
00254 const string &defaultUser = "",
00255 const string &defaultGroup = "",
00256 long frameworkSpawnerTimeout = -1,
00257 long appSpawnerTimeout = -1,
00258 const string &baseURI = "/",
00259 unsigned long maxRequests = 0,
00260 unsigned long minProcesses = 0,
00261 bool useGlobalQueue = false,
00262 unsigned long statThrottleRate = 0,
00263 const string &restartDir = "",
00264 Account::Rights rights = DEFAULT_BACKEND_ACCOUNT_RIGHTS,
00265 bool debugger = false,
00266 const AnalyticsLogPtr &log = AnalyticsLogPtr()
00267 ) {
00268 this->appRoot = appRoot;
00269 this->appGroupName = appGroupName;
00270 this->appType = appType;
00271 this->environment = environment;
00272 this->spawnMethod = spawnMethod;
00273 this->user = user;
00274 this->group = group;
00275 this->defaultUser = defaultUser;
00276 this->defaultGroup = defaultGroup;
00277 this->frameworkSpawnerTimeout = frameworkSpawnerTimeout;
00278 this->appSpawnerTimeout = appSpawnerTimeout;
00279 this->baseURI = baseURI;
00280 this->maxRequests = maxRequests;
00281 this->minProcesses = minProcesses;
00282 this->useGlobalQueue = useGlobalQueue;
00283 this->statThrottleRate = statThrottleRate;
00284 this->restartDir = restartDir;
00285 this->rights = rights;
00286 this->debugger = debugger;
00287 this->log = log;
00288 this->initiateSession = true;
00289 this->printExceptions = true;
00290
00291
00292 }
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315 PoolOptions(const vector<string> &vec, unsigned int startIndex = 0,
00316 AnalyticsLoggerPtr analyticsLogger = AnalyticsLoggerPtr()
00317 ) {
00318 int offset = 1;
00319 bool hasEnvVars;
00320
00321 appRoot = vec[startIndex + offset]; offset += 2;
00322 appGroupName = vec[startIndex + offset]; offset += 2;
00323 appType = vec[startIndex + offset]; offset += 2;
00324 environment = vec[startIndex + offset]; offset += 2;
00325 spawnMethod = vec[startIndex + offset]; offset += 2;
00326 user = vec[startIndex + offset]; offset += 2;
00327 group = vec[startIndex + offset]; offset += 2;
00328 defaultUser = vec[startIndex + offset]; offset += 2;
00329 defaultGroup = vec[startIndex + offset]; offset += 2;
00330 frameworkSpawnerTimeout = atol(vec[startIndex + offset]); offset += 2;
00331 appSpawnerTimeout = atol(vec[startIndex + offset]); offset += 2;
00332 baseURI = vec[startIndex + offset]; offset += 2;
00333 maxRequests = atol(vec[startIndex + offset]); offset += 2;
00334 minProcesses = atol(vec[startIndex + offset]); offset += 2;
00335 useGlobalQueue = vec[startIndex + offset] == "true"; offset += 2;
00336 statThrottleRate = atol(vec[startIndex + offset]); offset += 2;
00337 restartDir = vec[startIndex + offset]; offset += 2;
00338 rights = (Account::Rights) atol(vec[startIndex + offset]);
00339 offset += 2;
00340 debugger = vec[startIndex + offset] == "true"; offset += 2;
00341 if (vec[startIndex + offset - 1] == "analytics_log_group_name") {
00342 if (analyticsLogger != NULL) {
00343 string groupName = vec[startIndex + offset];
00344 string txnId = vec[startIndex + offset + 2];
00345 log = analyticsLogger->continueTransaction(groupName, txnId);
00346 }
00347 offset += 4;
00348 }
00349 initiateSession = vec[startIndex + offset] == "true"; offset += 2;
00350 printExceptions = vec[startIndex + offset] == "true"; offset += 2;
00351 hasEnvVars = vec[startIndex + offset] == "true"; offset += 2;
00352 if (hasEnvVars) {
00353 environmentVariables = ptr(new SimpleStringListCreator(vec[startIndex + offset]));
00354 }
00355 offset += 2;
00356
00357
00358 }
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369 void toVector(vector<string> &vec, bool storeEnvVars = true) const {
00370 if (vec.capacity() < vec.size() + 40) {
00371 vec.reserve(vec.size() + 40);
00372 }
00373 appendKeyValue (vec, "app_root", appRoot);
00374 appendKeyValue (vec, "app_group_name", getAppGroupName());
00375 appendKeyValue (vec, "app_type", appType);
00376 appendKeyValue (vec, "environment", environment);
00377 appendKeyValue (vec, "spawn_method", spawnMethod);
00378 appendKeyValue (vec, "user", user);
00379 appendKeyValue (vec, "group", group);
00380 appendKeyValue (vec, "default_user", defaultUser);
00381 appendKeyValue (vec, "default_group", defaultGroup);
00382 appendKeyValue2(vec, "framework_spawner_timeout", frameworkSpawnerTimeout);
00383 appendKeyValue2(vec, "app_spawner_timeout", appSpawnerTimeout);
00384 appendKeyValue (vec, "base_uri", baseURI);
00385 appendKeyValue3(vec, "max_requests", maxRequests);
00386 appendKeyValue3(vec, "min_processes", minProcesses);
00387 appendKeyValue4(vec, "use_global_queue", useGlobalQueue);
00388 appendKeyValue3(vec, "stat_throttle_rate", statThrottleRate);
00389 appendKeyValue (vec, "restart_dir", restartDir);
00390 appendKeyValue3(vec, "rights", rights);
00391 appendKeyValue4(vec, "debugger", debugger);
00392 if (log) {
00393 appendKeyValue(vec, "analytics_log_group_name", log->getGroupName());
00394 appendKeyValue(vec, "analytics_log_id", log->getTxnId());
00395 }
00396 appendKeyValue4(vec, "initiate_session", initiateSession);
00397 appendKeyValue4(vec, "print_exceptions", printExceptions);
00398 if (storeEnvVars) {
00399 appendKeyValue(vec, "has_environment_variables", "true");
00400 appendKeyValue(vec, "environment_variables", serializeEnvironmentVariables());
00401 } else {
00402 appendKeyValue(vec, "has_environment_variables", "false");
00403 appendKeyValue(vec, "environment_variables", "");
00404 }
00405
00406
00407 }
00408
00409
00410
00411
00412
00413 string getAppGroupName() const {
00414 if (appGroupName.empty()) {
00415 return appRoot;
00416 } else {
00417 return appGroupName;
00418 }
00419 }
00420
00421
00422
00423
00424
00425
00426
00427 string serializeEnvironmentVariables() const {
00428 vector<string>::const_iterator it, end;
00429 string result;
00430
00431 if (environmentVariables) {
00432 result.reserve(1024);
00433
00434 StringListPtr items = environmentVariables->getItems();
00435 end = items->end();
00436
00437 for (it = items->begin(); it != end; it++) {
00438 result.append(*it);
00439 result.append(1, '\0');
00440 it++;
00441 result.append(*it);
00442 result.append(1, '\0');
00443 }
00444 }
00445 return Base64::encode(result);
00446 }
00447
00448 private:
00449 static inline void
00450 appendKeyValue(vector<string> &vec, const char *key, const string &value) {
00451 vec.push_back(key);
00452 vec.push_back(const_cast<string &>(value));
00453 }
00454
00455 static inline void
00456 appendKeyValue(vector<string> &vec, const char *key, const char *value) {
00457 vec.push_back(key);
00458 vec.push_back(value);
00459 }
00460
00461 static inline void
00462 appendKeyValue2(vector<string> &vec, const char *key, long value) {
00463 vec.push_back(key);
00464 vec.push_back(toString(value));
00465 }
00466
00467 static inline void
00468 appendKeyValue3(vector<string> &vec, const char *key, unsigned long value) {
00469 vec.push_back(key);
00470 vec.push_back(toString(value));
00471 }
00472
00473 static inline void
00474 appendKeyValue4(vector<string> &vec, const char *key, bool value) {
00475 vec.push_back(key);
00476 vec.push_back(value ? "true" : "false");
00477 }
00478 };
00479
00480 }
00481
00482 #endif
00483