Help us understand the problem. What is going on with this article?

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

ポイント

  • 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

https://www.rubydoc.info/github/phstc/shoryuken

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

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

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away