When developing Rails (or other) apps you find some tasks that need to run on the background to make the application more scalable. We normally use Sidekiq to execute these background jobs, but often we need to perform tasks that clients need the response, async_request
gives us the possibility of handling these type of tasks in a simple way.
Add gem 'async_request'
to your gemfile
Run rails g async_request
This gem provides a Sidekiq worker that will call an instance method named execute
of any given class and store the response in the database. In order to do this, we provide a model named AsyncRequest::Job
that will handle the queue of the worker with the params.
- IMPORTANT: The response of the worker will store the response as a string and the status code as an integer.
class SomeController < ApplicationController
def my_compute_heavy_endpoint_option_1
job = AsyncRequest::Job.create_and_enqueue(MyComputeHeavyWorker, { some: 'args' }, 'another arg')
render json: { token: job.token, url: async_request.job_url }, status: :accepted
end
def my_compute_heavy_endpoint_option_2
job = AsyncRequest::Job.create_and_enqueue(MyComputeHeavyWorker, { some: 'args' }, 'another arg')
render json: { token: job.token }, status: :accepted, location: async_request.job_url
end
end
class MyComputeHeavyWorker
def execute(options, string)
# Perform heavy task
[:ok, { message: 'success' }.to_json] # returning 200 is valid too
end
end
This will enqueue a JobProcessor
worker that will call MyComputeHeavyWorker.new.execute
with the args passed as parameters to create_and_enqueue
.
The client can then make a GET request to the returned URL sending the token
in a X-JOB-AUTHORIZATION
header (configurable). If the job's completed, it will get a response code and a json body. Otherwise, a HTTP status code accepted (202)
will be returned.
MyComputeHeavyWorker
must return an array with two components. The first one has be the status code (number or Rails symbol), the second one has be the response that will be sent to the client. Take into account that the response must be a string because it will be stored in the database.
There are some aspects of the gem you can configure:
# config/initializers/async_request.rb
AsyncRequest.configure do |config|
config.sign_algorithm = 'HS256' # This is the default, valid algorithms: HS256 and RS256
config.request_header_key = 'X-JOB-AUTHORIZATION' # This is the default
config.encode_key = Rails.application.secrets.secret_key_base
config.decode_key = Rails.application.secrets.secret_key_base
config.token_expiration = 1.day
end
If you were using an older version of the gem and would like to migrate, check our migration guide
This project is maintained by Alejandro Bezdjian and Alan Halatian and it was written by Wolox.
AsyncRequest is available under the MIT license.