> >>> To best plugin for starting is plugins/emperor_pg (the postgresql one). >>> >>> Basically you "register" an "imperial monitor" and it will be triggered >>> at >>> the right moment. >> >> In this case, the emperor doesn't know the right moment. My plugin will >> be notified via a socket when it needs to do something. So I don't want >> to add/stop/respawn the vassals when the emperor loops through my >> plugin. >> >> I only need to add vassals on startup. How do I do that? A flag in >> _init, and then clear the flag in the monitor func? >> >>> Only the emperor process has access to the "ui" pointer, while the >>> init() >>> hook is called on startup (before it is available as you have already >>> noted) >> >> That simple change seemd to work still >> >> -- >> дамјан >> _______________________________________________ >> uWSGI mailing list >> [email protected] >> http://lists.unbit.it/cgi-bin/mailman/listinfo/uwsgi >> > > > You can register a uwsgi_thread (is a funny abstraction that gives you a > thread and a communication channel) in the imperial monitor and just call > the various emperor_add() on the first round and after that you simply > wait for events. > > In pseudo-code: > > // this run continuously from a thread waiting for your socket > my_loop(ut) { > for(;;) { > wait_for_event_on_your_socket(); > // write a message to the thread communication channel > write(ut->pipe[0], "message", msg_len); > } > } > > > // this function is called by the emperor whenever the thread send a > message > void foobar_event(ues) { > char msg[4096]; > ssize_t len = read(ues->fd, msg, 4096); > > ...do something with the message... > } > > the imperial monitor > yourplugin_imperial_cycle(ues) { > // on the first round you create the thread > // and you spawns the static vassals > // on the second round you start monitoring events from your thread > int round = 0; > if (round == 0) { > uwsgi_thread *ut = uwsgi_thread_new(my_loop); > emperor_add() > emperor_add() > emperor_add() > round++; > } > else if (round == 1) { > // this set the function to call whenever a message > //is generated on the thread > ues->event_func = foobar_event; > // here we start waiting for thread events > event_queue_add_fd_read(uwsgi.emperor_queue, ut->pipe[1]); > round++; > } > } >
Another approach (but implies working with non-blocking sockets) is the one used by the emperor_amqp plugin, where the events are directly triggered by the amqp socket. https://github.com/unbit/uwsgi/blob/master/plugins/emperor_amqp/emperor_amqp.c this is similar to the thread example, but without the thread ;) -- Roberto De Ioris http://unbit.it _______________________________________________ uWSGI mailing list [email protected] http://lists.unbit.it/cgi-bin/mailman/listinfo/uwsgi
