我有以下代码。它的工作是根据浏览器提供的数据(使用sinatra)发送电子邮件。它会在20秒后将电子邮件发送到给定的地址。当我运行程序时,它会立即发送电子邮件,而不必等待时间。有谁能帮我解决这个问题吗?
require 'rubygems'
require 'sinatra'
require 'pony'
require 'resque'
require 'resque_scheduler'
require 'active_support/time'
Resque.redis = 'localhost:6379'
Resque::Scheduler.dynamic = true
def sendMail
Pony.mail({
:to => 'eldurotaduro@gmail.com',
:via => :smtp,
:via_options => {
:address => 'smtp.gmail.com',
:port => '587',
:enable_starttls_auto => true,
:user_name => 'EMAIL',
:password => 'PASSWD',
:authentication => :plain, # :plain, :login, :cram_md5, no auth by default
:domain => "localhost.localdomain" # the HELO domain provided by the client to the server
},
:body => 'roar'
})
end
class Roar
def self.queue; :app; end
end
class ChildJob
@message
@email
def setMess(mes)
@message = mes
end
def setMail(mail)
@email = mail
end
def self.queue; :app; sendMail; end
def self.perform
Pony.mail({
:to => 'eldurotaduro@gmail.com',
:via => :smtp,
:via_options => {
:address => 'smtp.gmail.com',
:port => '587',
:enable_starttls_auto => true,
:user_name => 'EMAILHERE@gmail.com',
:password => 'PASSWD',
:authentication => :plain, # :plain, :login, :cram_md5, no auth by default
:domain => "localhost.localdomain" # the HELO domain provided by the client to the server
},
:body => 'HAHAH'
})
end
end
get '/:email/:message/:time' do
email = params[:email]
message = params[:message]
time = params[:time]
time = time.to_i
Resque.enqueue_in(20.seconds, ChildJob)
end
最佳答案
将:app
符号保留在self.queue
中,因为这将设置默认队列(请参见this StackOverflow answer)。将sendMail
放入self.perform
方法中,因为这是您希望在满足计划时完成的事情。例如
def self.queue
:app
end
def self.perform
sendMail
end
关于ruby - Ruby:Resque立即排队,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15603409/