service_config.rb 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class BackgroundServices
  3. class ServiceConfig
  4. attr_reader :service, :disabled
  5. def self.configuration_from_env(input)
  6. if !input['ZAMMAD_PROCESS_SESSIONS_JOBS_WORKERS'] && input['ZAMMAD_SESSION_JOBS_CONCURRENT']
  7. input['ZAMMAD_PROCESS_SESSIONS_JOBS_WORKERS'] = input['ZAMMAD_SESSION_JOBS_CONCURRENT']
  8. ActiveSupport::Deprecation.send(:warn, 'The environment variable ZAMMAD_SESSION_JOBS_CONCURRENT is deprecated, please use ZAMMAD_PROCESS_SESSIONS_JOBS_WORKERS instead.') # rubocop:disable Zammad/DetectTranslatableString
  9. end
  10. BackgroundServices
  11. .available_services
  12. .map { |service| single_configuration_from_env(service, input) }
  13. end
  14. def self.single_configuration_from_env(service, input)
  15. env_prefix = "ZAMMAD_#{service.service_name.underscore.upcase}"
  16. new(
  17. service: service,
  18. disabled: ActiveModel::Type::Boolean.new.cast(input["#{env_prefix}_DISABLE"]) || false,
  19. workers: input["#{env_prefix}_WORKERS"].to_i,
  20. )
  21. end
  22. def initialize(service:, disabled:, workers:)
  23. @service = service
  24. @disabled = disabled
  25. @workers = workers
  26. end
  27. def enabled?
  28. !disabled
  29. end
  30. def start_as
  31. if workers.positive?
  32. :fork
  33. else
  34. :thread
  35. end
  36. end
  37. def workers
  38. [@workers, service.max_workers].min
  39. end
  40. end
  41. end