Search code examples
ruby-on-railsrubyactionmailersidekiq

How to Query A Mailer Task


I'm trying get my system to automatically remove relevant scheduled sidekiq tasks when their relevant object is removed, via the 'destroy' method in the controller.

The tasks in question are Action Mailers added to the sidekiq 'scheduled' queue. At a specific point, I need to check each of these tasks and compare the arguments with a model ID.

To do this I populate an array of scheduled tasks, and iterate through them;

scheduled_queue = Sidekiq::ScheduledSet.new
scheduled_queue.each do |job| 
  condition = true if job.args[0] == @object.id
end

However I can't manipulate the arguments into any comparable form. All of the parameters seem to be present in [0] of the job.args array.

<%= job.args.size %> #Returns 1
<%= job.args.first %> #Returns below
---
job_class: ActionMailer::DeliveryJob
job_id: 28c966f3-3fbd-4963-a9cc-64e609803246
provider_job_id: 
queue_name: mailers
priority: 
arguments:
- ResponseMailer
- reminder_email
- deliver_now
- 29
- Third reminder
- 9095
executions: 0
locale: en
<%= job.args.first.class %> #Returns !ruby/class 'Hash'

Apparently this is a hash but job.args.first.keys throws an error.

undefined method `keys' for 9:Integer

How can I compare variables with the mailer arguments?


Solution

  • Managed to find the code to access a specific argument (x);

    job.args.first['arguments'].[x]
    

    The undefined method 'keys' error was being caused by tasks in the scheduled queue that were formatted differently. My mistake was to iterate through every task in the queue, rather than filtering out the irrelevant ones.