Closed dinesh-rdk closed 4 years ago
And this is my initializer file config/initializers/shoryuken.rb
# Create SQS queues
require 'aws-sdk-sqs'
require 'shoryuken'
# We inline the jobs in development & testing
# Start mock server by "moto_server sqs -p 4576 -H localhost"
if Rails.env.development? || Rails.env.test?
client = Aws::SQS::Client.new(
region: ENV["AWS_REGION"],
access_key_id: ENV["AWS_ACCESS_KEY_ID"],
secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"],
endpoint: 'http://localhost:4576',
verify_checksums: false
)
Shoryuken.configure_client do |config|
config.sqs_client = client
end
Shoryuken.configure_server do |config|
config.sqs_client = client
end
else
client = Aws::SQS::Client.new(
region: ENV['AWS_REGION'],
access_key_id: ENV['AWS_ACCESS_KEY_ID'],
secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
)
Shoryuken.sqs_client = client
end
# We have environment prepended queues in ActiveJob
all_queues = <AllQueues[]>
available_queues = client.list_queues.queue_urls.map{ |q| q.split('/').last }
queues_to_create = all_queues - available_queues
queues_to_create.each do |queue|
begin
puts "Creating queue #{queue}"
client.create_queue(
queue_name: queue,
attributes: {
"VisibilityTimeout" => "3600", # 3600 => 60 minutes
}
)
rescue => exception
puts "Error in creating queue #{queue}"
end
end
# After creating queues, make sure to subscribe with shoryuken
# RAILS_CACHE_CLASSES=true shoryuken -R -C ./shoryuken.yml
# By default, Shoryuken will make requests against SQS for getting the updated queue visibility timeout, so if you change the visibility timeout in SQS, Shoryuken will automatically update it.
# If you want to reduce the number of requests against SQS (therefore reducing your quota usage), you can disable this behavior by:
Shoryuken.cache_visibility_timeout = true
# Active Job allows you to prefix the queue names of all jobs. Shoryuken supports this behavior natively. By default, though, queue names defined in the config file (or passed to the CLI), are not prefixed in the same way. To have Shoryuken honor Active Job prefixes you must enable that option explicitly
Shoryuken.active_job_queue_name_prefixing = true
Hi
concurrency
is the number of threads that Shoryuken will initiate per group. Check this. You are probably getting some I/O waiting.
Understand you probably have some specific needs but having threads allocated per group/per queue, oftentimes is not the best resource usage.
Concurrency works, but there you need to check if your server/container can handle that I/O based on the number of threads.
BTW Shoryuken does batch fetching, so a single request can request up to 10 messages. That depends on the number of available threads.
Hi @phstc Thank you for your explanation. I will look forward to modify the way I use it :)
Hi @phstc
Just for testing purpose, I modified my code to
class Queue1Job < ActiveJob::Base
queue_as 'Queue1'
def perform(message)
if message.nil?
10.times { |i| Queue2Job.perform_later(i) }
else
sleep(2)
Queue2Job.perform_later(message)
end
end
end
class Queue2Job < ActiveJob::Base
queue_as 'Queue2'
def perform(message)
sleep(2)
Queue3Job.perform_later(message)
end
end
class Queue3Job < ActiveJob::Base
queue_as 'Queue3'
def perform(message)
sleep(2)
Queue1Job.perform_later(message)
end
end
And my shoryuken.yml
config is as
groups:
group1:
concurrency: 20
queues:
- Queue1
group2:
concurrency: 20
queues:
- Queue2
group3:
concurrency: 20
queues:
- Queue3
After running Queue1Job.perform_later(nil)
from rails console, the messages started enquing and jobs get executed.
After a minute of running, how many requests per second should Shoryuken send to Mock SQS server?
Hi @dinesh-rdk
Every time you call perform_later
it should enqueue a message to SQS.
I also believe concurrency 60 might be a lot of threads depending of the type of processing you are doing. You may want to check htop
to see if you are getting some I/O wait.
Hi @phstc Thank you for this wonderful library. I have been using RabbitMQ for queuing messages and now I am thinking of shifting to SQS.
I tested in my local with mock server as suggested in https://github.com/phstc/shoryuken/wiki/Using-a-local-mock-SQS-server
I have a lot (67) queues & I want them all to be independent (i.e. Don't want any queue to wait for any other queue to process)
So I configured shoryuken as
By default
delay
is 0. So I expect shoryuken to poll messages from each queue in SQS every second.But the logs in mock server shows ~20 requests per second on average.
Because of it SQS seems very slow compared to Rabbit. Can you please guide me to make here?