Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 334 lines (274 sloc) 7.1 kb
bed7682 useful XML support, first commit of uwsgiconfig.py
roberto@sirius authored
1 #ifdef UWSGI_SPOOLER
d3058b9 added spooler.c
roberto@sirius authored
2 #include "uwsgi.h"
3
4 #include <dirent.h>
5
6
18efb25 finally a worker (almost) solid spooler. Ready for beta1
roberto@voldemort authored
7 extern char *spool_dir;
d3058b9 added spooler.c
roberto@sirius authored
8
18efb25 finally a worker (almost) solid spooler. Ready for beta1
roberto@voldemort authored
9
aa9c533 re-indent all the code
roberto@sirius authored
10 int spool_request(char *filename, int rn, char *buffer, int size) {
d3058b9 added spooler.c
roberto@sirius authored
11
e0882ed big update: hooks and plugins
roberto@sirius authored
12 char hostname[256 + 1];
13 struct timeval tv;
d3058b9 added spooler.c
roberto@sirius authored
14 int fd;
bed7682 useful XML support, first commit of uwsgiconfig.py
roberto@sirius authored
15 struct uwsgi_header uh;
d3058b9 added spooler.c
roberto@sirius authored
16
aa9c533 re-indent all the code
roberto@sirius authored
17 if (gethostname(hostname, 256)) {
18 perror("gethostname()");
e0882ed big update: hooks and plugins
roberto@sirius authored
19 return 0;
20 }
d3058b9 added spooler.c
roberto@sirius authored
21
aa9c533 re-indent all the code
roberto@sirius authored
22 gettimeofday(&tv, NULL);
d3058b9 added spooler.c
roberto@sirius authored
23
e0882ed big update: hooks and plugins
roberto@sirius authored
24 hostname[256] = 0;
d3058b9 added spooler.c
roberto@sirius authored
25
aa9c533 re-indent all the code
roberto@sirius authored
26 if (snprintf(filename, 1024, "%s/uwsgi_spoolfile_on_%s_%d_%d_%llu_%llu", spool_dir, hostname, getpid(), rn, (unsigned long long) tv.tv_sec, (unsigned long long) tv.tv_usec) <= 0) {
d3058b9 added spooler.c
roberto@sirius authored
27 return 0;
28 }
29
aa9c533 re-indent all the code
roberto@sirius authored
30 fd = open(filename, O_CREAT | O_EXCL | O_WRONLY, S_IRUSR | S_IWUSR);
d3058b9 added spooler.c
roberto@sirius authored
31 if (fd < 0) {
aa9c533 re-indent all the code
roberto@sirius authored
32 perror("open()");
e0882ed big update: hooks and plugins
roberto@sirius authored
33 return 0;
d3058b9 added spooler.c
roberto@sirius authored
34 }
35
ce65601 fix OpenSolaris file locking
roberto@opensolaris authored
36 #ifdef __sun__
aa9c533 re-indent all the code
roberto@sirius authored
37 if (lockf(fd, F_LOCK, 0)) {
38 perror("lockf()");
ce65601 fix OpenSolaris file locking
roberto@opensolaris authored
39 #else
aa9c533 re-indent all the code
roberto@sirius authored
40 if (flock(fd, LOCK_EX)) {
41 perror("flock()");
ce65601 fix OpenSolaris file locking
roberto@opensolaris authored
42 #endif
aa9c533 re-indent all the code
roberto@sirius authored
43 close(fd);
ec077bc fixed spooler, ping implementation, uwsgi.applications and uwsgi.applist...
roberto@voldemort authored
44 return 0;
d3058b9 added spooler.c
roberto@sirius authored
45 }
46
e0882ed big update: hooks and plugins
roberto@sirius authored
47 uh.modifier1 = 17;
48 uh.modifier2 = 0;
bed7682 useful XML support, first commit of uwsgiconfig.py
roberto@sirius authored
49 uh.pktsize = (uint16_t) size;
18efb25 finally a worker (almost) solid spooler. Ready for beta1
roberto@voldemort authored
50 #ifdef __BIG_ENDIAN__
aa9c533 re-indent all the code
roberto@sirius authored
51 uh.pktsize = uwsgi_swap16(uh.pktsize);
18efb25 finally a worker (almost) solid spooler. Ready for beta1
roberto@voldemort authored
52 #endif
53
aa9c533 re-indent all the code
roberto@sirius authored
54 if (write(fd, &uh, 4) != 4) {
e0882ed big update: hooks and plugins
roberto@sirius authored
55 goto clear;
18efb25 finally a worker (almost) solid spooler. Ready for beta1
roberto@voldemort authored
56 }
57
aa9c533 re-indent all the code
roberto@sirius authored
58 if (write(fd, buffer, size) != size) {
e0882ed big update: hooks and plugins
roberto@sirius authored
59 goto clear;
d3058b9 added spooler.c
roberto@sirius authored
60 }
61
aa9c533 re-indent all the code
roberto@sirius authored
62 close(fd);
d3058b9 added spooler.c
roberto@sirius authored
63
aa9c533 re-indent all the code
roberto@sirius authored
64 fprintf(stderr, "written %d bytes to spool file %s.\n", size + 4, filename);
18efb25 finally a worker (almost) solid spooler. Ready for beta1
roberto@voldemort authored
65
d3058b9 added spooler.c
roberto@sirius authored
66 return 1;
67
68
e0882ed big update: hooks and plugins
roberto@sirius authored
69 clear:
70
aa9c533 re-indent all the code
roberto@sirius authored
71 perror("write()");
72 unlink(filename);
73 close(fd);
d3058b9 added spooler.c
roberto@sirius authored
74 return 0;
75 }
76
aa9c533 re-indent all the code
roberto@sirius authored
77 void spooler(PyObject * uwsgi_module) {
e0882ed big update: hooks and plugins
roberto@sirius authored
78 DIR *sdir;
d3058b9 added spooler.c
roberto@sirius authored
79 struct dirent *dp;
e0882ed big update: hooks and plugins
roberto@sirius authored
80 PyObject *uwsgi_module_dict, *spooler_callable, *spool_result, *spool_tuple, *spool_env;
81 int spool_fd;
82 uint16_t uwstrlen;
18efb25 finally a worker (almost) solid spooler. Ready for beta1
roberto@voldemort authored
83 int rlen = 0;
e0882ed big update: hooks and plugins
roberto@sirius authored
84 int datasize;
18efb25 finally a worker (almost) solid spooler. Ready for beta1
roberto@voldemort authored
85
bed7682 useful XML support, first commit of uwsgiconfig.py
roberto@sirius authored
86 struct uwsgi_header uh;
d3058b9 added spooler.c
roberto@sirius authored
87
88 char *key;
89 char *val;
90
aa9c533 re-indent all the code
roberto@sirius authored
91 uwsgi_module_dict = PyModule_GetDict(uwsgi_module);
e0882ed big update: hooks and plugins
roberto@sirius authored
92 if (!uwsgi_module_dict) {
aa9c533 re-indent all the code
roberto@sirius authored
93 fprintf(stderr, "could not get uwsgi module __dict__\n");
94 exit(1);
e0882ed big update: hooks and plugins
roberto@sirius authored
95 }
d3058b9 added spooler.c
roberto@sirius authored
96
97
aa9c533 re-indent all the code
roberto@sirius authored
98 spool_tuple = PyTuple_New(1);
d3058b9 added spooler.c
roberto@sirius authored
99
100 if (!spool_tuple) {
aa9c533 re-indent all the code
roberto@sirius authored
101 fprintf(stderr, "could not create spooler tuple.\n");
102 exit(1);
d3058b9 added spooler.c
roberto@sirius authored
103 }
104
105
aa9c533 re-indent all the code
roberto@sirius authored
106 spool_env = PyDict_New();
18efb25 finally a worker (almost) solid spooler. Ready for beta1
roberto@voldemort authored
107 if (!spool_env) {
aa9c533 re-indent all the code
roberto@sirius authored
108 fprintf(stderr, "could not create spooler env.\n");
109 exit(1);
18efb25 finally a worker (almost) solid spooler. Ready for beta1
roberto@voldemort authored
110 }
111
aa9c533 re-indent all the code
roberto@sirius authored
112 if (PyTuple_SetItem(spool_tuple, 0, spool_env)) {
113 PyErr_Print();
114 exit(1);
18efb25 finally a worker (almost) solid spooler. Ready for beta1
roberto@voldemort authored
115 }
e0882ed big update: hooks and plugins
roberto@sirius authored
116
aa9c533 re-indent all the code
roberto@sirius authored
117 if (chdir(spool_dir)) {
118 perror("chdir()");
119 exit(1);
d3058b9 added spooler.c
roberto@sirius authored
120 }
121
4ee9b36 lower the scheduler priority of the spooler process
roberto@sirius authored
122 // asked by Marco Beri
aa9c533 re-indent all the code
roberto@sirius authored
123 fprintf(stderr, "lowering spooler priority to %d\n", PRIO_MAX);
4ee9b36 lower the scheduler priority of the spooler process
roberto@sirius authored
124 setpriority(PRIO_PROCESS, getpid(), PRIO_MAX);
125
e0882ed big update: hooks and plugins
roberto@sirius authored
126 for (;;) {
aa9c533 re-indent all the code
roberto@sirius authored
127 sdir = opendir(".");
d3058b9 added spooler.c
roberto@sirius authored
128 if (sdir) {
aa9c533 re-indent all the code
roberto@sirius authored
129 while ((dp = readdir(sdir)) != NULL) {
130 if (!strncmp("uwsgi_spoolfile_on_", dp->d_name, 19)) {
6b3c11f Official support for OpenSolaris
roberto@localhost authored
131 struct stat sf_lstat;
aa9c533 re-indent all the code
roberto@sirius authored
132 if (lstat(dp->d_name, &sf_lstat)) {
6b3c11f Official support for OpenSolaris
roberto@localhost authored
133 continue;
134 }
aa9c533 re-indent all the code
roberto@sirius authored
135 if (!S_ISREG(sf_lstat.st_mode)) {
6b3c11f Official support for OpenSolaris
roberto@localhost authored
136 continue;
137 }
aa9c533 re-indent all the code
roberto@sirius authored
138 if (!access(dp->d_name, R_OK | W_OK)) {
139 fprintf(stderr, "managing spool request %s...\n", dp->d_name);
d3058b9 added spooler.c
roberto@sirius authored
140
aa9c533 re-indent all the code
roberto@sirius authored
141 spooler_callable = PyDict_GetItemString(uwsgi_module_dict, "spooler");
95b7042 fixes for osx
roberto@fierobecco authored
142 if (!spooler_callable) {
aa9c533 re-indent all the code
roberto@sirius authored
143 fprintf(stderr, "you have to define uwsgi.spooler to use the spooler !!!\n");
95b7042 fixes for osx
roberto@fierobecco authored
144 continue;
145 }
146
aa9c533 re-indent all the code
roberto@sirius authored
147 spool_fd = open(dp->d_name, O_RDONLY);
18efb25 finally a worker (almost) solid spooler. Ready for beta1
roberto@voldemort authored
148 if (spool_fd < 0) {
aa9c533 re-indent all the code
roberto@sirius authored
149 perror("open()");
18efb25 finally a worker (almost) solid spooler. Ready for beta1
roberto@voldemort authored
150 continue;
e0882ed big update: hooks and plugins
roberto@sirius authored
151 }
18efb25 finally a worker (almost) solid spooler. Ready for beta1
roberto@voldemort authored
152
ce65601 fix OpenSolaris file locking
roberto@opensolaris authored
153 #ifdef __sun__
aa9c533 re-indent all the code
roberto@sirius authored
154 if (lockf(spool_fd, F_LOCK, 0)) {
155 perror("lockf()");
ce65601 fix OpenSolaris file locking
roberto@opensolaris authored
156 #else
aa9c533 re-indent all the code
roberto@sirius authored
157 if (flock(spool_fd, LOCK_EX)) {
158 perror("flock()");
ce65601 fix OpenSolaris file locking
roberto@opensolaris authored
159 #endif
aa9c533 re-indent all the code
roberto@sirius authored
160 close(spool_fd);
ec077bc fixed spooler, ping implementation, uwsgi.applications and uwsgi.applist...
roberto@voldemort authored
161 continue;
162 }
163
aa9c533 re-indent all the code
roberto@sirius authored
164 if (read(spool_fd, &uh, 4) != 4) {
165 perror("read()");
166 close(spool_fd);
d3058b9 added spooler.c
roberto@sirius authored
167 continue;
168 }
169
e0882ed big update: hooks and plugins
roberto@sirius authored
170 #ifdef __BIG_ENDIAN__
aa9c533 re-indent all the code
roberto@sirius authored
171 uh.pktsize = uwsgi_swap16(uh.pktsize);
e0882ed big update: hooks and plugins
roberto@sirius authored
172 #endif
18efb25 finally a worker (almost) solid spooler. Ready for beta1
roberto@voldemort authored
173
e0882ed big update: hooks and plugins
roberto@sirius authored
174 datasize = 0;
175
bed7682 useful XML support, first commit of uwsgiconfig.py
roberto@sirius authored
176 while (datasize < uh.pktsize) {
aa9c533 re-indent all the code
roberto@sirius authored
177 rlen = read(spool_fd, &uwstrlen, 2);
18efb25 finally a worker (almost) solid spooler. Ready for beta1
roberto@voldemort authored
178 if (rlen != 2) {
aa9c533 re-indent all the code
roberto@sirius authored
179 perror("read()");
e0882ed big update: hooks and plugins
roberto@sirius authored
180 goto next_spool;
18efb25 finally a worker (almost) solid spooler. Ready for beta1
roberto@voldemort authored
181 }
e0882ed big update: hooks and plugins
roberto@sirius authored
182 datasize += rlen;
d3058b9 added spooler.c
roberto@sirius authored
183 key = NULL;
184 val = NULL;
185 if (uwstrlen > 0) {
aa9c533 re-indent all the code
roberto@sirius authored
186 key = malloc(uwstrlen + 1);
d3058b9 added spooler.c
roberto@sirius authored
187 if (!key) {
aa9c533 re-indent all the code
roberto@sirius authored
188 perror("malloc()");
d3058b9 added spooler.c
roberto@sirius authored
189 goto retry_later;
190 }
aa9c533 re-indent all the code
roberto@sirius authored
191 rlen = read(spool_fd, key, uwstrlen);
d3058b9 added spooler.c
roberto@sirius authored
192 if (rlen != uwstrlen) {
aa9c533 re-indent all the code
roberto@sirius authored
193 perror("read()");
194 free(key);
d3058b9 added spooler.c
roberto@sirius authored
195 goto next_spool;
196 }
e0882ed big update: hooks and plugins
roberto@sirius authored
197 datasize += rlen;
198 key[rlen] = 0;
d3058b9 added spooler.c
roberto@sirius authored
199
200
aa9c533 re-indent all the code
roberto@sirius authored
201 rlen = read(spool_fd, &uwstrlen, 2);
d3058b9 added spooler.c
roberto@sirius authored
202 if (rlen != 2) {
aa9c533 re-indent all the code
roberto@sirius authored
203 perror("read()");
204 free(key);
d3058b9 added spooler.c
roberto@sirius authored
205 goto next_spool;
206 }
e0882ed big update: hooks and plugins
roberto@sirius authored
207 datasize += rlen;
d3058b9 added spooler.c
roberto@sirius authored
208
209 if (uwstrlen > 0) {
aa9c533 re-indent all the code
roberto@sirius authored
210 val = malloc(uwstrlen + 1);
d3058b9 added spooler.c
roberto@sirius authored
211 if (!val) {
aa9c533 re-indent all the code
roberto@sirius authored
212 free(key);
213 perror("malloc()");
d3058b9 added spooler.c
roberto@sirius authored
214 goto retry_later;
215 }
216
aa9c533 re-indent all the code
roberto@sirius authored
217 rlen = read(spool_fd, val, uwstrlen);
d3058b9 added spooler.c
roberto@sirius authored
218 if (rlen != uwstrlen) {
aa9c533 re-indent all the code
roberto@sirius authored
219 perror("read()");
220 free(key);
e0882ed big update: hooks and plugins
roberto@sirius authored
221 goto next_spool;
222 }
223 datasize += rlen;
224 val[rlen] = 0;
d3058b9 added spooler.c
roberto@sirius authored
225 /* ready to add item to the dict */
226 }
227
aa9c533 re-indent all the code
roberto@sirius authored
228 if (PyDict_SetItemString(spool_env, key, PyString_FromStringAndSize(val, uwstrlen))) {
229 PyErr_Print();
230 free(key);
231 free(val);
e0882ed big update: hooks and plugins
roberto@sirius authored
232 goto retry_later;
d3058b9 added spooler.c
roberto@sirius authored
233 }
234
aa9c533 re-indent all the code
roberto@sirius authored
235 free(key);
236 free(val);
d3058b9 added spooler.c
roberto@sirius authored
237 }
238 else {
239 break;
240 }
241 }
242
e0882ed big update: hooks and plugins
roberto@sirius authored
243
aa9c533 re-indent all the code
roberto@sirius authored
244 spool_result = PyEval_CallObject(spooler_callable, spool_tuple);
ec077bc fixed spooler, ping implementation, uwsgi.applications and uwsgi.applist...
roberto@voldemort authored
245 if (!spool_result) {
aa9c533 re-indent all the code
roberto@sirius authored
246 PyErr_Print();
247 fprintf(stderr, "error detected. spool request canceled.\n");
ec077bc fixed spooler, ping implementation, uwsgi.applications and uwsgi.applist...
roberto@voldemort authored
248 goto next_spool;
249 }
aa9c533 re-indent all the code
roberto@sirius authored
250 if (PyInt_Check(spool_result)) {
251 if (PyInt_AsLong(spool_result) == 17) {
252 Py_DECREF(spool_result);
253 fprintf(stderr, "retry this task later...\n");
d3058b9 added spooler.c
roberto@sirius authored
254 goto retry_later;
255 }
256 }
257
aa9c533 re-indent all the code
roberto@sirius authored
258 Py_DECREF(spool_result);
18efb25 finally a worker (almost) solid spooler. Ready for beta1
roberto@voldemort authored
259
aa9c533 re-indent all the code
roberto@sirius authored
260 fprintf(stderr, "done with task/spool %s\n", dp->d_name);
e0882ed big update: hooks and plugins
roberto@sirius authored
261 next_spool:
d3058b9 added spooler.c
roberto@sirius authored
262
aa9c533 re-indent all the code
roberto@sirius authored
263 if (unlink(dp->d_name)) {
264 perror("unlink");
265 fprintf(stderr, "something horrible happened to the spooler. Better to kill it.\n");
266 exit(1);
d3058b9 added spooler.c
roberto@sirius authored
267 }
e0882ed big update: hooks and plugins
roberto@sirius authored
268 retry_later:
aa9c533 re-indent all the code
roberto@sirius authored
269 PyDict_Clear(spool_env);
270 close(spool_fd);
d3058b9 added spooler.c
roberto@sirius authored
271 }
272 }
273 }
aa9c533 re-indent all the code
roberto@sirius authored
274 closedir(sdir);
d3058b9 added spooler.c
roberto@sirius authored
275 }
276 else {
aa9c533 re-indent all the code
roberto@sirius authored
277 perror("opendir()");
d3058b9 added spooler.c
roberto@sirius authored
278 }
279
7786296 first public commit for alpha 0.9.4
roberto@localhost.localdomain authored
280 /* TODO spooler frequency user-configurable */
aa9c533 re-indent all the code
roberto@sirius authored
281 sleep(5);
e0882ed big update: hooks and plugins
roberto@sirius authored
282 }
283 }
284
aa9c533 re-indent all the code
roberto@sirius authored
285 int uwsgi_request_spooler(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req) {
286
e0882ed big update: hooks and plugins
roberto@sirius authored
287 int i;
288 char spool_filename[1024];
289
290 if (spool_dir == NULL) {
aa9c533 re-indent all the code
roberto@sirius authored
291 fprintf(stderr, "the spooler is inactive !!!...skip\n");
e0882ed big update: hooks and plugins
roberto@sirius authored
292 wsgi_req->modifier = 255;
293 wsgi_req->size = 0;
294 wsgi_req->modifier_arg = 0;
aa9c533 re-indent all the code
roberto@sirius authored
295 i = write(uwsgi->poll.fd, wsgi_req, 4);
e0882ed big update: hooks and plugins
roberto@sirius authored
296 if (i != 4) {
aa9c533 re-indent all the code
roberto@sirius authored
297 perror("write()");
e0882ed big update: hooks and plugins
roberto@sirius authored
298 }
aa9c533 re-indent all the code
roberto@sirius authored
299 return -1;
e0882ed big update: hooks and plugins
roberto@sirius authored
300 }
301
aa9c533 re-indent all the code
roberto@sirius authored
302 fprintf(stderr, "managing spool request...\n");
303 i = spool_request(spool_filename, uwsgi->workers[0].requests + 1, uwsgi->buffer, wsgi_req->size);
e0882ed big update: hooks and plugins
roberto@sirius authored
304 wsgi_req->modifier = 255;
305 wsgi_req->size = 0;
306 if (i > 0) {
307 wsgi_req->modifier_arg = 1;
aa9c533 re-indent all the code
roberto@sirius authored
308 if (write(uwsgi->poll.fd, wsgi_req, 4) != 4) {
309 fprintf(stderr, "disconnected client, remove spool file.\n");
e0882ed big update: hooks and plugins
roberto@sirius authored
310 /* client disconnect, remove spool file */
aa9c533 re-indent all the code
roberto@sirius authored
311 if (unlink(spool_filename)) {
312 perror("unlink()");
313 fprintf(stderr, "something horrible happened !!! check your spooler ASAP !!!\n");
314 goodbye_cruel_world();
e0882ed big update: hooks and plugins
roberto@sirius authored
315 }
316 }
317 return 0;
d3058b9 added spooler.c
roberto@sirius authored
318 }
e0882ed big update: hooks and plugins
roberto@sirius authored
319 else {
320 /* announce a failed spool request */
321 wsgi_req->modifier_arg = 0;
aa9c533 re-indent all the code
roberto@sirius authored
322 i = write(uwsgi->poll.fd, wsgi_req, 4);
e0882ed big update: hooks and plugins
roberto@sirius authored
323 if (i != 4) {
aa9c533 re-indent all the code
roberto@sirius authored
324 perror("write()");
e0882ed big update: hooks and plugins
roberto@sirius authored
325 }
326 }
327
aa9c533 re-indent all the code
roberto@sirius authored
328 return -1;
d3058b9 added spooler.c
roberto@sirius authored
329 }
6062bf8 new makefiles for py2.4, 3.0, 3.1 and ROCK_SOLID mode
roberto@voldemort authored
330
bed7682 useful XML support, first commit of uwsgiconfig.py
roberto@sirius authored
331 #else
332 #warning "*** Spooler support is disabled ***"
6062bf8 new makefiles for py2.4, 3.0, 3.1 and ROCK_SOLID mode
roberto@voldemort authored
333 #endif
Something went wrong with that request. Please try again.