added signal plugin

This commit is contained in:
roberto@goyle
2011-11-15 18:06:37 +01:00
parent 8f79409ed1
commit 2086500405
6 changed files with 101 additions and 5 deletions
+1 -1
View File
@@ -27,7 +27,7 @@ plugins =
bin_name = uwsgi
append_version =
plugin_dir = .
embedded_plugins = python, ping, cache, nagios, rrdtool, carbon, rpc, fastrouter, http, ugreen
embedded_plugins = python, ping, cache, nagios, rrdtool, carbon, rpc, fastrouter, http, ugreen, signal
as_shared_library = false
locking = auto
+18 -4
View File
@@ -518,17 +518,31 @@ PyObject *py_uwsgi_register_signal(PyObject * self, PyObject * args) {
PyObject *py_uwsgi_signal(PyObject * self, PyObject * args) {
uint8_t uwsgi_signal;
char *remote = NULL;
if (!PyArg_ParseTuple(args, "B:signal", &uwsgi_signal)) {
if (!PyArg_ParseTuple(args, "B|s:signal", &uwsgi_signal, &remote)) {
return NULL;
}
if (remote) {
#ifdef UWSGI_DEBUG
uwsgi_log("sending %d to master\n", uwsgi_signal);
uwsgi_log("sending signal %d to node %s\n", uwsgi_signal, remote);
#endif
int ret = uwsgi_remote_signal_send(remote, uwsgi_signal);
if (ret == 1) goto clear;
if (ret == -1)
return PyErr_Format(PyExc_IOError, "unable to deliver signal %d to node %s", uwsgi_signal, remote);
if (ret == 0)
return PyErr_Format(PyExc_ValueError, "node %s rejected signal %d", remote, uwsgi_signal);
}
else {
#ifdef UWSGI_DEBUG
uwsgi_log("sending signal %d to master\n", uwsgi_signal);
#endif
uwsgi_signal_send(uwsgi.signal_socket, uwsgi_signal);
}
uwsgi_signal_send(uwsgi.signal_socket, uwsgi_signal);
clear:
Py_INCREF(Py_None);
return Py_None;
+32
View File
@@ -0,0 +1,32 @@
#include "../../uwsgi.h"
extern struct uwsgi_server uwsgi;
/* request 110 */
int uwsgi_request_signal(struct wsgi_request *wsgi_req) {
ssize_t len;
uint8_t ret_status = 1;
if (uwsgi_signal_send(uwsgi.signal_socket, wsgi_req->uh.modifier2) < 0) {
ret_status = 0;
}
wsgi_req->uh.modifier1 = 255;
wsgi_req->uh.pktsize = 0;
wsgi_req->uh.modifier2 = ret_status;
len = write(wsgi_req->poll.fd, wsgi_req, 4);
if (len != 4) {
uwsgi_error("write()");
}
return UWSGI_OK;
}
struct uwsgi_plugin signal_plugin = {
.name = "signal",
.modifier1 = 110,
.request = uwsgi_request_signal,
};
+7
View File
@@ -0,0 +1,7 @@
NAME='signal'
CFLAGS = []
LDFLAGS = []
LIBS = []
GCC_LIST = ['signal_plugin']
+42
View File
@@ -224,6 +224,48 @@ void create_signal_pipe(int *sigpipe) {
}
}
int uwsgi_remote_signal_send(char *addr, uint8_t sig) {
struct uwsgi_header uh;
size_t remains = 4;
char *ptr = (char *) &uh;
uh.modifier1 = 110;
uh.pktsize = 0;
uh.modifier2 = sig;
int fd = uwsgi_connect(addr, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], 0);
if (fd < 0) return -1;
if (write(fd, (char *) &uh, 4) != 4) {
uwsgi_error("uwsgi_remote_signal_send()");
close(fd);
return -1;
}
while(remains > 0) {
int rlen = uwsgi_waitfd(fd, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
if (rlen > 0) {
ssize_t len = read(fd, ptr, remains);
if (len <= 0) {
break;
}
remains -= len;
ptr += len;
if (remains == 0) {
close(fd);
return uh.modifier2;
}
continue;
}
break;
}
close(fd);
return -1;
}
int uwsgi_signal_send(int fd, uint8_t sig) {
socklen_t so_bufsize_len = sizeof(int);
+1
View File
@@ -2456,6 +2456,7 @@ ssize_t uwsgi_mule_get_msg(int, int, char *, size_t, int);
uint8_t uwsgi_signal_wait(int);
void uwsgi_add_app(int, uint8_t, char *, int);
int uwsgi_signal_send(int, uint8_t);
int uwsgi_remote_signal_send(char *, uint8_t);
#ifdef UWSGI_CAP
void uwsgi_build_cap(char *);