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.
-
Add this line to your application's Gemfile:
gem 'active_elastic_job'
-
Create an SQS queue:
- Log into your Amazon Web Service Console and select SQS from the services menu.
- Create a new queue.
- 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.
- 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.
- 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
- 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.
- 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.
- Deploy the application to both environments (web and worker).
- 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.