cli.rb 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. class BackgroundServices
  3. class Cli < ::Thor
  4. # rubocop:disable Zammad/DetectTranslatableString
  5. def self.exit_on_failure?
  6. # Signal to Thor API that failures should be reflected in the exit code.
  7. true
  8. end
  9. desc 'start', 'Execute background services.'
  10. def start
  11. lock
  12. config = BackgroundServices::ServiceConfig.configuration_from_env(ENV)
  13. BackgroundServices.new(config).run
  14. ensure
  15. release_lock
  16. end
  17. def self.help(shell, subcommand = nil)
  18. super
  19. shell.say 'Startup behaviour can be customized with these environment variables:'
  20. shell.say
  21. list = [
  22. ['Service', 'Set worker count', 'Max. workers', 'Disable this service'],
  23. ['-------', '----------------', '------------', '--------------------'],
  24. ]
  25. BackgroundServices.available_services.each do |service|
  26. service_name = service.name.demodulize
  27. env_prefix = "ZAMMAD_#{service_name.underscore.upcase}"
  28. list.push [service_name, "#{env_prefix}_WORKERS", service.max_workers, "#{env_prefix}_DISABLE"]
  29. end
  30. shell.print_table(list, indent: 2)
  31. shell.say
  32. shell.say 'For more information, please see https://docs.zammad.org/en/latest/appendix/configure-env-vars.html.'
  33. end
  34. private
  35. def lock
  36. @lock_file = File.open(__FILE__, 'r')
  37. return if @lock_file.flock(File::LOCK_EX | File::LOCK_NB)
  38. raise 'Cannot start BackgroundServices, another process seems to be running.'
  39. end
  40. def release_lock
  41. @lock_file.close
  42. end
  43. # rubocop:enable Zammad/DetectTranslatableString
  44. end
  45. end