每当宝石不自动运行?

时间:2016-03-17 19:30:54

标签: ruby-on-rails ruby heroku rubygems whenever

我有三个任务。每个人都使用heroku run rake challenges:"name of rake" --trace

Running rake challenges:challenge_accomplished --trace on livetochallenge... up, run.3478
** Invoke challenges:challenge_accomplished (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute challenges:challenge_accomplished
Anthony-Gallis-MacBook-Pro:livetochallenge galli01anthony$ heroku run rake challenges:challenge_clear_deadline --trace
Running rake challenges:challenge_clear_deadline --trace on livetochallenge... up, run.2956
** Invoke challenges:challenge_clear_deadline (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute challenges:challenge_clear_deadline
Anthony-Gallis-MacBook-Pro:livetochallenge galli01anthony$ heroku run rake challenges:challenge_reminder --trace
Running rake challenges:challenge_reminder --trace on livetochallenge... up, run.9671
** Invoke challenges:challenge_reminder (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute challenges:challenge_reminder
  Rendered user_mailer/challenge_reminder.html.erb within layouts/mailer (213.7ms)

但他们不会在指定的时间内自动触发。怎么会这样?

enter image description here

应用/ LIB /任务/ accomplished.rake

namespace :challenges do
    desc 'Make completed habit challenges become past & accomplished'
    task challenge_accomplished: :environment do
    Challenge.all.each do |challenge| 
        if challenge.date_started != nil 
          if challenge.days_left_challenged <= 0
            challenge.update_attributes(deadline: :date_started, accomplished: true, send_email: nil)
          end
        end  
    end 
    end
end

应用/ LIB /任务/ clear_deadline.rake

namespace :challenges do
    desc 'Removes :deadline from present habit challenges'
    task challenge_clear_deadline: :environment do
    Challenge.all.each do |challenge|
        if challenge.date_started == Date.current
          challenge.update_attributes(deadline: nil, accomplished: false)
        end
    end 
    end
end

应用/ LIB /任务/ reminder.rake

namespace :challenges do
  desc 'Send email to users who want challenge reminder'
  task challenge_reminder: :environment do
    Challenge.all.each do |challenge|
        if challenge.send_email[Date.current.wday] == Date::ABBR_DAYNAMES[Date.current.wday].downcase 
        UserMailer.challenge_reminder(challenge).deliver_now  
      end      
    end 
    end
end

的Gemfile

gem 'whenever', :require => false

1 个答案:

答案 0 :(得分:4)

这是命名空间:

namespace :challenges do

运行您运行rake challenges:challenge_reminder(或其他)的任务,而不仅仅是rake challenge_reminder,如果您没有将它放在命名空间中。您也可以删除命名空间(这是我可能会做的)。如果保留命名空间,则需要向调度程序提供此信息:rake challenges:challenge_reminder

您不需要whenever来运行heroku调度程序上的任务。每当或另一个cron作业将替代计划的rake任务。也就是说,如果您尝试通过heroku调度程序安排工作人员或某事而不是运行计划的rake任务。