Module: Kaal::Sinatra
- Defined in:
- lib/kaal/sinatra.rb,
lib/kaal/sinatra/version.rb
Overview
Sinatra integration surface for Kaal.
Defined Under Namespace
Modules: Extension
Constant Summary collapse
- VERSION =
'0.5.0'
Class Method Summary collapse
- .configure_backend!(backend: nil, database: nil, redis: nil, adapter: nil, configuration: Kaal.configuration) ⇒ Object
- .detect_backend_name(database, adapter: nil) ⇒ Object
- .load_scheduler_file!(root:, environment: nil) ⇒ Object
- .register!(app, backend: nil, database: nil, redis: nil, scheduler_config_path: 'config/scheduler.yml', namespace: nil, start_scheduler: false, adapter: nil) ⇒ Object
- .start! ⇒ Object
- .stop!(timeout: 30) ⇒ Object
Class Method Details
.configure_backend!(backend: nil, database: nil, redis: nil, adapter: nil, configuration: Kaal.configuration) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/kaal/sinatra.rb', line 39 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 Sinatra 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 Sinatra datastore backend; use memory, redis, sqlite, postgres, or mysql' unless backend_name return configuration.backend = build_backend(backend_name, database) end |
.detect_backend_name(database, adapter: nil) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/kaal/sinatra.rb', line 64 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
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/kaal/sinatra.rb', line 72 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) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/kaal/sinatra.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 ) 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), environment: environment_name_for(app)) start_managed_scheduler! if start_scheduler app end |
.start! ⇒ Object
86 87 88 |
# File 'lib/kaal/sinatra.rb', line 86 def start! Kaal.start! end |
.stop!(timeout: 30) ⇒ Object
90 91 92 |
# File 'lib/kaal/sinatra.rb', line 90 def stop!(timeout: 30) Kaal.stop!(timeout:) end |