Class: Hatchet::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/hatchet/config.rb

Overview

Configuration class for Hatchet client settings

This class manages all configuration options for the Hatchet client, including token authentication, connection settings, and various client behaviors. Configuration can be set via constructor options, environment variables, or extracted from JWT tokens.

Examples:

Basic configuration

config = Config.new(token: "your-jwt-token")

Full configuration with all options

config = Config.new(
  token: "your-jwt-token",
  host_port: "localhost:7070",
  server_url: "https://api.hatchet.com",
  namespace: "production",
  worker_preset_labels: { "env" => "prod", "region" => "us-east-1" }
)

Configuration via environment variables

ENV["HATCHET_CLIENT_TOKEN"] = "your-jwt-token"
ENV["HATCHET_CLIENT_HOST_PORT"] = "localhost:7070"
config = Config.new  # Will load from environment

Constant Summary collapse

DEFAULT_HOST_PORT =
"localhost:7070"
DEFAULT_SERVER_URL =
"https://app.dev.hatchet-tools.com"
DEFAULT_GRPC_MAX_MESSAGE_LENGTH =

4MB

4 * 1024 * 1024
ENV_FILE_NAMES =
[".env", ".env.hatchet", ".env.dev", ".env.local"].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Config

Initialize a new configuration instance

Configuration values are loaded in the following priority order:

  1. Explicit constructor options (highest priority)

  2. Environment variables (HATCHET_CLIENT_*)

  3. JWT token payload (for tenant_id from ‘sub’ field)

  4. Default values (lowest priority)

Parameters:

  • options (Hash)

    Configuration options

Options Hash (**options):

  • :token (String)

    JWT token for authentication (required)

  • :host_port (String)

    gRPC server host and port (default: “localhost:7070”)

  • :server_url (String)

    Server URL for HTTP requests (default: “app.dev.hatchet-tools.com”)

  • :namespace (String)

    Namespace prefix for resource names (default: “”)

  • :logger (Logger)

    Custom logger instance (default: Logger.new($stdout))

  • :listener_v2_timeout (Integer)

    Timeout for listener v2 in milliseconds

  • :grpc_max_recv_message_length (Integer)

    Maximum gRPC receive message length in bytes (default: 4MB)

  • :grpc_max_send_message_length (Integer)

    Maximum gRPC send message length in bytes (default: 4MB)

  • :worker_preset_labels (Hash<String, String>)

    Default labels applied to all workers (default: {})

  • :tls_config (TLSConfig)

    Custom TLS configuration

  • :healthcheck (HealthcheckConfig)

    Custom healthcheck configuration

Raises:

  • (Error)

    if token is missing, empty, or not a valid JWT



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/hatchet/config.rb', line 186

def initialize(**options)
  load_env_files
  @explicitly_set = options.keys.to_set

  @token = options[:token] || env_var("HATCHET_CLIENT_TOKEN") || ""
  @host_port = options[:host_port] || env_var("HATCHET_CLIENT_HOST_PORT") || DEFAULT_HOST_PORT
  @server_url = options[:server_url] || env_var("HATCHET_CLIENT_SERVER_URL") || DEFAULT_SERVER_URL
  @namespace = options[:namespace] || env_var("HATCHET_CLIENT_NAMESPACE") || ""
  @logger = options[:logger] || Logger.new($stdout)

  @listener_v2_timeout = parse_int(options[:listener_v2_timeout] || env_var("HATCHET_CLIENT_LISTENER_V2_TIMEOUT"))
  @grpc_max_recv_message_length = parse_int(
    options[:grpc_max_recv_message_length] ||
    env_var("HATCHET_CLIENT_GRPC_MAX_RECV_MESSAGE_LENGTH"),
  ) || DEFAULT_GRPC_MAX_MESSAGE_LENGTH
  @grpc_max_send_message_length = parse_int(
    options[:grpc_max_send_message_length] ||
    env_var("HATCHET_CLIENT_GRPC_MAX_SEND_MESSAGE_LENGTH"),
  ) || DEFAULT_GRPC_MAX_MESSAGE_LENGTH

  @worker_preset_labels = options[:worker_preset_labels] ||
                          parse_hash(env_var("HATCHET_CLIENT_WORKER_PRESET_LABELS")) || {}

  # Initialize nested configurations
  @tls_config = options[:tls_config] || TLSConfig.new
  @healthcheck = options[:healthcheck] || HealthcheckConfig.new

  # Initialize tenant_id from JWT token
  @tenant_id = ""

  validate!
  apply_token_defaults if valid_jwt_token?
  apply_address_defaults if valid_jwt_token?
  normalize_namespace!
end

Instance Attribute Details

#grpc_max_recv_message_lengthInteger

Returns Maximum gRPC receive message length in bytes.

Returns:

  • (Integer)

    Maximum gRPC receive message length in bytes



157
158
159
160
# File 'lib/hatchet/config.rb', line 157

attr_reader :token, :host_port, :server_url, :namespace,
:logger, :listener_v2_timeout, :grpc_max_recv_message_length,
:grpc_max_send_message_length, :worker_preset_labels,
:tls_config, :healthcheck

#grpc_max_send_message_lengthInteger

Returns Maximum gRPC send message length in bytes.

Returns:

  • (Integer)

    Maximum gRPC send message length in bytes



157
158
159
160
# File 'lib/hatchet/config.rb', line 157

attr_reader :token, :host_port, :server_url, :namespace,
:logger, :listener_v2_timeout, :grpc_max_recv_message_length,
:grpc_max_send_message_length, :worker_preset_labels,
:tls_config, :healthcheck

#healthcheckObject

Returns the value of attribute healthcheck.



157
158
159
160
# File 'lib/hatchet/config.rb', line 157

attr_reader :token, :host_port, :server_url, :namespace,
:logger, :listener_v2_timeout, :grpc_max_recv_message_length,
:grpc_max_send_message_length, :worker_preset_labels,
:tls_config, :healthcheck

#host_portString

Returns gRPC server host and port.

Returns:

  • (String)

    gRPC server host and port



157
158
159
160
# File 'lib/hatchet/config.rb', line 157

attr_reader :token, :host_port, :server_url, :namespace,
:logger, :listener_v2_timeout, :grpc_max_recv_message_length,
:grpc_max_send_message_length, :worker_preset_labels,
:tls_config, :healthcheck

#listener_v2_timeoutInteger?

Returns Timeout for listener v2 in milliseconds.

Returns:

  • (Integer, nil)

    Timeout for listener v2 in milliseconds



157
158
159
160
# File 'lib/hatchet/config.rb', line 157

attr_reader :token, :host_port, :server_url, :namespace,
:logger, :listener_v2_timeout, :grpc_max_recv_message_length,
:grpc_max_send_message_length, :worker_preset_labels,
:tls_config, :healthcheck

#loggerLogger

Returns Logger instance.

Returns:

  • (Logger)

    Logger instance



157
158
159
160
# File 'lib/hatchet/config.rb', line 157

attr_reader :token, :host_port, :server_url, :namespace,
:logger, :listener_v2_timeout, :grpc_max_recv_message_length,
:grpc_max_send_message_length, :worker_preset_labels,
:tls_config, :healthcheck

#namespaceString

Returns Namespace prefix for resource names.

Returns:

  • (String)

    Namespace prefix for resource names



157
158
159
160
# File 'lib/hatchet/config.rb', line 157

attr_reader :token, :host_port, :server_url, :namespace,
:logger, :listener_v2_timeout, :grpc_max_recv_message_length,
:grpc_max_send_message_length, :worker_preset_labels,
:tls_config, :healthcheck

#server_urlString

Returns Server URL for HTTP requests.

Returns:

  • (String)

    Server URL for HTTP requests



157
158
159
160
# File 'lib/hatchet/config.rb', line 157

attr_reader :token, :host_port, :server_url, :namespace,
:logger, :listener_v2_timeout, :grpc_max_recv_message_length,
:grpc_max_send_message_length, :worker_preset_labels,
:tls_config, :healthcheck

#tenant_idString

Returns Tenant ID (extracted from JWT ‘sub’ field if not provided).

Returns:

  • (String)

    Tenant ID (extracted from JWT ‘sub’ field if not provided)



157
158
159
160
# File 'lib/hatchet/config.rb', line 157

attr_reader :token, :host_port, :server_url, :namespace,
:logger, :listener_v2_timeout, :grpc_max_recv_message_length,
:grpc_max_send_message_length, :worker_preset_labels,
:tls_config, :healthcheck

#tls_configTLSConfig

Returns TLS configuration.

Returns:



157
158
159
160
# File 'lib/hatchet/config.rb', line 157

attr_reader :token, :host_port, :server_url, :namespace,
:logger, :listener_v2_timeout, :grpc_max_recv_message_length,
:grpc_max_send_message_length, :worker_preset_labels,
:tls_config, :healthcheck

#tokenString

Returns JWT token for authentication.

Returns:

  • (String)

    JWT token for authentication



157
158
159
# File 'lib/hatchet/config.rb', line 157

def token
  @token
end

#worker_preset_labelsHash<String, String>

Returns Default labels applied to all workers.

Returns:

  • (Hash<String, String>)

    Default labels applied to all workers



157
158
159
160
# File 'lib/hatchet/config.rb', line 157

attr_reader :token, :host_port, :server_url, :namespace,
:logger, :listener_v2_timeout, :grpc_max_recv_message_length,
:grpc_max_send_message_length, :worker_preset_labels,
:tls_config, :healthcheck

Instance Method Details

#apply_namespace(resource_name, namespace_override: nil) ⇒ String?

Apply namespace prefix to a resource name

Examples:

Apply default namespace

config = Config.new(token: "token", namespace: "prod")
config.apply_namespace("workflow") #=> "prod_workflow"

Apply custom namespace

config.apply_namespace("workflow", namespace_override: "staging_") #=> "staging_workflow"

Skip namespace if already present

config.apply_namespace("prod_workflow") #=> "prod_workflow"

Parameters:

  • resource_name (String, nil)

    The resource name to namespace

  • namespace_override (String, nil) (defaults to: nil)

    Optional namespace to use instead of the configured one

Returns:

  • (String, nil)

    The namespaced resource name, or nil if resource_name is nil



237
238
239
240
241
242
243
244
245
# File 'lib/hatchet/config.rb', line 237

def apply_namespace(resource_name, namespace_override: nil)
  return resource_name if resource_name.nil?

  namespace_to_use = namespace_override || namespace
  return resource_name if namespace_to_use.empty?
  return resource_name if resource_name.start_with?(namespace_to_use)

  "#{namespace_to_use}#{resource_name}"
end

#auth_metadataHash<String, String>

Returns authentication metadata for gRPC calls.

Returns:

  • (Hash<String, String>)

    Metadata hash with authorization bearer token



270
271
272
# File 'lib/hatchet/config.rb', line 270

def 
  { "authorization" => "bearer #{token}" }
end

#hashObject



247
248
249
# File 'lib/hatchet/config.rb', line 247

def hash
  to_h.hash
end

#to_hHash<Symbol, Object>

Convert configuration to a hash representation

Returns:

  • (Hash<Symbol, Object>)

    Hash containing all configuration values



254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/hatchet/config.rb', line 254

def to_h
  {
    token: token,
    host_port: host_port,
    server_url: server_url,
    namespace: namespace,
    listener_v2_timeout: listener_v2_timeout,
    grpc_max_recv_message_length: grpc_max_recv_message_length,
    grpc_max_send_message_length: grpc_max_send_message_length,
    worker_preset_labels: worker_preset_labels,
  }
end