LoginSignup
1
0

More than 1 year has passed since last update.

#Rails の ActiveJob + shoryuken gem で #AWS SQS キューイング非同期処理を試してみる

Last updated at Posted at 2019-09-17

ポイント

  • shoryuken の Worker を起動する
  • 処理内容は Worker に表示される

Worker の起動

  • 必ず --rails フラグをつけること。こうしないとActiveJob からキューイング自体はできるものの、肝心の perform での処理内容が実行されな買った。
  • --config で yml ファイルを指定する。
  • --require で Worker の Ruby ファイルを指定する。
bundle exec shoryuken --config config/shoryuken.yml --require --rails

Help

shoryuken --help

shoryuken [options]
    -c, --concurrency INT            Processor threads to use
    -d, --daemon                     Daemonize process
    -q, --queue QUEUE[,WEIGHT]...    Queues to process with optional weights
    -r, --require [PATH|DIR]         Location of the worker
    -C, --config PATH                Path to YAML config file
    -R, --rails                      Attempts to load the containing Rails project
    -L, --logfile PATH               Path to writable logfile
    -P, --pidfile PATH               Path to pidfile
    -v, --verbose                    Print more verbose output
    -V, --version                    Print version and exit
    -h, --help                       Show help

AWS SQS での キュー作成

example1 / example2 / example3 を作成しておく
image

shoryuken.yml

  • AWSの接続情報を書く。
  • 扱うキューの種類を書いておく。ここに書いたものがWorkerに自動反映されるとか、そういうことではないっぽい?
aws:
  access_key_id:      <%= ENV['AWS_ACCESS_KEY_ID'] %>
  secret_access_key:  <%= ENV['AWS_SECRET_ACCESS_KEY'] %>
  region:             <%= ENV['AWS_REGION'] %>
concurrency: 1
queues:
  - example1
  - example2
  - example3

shoryuken_worker.rb

  • rails で起動する場合は無視されそう?
  • Rails + AcitiveJob の場合は ジョブ側に perform を書き、pure ruby の場合は Worker に perform を書くということなのかもしれない

Getting Started · phstc/shoryuken Wiki

initializers/shoryuken.rb

  • ログの扱いなどを記載する。
  • Rails.logger.level = :debug だと Worker で大量のメッセージが表示されるため :info にしておいた。
  • Shoryukenでは Shoryuken 専用の Logger を利用してロギングするっぽい。
Shoryuken.configure_server do |config|
  # Replace Rails logger so messages are logged wherever Shoryuken is logging
  # Note: this entire block is only run by the processor, so we don't overwrite
  #       the logger when the app is running as usual.

  Rails.logger = Shoryuken::Logging.logger
  Rails.logger.level = :info

  # config.server_middleware do |chain|
  #  chain.add Shoryuken::MyMiddleware
  # end

  # For dynamically adding queues prefixed by Rails.env
  # Shoryuken.add_group('default', 25)
  # %w(queue1 queue2).each do |name|
  #   Shoryuken.add_queue("#{Rails.env}_#{name}", 1, 'default')
  # end
end

# config/initializers/shoryuken.rb
Shoryuken.active_job_queue_name_prefixing = true

config/application.rb

アダプタにshoryukenを指定しておく。

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 RailsActiveJobExample
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 6.0

    # config.active_job.queue_adapter = ENV['QUEUE_ADAPTER'].present? ? ENV['QUEUE_ADAPTER'].to_sym : :sidekiq
    config.active_job.queue_adapter = :shoryuken

    # 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

make job

rails runner か何かでジョブを実行する。

bundle exec rails runner 'ShoryukenJob.perform_later("ABC")'

昇竜拳!

image

Original by Github issue

チャットメンバー募集

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

Twitter

1
0
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
1
0