Browse Source

Code cleaning & better worker exit status detection

And better handling of entrypoint import failure...
Yann Weber 4 years ago
parent
commit
76af007c69
3 changed files with 26 additions and 59 deletions
  1. 3
    34
      src/main.c
  2. 10
    22
      src/pyworker.c
  3. 13
    3
      src/responder.c

+ 3
- 34
src/main.c View File

@@ -56,17 +56,6 @@ int main(int argc, char **argv)
56 56
 	int child_ret;
57 57
 	unsigned int emerg_sleep = 3;
58 58
 
59
-	/*
60
-	if(argc > 1 || !py_entrypoint)
61
-	{
62
-		usage();
63
-		return 1;
64
-		EARLY_ERR("Usage : PY_ENTRYPOINT='PATH.py' spawn-fcgi [OPTIONS] ");
65
-		EARLY_ERR(argv[0]);
66
-		EARLY_ERR("\n");
67
-		return 1;
68
-	}
69
-	*/
70 59
 	default_conf();
71 60
 	pyfcgi_logger_init();
72 61
 	pyfcgi_logger_set_ident("pyfcgi(main)");
@@ -75,25 +64,6 @@ int main(int argc, char **argv)
75 64
 		return 1;
76 65
 	}
77 66
 
78
-
79
-	//pyfcgi_logger_add("/tmp/pyfcgi.log", 0xFF, 0xFF, NULL);
80
-
81
-	/*
82
-	cahr *ident;
83
-	size_t ident_len;
84
-	ident_len = snprintf(NULL, 0, IDENT_FMT, getpid());
85
-	ident_len++;
86
-	ident = malloc(sizeof(char)*ident_len);
87
-	if(ident == NULL)
88
-	{
89
-		EARLY_ERR("Unable to allocate memory for ident string...\n");
90
-		return -1;
91
-	}
92
-	snprintf(ident, ident_len, IDENT_FMT, getpid());
93
-	pyfcgi_logger_enable_syslog(ident);
94
-	free(ident);
95
-	*/
96
-
97 67
 	pyfcgi_log(LOG_INFO, "New server started");
98 68
 
99 69
 	while(1)
@@ -135,7 +105,7 @@ int main(int argc, char **argv)
135 105
  * @section main_what What is PyFCGI ?
136 106
  * PyFCGI is a simple python3 fastcgi runner.
137 107
  *
138
- * Usage : *PY_ENTRYPOINT='foo' spawn-fcgi -d . -n src/pyfcgi -p 9000 -a 127.0.0.1*
108
+ * Usage : Usage : spawn-fcgi [OPTIONS] -- pyfcgi -e PYMODULE [-E PYFUN] [OPTIONS]
139 109
  *
140 110
  * To run foo.entrypoint() python function.
141 111
  *
@@ -147,9 +117,8 @@ int main(int argc, char **argv)
147 117
  * PyFCGI should be runned with spawn-fcgi (or something similar), allowing
148 118
  * to configure & forward environnement variables to libFCGI.
149 119
  *
150
- * For the moment no configuration files exists. You have to pass a 
151
- * PY_ENTRYPOINT containing an importable python module containing an
152
- * entrypoint() (no arguments for the moment) function.
120
+ * For the moment no configuration files exists. You have to pass arguments
121
+ * to pyfcgi using -- argument of spawnn-fcgi
153 122
  *
154 123
  * When called this function will have to send valid CGI data to the webserver
155 124
  * using sys.stdin (the print() function for exemple) like :

+ 10
- 22
src/pyworker.c View File

@@ -106,6 +106,13 @@ int work(int wrk_id, int semid)
106 106
 		       wrk_id, strerror(err));
107 107
 	}
108 108
 
109
+	if(!entry_fun) //but exit if import failed
110
+	{
111
+		pyfcgi_log(LOG_ALERT, "Unable to import entrypoint");
112
+		exit(PYFCGI_FATAL);
113
+
114
+	}
115
+
109 116
 	if(pipe(pipe_ctl) == -1)
110 117
 	{
111 118
 		err = errno;
@@ -130,18 +137,9 @@ int work(int wrk_id, int semid)
130 137
 		}
131 138
 
132 139
 		count++;
133
-		/*
134
-		pyfcgi_log(	LOG_INFO,
135
-			"Worker[%d] request %d", wrk_id, count);
136
-		*/
137 140
 		piper_args.out = out_stream;
138 141
 		//piper_args.req_id = count;
139 142
 		worker_piper_sigrcv = 0;
140
-		/*
141
-		pid_t pid = fork();
142
-		if(!pid) { exit(worker_piper(&piper_args)); }
143
-		*/
144
-
145 143
 
146 144
 		pid_t pid = clone(worker_piper, piper_stack + PIPER_STACK_SZ - 1,
147 145
 		                  CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID | \
@@ -156,16 +154,6 @@ int work(int wrk_id, int semid)
156 154
 			       wrk_id, count, strerror(err));
157 155
 			exit(err);
158 156
 		}
159
-		/*
160
-		if(!pid)
161
-		{
162
-			
163
-			worker_piper(wrk_id, count, pipe_out[0], pipe_err[0],
164
-			             pipe_ctl[1], out_stream);
165
-			//printf("Content-type: text/html\r\n\r\nHello world !\n");
166
-			exit(1);
167
-		}
168
-		*/
169 157
 		update_pyenv(py_osmod, envp);
170 158
 		//close(pipe_ctl[1]);
171 159
 		PyObject_CallObject(entry_fun, NULL);
@@ -463,7 +451,7 @@ PyObject* import_entrypoint()
463 451
 			"%s", getcwd(NULL, 256));
464 452
 		log_expt(LOG_ERR);
465 453
 		sleep(1);
466
-		Py_Exit(1);
454
+		return NULL;
467 455
 	}
468 456
 
469 457
 	// getting entrypoint function
@@ -476,14 +464,14 @@ PyObject* import_entrypoint()
476 464
 		pyfcgi_log(	LOG_CRIT,
477 465
 			"Unable to import object '%s' from '%s'",
478 466
 			PyFCGI_conf.py_entryfun, PyFCGI_conf.py_entrymod);
479
-		Py_Exit(2);
467
+		return NULL;
480 468
 	}
481 469
 	if(!PyCallable_Check(entry_fun))
482 470
 	{
483 471
 		pyfcgi_log( LOG_CRIT,
484 472
 			"When imported from '%s', '%s' was not a callable",
485 473
 			PyFCGI_conf.py_entrymod, PyFCGI_conf.py_entryfun);
486
-		Py_Exit(3);
474
+		return NULL;
487 475
 	}
488 476
 	return entry_fun;
489 477
 }

+ 13
- 3
src/responder.c View File

@@ -112,9 +112,19 @@ int responder_loop()
112 112
 			}
113 113
 			if(status)
114 114
 			{
115
-				pyfcgi_log(LOG_WARNING,
116
-				       "Worker[%d] exited with status %d",
117
-				       n, status);
115
+				if(WEXITSTATUS(status) & PYFCGI_FATAL)
116
+				{
117
+					pyfcgi_log(LOG_ALERT,
118
+						"Worker[%d] exited with status FATAL",
119
+						n);
120
+					//TODO : restart ?
121
+				}
122
+				else
123
+				{
124
+					pyfcgi_log(LOG_WARNING,
125
+						"Worker[%d] exited with status %d",
126
+						n, WEXITSTATUS(status));
127
+				}
118 128
 			}
119 129
 			else
120 130
 			{

Loading…
Cancel
Save