samagotchi 0.190.0 → 0.191.0
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/CHANGELOG.md +38 -1
- data/docs/architecture.md +8 -7
- data/docs/cli.md +6 -3
- data/docs/context.md +9 -2
- data/docs/guardrails.md +5 -1
- data/docs/internals/attached-context.md +3 -1
- data/docs/internals/peers.md +15 -3
- data/docs/internals/remote-sessions.md +2 -2
- data/docs/internals/session-lifecycle.md +213 -12
- data/docs/peers.md +3 -1
- data/docs/plugins.md +2 -1
- data/docs/scheduling.md +31 -7
- data/docs/sessions.md +11 -11
- data/docs/sub-agent.md +1 -1
- data/lib/samagotchi/bridge.rb +1 -1
- data/lib/samagotchi/bundles/known-names/manifest.yml +2 -2
- data/lib/samagotchi/bundles/known-names/plugin.rb +17 -1
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/context_command.rb +5 -1
- data/lib/samagotchi/context_sources.rb +28 -0
- data/lib/samagotchi/continue_intent.rb +23 -43
- data/lib/samagotchi/hold_stop.rb +5 -2
- data/lib/samagotchi/participants.rb +13 -5
- data/lib/samagotchi/peer_say.rb +29 -14
- data/lib/samagotchi/plugin/sessions.rb +9 -5
- data/lib/samagotchi/schedule_command.rb +5 -1
- data/lib/samagotchi/schedules/prune_part.rb +138 -38
- data/lib/samagotchi/schedules.rb +29 -6
- data/lib/samagotchi/send_command.rb +6 -6
- data/lib/samagotchi/session_access/ask.rb +9 -3
- data/lib/samagotchi/session_access/exchange.rb +6 -2
- data/lib/samagotchi/session_access/local.rb +104 -31
- data/lib/samagotchi/session_access/remote.rb +10 -9
- data/lib/samagotchi/session_access/remote_peers.rb +2 -2
- data/lib/samagotchi/session_access.rb +17 -7
- data/lib/samagotchi/session_chain/delete.rb +100 -47
- data/lib/samagotchi/session_chain/delete_preview.rb +9 -5
- data/lib/samagotchi/session_chain/links.rb +68 -9
- data/lib/samagotchi/session_chain/successor.rb +78 -0
- data/lib/samagotchi/session_continue.rb +108 -40
- data/lib/samagotchi/session_inbox.rb +1 -1
- data/lib/samagotchi/session_inbox_delivery.rb +113 -26
- data/lib/samagotchi/session_lifecycle/allowed.rb +20 -1
- data/lib/samagotchi/session_lifecycle/reading.rb +9 -3
- data/lib/samagotchi/session_lifecycle.rb +13 -2
- data/lib/samagotchi/session_manager/delivery.rb +61 -23
- data/lib/samagotchi/session_manager/emptiness.rb +5 -2
- data/lib/samagotchi/session_manager/lifecycle.rb +50 -14
- data/lib/samagotchi/session_manager/removal.rb +65 -13
- data/lib/samagotchi/session_manager/spawn.rb +14 -6
- data/lib/samagotchi/session_retention.rb +28 -7
- data/lib/samagotchi/session_transition.rb +315 -0
- data/lib/samagotchi/sessions_command.rb +1 -1
- data/lib/samagotchi/terminal_ui/attached_loop/sending.rb +2 -0
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +10 -2
- data/lib/samagotchi/web/peer_routes.rb +5 -2
- data/lib/samagotchi/web/session_access_routes.rb +5 -2
- data/lib/samagotchi/web/session_control.rb +6 -1
- data/lib/samagotchi/web/session_create.rb +1 -1
- data/lib/samagotchi/worker_boot.rb +28 -6
- metadata +3 -1
|
@@ -12,7 +12,10 @@ module Samagotchi
|
|
|
12
12
|
# read again under the owner lock, no longer holds), :scheduled (an
|
|
13
13
|
# enabled schedule's target, anchor or head: +schedule_id+),
|
|
14
14
|
# :continuing (a continue of +session_id+ is in flight: this link, or
|
|
15
|
-
# the previous one a delete would bring back), :
|
|
15
|
+
# the previous one a delete would bring back), :transition_held (another
|
|
16
|
+
# change holds its transition lock: a delete, a send's shared hold; the
|
|
17
|
+
# words SessionTransition::Busy's, naming the holder: .held),
|
|
18
|
+
# :delegate_unmovable (a
|
|
16
19
|
# delegate the deleted link's continue moved can't go back: +ids+,
|
|
17
20
|
# +detail+ says why), :chain_scheduled (a schedule is on the chain a
|
|
18
21
|
# chain delete would take: +schedule_id+), :protection_unreadable (a
|
|
@@ -41,6 +44,13 @@ module Samagotchi
|
|
|
41
44
|
|
|
42
45
|
def worker_stopped = !@stopped_worker.nil?
|
|
43
46
|
|
|
47
|
+
# A delete's refusal for a transition lock not taken in time
|
|
48
|
+
# (SessionTransition::Busy, which names the holder): :continuing when
|
|
49
|
+
# a continue holds it, else :transition_held; Busy's words either way.
|
|
50
|
+
def self.held(busy)
|
|
51
|
+
new(busy.session_id, busy.kind == :continue ? :continuing : :transition_held, message: busy.message)
|
|
52
|
+
end
|
|
53
|
+
|
|
44
54
|
# This refusal, saying the op stopped +worker_id+'s worker (this
|
|
45
55
|
# session's by default) before the check under the lock refused.
|
|
46
56
|
def with_worker_stopped(worker_id = @session_id)
|
|
@@ -58,6 +68,7 @@ module Samagotchi
|
|
|
58
68
|
when :changed then "session #{@session_id} changed since it was checked"
|
|
59
69
|
when :scheduled then "this session is the target of an enabled schedule#{cancel}"
|
|
60
70
|
when :continuing then "a continue of #{short} is under way; try again in a moment"
|
|
71
|
+
when :transition_held then "session #{short} is busy with another change; try again in a moment"
|
|
61
72
|
when :delegate_unmovable
|
|
62
73
|
"#{@ids.map { |id| id[0, 8] }.join(", ")}, moved here by the continue, can't go back to the previous link" \
|
|
63
74
|
"#{": #{detail}" if detail}"
|
|
@@ -80,7 +91,7 @@ module Samagotchi
|
|
|
80
91
|
attr_reader :session_id, :reason, :busy_id, :tasks, :stopped_worker, :sessions, :stopped_workers
|
|
81
92
|
|
|
82
93
|
def initialize(session_id, reason, busy_id: nil, tasks: RunningTasks::EMPTY, worker_stopped: false, sessions: nil,
|
|
83
|
-
stopped_workers: [])
|
|
94
|
+
stopped_workers: [], message: nil)
|
|
84
95
|
@session_id = session_id
|
|
85
96
|
@reason = reason
|
|
86
97
|
@busy_id = busy_id
|
|
@@ -88,7 +99,16 @@ module Samagotchi
|
|
|
88
99
|
@sessions = sessions
|
|
89
100
|
@stopped_workers = stopped_workers
|
|
90
101
|
@stopped_worker = RunningTasks.stopped_worker(session_id, worker_stopped)
|
|
91
|
-
super(archive_refused_message)
|
|
102
|
+
super(message || archive_refused_message)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# An archive's refusal for a transition lock not taken in time (its
|
|
106
|
+
# own, or a worker's stop's: SessionTransition::Busy, which names the
|
|
107
|
+
# holder and the session held): :transition_held, Busy's words;
|
|
108
|
+
# +stopped_workers+ the workers it stopped before.
|
|
109
|
+
def self.held(busy, stopped_workers: [])
|
|
110
|
+
new(busy.session_id, :transition_held, busy_id: busy.session_id, stopped_workers: stopped_workers,
|
|
111
|
+
message: busy.message)
|
|
92
112
|
end
|
|
93
113
|
|
|
94
114
|
def worker_stopped = !@stopped_worker.nil?
|
|
@@ -113,6 +133,11 @@ module Samagotchi
|
|
|
113
133
|
end
|
|
114
134
|
end
|
|
115
135
|
|
|
136
|
+
# How long an archive waits for its tree's transition locks (another
|
|
137
|
+
# change holding one: a continue, a delete, a send's shared hold) before
|
|
138
|
+
# it is refused ArchiveRefused :transition_held.
|
|
139
|
+
ARCHIVE_LOCK_WAIT = 10
|
|
140
|
+
|
|
116
141
|
# What one archive did: id: the session it was asked for; archived: the
|
|
117
142
|
# ids (it and its delegates) now hidden for good; stopped: the live
|
|
118
143
|
# workers stopped first; discarded: empty sessions its stopping worker
|
|
@@ -158,16 +183,35 @@ module Samagotchi
|
|
|
158
183
|
ArchiveRefused.new(id, :tasks_running, tasks: listing, sessions: tree.size)
|
|
159
184
|
end
|
|
160
185
|
stopped = tree.select { |sid| owners[sid] }
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
186
|
+
done = []
|
|
187
|
+
# With +wait+, stop_session waits for the owner to let go (its own
|
|
188
|
+
# transition lock taken for its mark and TERM only).
|
|
189
|
+
stopped.each do |sid|
|
|
190
|
+
stop_session(sid, state_dir: sd, wait: wait)
|
|
191
|
+
done << sid
|
|
192
|
+
end
|
|
193
|
+
archived, discarded = under_archive_locks(tree, sd) do
|
|
194
|
+
stop_archived_tasks!(id, tree, stopped, sd, running_tasks) if running_tasks.stop?
|
|
195
|
+
tree.partition { |sid| ArchiveStore.archive(sid, state_dir: sd) }
|
|
196
|
+
end
|
|
165
197
|
# A restart's reminders hand-off no successor took (the stop above
|
|
166
198
|
# removes a live one's): archived sessions keep no reminders.
|
|
167
199
|
archived.each { |sid| ReminderCarry.remove(Session.session_dir(sid, state_dir: sd)) }
|
|
168
200
|
# D8: an archive ends the review (unarchiving doesn't restore it).
|
|
169
201
|
Participants.end_sessions(archived, by: "archive", state_dir: sd)
|
|
170
202
|
ArchiveResult.new(id: id, archived: archived, stopped: stopped, discarded: discarded, **running_tasks.result_fields)
|
|
203
|
+
rescue SessionTransition::Busy => e
|
|
204
|
+
raise ArchiveRefused.held(e, stopped_workers: done || [])
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
# The block under the exclusive transition locks (kind :archive) of
|
|
208
|
+
# +tree+'s sessions still on disk (one its stopping worker discarded
|
|
209
|
+
# gets no folder again), within ARCHIVE_LOCK_WAIT; ids this thread
|
|
210
|
+
# holds (a continue's previous link) pass through.
|
|
211
|
+
# @raise [SessionTransition::Busy]
|
|
212
|
+
private def under_archive_locks(tree, state_dir, &)
|
|
213
|
+
ids = tree.select { |sid| Session.exist?(sid, state_dir: state_dir) }
|
|
214
|
+
SessionTransition.exclusive(ids, kind: :archive, state_dir: state_dir, deadline: ARCHIVE_LOCK_WAIT, &)
|
|
171
215
|
end
|
|
172
216
|
|
|
173
217
|
# An archive's stop, after the workers let go: a stop with +only+
|
|
@@ -261,8 +305,10 @@ module Samagotchi
|
|
|
261
305
|
# the running ones stay; without them (the automatic deletes) they
|
|
262
306
|
# are left. The result then also has unarchived:, moved_back:,
|
|
263
307
|
# deleted: and left_running: (ids)
|
|
264
|
-
# @param refuse_continuing [Boolean] a user's delete
|
|
265
|
-
#
|
|
308
|
+
# @param refuse_continuing [Boolean] a user's delete (either keyword
|
|
309
|
+
# takes SessionChain::Delete.link, which holds the transition locks
|
|
310
|
+
# exclusively and so refuses, :continuing or :transition_held, while a
|
|
311
|
+
# continue or another change of the session holds them)
|
|
266
312
|
# @raise [DeleteRefused] also :scheduled for a schedule's anchor or
|
|
267
313
|
# head, and with the keywords :continuing and :delegate_unmovable
|
|
268
314
|
# @param still [#call, nil] the caller's own check, read again under
|
|
@@ -290,8 +336,7 @@ module Samagotchi
|
|
|
290
336
|
if revive_previous || refuse_continuing
|
|
291
337
|
require_relative "../session_chain/delete"
|
|
292
338
|
return SessionChain::Delete.link(id, state_dir: sd, stop: stop, wait: wait, revive_previous: revive_previous,
|
|
293
|
-
|
|
294
|
-
running_tasks: running_tasks)
|
|
339
|
+
expect: expect, running_tasks: running_tasks)
|
|
295
340
|
end
|
|
296
341
|
|
|
297
342
|
delete_resolved(id, state_dir: sd, stop: stop, wait: wait, purge_runs: purge_runs, allow_scheduled: allow_scheduled,
|
|
@@ -506,11 +551,18 @@ module Samagotchi
|
|
|
506
551
|
|
|
507
552
|
# Stop the worker that owns +id+ or is being started for it (the stop
|
|
508
553
|
# marker: one that takes the lock leaves at once), and wait for both to
|
|
509
|
-
# be gone.
|
|
554
|
+
# be gone. A stop that meets another change's transition hold (a plain
|
|
555
|
+
# delete takes none itself; SessionChain::Delete's pass through) is
|
|
556
|
+
# the delete's refusal (DeleteRefused.held), not a raw Busy.
|
|
510
557
|
# @return [Boolean] whether no owner and no start was left within +wait+
|
|
558
|
+
# @raise [DeleteRefused] :continuing / :transition_held
|
|
511
559
|
private def stop_held(id, state_dir:, wait:)
|
|
512
560
|
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + wait.to_f
|
|
513
|
-
|
|
561
|
+
begin
|
|
562
|
+
return false unless stop_session(id, state_dir: state_dir, wait: wait)
|
|
563
|
+
rescue SessionTransition::Busy => e
|
|
564
|
+
raise DeleteRefused.held(e)
|
|
565
|
+
end
|
|
514
566
|
|
|
515
567
|
while StartingMarker.fresh?(id, state_dir: state_dir) || session_owner(id, state_dir: state_dir)
|
|
516
568
|
return false if Process.clock_gettime(Process::CLOCK_MONOTONIC) >= deadline
|
|
@@ -197,15 +197,23 @@ module Samagotchi
|
|
|
197
197
|
# gone, stopped (the stop marker; the file's status never decides),
|
|
198
198
|
# archived, a scratch session, a one-turn run's or owned, or a
|
|
199
199
|
# continue's new link still taking its delegates (its own spawn
|
|
200
|
-
# comes): the ring waits on disk for whoever runs it next.
|
|
201
|
-
#
|
|
202
|
-
#
|
|
203
|
-
#
|
|
200
|
+
# comes): the ring waits on disk for whoever runs it next. The check
|
|
201
|
+
# and the resume run under the session's transition lock, held shared
|
|
202
|
+
# (SessionInbox.wake's hold: the resolved link's), so a stop (which
|
|
203
|
+
# takes it exclusively for its mark and TERM) lands before the check
|
|
204
|
+
# or after the start, never between them; a transition holding it now
|
|
205
|
+
# leaves the ring on disk (:busy, or :continued naming no one here:
|
|
206
|
+
# the continue carries the rings). Inside a continue's own hold (its
|
|
207
|
+
# undo: wake_for_rings) the hold passes through. An archived parent
|
|
208
|
+
# with a later link wakes that link (one hop: ChildReports reads its
|
|
209
|
+
# predecessor's rings, no further).
|
|
204
210
|
# @return [Symbol] :woken, or why not (:gone, :deleting: a deleter
|
|
205
|
-
# holds its lock, :stopped, :archived, :scratch, :owned, :starting
|
|
211
|
+
# holds its lock, :stopped, :archived, :scratch, :owned, :starting,
|
|
212
|
+
# :busy / :continued: a transition holds it)
|
|
206
213
|
def wake_for_report(session_id, state_dir: nil)
|
|
207
214
|
sd = state_dir || Session.default_state_dir
|
|
208
|
-
outcome = SessionInbox.wake(:ring, session_id, state_dir: sd, owner: ->(id) { session_owner(id, state_dir: sd) }
|
|
215
|
+
outcome = SessionInbox.wake(:ring, session_id, state_dir: sd, owner: ->(id) { session_owner(id, state_dir: sd) },
|
|
216
|
+
hold: :shared).wake
|
|
209
217
|
outcome == :live ? :owned : outcome
|
|
210
218
|
end
|
|
211
219
|
|
|
@@ -11,6 +11,7 @@ require_relative "log"
|
|
|
11
11
|
require_relative "model_profile"
|
|
12
12
|
require_relative "session_manager"
|
|
13
13
|
require_relative "session_protection"
|
|
14
|
+
require_relative "session_transition"
|
|
14
15
|
require_relative "system_one/runs"
|
|
15
16
|
|
|
16
17
|
module Samagotchi
|
|
@@ -95,9 +96,12 @@ module Samagotchi
|
|
|
95
96
|
|
|
96
97
|
unless dry_run
|
|
97
98
|
policy = SessionManager::RunningTasks::Policy.stop(by: "retention")
|
|
98
|
-
|
|
99
|
-
|
|
99
|
+
gone = unheld(name, state_dir) do
|
|
100
|
+
SessionManager.remove_files(name, path, dir, state_dir: state_dir, purge_runs: false,
|
|
101
|
+
still: -> { orphan_dir?(path, dir) }, running_tasks: policy)
|
|
102
|
+
end
|
|
100
103
|
SessionManager::RunningTasks.log_automatic!(policy)
|
|
104
|
+
next unless gone
|
|
101
105
|
end
|
|
102
106
|
name
|
|
103
107
|
rescue SessionManager::DeleteRefused, SystemCallError
|
|
@@ -121,6 +125,20 @@ module Samagotchi
|
|
|
121
125
|
StartingMarker.fresh?(session_id, state_dir: state_dir)
|
|
122
126
|
end
|
|
123
127
|
|
|
128
|
+
# The block under +session_id+'s transition lock held exclusively
|
|
129
|
+
# (kind :delete, one try, before the owner lock the removal takes): a
|
|
130
|
+
# transition or an arrival holding it now (a continue, a delete, a
|
|
131
|
+
# send's shared hold) keeps the session for a later sweep.
|
|
132
|
+
# @return [Boolean] whether the block ran
|
|
133
|
+
private_class_method def self.unheld(session_id, state_dir)
|
|
134
|
+
SessionTransition.exclusive([session_id], kind: :delete, state_dir: state_dir, deadline: 0) do
|
|
135
|
+
yield
|
|
136
|
+
true
|
|
137
|
+
end
|
|
138
|
+
rescue SessionTransition::Busy
|
|
139
|
+
false
|
|
140
|
+
end
|
|
141
|
+
|
|
124
142
|
# Being started or owned now (+alive_check+, when given, reads the owner).
|
|
125
143
|
private_class_method def self.held?(session_id, state_dir, alive_check)
|
|
126
144
|
starting?(session_id, state_dir) || alive_check&.call(session_id) == true
|
|
@@ -338,11 +356,14 @@ module Samagotchi
|
|
|
338
356
|
else
|
|
339
357
|
policy = SessionManager::RunningTasks::Policy.stop(by: "retention")
|
|
340
358
|
begin
|
|
341
|
-
# Under
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
359
|
+
# Under its transition lock (held: kept) and the owner lock, the
|
|
360
|
+
# start read again (as a delete).
|
|
361
|
+
gone = unheld(session.id, state_dir) do
|
|
362
|
+
SessionManager.remove_files(session.id, path, File.join(state_dir, session.id), state_dir: state_dir,
|
|
363
|
+
purge_runs: false, running_tasks: policy)
|
|
364
|
+
end
|
|
365
|
+
(gone ? deleted : kept) << session.id
|
|
366
|
+
purge_ids << session.id if gone
|
|
346
367
|
rescue SessionManager::DeleteRefused
|
|
347
368
|
kept << session.id
|
|
348
369
|
rescue StandardError
|
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "fileutils"
|
|
4
|
+
require_relative "session"
|
|
5
|
+
|
|
6
|
+
module Samagotchi
|
|
7
|
+
# A session's transition lock: the one code that flocks a session folder's
|
|
8
|
+
# LOCK (continue.lock). A multi-step lifecycle transition (a continue
|
|
9
|
+
# today) holds it exclusively; an arrival or a check that must not overlap
|
|
10
|
+
# one (a followed send, a link or chain delete) takes it shared and
|
|
11
|
+
# non-blocking, and a read-only caller only probes it (#held?).
|
|
12
|
+
#
|
|
13
|
+
# Rules (docs/internals/session-lifecycle.md, "Locks and crash repair"):
|
|
14
|
+
# - Order: transition locks (several ids sorted) before any owner.lock.
|
|
15
|
+
# - The hold is the current thread's (Thread#thread_variable_*, not
|
|
16
|
+
# fiber-local and never per process: chi web is multi-threaded). A
|
|
17
|
+
# thread holding an id exclusively passes through #exclusive and #shared
|
|
18
|
+
# of that id without touching the file (each acquisition is a fresh open:
|
|
19
|
+
# a second flock on a held fd, or a dup of it, would convert the lock).
|
|
20
|
+
# Work handed to another thread does not carry the hold: it takes it
|
|
21
|
+
# again, or is refused.
|
|
22
|
+
# - No fork while held: a forked child keeps the flock. Workers start
|
|
23
|
+
# through Process.spawn, whose descriptors are close-on-exec.
|
|
24
|
+
module SessionTransition
|
|
25
|
+
# The lock file in a session's folder. The name stays continue.lock: an
|
|
26
|
+
# older chi's continue, follow_guard and delete honour it.
|
|
27
|
+
LOCK = "continue.lock"
|
|
28
|
+
# How often an exclusive try with a deadline tries again.
|
|
29
|
+
RETRY = 0.01
|
|
30
|
+
# The thread variable holding this thread's exclusive holds
|
|
31
|
+
# ({ lock path => true }).
|
|
32
|
+
REGISTRY = :samagotchi_session_transition
|
|
33
|
+
|
|
34
|
+
# What #exclusive yields: the ids it holds and why.
|
|
35
|
+
Token = Data.define(:ids, :kind)
|
|
36
|
+
|
|
37
|
+
# An exclusive hold not taken by its deadline, or an arrival that met
|
|
38
|
+
# one. It owns its refusal text, which names the holder (.held) when it
|
|
39
|
+
# can be told: a continue ("being continued", "as X" once its intent
|
|
40
|
+
# names the new link), a delete ("being deleted"), a stop ("being
|
|
41
|
+
# stopped"), an archive ("being archived"), else "busy with another
|
|
42
|
+
# change" (a shared holder, an older chi's, a kind this chi doesn't
|
|
43
|
+
# name). A nil +session_id+ says "this session" (a peer's line,
|
|
44
|
+
# which carries no id).
|
|
45
|
+
class Busy < StandardError
|
|
46
|
+
WORDS = { continue: "being continued", delete: "being deleted", stop: "being stopped", archive: "being archived",
|
|
47
|
+
nil => "busy with another change" }.freeze
|
|
48
|
+
|
|
49
|
+
# +kind+ the holder's (nil: not told), +successor_id+ the new link a
|
|
50
|
+
# continue holding it names.
|
|
51
|
+
attr_reader :session_id, :kind, :successor_id
|
|
52
|
+
|
|
53
|
+
def initialize(session_id, kind: nil, successor_id: nil)
|
|
54
|
+
@session_id = session_id
|
|
55
|
+
@kind = kind
|
|
56
|
+
@successor_id = successor_id
|
|
57
|
+
super("#{words}; try again in a moment")
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# The refusal of an arrival or a taker that met +session_id+'s lock
|
|
61
|
+
# held: the one place that says who holds it. The kind the exclusive
|
|
62
|
+
# holder recorded (SessionTransition.holder) first: a delete, a stop
|
|
63
|
+
# or an archive is said as such whatever else is on disk (a crashed
|
|
64
|
+
# continue's intent); else a continue in flight
|
|
65
|
+
# (ContinueIntent.in_flight: an older chi's records no kind) names
|
|
66
|
+
# its new link.
|
|
67
|
+
# @return [Busy]
|
|
68
|
+
def self.held(session_id, state_dir:)
|
|
69
|
+
kind = SessionTransition.holder(session_id, state_dir: state_dir)
|
|
70
|
+
return new(session_id, kind: kind) if SessionTransition.other_than_continue?(kind)
|
|
71
|
+
|
|
72
|
+
require_relative "continue_intent"
|
|
73
|
+
successor = session_id && ContinueIntent.in_flight(session_id, state_dir: state_dir)
|
|
74
|
+
new(session_id, kind: successor ? :continue : kind, successor_id: successor)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
private
|
|
78
|
+
|
|
79
|
+
def words
|
|
80
|
+
if @successor_id
|
|
81
|
+
require_relative "session_chain/successor"
|
|
82
|
+
return SessionChain::Continued.new(@session_id, @successor_id, in_flight: true).message
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
subject = @session_id ? "session #{@session_id.to_s[0, 8]}" : "this session"
|
|
86
|
+
"#{subject} is #{WORDS.fetch(@kind, WORDS[nil])}"
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
module_function
|
|
91
|
+
|
|
92
|
+
# The block under an exclusive hold of every id's lock, taken in sorted
|
|
93
|
+
# order, each folder created when missing. Ids this thread already holds
|
|
94
|
+
# are passed through. +deadline+ nil blocks until each lock is free;
|
|
95
|
+
# seconds try each until then.
|
|
96
|
+
# @yieldparam token [Token]
|
|
97
|
+
# @return the block's value
|
|
98
|
+
# @raise [Busy] an id not taken in time (the ids taken before it are let go)
|
|
99
|
+
# @raise [ArgumentError] an id that isn't a session id
|
|
100
|
+
def exclusive(ids, kind:, state_dir:, deadline: nil)
|
|
101
|
+
ids = Array(ids).uniq.sort
|
|
102
|
+
bad = ids.reject { |id| Session.valid_id?(id) }
|
|
103
|
+
raise ArgumentError, "not a session id: #{bad.first.inspect}" unless bad.empty?
|
|
104
|
+
|
|
105
|
+
ends_at = monotonic + deadline if deadline
|
|
106
|
+
taken = []
|
|
107
|
+
ids.each do |id|
|
|
108
|
+
path = lock_path(id, state_dir)
|
|
109
|
+
next if holds.key?(path)
|
|
110
|
+
|
|
111
|
+
FileUtils.mkdir_p(File.dirname(path))
|
|
112
|
+
file = File.open(path, File::RDWR | File::CREAT, 0o600) # rubocop:disable Style/FileOpen -- held past this loop, closed in the ensure
|
|
113
|
+
taken << [path, file]
|
|
114
|
+
lock_until(file, ends_at) or raise Busy.held(id, state_dir: state_dir)
|
|
115
|
+
holds[path] = true
|
|
116
|
+
mark(file, kind)
|
|
117
|
+
end
|
|
118
|
+
yield Token.new(ids: ids, kind: kind)
|
|
119
|
+
ensure
|
|
120
|
+
# Only ids this call took: none of them was held before it. A kind is
|
|
121
|
+
# cleared only from a lock this call held (never another holder's).
|
|
122
|
+
taken&.reverse_each do |path, file|
|
|
123
|
+
mark(file, nil) if holds.delete(path)
|
|
124
|
+
file.close
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# The block under an exclusive hold of +id+'s lock taken in one try,
|
|
129
|
+
# its argument nil when it isn't (another holder, this thread's included:
|
|
130
|
+
# a crash repair never undoes the continue running on its own thread).
|
|
131
|
+
# The folder is not created (its lock file is). Crash repair's
|
|
132
|
+
# (SessionContinue.repair_interrupted).
|
|
133
|
+
# @yieldparam token [Token, nil]
|
|
134
|
+
# @raise [Errno::ENOENT] the folder is gone
|
|
135
|
+
def try_exclusive(id, kind:, state_dir:)
|
|
136
|
+
return yield(nil) unless Session.valid_id?(id)
|
|
137
|
+
|
|
138
|
+
path = lock_path(id, state_dir)
|
|
139
|
+
return yield(nil) if holds.key?(path)
|
|
140
|
+
|
|
141
|
+
File.open(path, File::RDWR | File::CREAT, 0o600) do |file|
|
|
142
|
+
next yield(nil) unless file.flock(File::LOCK_EX | File::LOCK_NB)
|
|
143
|
+
|
|
144
|
+
holds[path] = true
|
|
145
|
+
mark(file, kind)
|
|
146
|
+
begin
|
|
147
|
+
yield Token.new(ids: [id], kind: kind)
|
|
148
|
+
ensure
|
|
149
|
+
holds.delete(path)
|
|
150
|
+
mark(file, nil)
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# The block under a shared hold of +id+'s lock, so no transition of it
|
|
156
|
+
# starts until the block returns; its argument is false when another
|
|
157
|
+
# holds it exclusively now (not waited for: nothing is held then) or
|
|
158
|
+
# +id+ isn't a session id, true when this thread holds it. The lock
|
|
159
|
+
# file is created when missing, and the folder only for a session whose
|
|
160
|
+
# file is there: a deleted session's folder is never made again
|
|
161
|
+
# (nothing is held, true: no transition holds a lock in a folder that
|
|
162
|
+
# isn't there) and the caller's own gone refusal follows. A lock file this call made for a
|
|
163
|
+
# session whose file is gone once the block returns (a delete that
|
|
164
|
+
# removed it meanwhile, a folder with no session) is taken back
|
|
165
|
+
# (#forget). A caller that may not write the session folder (a peer's
|
|
166
|
+
# chi behind the mailbox door: the sessions folder read-only) holds an
|
|
167
|
+
# existing lock file opened read-only, and, with none there to open,
|
|
168
|
+
# holds nothing (true).
|
|
169
|
+
# @yieldparam held [Boolean]
|
|
170
|
+
# @return the block's value
|
|
171
|
+
def shared(id, state_dir:)
|
|
172
|
+
return yield(false) unless Session.valid_id?(id)
|
|
173
|
+
|
|
174
|
+
path = lock_path(id, state_dir)
|
|
175
|
+
return yield(true) if holds.key?(path)
|
|
176
|
+
|
|
177
|
+
made = !File.exist?(path)
|
|
178
|
+
file = open_shared(id, path, state_dir) or return yield(true)
|
|
179
|
+
begin
|
|
180
|
+
yield(file.flock(File::LOCK_SH | File::LOCK_NB) ? true : false)
|
|
181
|
+
ensure
|
|
182
|
+
file.close
|
|
183
|
+
forget(id, state_dir: state_dir) if made
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
# Whether +id+'s lock is held exclusively now, by anyone (this thread
|
|
188
|
+
# too: a kernel probe, the registry is not read). Creates nothing; the
|
|
189
|
+
# shared try is let go at once.
|
|
190
|
+
def held?(id, state_dir:)
|
|
191
|
+
return false unless Session.valid_id?(id)
|
|
192
|
+
|
|
193
|
+
File.open(lock_path(id, state_dir), File::RDONLY) { |file| !file.flock(File::LOCK_SH | File::LOCK_NB) }
|
|
194
|
+
rescue Errno::ENOENT
|
|
195
|
+
false
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
# The kind the exclusive holder of +id+'s lock recorded (#exclusive's
|
|
199
|
+
# and #try_exclusive's +kind+, written into the lock file while held),
|
|
200
|
+
# one of Busy::WORDS' kinds: nil when nobody holds it exclusively now (a
|
|
201
|
+
# shared holder, such as a send, is no transition), or the holder
|
|
202
|
+
# recorded none this chi names (an older chi, a raw flock). Read only
|
|
203
|
+
# behind a failed shared try (#held?), so a crash's leftover is never
|
|
204
|
+
# believed.
|
|
205
|
+
# @return [Symbol, nil]
|
|
206
|
+
def holder(id, state_dir:)
|
|
207
|
+
return nil unless held?(id, state_dir: state_dir)
|
|
208
|
+
|
|
209
|
+
recorded = File.read(lock_path(id, state_dir), 64).to_s.strip
|
|
210
|
+
Busy::WORDS.keys.compact.find { |kind| kind.to_s == recorded }
|
|
211
|
+
rescue SystemCallError
|
|
212
|
+
nil
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
# Whether +kind+ (#holder's) is a transition this chi names that is no
|
|
216
|
+
# continue (a delete, a stop, an archive): its hold names no new link,
|
|
217
|
+
# whatever intent a crashed continue left.
|
|
218
|
+
def other_than_continue?(kind) = !kind.nil? && kind != :continue
|
|
219
|
+
|
|
220
|
+
# Whether the current thread holds +id+ exclusively (the registry).
|
|
221
|
+
def holding?(id, state_dir:)
|
|
222
|
+
Session.valid_id?(id) && holds.key?(lock_path(id, state_dir))
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
# Take back the lock file #exclusive left in the folder of a session
|
|
226
|
+
# with no session file (a continue's successor never saved, a link
|
|
227
|
+
# deleted while a continue waited for it), and the folder when that
|
|
228
|
+
# leaves it empty: an empty folder holding only the lock is no session.
|
|
229
|
+
# Only when nobody holds it (one try; held, it stays) and this thread
|
|
230
|
+
# doesn't; a saved session's lock is never touched.
|
|
231
|
+
# @return [Boolean] whether the lock file went
|
|
232
|
+
def forget(id, state_dir:)
|
|
233
|
+
return false unless Session.valid_id?(id) && !Session.exist?(id, state_dir: state_dir)
|
|
234
|
+
|
|
235
|
+
path = lock_path(id, state_dir)
|
|
236
|
+
return false if holds.key?(path)
|
|
237
|
+
|
|
238
|
+
gone = File.open(path, File::RDWR) do |file|
|
|
239
|
+
next false unless file.flock(File::LOCK_EX | File::LOCK_NB)
|
|
240
|
+
|
|
241
|
+
File.unlink(path)
|
|
242
|
+
true
|
|
243
|
+
end
|
|
244
|
+
begin
|
|
245
|
+
Dir.rmdir(File.dirname(path))
|
|
246
|
+
rescue Errno::ENOTEMPTY, Errno::EEXIST, Errno::ENOENT
|
|
247
|
+
nil
|
|
248
|
+
end
|
|
249
|
+
gone
|
|
250
|
+
rescue Errno::ENOENT
|
|
251
|
+
false
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
def lock_path(id, state_dir) = File.join(Session.session_dir(id, state_dir: state_dir), LOCK)
|
|
255
|
+
|
|
256
|
+
# #shared's descriptor: made when missing (its folder too, only for a
|
|
257
|
+
# session whose file is there), else (no write access) the file opened
|
|
258
|
+
# read-only, or nil when there is nothing to open.
|
|
259
|
+
def open_shared(id, path, state_dir)
|
|
260
|
+
begin
|
|
261
|
+
File.open(path, File::RDWR | File::CREAT, 0o600)
|
|
262
|
+
rescue Errno::ENOENT
|
|
263
|
+
return nil unless Session.exist?(id, state_dir: state_dir)
|
|
264
|
+
|
|
265
|
+
FileUtils.mkdir_p(File.dirname(path))
|
|
266
|
+
File.open(path, File::RDWR | File::CREAT, 0o600)
|
|
267
|
+
end
|
|
268
|
+
rescue Errno::ENOENT
|
|
269
|
+
nil
|
|
270
|
+
rescue Errno::EACCES, Errno::EPERM, Errno::EROFS
|
|
271
|
+
begin
|
|
272
|
+
File.open(path, File::RDONLY)
|
|
273
|
+
rescue Errno::ENOENT, Errno::EACCES
|
|
274
|
+
nil
|
|
275
|
+
end
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
# Record +kind+ (nil: none) as the holder's in a lock file this thread
|
|
279
|
+
# holds exclusively (#holder reads it). The file is the lock's only
|
|
280
|
+
# content: an older chi only flocks it. Never fatal.
|
|
281
|
+
def mark(file, kind)
|
|
282
|
+
file.truncate(0)
|
|
283
|
+
file.rewind
|
|
284
|
+
file.write(kind.to_s) if kind
|
|
285
|
+
file.flush
|
|
286
|
+
rescue IOError, SystemCallError
|
|
287
|
+
nil
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
# This thread's holds.
|
|
291
|
+
def holds
|
|
292
|
+
Thread.current.thread_variable_get(REGISTRY) || Thread.current.thread_variable_set(REGISTRY, {})
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
# Lock +file+ exclusively: blocking when +ends_at+ is nil, else tried
|
|
296
|
+
# until that monotonic time. Whether it was taken.
|
|
297
|
+
def lock_until(file, ends_at)
|
|
298
|
+
unless ends_at
|
|
299
|
+
file.flock(File::LOCK_EX)
|
|
300
|
+
return true
|
|
301
|
+
end
|
|
302
|
+
|
|
303
|
+
loop do
|
|
304
|
+
return true if file.flock(File::LOCK_EX | File::LOCK_NB)
|
|
305
|
+
return false if monotonic >= ends_at
|
|
306
|
+
|
|
307
|
+
sleep RETRY
|
|
308
|
+
end
|
|
309
|
+
end
|
|
310
|
+
|
|
311
|
+
def monotonic = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
312
|
+
|
|
313
|
+
private_class_method :lock_path, :open_shared, :mark, :holds, :lock_until, :monotonic
|
|
314
|
+
end
|
|
315
|
+
end
|
|
@@ -289,7 +289,7 @@ module Samagotchi
|
|
|
289
289
|
begin
|
|
290
290
|
id = @access.resolve_id(given)
|
|
291
291
|
released = @access.stop(id, wait: Samagotchi::SessionManager::STOP_WAIT)
|
|
292
|
-
rescue Samagotchi::SessionManager::OwnedByTUI => e
|
|
292
|
+
rescue Samagotchi::SessionManager::OwnedByTUI, Samagotchi::SessionTransition::Busy => e
|
|
293
293
|
@stdout.flush
|
|
294
294
|
@stderr.puts e.message
|
|
295
295
|
next false
|
|
@@ -146,6 +146,8 @@ module Samagotchi
|
|
|
146
146
|
options[:no_interrupt] = true if @no_interrupt
|
|
147
147
|
options[:images] = images unless images.empty?
|
|
148
148
|
options[:delivery] = delivery if delivery
|
|
149
|
+
# Transition hold: worker-side (its live worker's Bridge); over
|
|
150
|
+
# --remote, chi web's turn route (deliver_turn's :shared seat).
|
|
149
151
|
reply = @client.post_turn(**options)
|
|
150
152
|
if reply.status == 202
|
|
151
153
|
enqueued_id = reply.json&.fetch("enqueued_id", nil)
|
data/lib/samagotchi/version.rb
CHANGED
data/lib/samagotchi/web/app.rb
CHANGED
|
@@ -1497,10 +1497,14 @@ module Samagotchi
|
|
|
1497
1497
|
return error_response(422, "no_provider", "no installed bundle resolves #{url[0, 200]}") unless resolved
|
|
1498
1498
|
|
|
1499
1499
|
own = ContextSources.session_location(session.id, state_dir: default_state_dir)
|
|
1500
|
-
source =
|
|
1500
|
+
source = ContextSources.changing(session.id, state_dir: default_state_dir) do
|
|
1501
|
+
own.add(web_source(resolved, body, own.scope))
|
|
1502
|
+
end
|
|
1501
1503
|
json_response(201, { status: "attached", name: resolved.name, movable: source.movable? })
|
|
1502
1504
|
rescue ContextProviders::Invalid => e
|
|
1503
1505
|
error_response(422, "no_provider", e.message)
|
|
1506
|
+
rescue ContextSources::Held => e
|
|
1507
|
+
error_response(423, "context_busy", e.message)
|
|
1504
1508
|
rescue ContextSources::Invalid => e
|
|
1505
1509
|
error_response(409, "exists", e.message)
|
|
1506
1510
|
end
|
|
@@ -1552,12 +1556,14 @@ module Samagotchi
|
|
|
1552
1556
|
session = @session_class.load(id, state_dir: default_state_dir)
|
|
1553
1557
|
attached = find_context(session, name) or return error_response(404, "context_missing", "no source #{name[0, 40]}")
|
|
1554
1558
|
if attached.location.session?
|
|
1555
|
-
attached.location.remove(attached.name)
|
|
1559
|
+
ContextSources.changing(id, state_dir: default_state_dir) { attached.location.remove(attached.name) }
|
|
1556
1560
|
json_response(200, { status: "detached", name: attached.name })
|
|
1557
1561
|
else
|
|
1558
1562
|
ContextSources.session_location(id, state_dir: default_state_dir).mute(attached.name)
|
|
1559
1563
|
json_response(200, { status: "muted", name: attached.name })
|
|
1560
1564
|
end
|
|
1565
|
+
rescue ContextSources::Held => e
|
|
1566
|
+
error_response(423, "context_busy", e.message)
|
|
1561
1567
|
end
|
|
1562
1568
|
|
|
1563
1569
|
def context_remove_own(id, name)
|
|
@@ -1567,6 +1573,8 @@ module Samagotchi
|
|
|
1567
1573
|
error_response(409, "project_source", ContextCommand.project_source_message(name))
|
|
1568
1574
|
else error_response(404, "context_missing", "no source #{name[0, 40]}")
|
|
1569
1575
|
end
|
|
1576
|
+
rescue ContextSources::Held => e
|
|
1577
|
+
error_response(423, "context_busy", e.message)
|
|
1570
1578
|
rescue ContextSources::Invalid
|
|
1571
1579
|
error_response(404, "context_missing", "no source #{name[0, 40]}")
|
|
1572
1580
|
end
|
|
@@ -101,7 +101,8 @@ module Samagotchi
|
|
|
101
101
|
paired = session_access.peer_pair(id, name, address: address, from: from)
|
|
102
102
|
return json_response(200, { status: "paired", link: paired.link.to_h }) if paired.status == :paired
|
|
103
103
|
|
|
104
|
-
json_response(409, { status: paired.status.to_s, next_id: paired.next_id,
|
|
104
|
+
json_response(409, { status: paired.status.to_s, next_id: paired.next_id, in_flight: (true if paired.in_flight),
|
|
105
|
+
detail: paired.refusal }.compact)
|
|
105
106
|
end
|
|
106
107
|
|
|
107
108
|
# POST /api/sessions/:id/peers/:name/say {text, summary, wake, re,
|
|
@@ -120,7 +121,9 @@ module Samagotchi
|
|
|
120
121
|
|
|
121
122
|
said = session_access.peer_say(id, name, text: body["text"], summary: string_or_nil(body["summary"]),
|
|
122
123
|
wake: body["wake"] == true, re: string_or_nil(body["re"]), wait: wait)
|
|
123
|
-
|
|
124
|
+
line = said.line(name)
|
|
125
|
+
# An older chi --remote knows no busy status: it prints the detail.
|
|
126
|
+
json_response(said.received? ? 200 : 409, said.to_h.merge("line" => line, **(said.status == :busy ? { "detail" => line } : {})))
|
|
124
127
|
rescue PeerMailbox::Invalid => e
|
|
125
128
|
error_response(400, "invalid_text", e.message)
|
|
126
129
|
end
|