1
0

huginn_scheduler_spec.rb 4.1 KB

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