This article introduces how to install ActiveJob using Sidekiq in Rails.
๐ Installation
Prepare Redis
Install Redis on macOS by Homebrew:
brew install redis |
Install Redis on Linux(CentOS) by yum
or etc:
sudo yum install -y redis |
Edit Gemfile
Add some gem names to Gemfile
. After then, please execute bundle install
in terminal.
# Job queue |
๐ณ Configuration
Please edit config/application.rb
:
config.active_job.queue_adapter = :sidekiq |
Please add & edit config/sidekiq.yml
:
:concurrency: 2 |
Please edit routes.rb
:
require 'sidekiq/web' |
After then, you can see sidekiq admin page from http://localhost:3000/sidekiq.
๐ How to create class of ActiveJob
rails g job hoge |
after then, please edit job file.
class HogeJob < ActiveJob::Base |
If you want to call the job function, please write the follow.
HogeJob.perform_later("hoge") |
๐ฃ Admin page
Authentication with Devise
If you want to add authentication with Devise, please add config/routes.rb
.
# config/routes.rb |
Basic Authentication
require "sidekiq/web" |
๐ Execution
You can run a process of sidekiq by as follows:
bundle exec sidekiq -C config/sidekiq.yml |
๐ค RSpec
Before you write RSpec for the above job, you should set configuration to spec/rails_helper.rb
.
RSpec.configure do |config| |
Now, you can write RSpec for the Job in spec/jobs/example_job_spec.rb
like this:
require 'rails_helper' |
๐ Health Check by rake task(cron job)
namespace :active_job_sidekiq_queue do |
๐ Health Check by API(Zapix)
Zapix ใจ API ใ็ตใฟๅใใใฆ Health Check ใ่กใๆนๆณใใใใใใงใใ
There is a a way to check queue status by combining Zapix and API.
require 'sidekiq/api' |
๐ Special Thanks
- Testing Rails ActiveJob with RSpec โ Medium
- http://milk1000cc.hatenablog.com/entry/2015/05/15/175113
- http://qiita.com/nysalor/items/94ecd53c2141d1c27d1f
- https://qiita.com/zaru/items/8385fdddbd1be25fe370
- https://github.com/mperham/sidekiq/wiki/Active-Job
- https://itnext.io/sidekiq-overview-and-how-to-deploy-it-to-heroku-b8811fea9347
- http://system.blog.uuum.jp/entry/2017/10/17/110000
- https://github.com/mperham/sidekiq/wiki/Monitoring
- https://qiita.com/zaru/items/bfa87b422bcad30b11fe
๐ฅ Recommended VPS Service
VULTR provides high performance cloud compute environment for you.
Vultr has 15 data-centers strategically placed around the globe, you can use a VPS with 512 MB memory for just $ 2.5 / month ($ 0.004 / hour).
In addition, Vultr is up to 4 times faster than the competition, so please check it => Check Benchmark Results!!