service.rb 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. # Base class for background services
  3. class BackgroundServices::Service
  4. include Mixin::RequiredSubPaths
  5. def self.service_name
  6. name.demodulize
  7. end
  8. # Override this method in service classes that support more than one worker process.
  9. def self.max_workers
  10. 1
  11. end
  12. # Use this method to prepare for a service task.
  13. # This would be called only once regardless of how many workers would start.
  14. def self.pre_run
  15. run_in_service_context do
  16. pre_launch
  17. end
  18. end
  19. # Use this method to run a background service.
  20. def run
  21. self.class.run_in_service_context do
  22. launch
  23. end
  24. end
  25. def self.run_in_service_context(&)
  26. Rails.application.executor.wrap do
  27. ApplicationHandleInfo.use('scheduler', &)
  28. end
  29. end
  30. protected
  31. # Override this method in service classes.
  32. def launch; end
  33. class << self
  34. protected
  35. # Override this method in service classes that need to perform tasks once
  36. # before threads/workers are started.
  37. def pre_launch; end
  38. end
  39. end