Hello!
On Wed, Aug 20, 2014 at 9:32 AM, Vitaly Kosenko wrote:
> Hello! I successfully use resty-mysql in lua-nginx but I also need some
> tasks running daily to work with mysql. Can I use resty-mysql outside the
> nginx? Or I need to add fake locations or servers for nginx configuration
> and access them in my tasks and run there resty-mysql hm?
>
I've just implemented a "resty" command-line utility for this purpose.
Please try out the following OpenResty pre-release tarball:
http://openresty.org/download/ngx_openresty-1.7.4.1rc1.1.tar.gz
The "resty" tool is installed under <prefix>/bin/ where <prefix> is
your OpenResty installation prefix (specified by --prefix=PATH,
default to "/usr/local/openresty").
Below are some examples:
$ export PATH=/usr/local/openresty/bin:$PATH
$ which resty
/usr/local/openresty/bin/resty
$ resty -h
/usr/local/openresty/bin/resty [-h] [-c num] [-e prog] [-V] [lua-file]
Options:
-c num set maximal connection count (default: 64).
-e prog run the inlined Lua code in "prog".
-h print this help.
-V print the underlying nginx version and configurations.
For bug reporting instructions, please see:
<http://openresty.org/#Community>
$ resty -e 'print("hello")'
hello
$ time resty -e 'ngx.sleep(3) print("done\n")'
done
real 0m3.085s
user 0m0.071s
sys 0m0.010s
$ resty -e 'ngx.say(ngx.md5("hello"))'
5d41402abc4b2a76b9719d911017c592
$ resty -e 'io.stderr:write("hello world\n")' > /dev/null
hello world
I've also tried the code example in lua-resty-mysql's doc's "Synopsis"
section which works out of the box:
$ resty mysql-example.lua
connected to mysql.
table cats created.
3 rows inserted into table cats (last insert id: 1)
result: [{"name":"Bob","id":"1"},{"name":"","id":"2"},{"name":null,"id":"3"}]
where a.lua is from lua-resty-mysql's docs.
Stdin also works (in addition to stdout and stderr):
$ resty -e 'print("got: ", io.stdin:read("*l"))'
hiya
got: hiya
where the first "hiya" line was entered from my keyboard ;)
"Light theads" also work:
$ time resty -e 'local ths = {}
for i = 1, 3 do
ths[i] = ngx.thread.spawn(function ()
ngx.sleep(3) ngx.say("done ", i)
end)
end
for i = 1, #ths do ngx.thread.wait(ths[i]) end'
done 1
done 2
done 3
real 0m3.073s
user 0m0.053s
sys 0m0.015s
Under the hood, the "resty" script invokes the "nginx" executable,
disabling daemon, master_process, access_log, and other things it does
not need. No server {} is configured hence no listening sockets
involved. The Lua code is initiated by the init_worker_by_lua
directive and run in the context of ngx.timer callback. So all of
ngx_lua's Lua APIs available in the ngx.timer callback context are
also available in the "resty" utility. We may remove some of the
remaining limitations in the future though :)
This tool is still experimental. You're encouraged to try it out and
report back any issues that you have. Feature requests and patches
will also be very welcome :)
BTW, do *NOT* use this "resty" script separately with older versions
of ngx_lua, which may lead to memory corruptions. Use the openresty
tarball given above (or any future versions of openresty) :)
Thanks!
-agentzh