manager_spec.rb 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe BackgroundServices::Service::ProcessScheduledJobs::Manager do
  4. let(:instance) { described_class.new(job, container) }
  5. let(:container) { Concurrent::Hash.new }
  6. let(:job) { create(:scheduler) }
  7. describe '#run' do
  8. before do
  9. allow(instance).to receive_messages(skip?: skipping, start: :thread)
  10. end
  11. context 'when #skip? returns false' do
  12. let(:skipping) { false }
  13. it 'starts job' do
  14. instance.run
  15. expect(instance).to have_received(:start)
  16. end
  17. it 'adds job to container' do
  18. instance.run
  19. expect(container).to include(job.id => :thread)
  20. end
  21. end
  22. context 'when #skip? returns true' do
  23. let(:skipping) { true }
  24. it 'skips job' do
  25. instance.run
  26. expect(instance).not_to have_received(:start)
  27. end
  28. end
  29. end
  30. describe '#skip?' do
  31. let(:skipping_already_running) { false }
  32. let(:skipping_job_last_run) { false }
  33. let(:skipping_job_timeplan) { false }
  34. before do
  35. allow(instance).to receive_messages(skip_already_running?: skipping_already_running, skip_job_last_run?: skipping_job_last_run, skip_job_timeplan?: skipping_job_timeplan)
  36. end
  37. it 'does not skip' do
  38. expect(instance.send(:skip?)).to be_falsey
  39. end
  40. context 'when already running' do
  41. let(:skipping_already_running) { true }
  42. it 'skips' do
  43. expect(instance.send(:skip?)).to be_truthy
  44. end
  45. end
  46. context 'when last run was recent' do
  47. let(:skipping_job_last_run) { true }
  48. it 'skips' do
  49. expect(instance.send(:skip?)).to be_truthy
  50. end
  51. end
  52. context 'when timeplan is ok' do
  53. let(:skipping_job_timeplan) { true }
  54. it 'skips' do
  55. expect(instance.send(:skip?)).to be_truthy
  56. end
  57. end
  58. end
  59. describe '#skip_already_running?' do
  60. it 'not skip if no thread' do
  61. expect(instance.send(:skip_already_running?)).to be_falsey
  62. end
  63. it 'not skip if no valid thread' do
  64. container[job.id] = :asd
  65. expect(instance.send(:skip_already_running?)).to be_falsey
  66. end
  67. it 'skip if alive thread', ensure_threads_exited: true do
  68. thread = Thread.new { sleep 1000 } # will be stopped by ensure_threads_exited
  69. container[job.id] = thread
  70. expect(instance.send(:skip_already_running?)).to be_truthy
  71. end
  72. it 'not skip if dead thread', ensure_threads_exited: true do
  73. thread = Thread.new { 'do nothing' }
  74. container[job.id] = thread
  75. expect(instance.send(:skip_already_running?)).to be_falsey
  76. end
  77. end
  78. describe '#skip_job_last_run?' do
  79. it 'returns false if last run is not logged' do
  80. job.last_run = nil
  81. expect(instance.send(:skip_job_last_run?)).to be_falsey
  82. end
  83. it 'returns false if last run is long ago' do
  84. job.last_run = 1.year.ago
  85. expect(instance.send(:skip_job_last_run?)).to be_falsey
  86. end
  87. it 'returns true if last run is recent' do
  88. job.last_run = 5.minutes.ago
  89. expect(instance.send(:skip_job_last_run?)).to be_truthy
  90. end
  91. end
  92. describe '#skip_job_timeplan?' do
  93. it 'not skip if no timeplan' do
  94. job.timeplan = nil
  95. expect(instance.send(:skip_job_timeplan?)).to be_falsey
  96. end
  97. it 'skip if does not match timeplan' do
  98. travel_to Time.current.noon
  99. job.timeplan = timeplan(hour: 10)
  100. expect(instance.send(:skip_job_timeplan?)).to be_truthy
  101. end
  102. it 'not skip if match timeplan' do
  103. travel_to Time.current.noon
  104. job.timeplan = timeplan(hour: 12)
  105. expect(instance.send(:skip_job_timeplan?)).to be_falsey
  106. end
  107. def timeplan(hour:)
  108. {
  109. days: { Mon: true, Tue: true, Wed: true, Thu: true, Fri: true, Sat: true, Sun: true },
  110. hours: { hour => true },
  111. minutes: { 0 => true }
  112. }
  113. end
  114. end
  115. describe '#start', ensure_threads_exited: true do
  116. it 'starts a thread' do
  117. container[job.id] = :thread
  118. allow(instance).to receive(:start_in_thread).and_invoke(-> { sleep 1000 }) # will be stopped by ensure_threads_exited
  119. thread = instance.send(:start)
  120. expect(thread).to be_alive
  121. end
  122. it 'clears job from job container if error was raised in the job' do
  123. container[job.id] = :thread
  124. allow(instance).to receive(:start_in_thread).and_raise('error')
  125. thread = instance.send(:start)
  126. thread.join
  127. expect(container).not_to include(job.id => :thread)
  128. end
  129. it 'does not bubble up error if raised' do
  130. allow(BackgroundServices::Service::ProcessScheduledJobs::JobExecutor).to receive(:run).and_raise('error')
  131. thread = instance.send(:start)
  132. thread.join
  133. expect { instance.send(:start) }.not_to raise_error
  134. end
  135. end
  136. describe '#start_in_thread' do
  137. it 'launches job in scheduler context' do
  138. handle_info = nil
  139. allow(BackgroundServices::Service::ProcessScheduledJobs::JobExecutor)
  140. .to receive(:run).and_invoke(->(_) { handle_info = ApplicationHandleInfo.current })
  141. instance.send(:start_in_thread)
  142. expect(handle_info).to eq 'scheduler'
  143. end
  144. it 'wraps up after job' do
  145. allow(BackgroundServices::Service::ProcessScheduledJobs::JobExecutor).to receive(:run)
  146. allow(instance).to receive(:wrapup)
  147. instance.send(:start_in_thread)
  148. expect(instance).to have_received(:wrapup)
  149. end
  150. it 'does not wrap up after job with error' do
  151. allow(BackgroundServices::Service::ProcessScheduledJobs::JobExecutor).to receive(:run).and_raise('error')
  152. allow(instance).to receive(:wrapup)
  153. instance.send(:start_in_thread) rescue nil # rubocop:disable Style/RescueModifier
  154. expect(instance).not_to have_received(:wrapup)
  155. end
  156. end
  157. describe '#wrapup' do
  158. context 'when job present' do
  159. it 'clears pid' do
  160. expect { instance.send(:wrapup) }.to change { job.reload.pid }.to('')
  161. end
  162. it 'removes job from the jobs container' do
  163. container[job.id] = :sample
  164. expect { instance.send(:wrapup) }.to change(instance, :thread).to(nil)
  165. end
  166. end
  167. end
  168. describe '#invalid_thread_log' do
  169. it 'logs to error log' do
  170. allow(instance).to receive(:build_invalid_thread_log).and_return('sample error')
  171. allow(Rails.logger).to receive(:error)
  172. instance.send(:invalid_thread_log, :thread, :status)
  173. expect(Rails.logger).to have_received(:error).with('sample error')
  174. end
  175. end
  176. describe '#build_invalid_thread_log' do
  177. it 'declare thread status for a valid thread', ensure_threads_exited: true do
  178. thread = Thread.new { 'do nothing' }
  179. expect(instance.send(:build_invalid_thread_log, thread, 'test')).to match %r{^Invalid thread stored}
  180. end
  181. it 'declare unknown when status is given, but thread not given' do
  182. expect(instance.send(:build_invalid_thread_log, nil, 'test')).to match %r{^Job thread terminated unknownly}
  183. end
  184. it 'declare normal when status is false and thread not given' do
  185. expect(instance.send(:build_invalid_thread_log, nil, false)).to match %r{^Job thread terminated normally}
  186. end
  187. it 'declare normal when status is missing and thread not given' do
  188. expect(instance.send(:build_invalid_thread_log, nil, nil)).to match %r{^Job thread terminated via an exception}
  189. end
  190. end
  191. end