shikibu 0.3.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +82 -0
- data/lib/shikibu/activity.rb +84 -48
- data/lib/shikibu/actors.rb +129 -0
- data/lib/shikibu/app.rb +37 -23
- data/lib/shikibu/blobs.rb +42 -0
- data/lib/shikibu/channels.rb +240 -64
- data/lib/shikibu/constants.rb +8 -0
- data/lib/shikibu/context.rb +184 -14
- data/lib/shikibu/errors.rb +56 -0
- data/lib/shikibu/fs.rb +93 -0
- data/lib/shikibu/integrations/sidekiq.rb +9 -13
- data/lib/shikibu/middleware/rack_app.rb +17 -1
- data/lib/shikibu/notify/pg_notify.rb +28 -3
- data/lib/shikibu/replay.rb +242 -107
- data/lib/shikibu/storage/actor_ops.rb +331 -0
- data/lib/shikibu/storage/channel_ops.rb +421 -0
- data/lib/shikibu/storage/cursor_ops.rb +37 -0
- data/lib/shikibu/storage/sequel_storage.rb +400 -324
- data/lib/shikibu/storage/sqlite_ops.rb +58 -0
- data/lib/shikibu/supervisor.rb +279 -0
- data/lib/shikibu/tasks.rb +193 -0
- data/lib/shikibu/version.rb +1 -1
- data/lib/shikibu/worker.rb +155 -73
- data/lib/shikibu/workflow.rb +106 -48
- data/lib/shikibu.rb +4 -0
- data/schema/SEMANTICS.md +351 -0
- data/schema/db/migrations/mysql/20261123000000_outbox_claimed_at.sql +5 -0
- data/schema/db/migrations/mysql/20261201000000_actor_registry.sql +24 -0
- data/schema/db/migrations/mysql/20261202000000_actor_capabilities.sql +15 -0
- data/schema/db/migrations/mysql/20261203000000_payload_blobs.sql +11 -0
- data/schema/db/migrations/mysql/20261204000000_actor_fs.sql +13 -0
- data/schema/db/migrations/mysql/20261205000000_actor_tasks.sql +15 -0
- data/schema/db/migrations/mysql/20261206000000_actor_tasks_claimed_at.sql +7 -0
- data/schema/db/migrations/mysql/20261207000000_add_sandboxed.sql +5 -0
- data/schema/db/migrations/mysql/20261208000000_widen_actor_owner_columns.sql +15 -0
- data/schema/db/migrations/postgresql/20261123000000_outbox_claimed_at.sql +5 -0
- data/schema/db/migrations/postgresql/20261201000000_actor_registry.sql +23 -0
- data/schema/db/migrations/postgresql/20261202000000_actor_capabilities.sql +15 -0
- data/schema/db/migrations/postgresql/20261203000000_payload_blobs.sql +11 -0
- data/schema/db/migrations/postgresql/20261204000000_actor_fs.sql +13 -0
- data/schema/db/migrations/postgresql/20261205000000_actor_tasks.sql +15 -0
- data/schema/db/migrations/postgresql/20261206000000_actor_tasks_claimed_at.sql +7 -0
- data/schema/db/migrations/postgresql/20261207000000_add_sandboxed.sql +5 -0
- data/schema/db/migrations/postgresql/20261208000000_widen_actor_owner_columns.sql +7 -0
- data/schema/db/migrations/sqlite/20261123000000_outbox_claimed_at.sql +5 -0
- data/schema/db/migrations/sqlite/20261201000000_actor_registry.sql +23 -0
- data/schema/db/migrations/sqlite/20261202000000_actor_capabilities.sql +15 -0
- data/schema/db/migrations/sqlite/20261203000000_payload_blobs.sql +11 -0
- data/schema/db/migrations/sqlite/20261204000000_actor_fs.sql +13 -0
- data/schema/db/migrations/sqlite/20261205000000_actor_tasks.sql +15 -0
- data/schema/db/migrations/sqlite/20261206000000_actor_tasks_claimed_at.sql +7 -0
- data/schema/db/migrations/sqlite/20261207000000_add_sandboxed.sql +5 -0
- data/schema/db/migrations/sqlite/20261208000000_widen_actor_owner_columns.sql +7 -0
- data/schema/docs/column-values.md +10 -0
- data/schema/spec/ActorFS.tla +117 -0
- data/schema/spec/ActorFS_lww_monotonic.cfg +7 -0
- data/schema/spec/ActorFS_new.cfg +8 -0
- data/schema/spec/ActorFS_old.cfg +7 -0
- data/schema/spec/CompetingMessageClaim.tla +127 -0
- data/schema/spec/CompetingMessageClaimSplit.tla +132 -0
- data/schema/spec/CompetingMessageClaimSplit_atomic_lost.cfg +7 -0
- data/schema/spec/CompetingMessageClaimSplit_atomic_once.cfg +7 -0
- data/schema/spec/CompetingMessageClaimSplit_fixed.cfg +7 -0
- data/schema/spec/CompetingMessageClaimSplit_split_lost.cfg +7 -0
- data/schema/spec/CompetingMessageClaimSplit_split_once.cfg +7 -0
- data/schema/spec/CompetingMessageClaimSplit_waitonly_lost.cfg +7 -0
- data/schema/spec/CompetingMessageClaimSplit_waitonly_once.cfg +7 -0
- data/schema/spec/CompetingMessageClaim_new.cfg +10 -0
- data/schema/spec/CompetingMessageClaim_old.cfg +11 -0
- data/schema/spec/EpochFencing.tla +87 -0
- data/schema/spec/EpochFencingRuns.tla +107 -0
- data/schema/spec/EpochFencingRuns_epoch.cfg +6 -0
- data/schema/spec/EpochFencingRuns_none.cfg +6 -0
- data/schema/spec/EpochFencingRuns_witness.cfg +6 -0
- data/schema/spec/EpochFencingRuns_worker.cfg +6 -0
- data/schema/spec/EpochFencingRuns_worker1.cfg +6 -0
- data/schema/spec/EpochFencing_new.cfg +8 -0
- data/schema/spec/EpochFencing_old.cfg +9 -0
- data/schema/spec/InstanceClaim.tla +196 -0
- data/schema/spec/InstanceClaimVerdict.tla +116 -0
- data/schema/spec/InstanceClaimVerdict_changedrows.cfg +6 -0
- data/schema/spec/InstanceClaimVerdict_changedrows_live.cfg +6 -0
- data/schema/spec/InstanceClaimVerdict_foundrows.cfg +6 -0
- data/schema/spec/InstanceClaimVerdict_foundrows_live.cfg +6 -0
- data/schema/spec/InstanceClaim_atomic_unfenced.cfg +9 -0
- data/schema/spec/InstanceClaim_live.cfg +9 -0
- data/schema/spec/InstanceClaim_new.cfg +9 -0
- data/schema/spec/InstanceClaim_old.cfg +11 -0
- data/schema/spec/InstanceClaim_racy_fenced.cfg +9 -0
- data/schema/spec/README.md +97 -0
- data/schema/spec/SupervisorRestart.tla +108 -0
- data/schema/spec/SupervisorRestartCAS.tla +84 -0
- data/schema/spec/SupervisorRestartCAS_nonsticky.cfg +6 -0
- data/schema/spec/SupervisorRestartCAS_snapshot.cfg +6 -0
- data/schema/spec/SupervisorRestartCAS_sticky.cfg +6 -0
- data/schema/spec/SupervisorRestart_racy.cfg +7 -0
- data/schema/spec/SupervisorRestart_strict.cfg +7 -0
- data/schema/spec/TaskClaim.tla +92 -0
- data/schema/spec/TaskClaimReadBack.tla +84 -0
- data/schema/spec/TaskClaimReadBack_byid.cfg +6 -0
- data/schema/spec/TaskClaimReadBack_order.cfg +6 -0
- data/schema/spec/TaskClaim_new.cfg +8 -0
- data/schema/spec/TaskClaim_old.cfg +9 -0
- data/schema/spec/TaskReclaim.tla +110 -0
- data/schema/spec/TaskReclaimAttempts.tla +73 -0
- data/schema/spec/TaskReclaimAttempts_attempt.cfg +6 -0
- data/schema/spec/TaskReclaimAttempts_claimant.cfg +6 -0
- data/schema/spec/TaskReclaim_new.cfg +8 -0
- data/schema/spec/TaskReclaim_old.cfg +8 -0
- data/schema/spec/WaitRegistration.tla +170 -0
- data/schema/spec/WaitRegistrationSub.tla +133 -0
- data/schema/spec/WaitRegistrationSub_fixed.cfg +6 -0
- data/schema/spec/WaitRegistrationSub_fixed_guarded.cfg +6 -0
- data/schema/spec/WaitRegistrationSub_guardonly_reg.cfg +6 -0
- data/schema/spec/WaitRegistrationSub_guardonly_res.cfg +6 -0
- data/schema/spec/WaitRegistrationSub_head_reg.cfg +6 -0
- data/schema/spec/WaitRegistrationSub_head_res.cfg +6 -0
- data/schema/spec/WaitRegistrationSub_noclear_reg.cfg +6 -0
- data/schema/spec/WaitRegistrationSub_noclear_res.cfg +6 -0
- data/schema/spec/WaitRegistrationSub_unfencedreg_reg.cfg +6 -0
- data/schema/spec/WaitRegistrationSub_unfencedreg_res.cfg +6 -0
- data/schema/spec/WaitRegistration_new.cfg +10 -0
- data/schema/spec/WaitRegistration_old.cfg +11 -0
- data/schema/spec/WaitRegistration_residual.cfg +11 -0
- data/schema/spec/WaitRegistration_sweep.cfg +11 -0
- data/schema/spec/actorfs_check_test.go +165 -0
- data/schema/spec/claim_check_test.go +387 -0
- data/schema/spec/competing_claim_test.go +194 -0
- data/schema/spec/epoch_check_test.go +128 -0
- data/schema/spec/go.mod +3 -0
- data/schema/spec/supervisorrestart_check_test.go +131 -0
- data/schema/spec/taskclaim_check_test.go +135 -0
- data/schema/spec/taskreclaim_check_test.go +125 -0
- data/schema/spec/tlc-check.sh +45 -0
- data/schema/spec/tlc-matrix.txt +68 -0
- data/schema/spec/wait_check_test.go +256 -0
- metadata +121 -2
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Shikibu
|
|
4
|
+
module Storage
|
|
5
|
+
# Actor name registry + monitors (Erlang register/whereis/monitor).
|
|
6
|
+
# Mixed into SequelStorage — shares @db and helpers.
|
|
7
|
+
module ActorOps
|
|
8
|
+
# Bind a unique actor name to an instance. Idempotent for the holder;
|
|
9
|
+
# raises ActorNameTakenError when another instance holds the name. A
|
|
10
|
+
# conflicting insert is skipped, not rescued: on PostgreSQL a unique
|
|
11
|
+
# violation would abort an enclosing transaction.
|
|
12
|
+
def register_actor_name(name, instance_id, framework: FRAMEWORK)
|
|
13
|
+
names = @db[:actor_names]
|
|
14
|
+
names = mysql? ? names.on_duplicate_key_update(name: Sequel[:name]) : names.insert_conflict
|
|
15
|
+
names.insert(
|
|
16
|
+
name: name, instance_id: instance_id, framework: framework,
|
|
17
|
+
created_at: current_timestamp
|
|
18
|
+
)
|
|
19
|
+
holder = whereis_actor(name)
|
|
20
|
+
raise ActorNameTakenError, "actor name '#{name}' is bound to #{holder}" unless holder == instance_id
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Remove an actor name binding (idempotent).
|
|
24
|
+
def unregister_actor_name(name)
|
|
25
|
+
@db[:actor_names].where(name: name).delete
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Unbind name only if owner holds it: one conditional DELETE. A name
|
|
29
|
+
# bound to another instance raises NotOwnerError; an unbound name is a
|
|
30
|
+
# no-op.
|
|
31
|
+
def unregister_own_actor_name(name, owner)
|
|
32
|
+
return if @db[:actor_names].where(name: name, instance_id: owner).delete.positive?
|
|
33
|
+
|
|
34
|
+
holder = whereis_actor(name)
|
|
35
|
+
raise NotOwnerError, "actor name '#{name}' is bound to #{holder}" if holder
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Resolve an actor name to an instance ID (nil if unbound).
|
|
39
|
+
def whereis_actor(name)
|
|
40
|
+
row = @db[:actor_names].where(name: name).first
|
|
41
|
+
row && row[:instance_id]
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Register that watcher is notified on channel when target terminates.
|
|
45
|
+
def monitor_actor(watcher_instance_id, target_instance_id, channel)
|
|
46
|
+
insert_unless_exists(:actor_monitors, %i[watcher_instance_id target_instance_id],
|
|
47
|
+
watcher_instance_id: watcher_instance_id,
|
|
48
|
+
target_instance_id: target_instance_id,
|
|
49
|
+
channel: channel, created_at: current_timestamp)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Remove a monitor registration (idempotent).
|
|
53
|
+
def unmonitor_actor(watcher_instance_id, target_instance_id)
|
|
54
|
+
@db[:actor_monitors]
|
|
55
|
+
.where(watcher_instance_id: watcher_instance_id, target_instance_id: target_instance_id)
|
|
56
|
+
.delete
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Publish DOWN messages for monitored actors already in a terminal
|
|
60
|
+
# status (recurred is alive) and remove their monitor rows, then free the names of every
|
|
61
|
+
# terminated instance, monitored or not — all in one transaction
|
|
62
|
+
# (leader sweep, safe to run repeatedly). A monitor row is deleted
|
|
63
|
+
# before its DOWN is published and only the delete that removed it
|
|
64
|
+
# publishes, so overlapping sweeps emit each DOWN exactly once.
|
|
65
|
+
# Returns the number of notifications emitted.
|
|
66
|
+
def emit_actor_down_for_terminated
|
|
67
|
+
immediate_transaction do
|
|
68
|
+
rows = @db[:actor_monitors]
|
|
69
|
+
.join(:workflow_instances, instance_id: :target_instance_id)
|
|
70
|
+
.where(status: Status::TERMINAL)
|
|
71
|
+
.select(
|
|
72
|
+
Sequel[:actor_monitors][:id].as(:id),
|
|
73
|
+
Sequel[:actor_monitors][:target_instance_id].as(:target_instance_id),
|
|
74
|
+
Sequel[:actor_monitors][:channel].as(:channel),
|
|
75
|
+
Sequel[:workflow_instances][:status].as(:status)
|
|
76
|
+
).all
|
|
77
|
+
|
|
78
|
+
now = current_timestamp
|
|
79
|
+
emitted = rows.count do |m|
|
|
80
|
+
next false unless @db[:actor_monitors].where(id: m[:id]).delete == 1
|
|
81
|
+
|
|
82
|
+
payload = JSON.generate('type' => 'DOWN', 'actor' => m[:target_instance_id],
|
|
83
|
+
'status' => m[:status])
|
|
84
|
+
@db[:channel_messages].insert(
|
|
85
|
+
channel: m[:channel], message_id: SecureRandom.uuid,
|
|
86
|
+
data_type: 'json', data: payload, published_at: now
|
|
87
|
+
)
|
|
88
|
+
true
|
|
89
|
+
end
|
|
90
|
+
# A recurred instance's lineage lives on: its names are not freed
|
|
91
|
+
terminated = @db[:workflow_instances].where(status: %w[completed failed cancelled]).select(:instance_id)
|
|
92
|
+
@db[:actor_names].where(instance_id: terminated).delete
|
|
93
|
+
emitted
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# Persist a capability grant (secret stored only as a hash).
|
|
98
|
+
def grant_capability(capability_id:, kind:, target:, secret_hash:, granted_by: nil, expires_at: nil)
|
|
99
|
+
@db[:actor_capabilities].insert(
|
|
100
|
+
id: capability_id, kind: kind, target: target,
|
|
101
|
+
secret_hash: secret_hash, granted_by: granted_by,
|
|
102
|
+
expires_at: expires_at, created_at: current_timestamp
|
|
103
|
+
)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# Remove a capability grant by ID (idempotent).
|
|
107
|
+
def revoke_capability(capability_id)
|
|
108
|
+
@db[:actor_capabilities].where(id: capability_id).delete
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# Remove a capability grant only if owner issued it (granted_by): one
|
|
112
|
+
# conditional DELETE. A grant issued by another instance raises
|
|
113
|
+
# NotOwnerError; an unknown ID is a no-op.
|
|
114
|
+
def revoke_own_capability(capability_id, owner)
|
|
115
|
+
return if @db[:actor_capabilities].where(id: capability_id, granted_by: owner).delete.positive?
|
|
116
|
+
|
|
117
|
+
row = @db[:actor_capabilities].where(id: capability_id).first
|
|
118
|
+
raise NotOwnerError, "capability #{capability_id} was issued by #{row[:granted_by]}" if row
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# Store data under its content hash (idempotent CAS write).
|
|
122
|
+
def put_blob(hash, data, created_by: nil)
|
|
123
|
+
insert_unless_exists(:payload_blobs, :hash,
|
|
124
|
+
hash: hash, data: Sequel.blob(data), size_bytes: data.bytesize,
|
|
125
|
+
created_by: created_by, created_at: current_timestamp)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# Return the blob for hash (nil if absent).
|
|
129
|
+
def get_blob(hash)
|
|
130
|
+
row = @db[:payload_blobs].where(hash: hash).first
|
|
131
|
+
row && row[:data]
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def capability_grants?(kind, target)
|
|
135
|
+
@db[:actor_capabilities].where(kind: kind, target: target).any?
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def check_capability(kind, target, secret_hash)
|
|
139
|
+
row = @db[:actor_capabilities]
|
|
140
|
+
.where(kind: kind, target: target, secret_hash: secret_hash)
|
|
141
|
+
.first
|
|
142
|
+
return false unless row
|
|
143
|
+
|
|
144
|
+
exp = row[:expires_at]
|
|
145
|
+
if exp.is_a?(String)
|
|
146
|
+
# Naive timestamps are stored in UTC; parse them as such, not
|
|
147
|
+
# as local time.
|
|
148
|
+
exp = Time.parse("#{exp} UTC").utc
|
|
149
|
+
end
|
|
150
|
+
exp.nil? || exp > Time.now.utc
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# Return FS object at path ({data:, version:, owner:}) or nil.
|
|
154
|
+
def fs_get(path)
|
|
155
|
+
@db[:actor_fs].where(path: path).first
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
# CAS write: expected 0 = create, >0 = update iff version matches.
|
|
159
|
+
# Raises FSConflictError on mismatch.
|
|
160
|
+
def fs_put(path, data, owner, expected_version)
|
|
161
|
+
n = if expected_version.zero?
|
|
162
|
+
begin
|
|
163
|
+
# Savepoint: a create conflict must not abort the enclosing
|
|
164
|
+
# activity transaction (PostgreSQL). The insert's return value
|
|
165
|
+
# is adapter-specific (PG: the path), so count it explicitly.
|
|
166
|
+
@db.transaction(savepoint: true) do
|
|
167
|
+
@db[:actor_fs].insert(
|
|
168
|
+
path: path, owner: owner, data: Sequel.blob(data), version: 1,
|
|
169
|
+
created_at: current_timestamp, updated_at: current_timestamp
|
|
170
|
+
)
|
|
171
|
+
end
|
|
172
|
+
1
|
|
173
|
+
rescue Sequel::UniqueConstraintViolation
|
|
174
|
+
0
|
|
175
|
+
end
|
|
176
|
+
else
|
|
177
|
+
@db[:actor_fs].where(path: path, version: expected_version)
|
|
178
|
+
.update(data: Sequel.blob(data), owner: owner,
|
|
179
|
+
version: Sequel[:version] + 1, updated_at: current_timestamp)
|
|
180
|
+
end
|
|
181
|
+
raise FSConflictError, "actor_fs version conflict: #{path}" if n.zero?
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
# Delete path (expected 0 = unconditional, >0 = version-checked).
|
|
185
|
+
def fs_delete(path, expected_version)
|
|
186
|
+
return @db[:actor_fs].where(path: path).delete if expected_version.zero?
|
|
187
|
+
|
|
188
|
+
n = @db[:actor_fs].where(path: path, version: expected_version).delete
|
|
189
|
+
raise FSConflictError, "actor_fs version conflict: #{path}" if n.zero?
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
# Return FS objects (path/owner/version — no data) under prefix,
|
|
193
|
+
# ordered by path. LIKE metachars in prefix are matched literally.
|
|
194
|
+
def fs_list(prefix)
|
|
195
|
+
escaped = prefix.gsub(/[\\%_]/) { |c| "\\#{c}" }
|
|
196
|
+
# Backslash is the LIKE escape on every dialect (Sequel adds
|
|
197
|
+
# ESCAPE '\' where it is not the default).
|
|
198
|
+
@db[:actor_fs].where(Sequel.like(:path, "#{escaped}%"))
|
|
199
|
+
.select(:path, :owner, :version).order(:path).all
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
# Return blob metadata ({size_bytes:, created_by:, created_at:}) or nil.
|
|
203
|
+
def get_blob_info(hash)
|
|
204
|
+
@db[:payload_blobs].where(hash: hash)
|
|
205
|
+
.select(:size_bytes, :created_by, :created_at).first
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
# Remove expired capability grants (leader sweep), except that the
|
|
209
|
+
# newest grant of a (kind, target) stays unless another grant that is
|
|
210
|
+
# unexpired or newer (created_at, then id) remains: an expired grant
|
|
211
|
+
# still seals its channel, only a revoke re-opens it. The doomed ids
|
|
212
|
+
# go through a derived table because MySQL cannot read the DELETE's
|
|
213
|
+
# target table in a subquery (error 1093). Returns count.
|
|
214
|
+
def delete_expired_capabilities
|
|
215
|
+
now = Time.now.utc
|
|
216
|
+
grant = Sequel[:actor_capabilities]
|
|
217
|
+
other = Sequel[:other]
|
|
218
|
+
supersedes = Sequel.|(
|
|
219
|
+
Sequel.expr(other[:expires_at] => nil), other[:expires_at] > now,
|
|
220
|
+
other[:created_at] > grant[:created_at],
|
|
221
|
+
Sequel.&(Sequel.expr(other[:created_at] => grant[:created_at]), other[:id] > grant[:id])
|
|
222
|
+
)
|
|
223
|
+
siblings = @db[Sequel[:actor_capabilities].as(:other)]
|
|
224
|
+
.where(other[:kind] => grant[:kind], other[:target] => grant[:target])
|
|
225
|
+
.where(supersedes)
|
|
226
|
+
doomed = @db[:actor_capabilities].exclude(expires_at: nil).where { expires_at <= now }
|
|
227
|
+
.where(siblings.exists).select(:id)
|
|
228
|
+
@db[:actor_capabilities].where(id: @db.from(doomed.as(:doomed)).select(:id)).delete
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
# Insert a new open task on a channel + "task_posted" notice on
|
|
232
|
+
# the channel's task-event stream ({channel}:tasks).
|
|
233
|
+
def post_task(task_id:, channel:, data:)
|
|
234
|
+
@db[:actor_tasks].insert(
|
|
235
|
+
id: task_id, channel: channel, data: data, state: 'open',
|
|
236
|
+
created_at: current_timestamp, updated_at: current_timestamp
|
|
237
|
+
)
|
|
238
|
+
publish_message(channel: "#{channel}:tasks",
|
|
239
|
+
data: { type: 'task_posted', task_id: task_id, channel: channel })
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
# Return the task by ID, or nil.
|
|
243
|
+
def get_task(task_id)
|
|
244
|
+
@db[:actor_tasks].where(id: task_id).first
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
# Orphan reclaim: 'claimed' rows older than max_age_seconds were
|
|
248
|
+
# abandoned by a crashed claimant — return them to 'open' so another
|
|
249
|
+
# worker can pick them up. Returns the reclaimed count.
|
|
250
|
+
def reclaim_stale_tasks(max_age_seconds: 300)
|
|
251
|
+
@db[:actor_tasks]
|
|
252
|
+
.where(state: 'claimed')
|
|
253
|
+
.where { claimed_at < Time.now.utc - max_age_seconds }
|
|
254
|
+
.update(state: 'open', claimed_by: nil, claimed_at: nil,
|
|
255
|
+
updated_at: current_timestamp)
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
# Atomically claim the oldest open task on channel for claimant.
|
|
259
|
+
# Returns the task row or nil when no open task exists.
|
|
260
|
+
def claim_task(channel, claimant)
|
|
261
|
+
3.times do
|
|
262
|
+
candidate = @db[:actor_tasks].where(channel: channel, state: 'open')
|
|
263
|
+
.order(:created_at).limit(1).first
|
|
264
|
+
return nil if candidate.nil?
|
|
265
|
+
|
|
266
|
+
n = @db[:actor_tasks].where(id: candidate[:id], state: 'open')
|
|
267
|
+
.update(state: 'claimed', claimed_by: claimant,
|
|
268
|
+
claimed_at: current_timestamp,
|
|
269
|
+
attempts: Sequel[:attempts] + 1,
|
|
270
|
+
updated_at: current_timestamp)
|
|
271
|
+
next if n.zero? # lost the race to another claimant — retry
|
|
272
|
+
|
|
273
|
+
publish_message(channel: "#{channel}:tasks",
|
|
274
|
+
data: { type: 'task_claimed', task_id: candidate[:id],
|
|
275
|
+
claimed_by: claimant })
|
|
276
|
+
return @db[:actor_tasks].where(id: candidate[:id]).first
|
|
277
|
+
end
|
|
278
|
+
nil
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
# Mark a claimed task done/failed. Only the claimant may transition —
|
|
282
|
+
# raises TaskConflictError otherwise. Settling it again into the same
|
|
283
|
+
# state (same claimant) is an idempotent no-op.
|
|
284
|
+
def complete_task(task_id:, claimant:, result: nil, failed: false)
|
|
285
|
+
state = failed ? 'failed' : 'done'
|
|
286
|
+
n = @db[:actor_tasks]
|
|
287
|
+
.where(id: task_id, claimed_by: claimant, state: 'claimed')
|
|
288
|
+
.update(state: state, result: result,
|
|
289
|
+
updated_at: current_timestamp)
|
|
290
|
+
if n.zero?
|
|
291
|
+
current = @db[:actor_tasks].where(id: task_id).first
|
|
292
|
+
raise TaskNotFoundError, "actor_tasks: no task #{task_id}" if current.nil?
|
|
293
|
+
return nil if current[:claimed_by] == claimant && current[:state] == state
|
|
294
|
+
|
|
295
|
+
raise TaskConflictError, "actor_tasks conflict: #{task_id}"
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
channel = @db[:actor_tasks].where(id: task_id).get(:channel)
|
|
299
|
+
publish_message(channel: "#{channel}:tasks",
|
|
300
|
+
data: { type: 'task_state', task_id: task_id, state: state })
|
|
301
|
+
end
|
|
302
|
+
|
|
303
|
+
# Return tasks on channel filtered by state (nil = all).
|
|
304
|
+
def list_tasks(channel, state = nil)
|
|
305
|
+
ds = @db[:actor_tasks].where(channel: channel).order(:created_at)
|
|
306
|
+
ds = ds.where(state: state) if state
|
|
307
|
+
ds.all
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
# Return messages on channel published after the message_id cursor
|
|
311
|
+
# (nil or "" reads all, oldest first). A cursor that is not a message
|
|
312
|
+
# of this channel raises CursorNotFoundError.
|
|
313
|
+
def channel_messages_since(channel, message_id = nil)
|
|
314
|
+
ds = @db[:channel_messages].where(channel: channel).order(:id)
|
|
315
|
+
return ds.all if message_id.nil? || message_id == ''
|
|
316
|
+
|
|
317
|
+
cursor_id = @db[:channel_messages].where(channel: channel, message_id: message_id).get(:id)
|
|
318
|
+
raise CursorNotFoundError, "no message #{message_id} on channel #{channel}" unless cursor_id
|
|
319
|
+
|
|
320
|
+
ds.where { id > cursor_id }.all
|
|
321
|
+
end
|
|
322
|
+
|
|
323
|
+
# Return messages on channel with a row id above after_id (0 = all),
|
|
324
|
+
# oldest first. Unlike channel_messages_since it needs no surviving
|
|
325
|
+
# cursor message.
|
|
326
|
+
def channel_messages_after(channel, after_id)
|
|
327
|
+
@db[:channel_messages].where(channel: channel).where { id > after_id }.order(:id).all
|
|
328
|
+
end
|
|
329
|
+
end
|
|
330
|
+
end
|
|
331
|
+
end
|