Shutdown hooks in OpenResty?

561 views
Skip to first unread message

Rollin Crittendon

unread,
Aug 31, 2015, 10:43:46 AM8/31/15
to openresty-en
I was wondering if there is a shutdown-hook in OpenResty.

I would like to have something that cleans up resources on shutdown.


DeJiang Zhu

unread,
Aug 31, 2015, 11:12:23 AM8/31/15
to openre...@googlegroups.com
hi,

There is no shutdown-hook in OpenResty, but you can try like this:

init_worker_by_lua_file worker.lua

worker.lua
-----------------
local handler
local delay = 5

handle = function (premature)
    if ngx.worker.exiting() then
         --- cleanup()
         return
    end
    local ok, err = ngx.timer.at(delay, handle)
    if not ok then
        ngx.log(ngx.ERR, "failed to create timer: ", err)
    end
end

if not ok then
    ngx.log(ngx.ERR, "failed to create timer: ", err)
end

2015-08-31 22:43 GMT+08:00 Rollin Crittendon <rw...@rwag.net>:
I was wondering if there is a shutdown-hook in OpenResty.

I would like to have something that cleans up resources on shutdown.


--
You received this message because you are subscribed to the Google Groups "openresty-en" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openresty-en...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rollin Crittendon

unread,
Aug 31, 2015, 12:43:50 PM8/31/15
to openresty-en
Thanks DeJiang,

I have a question about this one.
I put in a timer just like that, see it log, but it never shows that the ngx.worker.exiting() is true, always displays as false, UNLESS I reload instead of stop.

Does it actually shutdown on "nginx -s stop" but just not display it?

DeJiang Zhu

unread,
Aug 31, 2015, 10:00:48 PM8/31/15
to openre...@googlegroups.com
hi, Rollin

This works only nginx graceful shutdown. `nginx -s quit` shoud works fine.

Rollin Crittendon

unread,
Sep 1, 2015, 10:30:52 AM9/1/15
to openresty-en
Excellent, thank you Dejiang, this is working great for me.
Reply all
Reply to author
Forward
0 new messages