ActiveJob

Episode #9 by Teacher's Avatar David Kimura

Summary

ActiveJob is a built in wrapper within Rails 4 for your background processors. By adding this layer to your application, swapping background processors is much easier as you will only be affecting a limited number of files.
rails activejob background processing 5:24

Resources

Summary

# bash
rails generate job contact_mailer

# app/jobs/contact_mailer_job.rb
  class ContactMailerJob < ActiveJob::Base
    queue_as :default

    def perform(from,subject,message)
      ContactMailer.send_contact(from,subject,message)
    end
  end

# visitors_controller.rb
  ContactMailerJob.perform_later(from,subject,message)

# Gemfile
  gem 'daemons'
  gem 'delayed_job'
  gem 'delayed_job_active_record'
  gem 'whenever', require: false

# bash
  rails generate delayed_job:active_record
  rake db:migrate

# config/application.rb
  config.active_job.queue_adapter = :delayed_job

# config/schedule.rb
  every :reboot do
    #`bin/delayed_job -n2 start`
    command 'bin/delayed_job -n2 start'
  end

# bash
  whenever -help
  whenever -w
  crontab -l