ActiveAsync aims to provide an interface for easily setting up ActiveRecord objects and ruby classes to run methods asynchronously.
ActiveAsync currently depends on ActiveSupport.
In your Gemfile
gem "activeasync"
Or via command line
gem install activeasync
Configure one of the supported adapters: :sidekiq, :resque, or :inline (useful for testing synchronously).
# config/initializers/activeasync.rbrequire"active_async"ActiveAsync.queue_adapter=:sidekiqBackground 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 asynchronouslyRun 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 saveActiveAsync 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 foregroundendYou can also manually set the Async background adapter to ActiveAsync::FakeResque or
any similar module/class that responds to #enqueue(*args):
beforedoActiveAsync.queue_adapter=:inlineendTo 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

