Ruby API support

Status

The uWSGI API for Ruby is still incomplete (QueueFramework, SharedArea, custom routing and SNMP being the most missing players). The DSL will be extended as soon as the various API calls are ready.

Currently available API functions and constants (available in the 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

uWSGI DSL

In parallel to the uWSGI API Python decorators, a DSL for Ruby is available, allowing elegant access to the uWSGI API.

The module is available as uwsgidsl.rb in the source distribution. You can put this code in your config.ru file, or use the rbrequire option to auto-include it.

timer(n, block)

Execute code at regular intervals.

  1. timer 30 do |signum|
  2. puts "30 seconds elapsed"
  3. end

rbtimer(n, block)

As timer, but using a red-black tree timer.

  1. rbtimer 30 do |signum|
  2. puts "30 seconds elapsed"
  3. end

filemon(path, block)

Execute code at file modifications.

  1. filemon '/tmp' do |signum|
  2. puts "/tmp has been modified"
  3. end

cron(hours, mins, dom, mon, dow, block)

Execute a task periodically using the CronInterface.

  1. cron 20,16,-1,-1,-1 do |signum|
  2. puts "It's time for tea."
  3. end

signal(signum, block)

Register code as a signal handler for the SignalFramework.

  1. signal 17 do |signum|
  2. puts "Signal #{signum} was invoked."
  3. end

postfork(block)

Execute code after each fork().

  1. postfork do
  2. puts "uWSGI server called fork()"
  3. end

rpc(name, block)

Register code as a uWSGI RPC Stack function.

  1. rpc 'helloworld' do
  2. return "Hello World"
  3. end
  4.  
  5. rpc 'advancedhelloworld' do |x,y|
  6. return "x = #{x}, y = #{y}"
  7. end

mule(id?, block)

Execute code as a Mule brain.

  1. mule 1 do # Run in mule 1
  2. puts "I am the mule #{UWSGI.mule_id}"
  3. end
  4.  
  5. mule do # Run in first available mule
  6. puts "I am the mule #{UWSGI.mule_id}"
  7. end

After the function returns, the mule will be brainless. To avoid this, put the code in a loop, or use muleloop.

muleloop(id?, block)

Execute code in a mule in looped context.

  1. muleloop 3 do
  2. puts "I am the mule #{UWSGI.mule_id}"
  3. sleep(2)
  4. end

SpoolProc

A subclass of Proc, allowing you to define a task to be executed in the Spooler.

  1. # define the function
  2. my_long_running_task = SpoolProc.new {|args|
  3. puts "I am a task"
  4. UWSGI::SPOOL_OK
  5. }
  6.  
  7. # spool it
  8. my_long_running_task.call({'foo' => 'bar', 'one' => 'two'})

MuleFunc

Call a function from any process (such as a worker), but execute in a mule

  1. i_am_a_long_running_function = MuleFunc.new do |pippo, pluto|
  2. puts "i am mule #{UWSGI.mule_id} #{pippo}, #{pluto}"
  3. end
  4.  
  5. i_am_a_long_running_function.call("serena", "alessandro")

The worker calls 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, add an ID parameter. The following would only use mule #5.

  1. i_am_a_long_running_function = MuleFunc.new 5 do |pippo,pluto|
  2. puts "i am mule #{UWSGI.mule_id} #{pippo}, #{pluto}"
  3. end
  4.  
  5. i_am_a_long_running_function.call("serena", "alessandro")

Real world usage

A simple Sinatra app printing messages every 30 seconds:

  1. # This is config.ru
  2.  
  3. require 'rubygems'
  4. require 'sinatra'
  5. require 'uwsgidsl'
  6.  
  7. timer 30 do |signum|
  8. puts "30 seconds elapsed"
  9. end
  10.  
  11. get '/hi' do
  12. "Hello World!"
  13. end
  14.  
  15. run Sinatra::Application

Or you can put your code in a dedicated file (mytasks.rb here)

  1. require 'uwsgidsl'
  2.  
  3. timer 30 do |signum|
  4. puts "30 seconds elapsed"
  5. end
  6.  
  7. timer 60 do |signum|
  8. puts "60 seconds elapsed"
  9. end

and then load it with

  1. uwsgi --socket :3031 --rack config.ru --rbrequire mytasks.rb --master --processes 4