LoginSignup
1
0

More than 3 years have passed since last update.

Shoryuken の worker を Rails console で実行

Posted at

SQS への enqueue が何らかの AWS のサービスによるものだった場合、テストがしづらいことがあるので、 Rails console で叩いて動作確認できるようにする方法

name version
shoryuken 5.0.1
rails 5.2.4.1
aws sdk core 3.56.0
aws sdk sqs 1.17.0
$ rails console
[1] pry(main)> worker = SomethingSqsWorker # please change
[2] pry(main)> body = {} # please change
[3] pry(main)> endpoint = "" # config などで設定
[4] pry(main)> client = Aws::SQS::Client.new(endpoint: endpoint)
[5] pry(main)> message = Shoryuken::Message.new(client, Shoryuken::Queue.new(client, worker.shoryuken_options_hash["queue"]), Aws::SQS::Message.new(queue_url: queue.url, receipt_handle: "receipt_handle"))
[6] pry(main)> worker.new.perform(message, body)

receipt_handle は普通特に参照しないのかなと思って適当な文字列にしてあります。適宜変えてください

snippet

worker, body, endpoint を設定したあとの実行部分です。繰り返し使うときは最後の行だけで ok です

client = Aws::SQS::Client.new(endpoint: endpoint)
message = Shoryuken::Message.new(client, Shoryuken::Queue.new(client, worker.shoryuken_options_hash["queue"]), Aws::SQS::Message.new(queue_url: queue.url, receipt_handle: "receipt_handle"))
worker.new.perform(message, body)

一行を長くしたくない人向け

client = Aws::SQS::Client.new(endpoint: endpoint)
queue = Shoryuken::Queue.new(client, worker.shoryuken_options_hash["queue"])
sqs_msg = Aws::SQS::Message.new(queue_url: queue.url, receipt_handle: "receipt_handle")
message = Shoryuken::Message.new(client, queue, sqs_msg)

worker.new.perform(message, body)

一行にこだわりたい人向け

Aws::SQS::Client.new(endpoint: endpoint).instance_eval { worker.new.perform(Shoryuken::Message.new(self, Shoryuken::Queue.new(self, worker.shoryuken_options_hash["queue"]), Aws::SQS::Message.new(queue_url: queue.url, receipt_handle: "receipt_handle")), body) }
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