ayl-beanstalk

This gem provides a reference implementation of a beanstalk Engine for the ayl gem.

If you want to use this technology in a Rails 4.* application you can do the following in your config/application.rb or config/environments/*.rb files:

YourApp::Application.configure do

...

config.after_initialize do
  # Set up a delay for AYL so we make sure the object is
  # saved before we pick it up off the queue. Defaults to 0
  # Ayl::MessageOptions.default_delay = 2 

  # Set up a priority fo AYL. The lower the priority number
  # the higher the priority. The default is 512.
  # Ayl::MessageOptions.default_priority = 1

  # Set up a time to run for AYL. This is the number of seconds
  # that a worker has to complete the job before the queuing engine
  # re-inserts the message into the queue. Defaults to 120 seconds
  # Ayl::MessageOptions.default_time_to_run = 120

  # Set up the name of the queue to use in the queuing engine for
  # messages. Typically, you will use the application version for
  # the name of the queue so that you won't have problems with items
  # left in the queue when the version of the software changes.
  # The default is 'default'.
  # Ayl::MessageOptions.default_queue_name = '1.2.3'

  # Set the default failed job handler to 'decay'. The default is
  # 'delete', another option is to 'bury'. So, 'delete' will delete
  # a failed job. 'bury' will move the job to the 'buried' state
  # requiring a 'kick' to place it back in the ready state. 'decay'
  # means that if the job has been reserved less than 'failed_job_count'
  # (see below), it will be placed back on the tube with a delay of
  # 'failed_job_delay' (see below). This allows jobs to be retried
  # with a delay. This is handy for the case where the jobs may be
  # dependent on a third party that can have spotty availability 
  # (like anything on the internet).
  Ayl::MessageOptions.default_failed_job_handler = 'decay'

  # Set the default failed job count to '3'. That is the default.
  # If the failed job handler is 'decay', this means it retry the
  # job until it has been reserved three times (initial try, then
  # two failures).
  Ayl::MessageOptions.default_failed_job_count = 4

  # Set the default failed job delay to '30'. That is the default.
  # If the failed job handler is 'decay', this means that there 
  # will be a 30 second delay between each retry of the job.
  Ayl::MessageOptions.default_failed_job_delay = 10

  # Set up the logger for Ayl
  Ayl::Logger.instance.logger = ::Rails.logger

  # Set up the beanstalk engine for use
  Ayl::Engine.add_engine(Ayl::Beanstalk::Engine.new('localhost', 8903)
end

end

If beanstalkd is running at the time your application starts up, your async messages will be sent to beanstalkd. If beanstalkd is NOT running, your messages will be invoked synchronously (without errors).

Once you get your messages in beanstalkd, you will probably want them to get executed (or what would be the point?). This gem provides a couple of scripts to assist in starting up a worker process to read messages off the beanstalkd queue and process them.

  • ayl_worker - This script runs in the foreground and processes messages off the beanstalkd queue.

  • ayl_worker_control - This script runs the ayl_worker script as a daemon to process messages in the background.

For example, if you want to run the worker in the foreground you would invoke the following:

ayl_worker -a /var/www/application/current -r -e production -t default

If you want to have the script run in the background:

ayl_worker_control start -- -a /var/www/application/current -r -e production -t default -p /tmp

or to stop the background script:

ayl_worker_control stop -- -a /var/www/application/current -r -e production -t default -p /tmp

The ayl_worker_control script can easily be invoked by an /etc/init.d script to allow your workers to start at server boot time.

Contributing to ayl-beanstalk

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright © 2011-2015 [email protected]. See LICENSE.txt for further details.