How to run Lua code once with lua_code_cache off?

114 views
Skip to first unread message

Vadim Peretokin

unread,
Mar 28, 2016, 10:38:43 PM3/28/16
to openresty-en
I'd like to use Lua to do some database setup, and init_by_lua is an almost perfect fit for this, but unfortunately it is run every time when lua_code_cache is off - and having it off is real nice for development. Are there any other hooks I can use that'll run Lua code just once even with lua_code_cache off?

Robert Paprocki

unread,
Mar 28, 2016, 10:54:54 PM3/28/16
to openre...@googlegroups.com
I don't believe any such mechanism exists. Perhaps some sentinel flag that could examine your schema and signal if it's appropriate to run would be appropriate?

Curious as to what sort of environment would make database initialization appropriate for an http server? If it's truly a one-time action, would not a simple sql file work?

On Mar 28, 2016, at 19:38, Vadim Peretokin <vpere...@gmail.com> wrote:

I'd like to use Lua to do some database setup, and init_by_lua is an almost perfect fit for this, but unfortunately it is run every time when lua_code_cache is off - and having it off is real nice for development. Are there any other hooks I can use that'll run Lua code just once even with lua_code_cache off?

--
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.

Vadim Peretokin

unread,
Mar 29, 2016, 12:34:42 AM3/29/16
to openresty-en
I could have a once-off separate action that initializes the database, but then it's one more step people have to do and one more way it can be stuffed up. Unless there's a strong reason against it, I've gotten the auto-initialisation to work as I'd like - just having this issue finding a right hook.

Robert Paprocki

unread,
Mar 29, 2016, 12:48:26 AM3/29/16
to openre...@googlegroups.com
Not that there's not a reason, just that there's not a built-in mechanism for it. There simply isn't a run-only-once hook in nginx. You'll need to do a bit of work there on your own. I would imagine some sort of pre-flight check as part of your initialization to look for an existing scheme and bail if it looks like your schema exists. Shouldn't be more than a few lines of code :)

Andre R

unread,
Mar 29, 2016, 1:41:44 AM3/29/16
to openresty-en
IIRC the shared memory does NOT get reset on a "nginx -s reload". So you could just set a flag there and check it.

Vadim Peretokin

unread,
Mar 29, 2016, 3:59:07 AM3/29/16
to openresty-en
Thanks, that's what I've done and it works. For anyone else reading this in the future, this is essentially what I've done:

http {
  include mime.types;

  lua_shared_dict blah 12k;

  init_by_lua_block {
    local initialised = ngx.shared.blah("initialised")
    if initialised then return end
    local saved, err = ngx.shared.shared:set("initialised", true)
Reply all
Reply to author
Forward
0 new messages