huginn_scheduler_spec.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. require 'spec_helper'
  2. require 'huginn_scheduler'
  3. describe HuginnScheduler do
  4. before(:each) do
  5. @scheduler = HuginnScheduler.new
  6. stub
  7. end
  8. it "should stop the scheduler" do
  9. mock.instance_of(Rufus::Scheduler).stop
  10. @scheduler.stop
  11. end
  12. it "schould register the schedules with the rufus scheduler and run" do
  13. mock.instance_of(Rufus::Scheduler).join
  14. @scheduler.run!
  15. end
  16. it "should run scheduled agents" do
  17. mock(Agent).run_schedule('every_1h')
  18. mock.instance_of(IO).puts('Queuing schedule for every_1h')
  19. @scheduler.send(:run_schedule, 'every_1h')
  20. end
  21. it "should propagate events" do
  22. mock(Agent).receive!
  23. stub.instance_of(IO).puts
  24. @scheduler.send(:propagate!)
  25. end
  26. it "schould clean up expired events" do
  27. mock(Event).cleanup_expired!
  28. stub.instance_of(IO).puts
  29. @scheduler.send(:cleanup_expired_events!)
  30. end
  31. describe "#hour_to_schedule_name" do
  32. it "for 0h" do
  33. expect(@scheduler.send(:hour_to_schedule_name, 0)).to eq('midnight')
  34. end
  35. it "for the forenoon" do
  36. expect(@scheduler.send(:hour_to_schedule_name, 6)).to eq('6am')
  37. end
  38. it "for 12h" do
  39. expect(@scheduler.send(:hour_to_schedule_name, 12)).to eq('noon')
  40. end
  41. it "for the afternoon" do
  42. expect(@scheduler.send(:hour_to_schedule_name, 17)).to eq('5pm')
  43. end
  44. end
  45. describe "cleanup_failed_jobs!" do
  46. before do
  47. 3.times do |i|
  48. Delayed::Job.create(failed_at: Time.now - i.minutes)
  49. end
  50. @keep = Delayed::Job.order(:failed_at)[1]
  51. end
  52. it "work with set FAILED_JOBS_TO_KEEP env variable", focus: true do
  53. expect { @scheduler.send(:cleanup_failed_jobs!) }.to change(Delayed::Job, :count).by(-1)
  54. expect { @scheduler.send(:cleanup_failed_jobs!) }.to change(Delayed::Job, :count).by(0)
  55. expect(@keep.id).to eq(Delayed::Job.order(:failed_at)[0].id)
  56. end
  57. it "work without the FAILED_JOBS_TO_KEEP env variable" do
  58. old = ENV['FAILED_JOBS_TO_KEEP']
  59. ENV['FAILED_JOBS_TO_KEEP'] = nil
  60. expect { @scheduler.send(:cleanup_failed_jobs!) }.to change(Delayed::Job, :count).by(0)
  61. ENV['FAILED_JOBS_TO_KEEP'] = old
  62. end
  63. end
  64. end
  65. describe Rufus::Scheduler do
  66. before :each do
  67. @taoe, Thread.abort_on_exception = Thread.abort_on_exception, false
  68. @oso, @ose, $stdout, $stderr = $stdout, $stderr, StringIO.new, StringIO.new
  69. @scheduler = Rufus::Scheduler.new
  70. stub.any_instance_of(Agents::SchedulerAgent).second_precision_enabled { true }
  71. @agent1 = Agents::SchedulerAgent.new(name: 'Scheduler 1', options: { action: 'run', schedule: '*/1 * * * * *' }).tap { |a|
  72. a.user = users(:bob)
  73. a.save!
  74. }
  75. @agent2 = Agents::SchedulerAgent.new(name: 'Scheduler 2', options: { action: 'run', schedule: '*/1 * * * * *' }).tap { |a|
  76. a.user = users(:bob)
  77. a.save!
  78. }
  79. end
  80. after :each do
  81. @scheduler.shutdown
  82. Thread.abort_on_exception = @taoe
  83. $stdout, $stderr = @oso, @ose
  84. end
  85. describe '#schedule_scheduler_agents' do
  86. it 'registers active SchedulerAgents' do
  87. @scheduler.schedule_scheduler_agents
  88. expect(@scheduler.scheduler_agent_jobs.map(&:scheduler_agent).sort_by(&:id)).to eq([@agent1, @agent2])
  89. end
  90. it 'unregisters disabled SchedulerAgents' do
  91. @scheduler.schedule_scheduler_agents
  92. @agent1.update!(disabled: true)
  93. @scheduler.schedule_scheduler_agents
  94. expect(@scheduler.scheduler_agent_jobs.map(&:scheduler_agent)).to eq([@agent2])
  95. end
  96. it 'unregisters deleted SchedulerAgents' do
  97. @scheduler.schedule_scheduler_agents
  98. @agent2.delete
  99. @scheduler.schedule_scheduler_agents
  100. expect(@scheduler.scheduler_agent_jobs.map(&:scheduler_agent)).to eq([@agent1])
  101. end
  102. end
  103. end