Ruby uWSGI DSL module
As well as the python Decorators, a DSL for ruby, allowing elegant uWSGI api access is available.
The module is available as uwsgidsl.rb in the source distribution.
You can put the code in your config.ru files, or in ruby files you will load with --rbrequire options (see below)
timer
execute code at regular interval
require 'uwsgidsl' timer 30 do |signum| puts "30 seconds elapsed" end
rbtimer
same as timer but using red black tree timer
require 'uwsgidsl' rbtimer 30 do |signum| puts "30 seconds elapsed" end
filemon
execute code at file modifications
require 'uwsgidsl' filemon '/tmp' do |signum| puts "/tmp has been modified" end
cron
execute task periodically using the CronInterface
require 'uwsgidsl' cron 30,17,-1,-1,-1 do |signum| puts "it is 17:30 !!!" end
signal
register code as a signal handler
require 'uwsgidsl' signal 17 do |signum| puts "i am the uwsgi signal #{signum}" end
postfork
execute code after each fork()
require 'uwsgidsl' postfork do puts "uWSGI server called fork()" end
rpc
register code as a RPC function
require 'uwsgidsl' rpc 'helloworld' do return "Hello World" end rpc 'advancedhelloworld' do |x,y| return "x = #{x}, y = #{y}" end
mule
Execute code as a Mule brain
require 'uwsgidsl' mule 1 do puts "I am the mule #{UWSGI.mule_id}" end
will run the code in mule 1
mule do puts "I am the mule #{UWSGI.mule_id}" end
will run the code in the first available mule
After the function returns, the mule will be brainless.
To avoid this, put the code in a loop
mule 1 do loop do puts "I am the mule #{UWSGI.mule_id}" sleep(2) end end
or even better:
muleloop
execute code in a mule in looped context
muleloop 3 do puts "I am the mule #{UWSGI.mule_id}" sleep(2) end
SpoolProc
a subclass of Proc allowing you to define task to be executed in the Spooler
# define the function my_long_running_task = SpoolProc.new {|args| puts "I am task1" UWSGI::SPOOL_OK } # spool it my_long_running_task.call({'foo' => 'bar', 'one' => 'two'})
MuleFunc
call a function from any process (a worker for example) but execute in a mule
i_am_a_long_running_function = MuleFunc.new do |pippo,pluto| puts "i am mule #{UWSGI.mule_id} #{pippo}, #{pluto}" end i_am_a_long_running_function.call("serena", "alessandro")
the worker call i_am_a_long_running_function() but the function will be execute asynchronously in the first available mule.
If you want to run the function on a specific mule, simply use
i_am_a_long_running_function = MuleFunc.new 5 do |pippo,pluto| puts "i am mule #{UWSGI.mule_id} #{pippo}, #{pluto}" end i_am_a_long_running_function.call("serena", "alessandro")
this will use only the fifth mule
Real world usage
A simple sinatra app printing messages every 30 seconds
#config.ru require 'rubygems' require 'sinatra' require 'uwsgidsl' timer 30 do |signum| puts "30 seconds elapsed" end get '/hi' do "Hello World!" end run Sinatra::Application
or you can put your code in a dedicated file (mytasks.rb)
require 'uwsgidsl' timer 30 do |signum| puts "30 seconds elapsed" end timer 60 do |signum| puts "60 seconds elapsed" end
and loading it with
uwsgi --socket :3031 --rack config.ru --rbrequire mytasks.rb --master --processes 4
Status
The uWSGI api for ruby is still incomplete (QueueFramework, SharedArea, message/object passing and SNMP being the most missing players), the DSL will be extended as soon as the various api call will be ready.
Current available api functions and constants (available in UWSGI ruby module) are
UWSGI.suspend UWSGI.masterpid UWSGI.async_sleep UWSGI.wait_fd_read UWSGI.wait_fd_write UWSGI.async_connect UWSGI.signal UWSGI.register_signal UWSGI.register_rpc UWSGI.signal_registered UWSGI.signal_wait UWSGI.signal_received UWSGI.add_cron UWSGI.add_timer UWSGI.add_rb_timer UWSGI.add_file_monitor UWSGI.cache_get UWSGI.cache_get! UWSGI.cache_exists UWSGI.cache_exists? UWSGI.cache_del UWSGI.cache_set UWSGI.cache_set UWSGI.cache_set! UWSGI.cache_update UWSGI.cache_update! UWSGI.setprocname UWSGI.set_warning_message UWSGI.lock UWSGI.unlock UWSGI.mem UWSGI.mule_get_msg UWSGI.request_id UWSGI.mule_id UWSGI.mule_msg UWSGI.worker_id UWSGI.log UWSGI.logsize UWSGI.i_am_the_spooler UWSGI.send_to_spooler UWSGI.spool UWSGI::OPT UWSGI::VERSION UWSGI::HOSTNAME UWSGI::NUMPROC UWSGI::PIDFILE UWSGI::SPOOL_OK UWSGI::SPOOL_RETRY UWSGI::SPOLL_IGNORE
