简体   繁体   中英

How do I programmatically find out the schedule of a delayed mailer job with Resque Mailer and Resque scheduler?

I am trying to display the next time an email is scheduled using any or all of the below arguments as inputs. I'm using resque, resque-scheduler and resque-mailer.

resque-web显示

For example, above are the delayed jobs as displayed in the resque web interface. So I'd like to input "game_starting_reminder" and/or 226 and/or "Beat Box" and be able to then display the timestamp as such:

"Next scheduled email: 2017-10-31 at 9:30 pm".

However, when I try to call for the information in the console, the below is the output I receive

I've tried extending the delay_extensions and methods and using the find_delayed_selection method but that doesn't seem to work. For example this:

    [18] pry(main)> Resque.find_delayed_selection { |job| job["class"] == QuizMailer}
TypeError: no implicit conversion of String into Integer

Or this:

[32] pry(main)> Resque.find_delayed_selection { { 
[32] pry(main)*     "class": "QuizMailer",            
[32] pry(main)*     "args": ["game_starting_reminder", [226, "Beat Box"]],            
[32] pry(main)*     "queue": "mailer"            
[32] pry(main)* }}            
=> ["{\"class\":\"QuizMailer\",\"args\":[\"game_starting_reminder\",[226,\"Beat Box\"]],\"queue\":\"mailer\"}",
 "{\"class\":\"QuizMailer\",\"args\":[\"game_ending_reminder\",[226,\"Beat Box\"]],\"queue\":\"mailer\"}"]

Any other method I can use here? Or tips. Thank you!

Figured it out. The scheduled_at method is the best candidate here for the job.

First step is to add the DelayingExtensions module to the project. I just added the file from the resque source code on Github to initializers and then in resque.rb added the line:

 #resque.rb
rails_root = ENV['RAILS_ROOT'] || File.dirname(__FILE__) + '/../..'
rails_env = ENV['RAILS_ENV'] || 'development'

resque_config = YAML.load_file(rails_root + '/config/resque.yml')
Resque.redis = resque_config[rails_env]
include DelayingExtensions

I modified the scheduled_at method from the github source code slightly because I couldn't get it to work as is and changed the name of the method to scheduled_for_time

   #delaying_extensions.rb
   def scheduled_for_time(klass, *args)
    args = args[0]
    search = encode(job_to_hash(klass, args))
    redis.smembers("timestamps:#{search}").map do |key|
      key.tr('delayed:', '').to_i
    end
   end

In this case, we can do the following in the console:

    [2] pry(main)> klass =QuizMailer
    => QuizMailer
    [4] pry(main)> args = ["game_starting_reminder", [230, "Beat Box"]]
    => ["game_starting_reminder", [230, "Beat Box"]]
    [5] pry(main)> Resque.scheduled_for_time(QuizMailer, args)
    => [1515081600]
    [6] pry(main)> Time.at(_.first)
     => 2018-01-04 21:30:00 +0530

Voila!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM