How to invoke ngx_postgres in init_worker_by_lua_block

228 views
Skip to first unread message

Andy Ai

unread,
Sep 15, 2016, 4:14:20 AM9/15/16
to openresty-en
I want load all data from a PostgreSQL table. and using ngx.timer.at update incrementally.

Like this:

init_worker_by_lua_block {
  local handler = function()
    local res = -- load data from PostgreSQL
    cache:set(key, res.body)
    ngx.timer.at(delay, handler)
  end
  ngx.timer.at(delay, handler)
}

ngx_postgres need a sub-repquest, example:

local res = ngx.location.capture('/postgres', {args = {sql = sql}})

But, In ngx.timer.at the sub-request is disabled. 

What should I do ?



Ming

unread,
Sep 15, 2016, 9:26:40 AM9/15/16
to openresty-en

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andy Ai

unread,
Sep 16, 2016, 1:03:16 AM9/16/16
to openresty-en
Thanks,

But error.log is echo some errors, when start the OpenResty

2016/09/16 12:58:55 [error] 1097#0: init_worker_by_lua error: lib/lua-resty-postgres/lib/resty/postgres.lua:38: API disabled in the context of init_worker_by_lua*
stack traceback:
[C]: in function 'tcp'
lib/lua-resty-postgres/lib/resty/postgres.lua:38: in function 'new'
init_worker_by_lua:4: in function 'handler'
init_worker_by_lua:11: in main chunk

在 2016年9月15日星期四 UTC+8下午9:26:40,WenMing写道:
To unsubscribe from this group and stop receiving emails from it, send an email to openresty-en...@googlegroups.com.

Andy Ai

unread,
Sep 16, 2016, 1:06:53 AM9/16/16
to openresty-en
I find a solution. 

Like this:



在 2016年9月16日星期五 UTC+8下午1:03:16,Andy Ai写道:

Ming

unread,
Sep 17, 2016, 11:15:16 PM9/17/16
to openresty-en
The cosockets are disabled in the init_worker_by_lua* directive context, however you can create a 0-delay timer via ngx.time.at API to do cosocket works.

To unsubscribe from this group and stop receiving emails from it, send an email to openresty-en+unsubscribe@googlegroups.com.

Andy Ai

unread,
Sep 18, 2016, 2:39:01 AM9/18/16
to openresty-en
Thanks

在 2016年9月18日星期日 UTC+8上午11:15:16,WenMing写道:

Nelson, Erik - 2

unread,
Sep 28, 2016, 12:58:46 PM9/28/16
to openre...@googlegroups.com
I was thinking about adding a simple collaborative editor to my openresty-powered website, something along the lines of Etherpad Lite

https://github.com/ether/etherpad-lite

Unfortunately, it seems like that requires node.js, which isn't the end of the world but I'd like to avoid adding a new tool if pssible.

It occurred to me that someone maybe someone had done something similar to this- does anyone have experience with this kind of thing on openresty?

Thoughts welcome!

Erik

----------------------------------------------------------------------
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.
Reply all
Reply to author
Forward
0 new messages