service_config.rb 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. BackgroundServices
  7. .available_services
  8. .map { |service| single_configuration_from_env(service, input) }
  9. end
  10. def self.single_configuration_from_env(service, input)
  11. env_prefix = "ZAMMAD_#{service.service_name.underscore.upcase}"
  12. new(
  13. service: service,
  14. disabled: ActiveModel::Type::Boolean.new.cast(input["#{env_prefix}_DISABLED"]) || false,
  15. workers: input["#{env_prefix}_WORKERS"].to_i,
  16. )
  17. end
  18. def initialize(service:, disabled:, workers:)
  19. @service = service
  20. @disabled = disabled
  21. @workers = workers
  22. end
  23. def enabled?
  24. !disabled
  25. end
  26. def start_as
  27. if workers.positive?
  28. :fork
  29. else
  30. :thread
  31. end
  32. end
  33. def workers
  34. [@workers, service.max_workers].min
  35. end
  36. end
  37. end