Skip to content

challengepost/activeasync

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

77 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ActiveAsync

ActiveAsync aims to provide an interface for easily setting up ActiveRecord objects and ruby classes to run methods asynchronously.

ActiveAsync currently depends on ActiveSupport.

Build Status

Code Climate

Install

In your Gemfile

gem "activeasync"

Or via command line

gem install activeasync

Usage

Configure one of the supported adapters: :sidekiq, :resque, or :inline (useful for testing synchronously).

# config/initializers/activeasync.rb
require "active_async"

ActiveAsync.queue_adapter = :sidekiq

Background class methods

class HeavyLifter
  include ActiveAsync::Async

  def self.lift(*stuff)
    # heavy lifting
  end
end

HeavyLifter.async(:lift, 1, 2, 3)

With ActiveRecord and Rails

# app/models/risky_business.rb
class RiskyBusiness < ActiveRecord::Base
  def party_time
    # all night long
  end
end

business = RiskyBusiness.last
business.async(:party_time)     # runs business#party_time asynchronously

Run callbacks asynchronously

class LateNite < ActiveRecord::Base
  after_save :drive_home, :async => true

  def drive_home
    # traffic jam
  end
end

late_nite = LateNite.last
late_nite.save                # runs late_night#drive_home asynchronously after save

Testing

ActiveAsync comes with some helpers support for RSpec.

To remove Resque dependency from some of your specs, use the :stub_resque option in selected spec blocks. Async methods will run in the foreground.

# spec/spec_helper.rb
require 'active_async/rspec'

# spec/models/late_nite_spec.rb
it "drive home after late nite save", :stub_resque do
  # all methods run in foreground
end

You can also manually set the Async background adapter to ActiveAsync::FakeResque or any similar module/class that responds to #enqueue(*args):

before do
  ActiveAsync.queue_adapter = :inline
end

Contributing

To contribute to activeasync, clone the project and submit pull requests in a branch with tests.

To run tests, install the bundle and migrate the test database:

$ bundle
$ cd spec/sample && bundle exec rake db:migrate db:test:prepare

About

Async interface for ActiveRecord and ruby classes

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •