Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use ActiveJob with Rails 4.1?

For reasons (ahem ActiveAdmin), I'm currently unable to use Rails 4.2 on a new project.

However, the ActiveJob feature looks like a nice abstraction, so I'd like to use it if possible.

Does anyone have any experience getting it working with Rails 4.1?

like image 503
elsurudo Avatar asked Oct 20 '22 00:10

elsurudo


1 Answers

UPDATE

There's a better solution now :), try this gem activejob_backport, easier installation, same functions as rails 4.2.


CAUTION: OUTDATED CONTENT BELOW

To use ActiveJob in Rails 4.1, you need to do these first.

# in Gemfile
gem 'activejob'

# in your terminal
bundle

# create a config/initializers/active_job.rb
require 'active_job'
# or any other supported backend such as :sidekiq or :delayed_job
ActiveJob::Base.queue_adapter = :inline

Then you should be able to reference ActiveJob in your rails app, to create and enqueue a job:

# app/jobs/guests_cleanup_job.rb
class GuestsCleanupJob < ActiveJob::Base
  queue_as :default

  def perform(*args)
    # Do something later
  end
end

# usage
GuestsCleanupJob.enqueue(record)
GuestsCleanupJob.enqueue(record, options)

There're some gotchas though, biggest difference is it's not exactly the same ActiveJob inside Rails 4.2.beta, like lacks of generators, callbacks and syntax are a bit different. I've written a blog post if you want to dig more: http://kinopyo.com/blog/use-activejob-in-rails-4-1/

like image 54
kinopyo Avatar answered Oct 23 '22 01:10

kinopyo