Module: Kaal::Hanami

Defined in:
lib/kaal/hanami.rb,
lib/kaal/hanami/version.rb,
lib/kaal/hanami/middleware.rb

Overview

Hanami integration surface for Kaal.

Defined Under Namespace

Classes: Middleware

Constant Summary collapse

VERSION =
'0.4.0'

Class Method Summary collapse

Class Method Details

.configure!(app) ⇒ Object



17
18
19
20
# File 'lib/kaal/hanami.rb', line 17

def configure!(app, **)
  app.config.middleware.use(Kaal::Hanami::Middleware, hanami_app: app, **)
  app
end

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

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/kaal/hanami.rb', line 50

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 Hanami 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 Hanami 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



75
76
77
78
79
80
81
# File 'lib/kaal/hanami.rb', line 75

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



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/kaal/hanami.rb', line 83

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

  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



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/kaal/hanami.rb', line 22

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



97
98
99
# File 'lib/kaal/hanami.rb', line 97

def start!
  Kaal.start!
end

.stop!(timeout: 30) ⇒ Object



101
102
103
# File 'lib/kaal/hanami.rb', line 101

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