Skip to content

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.rbrequire"active_async"ActiveAsync.queue_adapter=:sidekiq

Background class methods

classHeavyLifterincludeActiveAsync::Asyncdefself.lift(*stuff)# heavy liftingendendHeavyLifter.async(:lift,1,2,3)

With ActiveRecord and Rails

# app/models/risky_business.rbclassRiskyBusiness < ActiveRecord::Basedefparty_time# all night longendendbusiness=RiskyBusiness.lastbusiness.async(:party_time)# runs business#party_time asynchronously

Run callbacks asynchronously

classLateNite < ActiveRecord::Baseafter_save:drive_home,:async=>truedefdrive_home# traffic jamendendlate_nite=LateNite.lastlate_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.rbrequire'active_async/rspec'# spec/models/late_nite_spec.rbit"drive home after late nite save",:stub_resquedo# all methods run in foregroundend

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

beforedoActiveAsync.queue_adapter=:inlineend

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

Stars

9 stars

Watchers

24 watching

Forks

Releases

Packages

Contributors

Languages