2011-07-06 3 views
2

곧 할인이 만료되면 사용자에게 오전 10시에 알려 드리고 싶습니다. 나는 heroku의 시간별 cron을 사용하고 있습니다. Ruby 1.8.7에서 Rails 3.0.9를 사용 중입니다. lib/tasks/cron에 다음 cron.rake가 있습니다.오이로 반복 크론 작업 테스트

# Notify users with expiring claims 
puts "Notifying users with expiring claims..." 
if Time.zone.now.hour == 10 
    Claim.expiring_tomorrow.reject(&:redeemed?).compact.each do |claim| 
    Mailer.delay.notify_customer_claim_expiration(claim) 
    claim.update_attribute(:expiration_notified, true) 
    end 
end 
puts "done." 

그게 훌륭합니다.

Scenario: Not receiving a expiration reminder too early 
Scenario: Receiving a expiration reminder only once 
Scenario: Not receiving a reminder for a redeemed claim 

을 그래서 나는 다음과 같은 기능 썼다 : 다음 cron.steps 단계 정의와

Feature: Being reminded that a claimed discount is expiring 

    Background: 
    Given the hourly cron job exists 

    Scenario: Not receiving a expiration reminder too early 
    And the hourly cron job has fired 

    Scenario: Receiving a expiration reminder only once 
    And the hourly cron job has fired 

    Scenario: Not receiving a reminder for a redeemed claim 
    And the hourly cron job has fired 

:

And /^the hourly cron job exists$/ do 
    require "rake" 
    @rake = Rake::Application.new 
    Rake.application = @rake 
    Rake.application.rake_require "lib/tasks/cron" 
end 

And /^the hourly cron job has fired$/ do 
    Rake::Task["cron"].invoke 
end 
내 문제는 내가 오이에 세 가지 시나리오를 테스트해야한다는 것입니다

각 시나리오를 개별적으로 실행하면 통과합니다. 나는 모든 기능을 실행하면, 그와 두 번째 (안 제 1 호) 시나리오에 실패

RuntimeError: Don't know how to build task 'cron' 

레이크 첫 번째 시나리오를지나 지속 또는 두 번째 시나리오가 실행되기 전에 재설정 할 필요가없는 것 같다. ..?

감사합니다 ... G

답변

0
Rake::Task[].invoke 

는 레이크 작업이 이미 호출 된 경우 확인하고, 그렇지 않으면 그것을 실행합니다. 즉 여러 번 호출되는 경우 한 번만 작업을 실행합니다.

Rake::Task[].execute 

은 호출 할 때마다 작업을 실행합니다.

+0

고마워. 나는 이것을 끝내기로 끝냈다 : –

1

감사합니다. 나는 이것을 끝내었다 :

And /^a cron job exists$/ do 
    require "rake" 
    @rake = Rake::Application.new 
    Rake.application = @rake 

    # remove cron.rake from list of required files to force file to be reloaded 
    loaded = $".reject {|file| file == Rails.root.join("lib", "tasks", "cron.rake").to_s } 

    Rake.application.rake_require("lib/tasks/cron", [Rails.root.to_s], loaded) 
    Rake::Task.define_task(:environment) 
end 

And /^the hourly cron job has fired$/ do 
    @rake["cron"].invoke 
end 
+0

[당신의 대답을 받아 들여야한다] (http://blog.stackoverflow.com/2009/01/accept-your-own-answers/). 명성을 올리지는 못하지만, 그 질문에 답이 나왔음을 나타낼 것입니다. – Alex