Skip to content

Active Elastic Job is a simple to use Active Job backend for Rails applications deployed on the Amazon Elastic Beanstalk platform.

License

Notifications You must be signed in to change notification settings

aonsager/active-elastic-job

 
 

Repository files navigation

Active Elastic Job

Build Status Gem Version

Active Elastic Job is a simple queuing backend implementation, targeting Rails >= 4.2 applications running on the Amazon Elastic Beanstalk platform. It provides an adapter for Rails' Active Job framework and a Rack middleware to consume messages pulled by the SQS daemon running in Elastic Beanstalk worker environments.

This gem allows Rails applications which run in Elastic Beanstalk environments to offload long running tasks into background jobs by simply

  • adding this gem to the bundle,
  • creating an Amazon SQS queue,
  • creating a worker environment
  • configuring Active Elastic Job as the queue adapter,
  • deploying the updated application (identical versions) to web and the worker environments.

Usage

  1. Add this line to your application's Gemfile:

     gem 'active_elastic_job'
    
  2. Create an SQS queue:

  • Log into your Amazon Web Service Console and select SQS from the services menu.
  • Create a new queue.
  1. Create a new user with SQS permissions:
  • Stay logged in and select the IAM service from the services menu.
  • Create a new user and store the credentials.
  • Attach the AmazonSQSFullAccess policy to this user.
  1. Create a worker environment:
  • Stay logged in and select the Elastic Beanstalk option from the services menu.
  • Select your application, click the Actions button and select Launch New Environment.
  • Click the create worker button, select the identical platform that you had chosen for your web environment.
  • In the Worker Details form, select the queue, that you created in step 2, as the worker queue, leave the MIME type to application/json.
  1. Configure Active Elastic Job as the queue adapter:
# config/application.rb
module YourApp
  class Application < Rails::Application
    config.active_job.queue_adapter = :active_elastic_job
  end
end
  1. Add three environment variables to the web environment
  • Select the web environment that is currently hosting your application and open the Software Configuration settings:
    • add AWS_ACCESS_KEY_ID and set it to access key id of the newly created user (from step 3),
    • add AWS_SECRET_ACCESS_KEY and set it to the secret access key of the newly created user (step 3),
    • add AWS_REGION and set it to the region of the SQS queue, created in step 2.
  1. Create an Active Job class:
$ bin/rails generate job resize_image --queue [name of queue that you chose in step 2]
invoke  test_unit
create    test/jobs/resize_image_job_test.rb
create  app/jobs/resize_image_job.rb

This generated job looks like this:

# app/jobs/resize_image_job.rb
class ResizeImageJob < ActiveJob::Base
  queue_as :default # here should be the name of the queue that was created in step 2.

  def perform(image)
    ImageResizer.resize(image) # long running operation
  end
end

Read more about Active Job in the Active Job Basics Guide.

  1. Deploy the application to both environments (web and worker).

Caveats

  • Both environments need to have the same value for the environment variable SECRET_KEY_BASE.
  • Jobs can not be scheduled more than 15 minutes into the future, see the Amazon SQS API reference.

About

Active Elastic Job is a simple to use Active Job backend for Rails applications deployed on the Amazon Elastic Beanstalk platform.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 81.0%
  • HTML 12.2%
  • Shell 4.3%
  • CSS 1.3%
  • JavaScript 1.2%