Skip to content

Instantly share code, notes, and snippets.

@adamluzsi
Last active May 18, 2016 09:02
Show Gist options
  • Save adamluzsi/4a990414dc4ddac16ce7e92328b64894 to your computer and use it in GitHub Desktop.
Save adamluzsi/4a990414dc4ddac16ce7e92328b64894 to your computer and use it in GitHub Desktop.
rack-app-worker

Simple usage

On the webs start the rack application as how you usualy do. On the worker containers or server start with $ rack-app workers start

or if you want run as a service in the background use the -d flag $ rack-app workers start -d

to stop the daemonized process $ rack-app workers stop

The workers use rabbitmq as message broker so you can easily scale horizontally the workers as how your pocket allow this. By default the worker will scale vertically on the server as much as possible if the load requires.

You can configurate the behavior with Linux variables:

ENV Variables

RABBITMQ_URL

this is required for connecting to rabbitmq

WORKER_QOS

this will manage how much message should be prefetched to a consumer. The default value is 50.

WORKER_HEARTBEAT_INTERVAL

This variable manage the time delay between a new consumer creation or remove based on queue size demand

WORKER_MAX_CONSUMER_NUMBER

This is used for limiting the maximum vertical of worker process count use

WORKER_LOG_LEVEL

This is used to set the log output level for the extensions log messages

WORKER_STDOUT

If this configure, the background process will pipe all stdout io actions to the given output.

WORKER_STDERR

Same as WORKER_STDOUT but for ERR

WORKER_NAMESPACE

If you plan using multiple application on the same container/server than you should use worker namespace to add application specific uniq part to the queues.

WORKER_CLUSTER

this is an optional env variable, this defines the workers cluster such as 'backup' or 'secondary'. This is useful when you have two seperated database and you want populate both at once, but you only want one public web api which accept the incoming content.

require 'rack/app'
require 'rack/app/worker'
class App < Rack::App
apply_extensions :worker
worker :payload_saver do
def persist_payload(payload, hash_with_sym)
puts 'event received'
File.write(hash_with_sym[:file_path], YAML.dump([payload, hash_with_sym]))
end
end
worker :sleepy do
def some_heavy_lifting_that_takes_time
Kernel.sleep(5)
end
end
get '/' do
workers[:payload_saver].send.persist_payload(payload, {:file_path => params['file_path']})
end
get '/sleepy' do
workers[:sleepy].send.some_heavy_lifting_that_takes_time
end
end
run App
source 'https://rubygems.org'
gem 'rack-app'
gem 'rack-app-worker'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment