2013-03-24 101 views
1

我有以下代碼。它的工作是根據通過瀏覽器提供的數據發送電子郵件(使用Sinatra)。它發送一封電子郵件到20秒後給出的地址。當我運行程序時,它立即發送電子郵件,而不用等待時間。任何人都可以幫我解決這個問題。Ruby:立即排隊排隊

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 => '[email protected]', 
    :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 => '[email protected]', 
    :via => :smtp, 
    :via_options => { 
    :address    => 'smtp.gmail.com', 
    :port     => '587', 
    :enable_starttls_auto => true, 
    :user_name   => '[email protected]', 
    :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 
+0

這不是問題嗎? 'def self.queue; :應用程序;發送郵件;看起來你試圖設置默認隊列,但實際上你正在運行'sendMail'(':app'什麼都不做)。 – iain 2013-03-24 23:12:45

+0

但是不會'self.queue'方法在隊列裏面運行whatevers? – user1938700 2013-03-26 13:18:44

+0

':app'是一個符號,那行代表什麼?沒有。方法的_last_行非常重要,因爲它是返回值,但是因爲'sendMail'是最後一行':app'本質上什麼都不做。 – iain 2013-03-26 16:14:23

回答

0

保持:app符號self.queue因爲這就是設置默認隊列(見this StackOverflow answer)。將sendMail放入self.perform方法中,因爲這是您在計劃滿足時要完成的事情。例如

def self.queue 
    :app 
end 

def self.perform 
    sendMail 
end