Module: Kaal::Roda

Defined in:
lib/kaal/roda.rb,
lib/kaal/roda/version.rb

Overview

Roda integration surface for Kaal.

Constant Summary collapse

VERSION =
'0.5.0'

Class Method Summary collapse

Class Method Details

.configure_backend!(backend: nil, database: nil, redis: nil, adapter: nil, configuration: Kaal.configuration) ⇒ Object

Raises:

  • (ArgumentError)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/kaal/roda.rb', line 44

def configure_backend!(backend: nil, database: nil, redis: nil, adapter: nil, configuration: Kaal.configuration)
  current_backend = configuration.backend
  return current_backend if current_backend

  return configuration.backend = backend if backend
  return configuration.backend = build_redis_backend(redis, configuration) if redis

  explicit_adapter = adapter.to_s.strip
  unless explicit_adapter.empty?
    raise ArgumentError, 'database is required when adapter is provided' unless database

    backend_name = normalize_backend_name(explicit_adapter)
    raise ArgumentError, "Unsupported Roda datastore backend: #{adapter.inspect}" unless backend_name

    return configuration.backend = build_backend(backend_name, database)
  end

  return configuration.backend = Kaal::Backend::MemoryAdapter.new unless database

  backend_name = detect_backend_name(database, adapter:)
  raise ArgumentError, 'Unsupported Roda datastore backend; use memory, redis, sqlite, postgres, or mysql' unless backend_name

  configuration.backend = build_backend(backend_name, database)
end

.detect_backend_name(database, adapter: nil) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/kaal/roda.rb', line 69

def detect_backend_name(database, adapter: nil)
  explicit_adapter = normalize_backend_name(adapter)
  return explicit_adapter if explicit_adapter

  inferred_adapter = database_adapter_name(database)
  normalize_backend_name(inferred_adapter)
end

.load_scheduler_file!(root:, environment: nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/kaal/roda.rb', line 77

def load_scheduler_file!(root:, environment: nil)
  runtime_context = Kaal::Runtime::RuntimeContext.new(
    root_path: root,
    environment_name: environment || Kaal::Runtime::RuntimeContext.environment_name_from(ENV)
  )

  Kaal::Runtime::SchedulerBootLoader.new(
    configuration_provider: -> { Kaal.configuration },
    logger: Kaal.configuration.logger,
    runtime_context: runtime_context,
    load_scheduler_file: -> { Kaal.load_scheduler_file!(runtime_context:) }
  ).load_on_boot!
end

.register!(app, backend: nil, database: nil, redis: nil, scheduler_config_path: 'config/scheduler.yml', namespace: nil, start_scheduler: false, adapter: nil, root: nil, environment: nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/kaal/roda.rb', line 16

def register!(
  app,
  backend: nil,
  database: nil,
  redis: nil,
  scheduler_config_path: 'config/scheduler.yml',
  namespace: nil,
  start_scheduler: false,
  adapter: nil,
  root: nil,
  environment: nil
)
  configuration = Kaal.configuration
  normalized_scheduler_config_path = scheduler_config_path.to_s.strip
  normalized_namespace = namespace.to_s.strip
  configuration.scheduler_config_path = normalized_scheduler_config_path unless normalized_scheduler_config_path.empty?
  configuration.namespace = normalized_namespace unless normalized_namespace.empty?

  configure_backend!(backend:, database:, redis:, adapter:, configuration:)
  load_scheduler_file!(
    root: root_path_for(app, root:),
    environment: environment_name_for(app, environment:)
  )

  start_managed_scheduler! if start_scheduler
  app
end

.start!Object



91
92
93
# File 'lib/kaal/roda.rb', line 91

def start!
  Kaal.start!
end

.stop!(timeout: 30) ⇒ Object



95
96
97
# File 'lib/kaal/roda.rb', line 95

def stop!(timeout: 30)
  Kaal.stop!(timeout:)
end