레일스 모델 클래스는 나중에 실행될 함수와 함께 Delayed::Job
에 의해 관리됩니다.레일스 테스트 환경에서 딜레이 함수가 지연되도록하는 방법
def fn_with_dj_delay
puts "puts output here"
do_somethting_else
end
handle_asynchronously :fn_with_dj_delay, :run_at => Proc.new { 24.hours.from_now }, :queue => 'my_queue'
상기 지연이 생략되고 : 여기 (단순화 된) 함수이다. 두 환경 모두에서이 작업을 수행 할 수 있습니까?
rails c test
에서 즉시 기능이 실행됩니다. 지시에 따라
2.3.1 :004 > x = MyClass.new
2.3.1 :005 > x.save!
2.3.1 :006 > x.fn_with_dj_delay
puts output here
=> #<Delayed::Backend::ActiveRecord::Job id: nil, priority: 0, attempts: 0 # ...
2.3.1 :007 > Delayed::Job.last
Delayed::Backend::ActiveRecord::Job Load (0.3ms) SELECT `delayed_jobs`.* # ...
=> nil
이 rails c
에서 함수가 자동으로 지연 : 여기에 약간 단순화 및 절단 콘솔 로그입니다. 다시 말하지만, 약간 단순화 및 절단 콘솔 로그 : 함수가 완료되면 내가 볼 수
2.3.1 :004 > x = MyClass.new
2.3.1 :005 > x.save!
2.3.1 :006 > x.fn_with_dj_delay
(0.2ms) BEGIN
SQL (0.4ms) INSERT INTO `delayed_jobs` (`handler`, `run_at`, # ...
(0.5ms) COMMIT
=> true
2.3.1 :007 > Delayed::Job.last
Delayed::Backend::ActiveRecord::Job Load (2.2ms) SELECT `delayed_jobs`.* # ...
=> #<Delayed::Backend::ActiveRecord::Job id: 1, priority: 0, attempts: 0 # ...
유일한 단서가 반환되고, 테스트 콘솔에서 Delayed::Backend::ActiveRecord::Job
개체는 uninstantiated. 객체에 무언가 무언가가 있다면, 오류가 발생할 것으로 예상되지만,이 실패를 이해할 수 있습니다. 그럼에도 불구하고,이 문제에되지 않습니다 :
2.3.1 :004 > res = p.check_for_similar_web_data
puts output here
=> #<Delayed::Backend::ActiveRecord::Job id: nil, priority: 0, attempts: 0 # ...
2.3.1 :005 > res.valid?
=> true
2.3.1 :006 > res.save!
(0.1ms) BEGIN
SQL (0.4ms) INSERT INTO `delayed_jobs` (`handler`, `run_at` # ...
(0.5ms) COMMIT
=> true