This collection of files is how I use PgDice in production. I'll describe the architecture here so you'll have a place to start.
-
tasks/poll_sqs.rake
is run using some sort of process manager like systemd on the ec2 instance. I like to run the poll_sqs stuff on my Sidekiq instances because they are the ones who eventually handle the work anyway. -
lib/sqs_poller.rb
is used to handle the looping logic for the rake task. It invokeslib/sqs_listener.rb
for each iteration. -
lib/sqs_listener.rb
calls AWS SQS to receive messages and then passes each one into thelib/sqs_listener/sqs_event_router.rb
to be routed to the correct message handler. -
Inside
lib/sqs_listener/sqs_event_router.rb
the message is parsed and passed through a case statement. This could be abstracted better but for now if the message has a field ofevent_type
and a value of"task"
then the router will send it off to theTaskEventHandler
which in this case islib/sqs_listener/typed_event_handler/task_event_handler.rb
-
In the
TaskEventHandler
the task is sent to a handler which responds to the task specified in the message body fieldtask
. -
The handler for the task (in this case,
DatabaseTasks
) handles the parameters for invoking the Sidekiq worker:PgDiceWorker
-
Finally, the
PgDiceWorker
is called and handles invokingPgDice
based on the parameters passed in.
Hopefully that wasn't too confusing. There's a lot of steps in here because the system that uses PgDice handles lots of different types of SQS events and needs to be as resilient as possible.