From c72ddc9dd8761f8b7293f5dd45b0a8f858f50a0e Mon Sep 17 00:00:00 2001 From: "roberto@sirius" Date: Wed, 8 Feb 2012 11:01:04 +0100 Subject: [PATCH] added command mode --- plugins/python/python_plugin.c | 29 ++++++++++++++++++++++++----- plugins/python/pyutils.c | 10 +++++----- plugins/python/uwsgi_python.h | 3 +++ uwsgi.c | 22 ++++++++++++++++------ uwsgi.h | 1 + 5 files changed, 49 insertions(+), 16 deletions(-) diff --git a/plugins/python/python_plugin.c b/plugins/python/python_plugin.c index 4f25d172..62a55a21 100644 --- a/plugins/python/python_plugin.c +++ b/plugins/python/python_plugin.c @@ -34,6 +34,12 @@ void uwsgi_opt_pyshell(char *opt, char *value, void *foobar) { } } +void uwsgi_opt_pyrun(char *opt, char *value, void *foobar) { + uwsgi.honour_stdin = 1; + uwsgi.command_mode = 1; + up.pyrun = value; +} + #ifdef UWSGI_INI void uwsgi_opt_ini_paste(char *opt, char *value, void *foobar) { @@ -110,11 +116,10 @@ struct uwsgi_option uwsgi_python_options[] = { #endif {"pyshell", no_argument, 0, "run an interactive python shell in the uWSGI environment", uwsgi_opt_pyshell, NULL, 0}, {"pyshell-oneshot", no_argument, 0, "run an interactive python shell in the uWSGI environment (one-shot variant)", uwsgi_opt_pyshell, NULL, 0}, -/* - FUTURE additions... - {"python", required_argument, 0, ...}, - {"py", required_argument, 0, ...}, -*/ + + {"python", required_argument, 0, "run a python script in the uWSGI environment", uwsgi_opt_pyrun, NULL, 0}, + {"py", required_argument, 0, "run a python script in the uWSGI environment", uwsgi_opt_pyrun, NULL, 0}, + {"pyrun", required_argument, 0, "run a python script in the uWSGI environment", uwsgi_opt_pyrun, NULL, 0}, {0, 0, 0, 0, 0, 0, 0}, }; @@ -1423,8 +1428,22 @@ void uwsgi_python_fixup() { } void uwsgi_python_hijack(void) { + // the pyshell will be execute only in the first worker + FILE *pyfile; + if (up.pyrun) { + uwsgi.workers[uwsgi.mywid].hijacked = 1; + pyfile = fopen(up.pyrun, "r"); + if (!pyfile) { + uwsgi_error_open(up.pyrun); + exit(1); + } + PyRun_SimpleFile(pyfile, up.pyrun); + // could be never executed + exit(0); + } + #ifndef UWSGI_PYPY if (up.pyshell_oneshot && uwsgi.workers[uwsgi.mywid].hijacked_count > 0) { uwsgi.workers[uwsgi.mywid].hijacked = 0; diff --git a/plugins/python/pyutils.c b/plugins/python/pyutils.c index 49afa174..c02fa48d 100644 --- a/plugins/python/pyutils.c +++ b/plugins/python/pyutils.c @@ -97,7 +97,8 @@ void init_pyargv() { up.py_argv[0] = pname; - if (up.argv != NULL) { + + if (up.argv) { up.argc = 1; #ifdef PYTHREE @@ -120,12 +121,11 @@ void init_pyargv() { #endif up.argc++; } - } } -#ifndef UWSGI_PYPY - PySys_SetArgv(up.argc, up.py_argv); -#endif + } + + PySys_SetArgv(up.argc, up.py_argv); PyObject *sys_dict = get_uwsgi_pydict("sys"); if (!sys_dict) { diff --git a/plugins/python/uwsgi_python.h b/plugins/python/uwsgi_python.h index 06d14458..5107a98b 100644 --- a/plugins/python/uwsgi_python.h +++ b/plugins/python/uwsgi_python.h @@ -112,6 +112,7 @@ struct uwsgi_python { int pyshell; int pyshell_oneshot; + struct uwsgi_string_list *python_path; struct uwsgi_string_list *import_list; @@ -169,6 +170,8 @@ struct uwsgi_python { PyObject *after_req_hook; PyObject *after_req_hook_args; + char *pyrun; + }; diff --git a/uwsgi.c b/uwsgi.c index a22b96bf..e0214b91 100644 --- a/uwsgi.c +++ b/uwsgi.c @@ -189,6 +189,7 @@ static struct uwsgi_option uwsgi_base_options[] = { //{"ldap-schema-ldif", no_argument, 0, LONG_ARGS_LDAP_SCHEMA_LDIF,0}, #endif {"no-server", no_argument, 0, "force no-server mode", uwsgi_opt_true, &uwsgi.no_server,0}, + {"command-mode", no_argument, 0, "force command mode", uwsgi_opt_true, &uwsgi.command_mode, UWSGI_OPT_IMMEDIATE}, {"no-defer-accept", no_argument, 0, "disable deferred-accept on sockets", uwsgi_opt_true, &uwsgi.no_defer_accept,0}, {"limit-as", required_argument, 0, "limit processes address space/vsz", uwsgi_opt_set_megabytes, &uwsgi.rl.rlim_max,0}, {"reload-on-as", required_argument, 0, "reload if address space is higher than specified megabytes", uwsgi_opt_set_megabytes, &uwsgi.reload_on_as, UWSGI_OPT_MEMORY}, @@ -1663,7 +1664,7 @@ int uwsgi_start(void *v_argv) { fclose(pidfile2); } - if (!uwsgi.master_process) { + if (!uwsgi.master_process && !uwsgi.command_mode) { uwsgi_log_initial("*** WARNING: you are running uWSGI without its master process manager ***\n"); } #ifndef __OpenBSD__ @@ -2190,7 +2191,7 @@ skipzero: // initialize request plugin only if workers or master are available - if (uwsgi.sockets || uwsgi.master_process || uwsgi.no_server) { + if (uwsgi.sockets || uwsgi.master_process || uwsgi.no_server || uwsgi.command_mode) { for (i = 0; i < 0xFF; i++) { if (uwsgi.p[i]->init) { uwsgi.p[i]->init(); @@ -2240,7 +2241,7 @@ skipzero: } #endif - if (!uwsgi.sockets && !uwsgi.gateways_cnt && !uwsgi.no_server && !uwsgi.udp_socket && !uwsgi.emperor_dir) { + if (!uwsgi.sockets && !uwsgi.gateways_cnt && !uwsgi.no_server && !uwsgi.udp_socket && !uwsgi.emperor_dir && !uwsgi.command_mode) { uwsgi_log("The -s/--socket option is missing and stdin is not a socket.\n"); exit(1); } @@ -2253,6 +2254,11 @@ skipzero: if (!uwsgi.sockets) uwsgi.numproc = 0; + if (uwsgi.command_mode) { + uwsgi.sockets = NULL; + uwsgi.numproc = 1; + } + #ifdef UWSGI_DEBUG uwsgi_sock = uwsgi.sockets; while (uwsgi_sock) { @@ -2277,7 +2283,8 @@ skipzero: #ifndef UNBIT - uwsgi_log("your server socket listen backlog is limited to %d connections\n", uwsgi.listen_queue); + if (uwsgi.sockets) + uwsgi_log("your server socket listen backlog is limited to %d connections\n", uwsgi.listen_queue); #endif if (uwsgi.crons) { @@ -2387,7 +2394,10 @@ skipzero: } - if (!uwsgi.numproc) { + if (uwsgi.command_mode) { + uwsgi_log("*** Operational MODE: command ***\n"); + } + else if (!uwsgi.numproc) { uwsgi_log("*** Operational MODE: no-workers ***\n"); } else if (uwsgi.threads > 1) { @@ -3165,7 +3175,7 @@ void uwsgi_init_all_apps() { } // no app initialized and virtualhosting enabled - if (uwsgi_apps_cnt == 0 && uwsgi.numproc > 0) { + if (uwsgi_apps_cnt == 0 && uwsgi.numproc > 0 && !uwsgi.command_mode) { uwsgi_log("*** no app loaded. going in full dynamic mode ***\n"); } diff --git a/uwsgi.h b/uwsgi.h index ad94c1d2..3c7bc03d 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -1337,6 +1337,7 @@ struct uwsgi_server { int vacuum; int no_server; + int command_mode; #ifdef UWSGI_LDAP char *ldap;