【问题标题】:Ruby: Resque queues instantlyRuby:Resque 立即排队
【发布时间】:2013-03-27 12:17:15
【问题描述】:

我有以下代码。它的工作是根据通过浏览器(使用 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

【问题讨论】:

  • 这行不是问题吗? def self.queue; :app; sendMail; end 看起来您正在尝试设置默认队列,但实际上您正在运行 sendMail(:app 什么都不做)。
  • 但是self.queue 方法不会在队列中运行任何东西吗?
  • :app 是一个符号,那行有什么作用?没有什么。方法的 last 行很重要,因为它是返回值,但因为 sendMail 是最后一行 :app 基本上什么都不做。
  • 所以问题的解决方案是删除:app 符号?
  • 我不使用 Resque,只使用 Ruby,但阅读文档不应该是 sendMail 在 the perform method 中吗? (你得到self.queue技巧from here了吗?他们也用过perform)。

标签: ruby web redis sinatra


【解决方案1】:

将:app 符号保留在self.queue 中,因为这是设置默认队列的原因(请参阅this StackOverflow answer)。将sendMail 放入self.perform 方法中,因为这是您在满足计划时想要完成的事情。例如

def self.queue
  :app
end

def self.perform
  sendMail
end

【讨论】:

    猜你喜欢
    • 2015-09-14
    • 2020-05-22
    • 2017-07-28
    • 1970-01-01
    • 2017-12-02
    • 2013-11-13
    • 2021-07-02
    • 1970-01-01
    • 2012-02-22
    相关资源
    最近更新 更多