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 bool showVersionInHeader;
00183
00184
00185
00186
00187
00188
00189 unsigned long statThrottleRate;
00190
00191
00192
00193
00194
00195 string restartDir;
00196
00197
00198
00199
00200
00201
00202 Account::Rights rights;
00203
00204
00205 bool debugger;
00206
00207
00208
00209
00210 bool analytics;
00211
00212
00213
00214
00215
00216 AnalyticsLogPtr log;
00217
00218
00219
00220
00221
00222 bool initiateSession;
00223
00224
00225
00226
00227
00228 bool printExceptions;
00229
00230
00231
00232
00233
00234
00235
00236 PoolOptions() {
00237 appType = "rails";
00238 environment = "production";
00239 spawnMethod = "smart-lv2";
00240 frameworkSpawnerTimeout = -1;
00241 appSpawnerTimeout = -1;
00242 baseURI = "/";
00243 maxRequests = 0;
00244 minProcesses = 0;
00245 useGlobalQueue = false;
00246 showVersionInHeader = true;
00247 statThrottleRate = 0;
00248 rights = DEFAULT_BACKEND_ACCOUNT_RIGHTS;
00249 debugger = false;
00250 analytics = false;
00251 initiateSession = true;
00252 printExceptions = true;
00253
00254
00255 }
00256
00257
00258
00259
00260 PoolOptions(const string &appRoot,
00261 string appGroupName = "",
00262 const string &appType = "rails",
00263 const string &environment = "production",
00264 const string &spawnMethod = "smart-lv2",
00265 const string &user = "",
00266 const string &group = "",
00267 const string &defaultUser = "",
00268 const string &defaultGroup = "",
00269 long frameworkSpawnerTimeout = -1,
00270 long appSpawnerTimeout = -1,
00271 const string &baseURI = "/",
00272 unsigned long maxRequests = 0,
00273 unsigned long minProcesses = 0,
00274 bool useGlobalQueue = false,
00275 bool showVersionInHeader = true,
00276 unsigned long statThrottleRate = 0,
00277 const string &restartDir = "",
00278 Account::Rights rights = DEFAULT_BACKEND_ACCOUNT_RIGHTS,
00279 bool debugger = false,
00280 bool analytics = false,
00281 const AnalyticsLogPtr &log = AnalyticsLogPtr()
00282 ) {
00283 this->appRoot = appRoot;
00284 this->appGroupName = appGroupName;
00285 this->appType = appType;
00286 this->environment = environment;
00287 this->spawnMethod = spawnMethod;
00288 this->user = user;
00289 this->group = group;
00290 this->defaultUser = defaultUser;
00291 this->defaultGroup = defaultGroup;
00292 this->frameworkSpawnerTimeout = frameworkSpawnerTimeout;
00293 this->appSpawnerTimeout = appSpawnerTimeout;
00294 this->baseURI = baseURI;
00295 this->maxRequests = maxRequests;
00296 this->minProcesses = minProcesses;
00297 this->useGlobalQueue = useGlobalQueue;
00298 this->showVersionInHeader = showVersionInHeader;
00299 this->statThrottleRate = statThrottleRate;
00300 this->restartDir = restartDir;
00301 this->rights = rights;
00302 this->debugger = debugger;
00303 this->analytics = analytics;
00304 this->log = log;
00305 this->initiateSession = true;
00306 this->printExceptions = true;
00307
00308
00309 }
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332 PoolOptions(const vector<string> &vec, unsigned int startIndex = 0,
00333 AnalyticsLoggerPtr analyticsLogger = AnalyticsLoggerPtr()
00334 ) {
00335 int offset = 1;
00336 bool hasEnvVars;
00337
00338 appRoot = vec[startIndex + offset]; offset += 2;
00339 appGroupName = vec[startIndex + offset]; offset += 2;
00340 appType = vec[startIndex + offset]; offset += 2;
00341 environment = vec[startIndex + offset]; offset += 2;
00342 spawnMethod = vec[startIndex + offset]; offset += 2;
00343 user = vec[startIndex + offset]; offset += 2;
00344 group = vec[startIndex + offset]; offset += 2;
00345 defaultUser = vec[startIndex + offset]; offset += 2;
00346 defaultGroup = vec[startIndex + offset]; offset += 2;
00347 frameworkSpawnerTimeout = atol(vec[startIndex + offset]); offset += 2;
00348 appSpawnerTimeout = atol(vec[startIndex + offset]); offset += 2;
00349 baseURI = vec[startIndex + offset]; offset += 2;
00350 maxRequests = atol(vec[startIndex + offset]); offset += 2;
00351 minProcesses = atol(vec[startIndex + offset]); offset += 2;
00352 useGlobalQueue = vec[startIndex + offset] == "true"; offset += 2;
00353 showVersionInHeader = vec[startIndex + offset] == "true"; offset += 2;
00354 statThrottleRate = atol(vec[startIndex + offset]); offset += 2;
00355 restartDir = vec[startIndex + offset]; offset += 2;
00356 rights = (Account::Rights) atol(vec[startIndex + offset]);
00357 offset += 2;
00358 debugger = vec[startIndex + offset] == "true"; offset += 2;
00359 analytics = vec[startIndex + offset] == "true"; offset += 2;
00360 if (vec[startIndex + offset - 1] == "analytics_log_txn_id") {
00361 if (analyticsLogger != NULL) {
00362 string txnId = vec[startIndex + offset];
00363 string groupName = vec[startIndex + offset + 2];
00364 string category = vec[startIndex + offset + 4];
00365 string unionStationKey = vec[startIndex + offset + 6];
00366 log = analyticsLogger->continueTransaction(txnId,
00367 groupName, category, unionStationKey);
00368 }
00369 offset += 8;
00370 }
00371 initiateSession = vec[startIndex + offset] == "true"; offset += 2;
00372 printExceptions = vec[startIndex + offset] == "true"; offset += 2;
00373 hasEnvVars = vec[startIndex + offset] == "true"; offset += 2;
00374 if (hasEnvVars) {
00375 environmentVariables = ptr(new SimpleStringListCreator(vec[startIndex + offset]));
00376 }
00377 offset += 2;
00378
00379
00380 }
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391 void toVector(vector<string> &vec, bool storeEnvVars = true) const {
00392 if (vec.capacity() < vec.size() + 40) {
00393 vec.reserve(vec.size() + 40);
00394 }
00395 appendKeyValue (vec, "app_root", appRoot);
00396 appendKeyValue (vec, "app_group_name", getAppGroupName());
00397 appendKeyValue (vec, "app_type", appType);
00398 appendKeyValue (vec, "environment", environment);
00399 appendKeyValue (vec, "spawn_method", spawnMethod);
00400 appendKeyValue (vec, "user", user);
00401 appendKeyValue (vec, "group", group);
00402 appendKeyValue (vec, "default_user", defaultUser);
00403 appendKeyValue (vec, "default_group", defaultGroup);
00404 appendKeyValue2(vec, "framework_spawner_timeout", frameworkSpawnerTimeout);
00405 appendKeyValue2(vec, "app_spawner_timeout", appSpawnerTimeout);
00406 appendKeyValue (vec, "base_uri", baseURI);
00407 appendKeyValue3(vec, "max_requests", maxRequests);
00408 appendKeyValue3(vec, "min_processes", minProcesses);
00409 appendKeyValue4(vec, "use_global_queue", useGlobalQueue);
00410 appendKeyValue4(vec, "show_version_in_header", showVersionInHeader);
00411 appendKeyValue3(vec, "stat_throttle_rate", statThrottleRate);
00412 appendKeyValue (vec, "restart_dir", restartDir);
00413 appendKeyValue3(vec, "rights", rights);
00414 appendKeyValue4(vec, "debugger", debugger);
00415 appendKeyValue4(vec, "analytics", analytics);
00416 if (log) {
00417 appendKeyValue(vec, "analytics_log_txn_id", log->getTxnId());
00418 appendKeyValue(vec, "analytics_log_group_name", log->getGroupName());
00419 appendKeyValue(vec, "analytics_log_category", log->getCategory());
00420 appendKeyValue(vec, "union_station_key", log->getUnionStationKey());
00421 }
00422 appendKeyValue4(vec, "initiate_session", initiateSession);
00423 appendKeyValue4(vec, "print_exceptions", printExceptions);
00424 if (storeEnvVars) {
00425 appendKeyValue(vec, "has_environment_variables", "true");
00426 appendKeyValue(vec, "environment_variables", serializeEnvironmentVariables());
00427 } else {
00428 appendKeyValue(vec, "has_environment_variables", "false");
00429 appendKeyValue(vec, "environment_variables", "");
00430 }
00431
00432
00433 }
00434
00435 PoolOptions own() const {
00436 PoolOptions copy = *this;
00437 if (copy.environmentVariables != NULL) {
00438 copy.environmentVariables->getItems();
00439 }
00440 copy.log.reset();
00441 return copy;
00442 }
00443
00444
00445
00446
00447
00448 string getAppGroupName() const {
00449 if (appGroupName.empty()) {
00450 return appRoot;
00451 } else {
00452 return appGroupName;
00453 }
00454 }
00455
00456
00457
00458
00459
00460
00461
00462 string serializeEnvironmentVariables() const {
00463 vector<string>::const_iterator it, end;
00464 string result;
00465
00466 if (environmentVariables) {
00467 result.reserve(1024);
00468
00469 StringListPtr items = environmentVariables->getItems();
00470 end = items->end();
00471
00472 for (it = items->begin(); it != end; it++) {
00473 result.append(*it);
00474 result.append(1, '\0');
00475 it++;
00476 result.append(*it);
00477 result.append(1, '\0');
00478 }
00479 }
00480 return Base64::encode(result);
00481 }
00482
00483 private:
00484 static inline void
00485 appendKeyValue(vector<string> &vec, const char *key, const string &value) {
00486 vec.push_back(key);
00487 vec.push_back(const_cast<string &>(value));
00488 }
00489
00490 static inline void
00491 appendKeyValue(vector<string> &vec, const char *key, const char *value) {
00492 vec.push_back(key);
00493 vec.push_back(value);
00494 }
00495
00496 static inline void
00497 appendKeyValue2(vector<string> &vec, const char *key, long value) {
00498 vec.push_back(key);
00499 vec.push_back(toString(value));
00500 }
00501
00502 static inline void
00503 appendKeyValue3(vector<string> &vec, const char *key, unsigned long value) {
00504 vec.push_back(key);
00505 vec.push_back(toString(value));
00506 }
00507
00508 static inline void
00509 appendKeyValue4(vector<string> &vec, const char *key, bool value) {
00510 vec.push_back(key);
00511 vec.push_back(value ? "true" : "false");
00512 }
00513 };
00514
00515 }
00516
00517 #endif
00518