🖥

#Rails | rails new してから sidekiq + redis を起動してジョブを動かすまでの最小手順

2019/09/13に公開

Rails new

rails new your-project
cd your-project

Gemfile

+ gem 'sidekiq'
+ gem 'redis'
bundle install

Job

bundle exec rails generate job sample
vim app/jobs/sample_job.rb
class SampleJob < ApplicationJob
  queue_as :default

  def perform(*args)
+    puts 'Worked!'
+    puts Time.current
  end
end

Config

vim config/application.rb
require_relative 'boot'

require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module RailsNewSidekiq
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 6.0

+    config.active_job.queue_adapter = :sidekiq

    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration can go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded after loading
    # the framework and any gems in your application.
  end
end

Redis

brew install redis (Mac)
redis-server

image

Worker

bundle exec sidekiq

image

Run

bundle exec rails runner 'SampleJob.perform_later'

result in worker

2019-09-12T23:40:33.677Z pid=29375 tid=owiiogm93 class=SampleJob jid=0c56247c9f77a055dd35e78b INFO: start
Worked!
2019-09-12 23:40:33 UTC
2019-09-12T23:40:33.692Z pid=29375 tid=owiiogm93 class=SampleJob jid=0c56247c9f77a055dd35e78b elapsed=0.015 INFO: done

image

ref

https://github.com/YumaInaura/rails-new-sidekiq

Original by Github issue

https://github.com/YumaInaura/YumaInaura/issues/2424

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ

Twitter

https://twitter.com/YumaInaura

公開日時

2019-09-13

Discussion