LoginSignup
7
1

More than 3 years have passed since last update.

railsでsidekiq-schedulerを動かす

Posted at

公式のUsageはrailsで動かす前提じゃなかったので、railsで動かす場合のUsage的な何か。

  • 0. ばーじょん
Gemfile.lock
    rails (6.0.1) 
    sidekiq (6.0.7)
    sidekiq-scheduler (3.0.1)
  • 1. Gemfile書く
Gemfile
gem 'sidekiq-scheduler'
gem 'sinatra', require: false # ダッシュボードいらない場合はいらない
  • 2. 初期設定を書く

herokuで動かす場合は環境変数に設定しておくこともできるので、その場合はなくても構わない、多分。

config/initializers/sidekiq.rb
Sidekiq.configure_server do |config|
  config.redis = { url: ENV.fetch('REDIS_URL') { 'redis://localhost:6379' } }
end

Sidekiq.configure_client do |config|
  config.redis = { url: ENV.fetch('REDIS_URL') { 'redis://localhost:6379' } }
end
  • 3. ジョブを作る

app/jobs/内に作る。ApplicationJobを継承していれば自動的にsidekiq-schedulerがジョブとして認識してくれる。ここ参照

app/jobs/hello_world_job.rb
class HelloWorldJob < ApplicationJob
  def perform
    puts 'test'
  end
end
  • 4. ジョブを登録する

こっちの拡張子はymlですよ。

config/sidekiq.yml
:schedule:
  hello_world:
    every: '1m'
    class: HelloWorldJob
  • 5. ダッシュボード見れるようにする
config/routes.rb
require 'sidekiq/web'
require 'sidekiq-scheduler/web'

Rails.application.routes.draw do
  mount Sidekiq::Web, at: "/sidekiq"
end

これで http://localhost:3000/sidekiq のURLでsidekiqのダッシュボードが見れるようになる。

  • 6. sidekiq動かす

rails sとは別プロセスで動かす必要がある。

$ bundle exec sidekiq

こんな感じで見れたら成功。

_DEVELOPMENT__Sidekiq.png

7
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
1