Project

sideband

0.0
No commit activity in last 3 years
No release in over 3 years
Run simple workers in a separate thread
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

>= 5.0.8
>= 0
 Project Readme

Sideband

Build Status Code Climate Coverage Status Dependency Status

Run simple jobs in a separate sideband thread.

Sideband makes it easy to pass small jobs off to a separate in-process thread. It makes no attempt to handle errors, nor return any results. Its primary focus is queueing up potentially IO blocking bits of code, where the results of which are not necessarily vital to your application's business logic.

Installation

Add this line to your application's Gemfile:

gem 'sideband'

And then execute:

$ bundle

Or install it yourself as:

$ gem install sideband

Usage

To be used Sideband needs to be intialized, typically in an Rails initializer (but can be used outside of Rails).

Sideband.initialize!

To pass work off to Sideband, you can add anything that is callable (procs, lambdas, workers) to its queue:

Sideband.queue << -> { Something.expensive }

Sideband will queue the work, then return immediately. The work will get called whenever the thread scheduler schedules the worker thread--typically after your controller renders.

Sideband is truly fire-and-forget, any exceptions are caught and thrown away. If you want to handle exception, you should probably do so in a custom worker.

class MetricWorker < Sideband::Worker
  def initialize(params)
    @params = params
  end

  def call
    Metric.create!(@params)
  rescue ActiveRecord::RecordInvalid
    Rails.logger.error("Could not save Metric: #{@params}")
  end
end

Sideband.queue << MetricWorker.new(params[:metric])

 # or Sideband::Workers can enqueue themselves

Metricworker.new(params[:metric]).enqueue

A practical Rails example:

class UsersController < ApplicationController

  def create
    @user = User.create(params[:user])
    Sideband.queue << -> { UserMailer.welcome_email(@user).deliver }
    render :welcome
  end
end

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Bitdeli Badge