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