Hi Erik,
I got it all to work, but it requires some shared-memory logic.
Here is the flow:
2.) init_lua calls a function that sets a shared memory field called NUM_WORKERS to 0
3.) init_worker_by_lua workers increment NUM_WORKERS by 1
4.) then workers set a timer for a function called check_shutdown
5.) check_shutdown checks for premature timeout,
6.) if timeout is normal set the same timer for check_shutdown (e.g. recurring timer)
7.) if timeout was premature call handle_shutdown
8.) handle_shutdown decements NUM_WORKERS by 1
9.) if NUM_WORKERS equals 0, this is the final worker to exit -> persist data :)
This works for both SIGHUP & SIGQUIT.
It does not work if a worker SEGVs or is killed, that will require more logic :)
thanks for getting me started on a solution.
- Russ
p.s. if I get my code into a decent abstraction I will post it here so others can use it
On Tuesday, November 24, 2015 at 8:46:08 PM UTC-8, Russell Sullivan wrote:
Hi Erik,
This approach would work if I could use
ngx.timer.at from init_by_lua, but
ngx.timer.at only works at the request level (e.g. init_worker_by_lua).
- Russ
On Tuesday, November 24, 2015 at 8:32:13 PM UTC-8, Russell Sullivan wrote:
Hi Erik,
nice trick, I coded it ... it kinda works.
It works well if I send a SIGQUIT to the master process, everything works correctly.
If I send a SIGHUP, it is a bit wonky: the new workers spin up before the (prematurely triggered) timer function runs and this function has no idea that a restart is happening.
If I can handle the SIGHUP case by doing nothing and handle the SIGQUIT case by running a persist-shm-dicts-to-file that would work, but how do I tell from inside the timer function whether a shutdown or restart is happening?
- Russ
On Saturday, November 21, 2015 at 12:17:59 PM UTC-8, Nelson, Erik - 2 wrote:
Russell Sullivan Sent: Friday, November 20, 2015 8:52 PM
>I am trying to call a Lua routine (to dump a few ngx.shared.DICTs to file) on worker restart, is this possible?
>I see 'ngx.worker.exiting' is a boolean that will tell me if a worker is actively exiting, but I don't see any way to catch the Nginx is exiting event
Maybe if you set a timer? I think the exit will fire it early with the flag that the worker is exiting.
----------------------------------------------------------------------
This message, and any attachments, is for the intended recipient(s) only, may contain information that is privileged, confidential and/or proprietary and subject to important terms and conditions available at http://www.bankofamerica.com/emaildisclaimer. If you are not the intended recipient, please delete this message.