manager_spec.rb 7.5 KB

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