mirror of
https://github.com/clearlinux/uwsgi.git
synced 2026-11-22 03:13:39 +00:00
fully working file monitor and timer support for Linux/epoll
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
#include "uwsgi.h"
|
||||
|
||||
extern struct uwsgi_server uwsgi;
|
||||
|
||||
#ifdef UWSGI_EVENT_USE_EPOLL
|
||||
|
||||
#include <sys/epoll.h>
|
||||
@@ -32,7 +34,7 @@ int event_queue_add_fd_read(int eq, int fd) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return fd;
|
||||
}
|
||||
|
||||
int event_queue_wait(int eq, int timeout, int *interesting_fd) {
|
||||
@@ -81,7 +83,7 @@ int event_queue_add_fd_read(int eq, int fd) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return fd;
|
||||
}
|
||||
|
||||
int event_queue_add_fd_write(int eq, int fd) {
|
||||
@@ -145,30 +147,93 @@ int event_queue_add_file_monitor(int eq, int fd) {
|
||||
|
||||
int event_queue_add_file_monitor(int eq, char *filename, int *id) {
|
||||
|
||||
int ifd = inotify_init();
|
||||
if (ifd < 0) {
|
||||
uwsgi_error("inotify_init()");
|
||||
return -1;
|
||||
}
|
||||
int ifd = -1;
|
||||
int i;
|
||||
int add_to_queue = 0;
|
||||
|
||||
*id = ifd;
|
||||
for (i=0;i<uwsgi.files_monitored_cnt;i++) {
|
||||
if (uwsgi.files_monitored[i].registered) {
|
||||
ifd = uwsgi.files_monitored[0].fd;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (ifd == -1) {
|
||||
ifd = inotify_init();
|
||||
if (ifd < 0) {
|
||||
uwsgi_error("inotify_init()");
|
||||
return -1;
|
||||
}
|
||||
add_to_queue = 1;
|
||||
}
|
||||
|
||||
*id = inotify_add_watch(ifd, filename, IN_ATTRIB|IN_CREATE|IN_DELETE|IN_DELETE_SELF|IN_MODIFY|IN_MOVE_SELF|IN_MOVED_FROM|IN_MOVED_TO);
|
||||
|
||||
uwsgi_log("added watch %d for filename %s\n", inotify_add_watch(ifd, filename, IN_ATTRIB|IN_CREATE|IN_DELETE|IN_DELETE_SELF|IN_MODIFY|IN_MOVE_SELF|IN_MOVED_FROM|IN_MOVED_TO), filename);
|
||||
uwsgi_log("added watch %d for filename %s\n", *id, filename);
|
||||
|
||||
|
||||
return event_queue_add_fd_read(eq, ifd);
|
||||
if (add_to_queue) {
|
||||
return event_queue_add_fd_read(eq, ifd);
|
||||
}
|
||||
else {
|
||||
return ifd;
|
||||
}
|
||||
}
|
||||
|
||||
void event_queue_ack_file_monitor(int id) {
|
||||
struct uwsgi_fmon *event_queue_ack_file_monitor(int id, void hook(char *, uint32_t, char *)) {
|
||||
|
||||
ssize_t rlen = 0;
|
||||
struct inotify_event ie;
|
||||
struct inotify_event ie, *bie, *iie;
|
||||
int i,j;
|
||||
int items = 0;
|
||||
|
||||
rlen = read(id, &ie, sizeof(struct inotify_event));
|
||||
unsigned int isize = sizeof(struct inotify_event);
|
||||
struct uwsgi_fmon *uf = NULL;
|
||||
|
||||
if (ioctl(id, FIONREAD, &isize) < 0) {
|
||||
uwsgi_error("ioctl()");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (isize > sizeof(struct inotify_event)) {
|
||||
bie = uwsgi_malloc(isize);
|
||||
rlen = read(id, bie, isize);
|
||||
}
|
||||
else {
|
||||
rlen = read(id, &ie, sizeof(struct inotify_event));
|
||||
bie = &ie;
|
||||
}
|
||||
|
||||
if (rlen < 0) {
|
||||
uwsgi_error("read()");
|
||||
}
|
||||
else {
|
||||
items = isize/(sizeof(struct inotify_event));
|
||||
uwsgi_log("inotify returned %d items\n", items);
|
||||
for(j=0;j<items;j++) {
|
||||
iie = &bie[j];
|
||||
for(i=0;i<uwsgi.files_monitored_cnt;i++) {
|
||||
if (uwsgi.files_monitored[i].registered) {
|
||||
if (uwsgi.files_monitored[i].fd == id && uwsgi.files_monitored[i].id == iie->wd) {
|
||||
if (hook) {
|
||||
hook(uwsgi.files_monitored[i].filename, iie->mask, iie->name);
|
||||
}
|
||||
else {
|
||||
uf = &uwsgi.files_monitored[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (items > 1) {
|
||||
free(bie);
|
||||
}
|
||||
|
||||
return uf;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
|
||||
}
|
||||
#endif
|
||||
@@ -203,17 +268,33 @@ int event_queue_add_timer(int eq, int *id, int sec) {
|
||||
return event_queue_add_fd_read(eq, tfd);
|
||||
}
|
||||
|
||||
void event_queue_ack_timer(int id) {
|
||||
struct uwsgi_timer *event_queue_ack_timer(int id, void hook(int, int)) {
|
||||
|
||||
int i;
|
||||
ssize_t rlen;
|
||||
uint64_t counter;
|
||||
struct uwsgi_timer *ut = NULL;
|
||||
|
||||
for(i=0;i<uwsgi.timers_cnt;i++) {
|
||||
if (uwsgi.timers[i].registered) {
|
||||
if (uwsgi.timers[i].id == id) {
|
||||
ut = &uwsgi.timers[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
rlen = read(id, &counter, sizeof(uint64_t));
|
||||
|
||||
if (rlen < 0) {
|
||||
uwsgi_error("read()");
|
||||
}
|
||||
else {
|
||||
if (hook && ut) {
|
||||
hook(ut->value, ut->id);
|
||||
}
|
||||
}
|
||||
|
||||
return ut;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -98,11 +98,11 @@ void master_loop(char **argv, char **environ) {
|
||||
|
||||
signal(SIGUSR1, (void *) &stats);
|
||||
|
||||
int master_queue = event_queue_init();
|
||||
uwsgi.master_queue = event_queue_init();
|
||||
|
||||
for(i=1;i<=uwsgi.numproc;i++) {
|
||||
uwsgi_log("adding %d to signal poll\n", uwsgi.workers[i].pipe[0]);
|
||||
event_queue_add_fd_read(master_queue, uwsgi.workers[i].pipe[0]);
|
||||
event_queue_add_fd_read(uwsgi.master_queue, uwsgi.workers[i].pipe[0]);
|
||||
}
|
||||
|
||||
uwsgi.wsgi_req->buffer = uwsgi.async_buf[0];
|
||||
@@ -200,10 +200,24 @@ void master_loop(char **argv, char **environ) {
|
||||
/*
|
||||
int fake_timer = 0xFFFF;
|
||||
event_queue_add_timer(master_queue, &fake_timer, 5);
|
||||
|
||||
int fd_mon = -1;
|
||||
event_queue_add_file_monitor(master_queue, "/tmp/topolino", &fd_mon);
|
||||
*/
|
||||
|
||||
|
||||
// add unregistered file monitors
|
||||
for(i=0;i<uwsgi.files_monitored_cnt;i++) {
|
||||
if (!uwsgi.files_monitored[i].registered) {
|
||||
uwsgi.files_monitored[i].fd = event_queue_add_file_monitor(uwsgi.master_queue, uwsgi.files_monitored[i].filename, &uwsgi.files_monitored[i].id);
|
||||
uwsgi.files_monitored[i].registered = 1;
|
||||
}
|
||||
}
|
||||
|
||||
// add unregistered timers
|
||||
for(i=0;i<uwsgi.timers_cnt;i++) {
|
||||
if (!uwsgi.timers[i].registered) {
|
||||
uwsgi.timers[i].fd = event_queue_add_timer(uwsgi.master_queue, &uwsgi.timers[i].id, uwsgi.timers[i].value);
|
||||
uwsgi.timers[i].registered = 1;
|
||||
}
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
//uwsgi_log("ready_to_reload %d %d\n", ready_to_reload, uwsgi.numproc);
|
||||
@@ -309,7 +323,7 @@ void master_loop(char **argv, char **environ) {
|
||||
check_interval = 1;
|
||||
|
||||
int interesting_fd = -1;
|
||||
rlen = event_queue_wait(master_queue, check_interval, &interesting_fd);
|
||||
rlen = event_queue_wait(uwsgi.master_queue, check_interval, &interesting_fd);
|
||||
|
||||
if (rlen > 0) {
|
||||
|
||||
@@ -395,19 +409,41 @@ void master_loop(char **argv, char **environ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
/*
|
||||
if (interesting_fd == fd_mon) {
|
||||
uwsgi_log("FileSystem event !!!\n");
|
||||
event_queue_ack_file_monitor(fd_mon);
|
||||
continue;
|
||||
}
|
||||
|
||||
//event_queue_ack_timer(fake_timer);
|
||||
|
||||
if (interesting_fd == fake_timer) {
|
||||
uwsgi_log("timer elapsed !!!\n");
|
||||
event_queue_ack_timer(fake_timer);
|
||||
continue;
|
||||
int next_iteration = 0;
|
||||
|
||||
for(i=0;i<uwsgi.files_monitored_cnt;i++) {
|
||||
if (uwsgi.files_monitored[i].registered) {
|
||||
if (interesting_fd == uwsgi.files_monitored[i].fd) {
|
||||
struct uwsgi_fmon *uf = event_queue_ack_file_monitor(interesting_fd, NULL);
|
||||
// now call the file_monitor handler
|
||||
if (uf) {
|
||||
uwsgi_log("fd event for %s\n", uf->filename);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
if (next_iteration) continue;
|
||||
|
||||
next_iteration = 0;
|
||||
|
||||
for(i=0;i<uwsgi.timers_cnt;i++) {
|
||||
if (uwsgi.timers[i].registered) {
|
||||
if (interesting_fd == uwsgi.timers[i].fd) {
|
||||
struct uwsgi_timer *ut = event_queue_ack_timer(interesting_fd, NULL);
|
||||
// now call the file_monitor handler
|
||||
if (ut) {
|
||||
uwsgi_log("fd event for timer %d\n", ut->value);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (next_iteration) continue;
|
||||
|
||||
|
||||
// finally check for uwsgi_signal
|
||||
for(i=1;i<=uwsgi.numproc;i++) {
|
||||
@@ -652,7 +688,7 @@ void master_loop(char **argv, char **environ) {
|
||||
else {
|
||||
uwsgi_log( "Respawned uWSGI worker (new pid: %d)\n", pid);
|
||||
close(uwsgi.workers[uwsgi.mywid].pipe[1]);
|
||||
event_queue_add_fd_read(master_queue, uwsgi.workers[uwsgi.mywid].pipe[0]);
|
||||
event_queue_add_fd_read(uwsgi.master_queue, uwsgi.workers[uwsgi.mywid].pipe[0]);
|
||||
#ifdef UWSGI_SPOOLER
|
||||
if (uwsgi.mywid <= 0 && diedpid != uwsgi.shared->spooler_pid) {
|
||||
#else
|
||||
|
||||
@@ -159,13 +159,40 @@ PyObject *py_uwsgi_signal(PyObject * self, PyObject * args) {
|
||||
|
||||
char uwsgi_signal;
|
||||
ssize_t rlen;
|
||||
char *payload = NULL;
|
||||
struct uwsgi_header uh;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "B:signal", &uwsgi_signal)) {
|
||||
if (!PyArg_ParseTuple(args, "B|s:signal", &uwsgi_signal, &payload)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// am i the master ?
|
||||
if (uwsgi.mywid == 0) {
|
||||
uwsgi_log("i am the master !!!\n");
|
||||
register_signal(uwsgi_signal, payload);
|
||||
goto done;
|
||||
}
|
||||
|
||||
uwsgi_log("sending %d to master\n", uwsgi_signal);
|
||||
rlen = write(uwsgi.workers[uwsgi.mywid].pipe[1], &uwsgi_signal, 1);
|
||||
uh.modifier1 = 110;
|
||||
if (payload) {
|
||||
uh.pktsize = strlen(payload);
|
||||
}
|
||||
else {
|
||||
uh.pktsize = 0;
|
||||
}
|
||||
uh.modifier2 = uwsgi_signal;
|
||||
|
||||
rlen = write(uwsgi.workers[uwsgi.mywid].pipe[1], &uh, 4);
|
||||
if (rlen == 4) {
|
||||
if (uh.pktsize) {
|
||||
if (write(uwsgi.workers[uwsgi.mywid].pipe[1], payload, uh.pktsize) < 0) {
|
||||
uwsgi_error("write()");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
done:
|
||||
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
#include "uwsgi.h"
|
||||
|
||||
extern struct uwsgi_server uwsgi;
|
||||
|
||||
int register_signal(uint8_t sig, char *payload) {
|
||||
|
||||
uwsgi_log("SIGNAL %d %s\n", sig, payload);
|
||||
|
||||
switch(sig) {
|
||||
|
||||
case 10:
|
||||
if (uwsgi.files_monitored_cnt < 64) {
|
||||
uwsgi.files_monitored[uwsgi.files_monitored_cnt].filename = uwsgi_concat2(payload,"");
|
||||
uwsgi.files_monitored[uwsgi.files_monitored_cnt].registered = 0;
|
||||
// master is not running
|
||||
if (uwsgi.master_queue != -1) {
|
||||
uwsgi.files_monitored[uwsgi.files_monitored_cnt].fd = event_queue_add_file_monitor(uwsgi.master_queue, payload, &uwsgi.files_monitored[uwsgi.files_monitored_cnt].id);
|
||||
uwsgi.files_monitored[uwsgi.files_monitored_cnt].registered = 1;
|
||||
}
|
||||
uwsgi.files_monitored_cnt++;
|
||||
}
|
||||
else {
|
||||
uwsgi_log("you can register max 64 file monitors !!!\n");
|
||||
}
|
||||
|
||||
case 11:
|
||||
if (uwsgi.timers_cnt < 64) {
|
||||
uwsgi.timers[uwsgi.timers_cnt].value = atoi(payload);
|
||||
uwsgi.timers[uwsgi.timers_cnt].registered = 0;
|
||||
// master is not running
|
||||
if (uwsgi.master_queue != -1) {
|
||||
uwsgi.timers[uwsgi.timers_cnt].fd = event_queue_add_timer(uwsgi.master_queue, &uwsgi.timers[uwsgi.timers_cnt].id, uwsgi.timers[uwsgi.timers_cnt].value);
|
||||
uwsgi.timers[uwsgi.timers_cnt].registered = 1;
|
||||
}
|
||||
uwsgi.timers_cnt++;
|
||||
}
|
||||
else {
|
||||
uwsgi_log("you can register max 64 timers !!!\n");
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -418,6 +418,8 @@ int main(int argc, char *argv[], char *envp[])
|
||||
uwsgi.p[i] = &unconfigured_plugin;
|
||||
}
|
||||
|
||||
uwsgi.master_queue = -1;
|
||||
|
||||
uwsgi.cluster_fd = -1;
|
||||
uwsgi.cores = 1;
|
||||
|
||||
|
||||
@@ -588,6 +588,20 @@ struct wsgi_request {
|
||||
|
||||
#define LOADER_MAX 8
|
||||
|
||||
struct uwsgi_fmon {
|
||||
char *filename;
|
||||
int fd;
|
||||
int id;
|
||||
int registered;
|
||||
};
|
||||
|
||||
struct uwsgi_timer {
|
||||
int value;
|
||||
int fd;
|
||||
int id;
|
||||
int registered;
|
||||
};
|
||||
|
||||
struct uwsgi_server {
|
||||
|
||||
|
||||
@@ -696,6 +710,7 @@ struct uwsgi_server {
|
||||
int post_buffering_bufsize;
|
||||
|
||||
int master_process;
|
||||
int master_queue;
|
||||
|
||||
int no_defer_accept;
|
||||
|
||||
@@ -843,6 +858,12 @@ struct uwsgi_server {
|
||||
void *cache_lock;
|
||||
|
||||
void *user_lock;
|
||||
|
||||
struct uwsgi_fmon files_monitored[64];
|
||||
int files_monitored_cnt;
|
||||
|
||||
struct uwsgi_timer timers[64];
|
||||
int timers_cnt;
|
||||
};
|
||||
|
||||
struct uwsgi_lb_group {
|
||||
@@ -1265,8 +1286,10 @@ int event_queue_add_fd_read(int, int);
|
||||
int event_queue_wait(int, int, int *);
|
||||
|
||||
int event_queue_add_timer(int, int *, int);
|
||||
void event_queue_ack_timer(int);
|
||||
struct uwsgi_timer *event_queue_ack_timer(int, void (*)(int, int));
|
||||
|
||||
int event_queue_add_file_monitor(int, char *, int *);
|
||||
void event_queue_ack_file_monitor(int);
|
||||
struct uwsgi_fmon *event_queue_ack_file_monitor(int, void (*)(char *, uint32_t, char *));
|
||||
|
||||
|
||||
int register_signal(uint8_t, char *);
|
||||
|
||||
+1
-1
@@ -137,7 +137,7 @@ class uConf(object):
|
||||
def __init__(self, filename):
|
||||
self.config = ConfigParser.ConfigParser()
|
||||
self.config.read(filename)
|
||||
self.gcc_list = ['utils', 'protocol', 'socket', 'logging', 'master', 'plugins', 'lock', 'cache', 'event', 'loop', 'uwsgi']
|
||||
self.gcc_list = ['utils', 'protocol', 'socket', 'logging', 'master', 'plugins', 'lock', 'cache', 'event', 'signal', 'loop', 'uwsgi']
|
||||
self.cflags = ['-O2', '-Wall', '-Werror', '-D_LARGEFILE_SOURCE', '-D_FILE_OFFSET_BITS=64'] + os.environ.get("CFLAGS", "").split()
|
||||
gcc_version = str(spcall2("%s -v" % GCC)).split('\n')[-1].split()[2]
|
||||
gcc_major = int(gcc_version.split('.')[0])
|
||||
|
||||
Reference in New Issue
Block a user