Resources

Summary

# Gemfile
gem 'sidekiq'

# config/application.rb
module BackgroundJobsWithSidekiq
  class Application < Rails::Application
    config.active_job.queue_adapter = :sidekiq 
    # config.active_job.queue_adapter = Rails.env.production? ? :sidekiq : :async
  end
end

# config/initializers/sidekiq.rb
Sidekiq.configure_server do |config|
  config.redis = { url: 'redis://localhost:6379/0'  }
end

Sidekiq.configure_client do |config|
  config.redis = { url: 'redis://localhost:6379/0'  }
end

# config/routes.rb
Rails.application.routes.draw do
  require 'sidekiq/web'
  mount Sidekiq::Web => '/sidekiq'
  get 'users/create_random'
  get 'users/destroy_all'
  root to: 'users#index'
end

# users_controller.rb
  def create_random
    GenerateRandomUserJob.perform_later
    redirect_to root_path
  end

# app/jobs/generate_random_user_job.rb
# rails g job generate_random_user
class GenerateRandomUserJob < ApplicationJob
  queue_as :default

  def perform(*args)
    user = User.new
    user.first_name = Faker::Name.first_name
    user.last_name = Faker::Name.last_name
    user.email = Faker::Internet.email
    user.save!
    sleep 2
  end
end

# Terminal
sidekiq
2016-12-19T02:45:23.797Z 52134 TID-oxnqxvyn8 INFO: Booting Sidekiq 4.2.7 with redis options {:url=>"redis://localhost:6379/0"}


         m,
         `$b
    .ss,  $$:         .,d$
    `$$P,d$P'    .,md$P"'
     ,$$$$$bmmd$$$P^'
   .d$$$$$$$$$$P'
   $$^' `"^$$$'       ____  _     _      _    _
   $:     ,$$:       / ___|(_) __| | ___| | _(_) __ _
   `b     :$$        \___ \| |/ _` |/ _ \ |/ / |/ _` |
          $$:         ___) | | (_| |  __/   <| | (_| |
          $$         |____/|_|\__,_|\___|_|\_\_|\__, |
        .d$$                                       |_|

2016-12-19T02:45:24.203Z 52134 TID-oxnqxvyn8 INFO: Running in ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-darwin16]
2016-12-19T02:45:24.203Z 52134 TID-oxnqxvyn8 INFO: See LICENSE and the LGPL-3.0 for licensing details.
2016-12-19T02:45:24.204Z 52134 TID-oxnqxvyn8 INFO: Upgrade to Sidekiq Pro for more features and support: http://sidekiq.org
2016-12-19T02:45:24.218Z 52134 TID-oxnqxvyn8 INFO: Starting processing, hit Ctrl-C to stop