Module: Smith::PersistenceAdapters
- Defined in:
- lib/smith/persistence_adapters.rb,
lib/smith/persistence_adapters/retry.rb,
lib/smith/persistence_adapters/memory.rb,
lib/smith/persistence_adapters/cache_store.rb,
lib/smith/persistence_adapters/rails_cache.rb,
lib/smith/persistence_adapters/redis_store.rb,
lib/smith/persistence_adapters/active_record_store.rb
Defined Under Namespace
Modules: Retry
Classes: ActiveRecordStore, CacheStore, Memory, RailsCache, RedisStore
Constant Summary
collapse
- SolidCache =
RailsCache
- REQUIRED_METHODS =
REQUIRED_METHODS is the immutable adapter contract: any object responding to these is a valid Smith persistence adapter. This contract is preserved across the Phase B persistence hardening work; new optional capabilities (store_versioned, TTL kwarg) are additive and queried via respond_to?.
%i[store fetch delete].freeze
- OPTIONAL_METHODS =
OPTIONAL_METHODS: capabilities adapters MAY implement. Callers check support via ‘supports?(adapter, capability)` and fall back gracefully (e.g., Workflow#persist! warns once and uses plain `store` when `store_versioned` is missing).
%i[store_versioned record_heartbeat last_heartbeat].freeze
Class Method Summary
collapse
Class Method Details
.adapter_like?(adapter) ⇒ Boolean
55
56
57
|
# File 'lib/smith/persistence_adapters.rb', line 55
def self.adapter_like?(adapter)
REQUIRED_METHODS.all? { |method_name| adapter.respond_to?(method_name) }
end
|
.resolve(adapter, **options) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/smith/persistence_adapters.rb', line 28
def self.resolve(adapter, **options)
return nil if adapter.nil?
return validate!(adapter) if adapter_like?(adapter)
if adapter.is_a?(Class)
instance = options.empty? ? adapter.new : adapter.new(**options)
return validate!(instance)
end
built_in = case adapter.to_sym
when :cache_store
CacheStore.new(**options)
when :rails_cache, :solid_cache
RailsCache.new(**options)
when :redis
RedisStore.new(**options)
when :active_record
ActiveRecordStore.new(**options)
when :memory
Memory.new
else
raise ArgumentError, "Unknown persistence adapter #{adapter.inspect}"
end
validate!(built_in)
end
|
.supports?(adapter, capability) ⇒ Boolean
Capability introspection used by Workflow#persist! to decide whether the adapter supports optimistic locking via store_versioned.
68
69
70
|
# File 'lib/smith/persistence_adapters.rb', line 68
def self.supports?(adapter, capability)
adapter.respond_to?(capability)
end
|
.validate!(adapter) ⇒ Object
59
60
61
62
63
64
|
# File 'lib/smith/persistence_adapters.rb', line 59
def self.validate!(adapter)
return adapter if adapter_like?(adapter)
missing = REQUIRED_METHODS.reject { |method_name| adapter.respond_to?(method_name) }
raise ArgumentError, "Persistence adapter must implement #{missing.join(', ')}"
end
|
.warn_missing_heartbeat(adapter) ⇒ Object
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/smith/persistence_adapters.rb', line 97
def self.warn_missing_heartbeat(adapter)
klass = adapter.class
@_warned_heartbeat_monitor.synchronize do
return if @_warned_heartbeat_classes.include?(klass)
@_warned_heartbeat_classes << klass
end
Smith.config.logger&.warn(
"#{klass.name} does not implement record_heartbeat/last_heartbeat; " \
"Smith::Workflow.stuck_for? falls back to payload['updated_at'] parsing. " \
"For accurate liveness probes, switch to RedisStore or Memory."
)
end
|
.warn_missing_versioning(adapter) ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/smith/persistence_adapters.rb', line 78
def self.warn_missing_versioning(adapter)
klass = adapter.class
@_warned_monitor.synchronize do
return if @_warned_classes.include?(klass)
@_warned_classes << klass
end
Smith.config.logger&.warn(
"#{klass.name} does not implement store_versioned; " \
"optimistic locking is disabled for this adapter. " \
"Switch to RedisStore, ActiveRecordStore (with lock_version column), " \
"or the Memory adapter for race protection."
)
end
|