Git Product home page Git Product logo

async-requests's Introduction

AsyncRequest

Gem Version Build Status Code Climate Test Coverage

Summary

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.

Installation

Add gem 'async_request' to your gemfile

Run rails g async_request

Usage

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.

Configuration

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

Migration

If you were using an older version of the gem and would like to migrate, check our migration guide

About

This project is maintained by Alejandro Bezdjian and Alan Halatian and it was written by Wolox.

Wolox

License

AsyncRequest is available under the MIT license.

async-requests's People

Contributors

mdesanti avatar leoncalermo avatar icoluccio avatar sbalay avatar dependabot[bot] avatar leamotta avatar alanhala avatar alebian avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.