class Cloudtasker::Config

Holds cloudtasker configuration. See Cloudtasker#configure

Constants

AUTHORIZATION_HEADER

Authorization header

CONTENT_TYPE_HEADER

Content Type

DEFAULT_DISPATCH_DEADLINE

Job timeout configuration for Cloud Tasks

DEFAULT_JOB_QUEUE

Default queue values

DEFAULT_LOCATION_ID

Default values

DEFAULT_MAX_RETRY_ATTEMPTS

The number of times jobs will be attempted before declaring them dead.

With the default retry configuration (maxDoublings = 16 and minBackoff = 0.100s) it means that jobs will be declared dead after 20h of consecutive failing.

Note that this configuration parameter is internal to Cloudtasker and does not affect the Cloud Task queue configuration. The number of retries configured on the Cloud Task queue should be higher than the number below to also cover failures due to the instance being unreachable.

DEFAULT_ON_ERROR

Default on_error Proc

DEFAULT_PROCESSOR_PATH
DEFAULT_QUEUE_CONCURRENCY
DEFAULT_QUEUE_RETRIES
ENCODING_HEADER

Content-Transfer-Encoding header in Cloud Task responses

MAX_DISPATCH_DEADLINE
MAX_TASK_SIZE

Max Cloud Task size in bytes

MIN_DISPATCH_DEADLINE
PROCESSOR_HOST_MISSING
PROJECT_ID_MISSING_ERROR
QUEUE_PREFIX_MISSING_ERROR
RETRY_HEADER

Retry header in Cloud Task responses

TODO: use 'X-CloudTasks-TaskExecutionCount' instead of 'X-CloudTasks-TaskRetryCount'

'X-CloudTasks-TaskExecutionCount' is currently bugged and remains at 0 even on retries.

See bug: issuetracker.google.com/issues/154532072

Definitions:

X-CloudTasks-TaskRetryCount: total number of retries (including 504 "instance unreachable")
X-CloudTasks-TaskExecutionCount: number of non-503 retries (= actual number of job failures)
SECRET_MISSING_ERROR
TASK_ID_HEADER

Cloud Task ID header

Attributes

dispatch_deadline[W]
gcp_location_id[W]
gcp_project_id[W]
gcp_queue_prefix[W]
logger[W]
max_retries[W]
mode[W]
on_dead[W]
on_error[W]
processor_path[W]
redis[RW]
secret[W]
store_payloads_in_redis[RW]

Public Instance Methods

client_middleware() { |client_middleware| ... } click to toggle source

Return the chain of client middlewares.

@return [Cloudtasker::Middleware::Chain] The chain of middlewares.

# File lib/cloudtasker/config.rb, line 266
def client_middleware
  @client_middleware ||= Middleware::Chain.new
  yield @client_middleware if block_given?
  @client_middleware
end
dispatch_deadline() click to toggle source

Return the Dispatch deadline duration. Cloud Tasks will timeout the job after this duration is elapsed.

@return [Integer] The value in seconds.

# File lib/cloudtasker/config.rb, line 225
def dispatch_deadline
  @dispatch_deadline || DEFAULT_DISPATCH_DEADLINE
end
environment() click to toggle source

Return the current environment.

@return [String] The environment name.

# File lib/cloudtasker/config.rb, line 129
def environment
  ENV['CLOUDTASKER_ENV'] || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
end
gcp_location_id() click to toggle source

Return the GCP location ID. Default to 'us-east1'

@return [String] The location ID where tasks will be processed.

# File lib/cloudtasker/config.rb, line 215
def gcp_location_id
  @gcp_location_id || DEFAULT_LOCATION_ID
end
gcp_project_id() click to toggle source

Return the GCP project ID.

@return [String] The ID of the project for which tasks will be processed.

# File lib/cloudtasker/config.rb, line 206
def gcp_project_id
  @gcp_project_id || raise(StandardError, PROJECT_ID_MISSING_ERROR)
end
gcp_queue_prefix() click to toggle source

Return the prefix used for queues.

@return [String] The prefix of the processing queues.

# File lib/cloudtasker/config.rb, line 197
def gcp_queue_prefix
  @gcp_queue_prefix || raise(StandardError, QUEUE_PREFIX_MISSING_ERROR)
end
logger() click to toggle source

Return the Cloudtasker logger.

@return [Logger, any] The cloudtasker logger.

# File lib/cloudtasker/config.rb, line 138
def logger
  @logger ||= defined?(Rails) ? Rails.logger : ::Logger.new(STDOUT)
end
max_retries() click to toggle source

The number of times jobs will be retried. This number of retries does not include failures due to the application being unreachable.

@return [Integer] The number of retries

# File lib/cloudtasker/config.rb, line 109
def max_retries
  @max_retries ||= DEFAULT_MAX_RETRY_ATTEMPTS
end
mode() click to toggle source

The operating mode.

- :production => process tasks via GCP Cloud Task.
- :development => process tasks locally via Redis.

@return [<Type>] <description>

# File lib/cloudtasker/config.rb, line 120
def mode
  @mode ||= environment == 'development' ? :development : :production
end
on_dead() click to toggle source

Return a Proc invoked whenever a worker DeadWorkerError is raised. See Cloudtasker::WorkerHandler.with_worker_handling

@return [Proc] A Proc handler

# File lib/cloudtasker/config.rb, line 257
def on_dead
  @on_dead || DEFAULT_ON_ERROR
end
on_error() click to toggle source

Return a Proc invoked whenever a worker runtime error is raised. See Cloudtasker::WorkerHandler.with_worker_handling

@return [Proc] A Proc handler

# File lib/cloudtasker/config.rb, line 247
def on_error
  @on_error || DEFAULT_ON_ERROR
end
processor_host() click to toggle source

The hostname of the application processing the workers. The hostname must be reachable from Cloud Task.

@return [String] The processor host.

# File lib/cloudtasker/config.rb, line 177
def processor_host
  @processor_host || raise(StandardError, PROCESSOR_HOST_MISSING)
end
processor_host=(val) click to toggle source

Set the processor host. In the context of Rails the host will also be added to the list of authorized Rails hosts.

@param [String] val The processor host to set.

# File lib/cloudtasker/config.rb, line 158
def processor_host=(val)
  @processor_host = val

  # Check if Rails supports host filtering
  return unless val &&
                defined?(Rails) &&
                Rails.application.config.respond_to?(:hosts) &&
                Rails.application.config.hosts&.any?

  # Add processor host to the list of authorized hosts
  Rails.application.config.hosts << val.gsub(%r{https?://}, '')
end
processor_path() click to toggle source

The path on the host when worker payloads will be sent. Default to `/cloudtasker/run`

@return [String] The processor path

# File lib/cloudtasker/config.rb, line 188
def processor_path
  @processor_path || DEFAULT_PROCESSOR_PATH
end
processor_url() click to toggle source

Return the full URL of the processor. Worker payloads will be sent to this URL.

@return [String] The processor URL.

# File lib/cloudtasker/config.rb, line 148
def processor_url
  File.join(processor_host, processor_path)
end
redis_payload_storage_threshold() click to toggle source

Return the threshold above which job arguments must be stored in Redis instead of being sent to the backend as part of the job payload.

Return nil if redis payload storage is disabled.

@return [Integer, nil] The threshold above which payloads will be stored in Redis.

# File lib/cloudtasker/config.rb, line 96
def redis_payload_storage_threshold
  return nil unless store_payloads_in_redis

  store_payloads_in_redis.respond_to?(:to_i) ? store_payloads_in_redis.to_i : 0
end
secret() click to toggle source

Return the secret to use to sign the verification tokens attached to tasks.

@return [String] The cloudtasker secret

# File lib/cloudtasker/config.rb, line 235
def secret
  @secret ||= (
    defined?(Rails) && Rails.application.credentials&.dig(:secret_key_base)
  ) || raise(StandardError, SECRET_MISSING_ERROR)
end
server_middleware() { |server_middleware| ... } click to toggle source

Return the chain of server middlewares.

@return [Cloudtasker::Middleware::Chain] The chain of middlewares.

# File lib/cloudtasker/config.rb, line 277
def server_middleware
  @server_middleware ||= Middleware::Chain.new
  yield @server_middleware if block_given?
  @server_middleware
end