Repository navigation
Expand file tree
/
Copy pathlifecycle.rs
More file actions
304 lines (288 loc) · 10.1 KB
/
Copy pathlifecycle.rs
File metadata and controls
304 lines (288 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//! Identity lookup, terminal transitions, retention, and idle reclamation.
use super::*;
impl PersistentShellManager {
pub fn status(
&self,
shell_id: &str,
workflow_session_id: &str,
runtime_project_id: &str,
) -> Result<ShellSummary, ShellError> {
self.sweep_idle();
let entry = self.lookup(shell_id, workflow_session_id, runtime_project_id)?;
self.refresh_exit(&entry);
Ok(entry.summary())
}
pub fn set_output_limit(
&self,
shell_id: &str,
workflow_session_id: &str,
runtime_project_id: &str,
max_output_bytes: usize,
) -> Result<(), ShellError> {
let entry = self.lookup(shell_id, workflow_session_id, runtime_project_id)?;
lock_unpoison(entry.process.stdout()).set_max_bytes(max_output_bytes);
lock_unpoison(entry.process.stderr()).set_max_bytes(max_output_bytes);
Ok(())
}
pub fn close(
&self,
shell_id: &str,
workflow_session_id: &str,
runtime_project_id: &str,
reason: &str,
) -> Result<ShellCloseResult, ShellError> {
let entry = self.lookup(shell_id, workflow_session_id, runtime_project_id)?;
self.refresh_exit(&entry);
let already_closed = {
let state = *lock_unpoison(&entry.state);
matches!(
state,
ShellState::Closed | ShellState::Exited | ShellState::Poisoned | ShellState::Lost
)
};
if !already_closed {
self.transition_terminal(&entry, ShellState::Closed, None, Some(reason.to_string()));
entry.process.shutdown();
}
Ok(ShellCloseResult {
summary: entry.summary(),
already_closed,
})
}
pub fn close_session(&self, workflow_session_id: &str, reason: &str) -> usize {
let entries = lock_unpoison(&self.inner.entries)
.values()
.filter(|entry| entry.identity.workflow_session_id == workflow_session_id)
.cloned()
.collect::<Vec<_>>();
let mut closed = 0;
for entry in entries {
self.refresh_exit(&entry);
if lock_unpoison(&entry.state).is_active() {
self.transition_terminal(
&entry,
ShellState::Closed,
None,
Some(reason.to_string()),
);
entry.process.shutdown();
closed += 1;
}
}
closed
}
pub fn close_project(&self, runtime_project_id: &str, reason: &str) -> usize {
let entries = lock_unpoison(&self.inner.entries)
.values()
.filter(|entry| entry.identity.runtime_project_id == runtime_project_id)
.cloned()
.collect::<Vec<_>>();
let mut closed = 0;
for entry in entries {
self.refresh_exit(&entry);
if lock_unpoison(&entry.state).is_active() {
self.transition_terminal(
&entry,
ShellState::Closed,
None,
Some(reason.to_string()),
);
entry.process.shutdown();
closed += 1;
}
}
closed
}
pub fn close_all(&self, reason: &str) -> usize {
let entries = lock_unpoison(&self.inner.entries)
.values()
.cloned()
.collect::<Vec<_>>();
let mut closed = 0;
for entry in entries {
self.refresh_exit(&entry);
if lock_unpoison(&entry.state).is_active() {
self.transition_terminal(
&entry,
ShellState::Closed,
None,
Some(reason.to_string()),
);
entry.process.shutdown();
closed += 1;
}
}
closed
}
pub fn active_count(&self) -> usize {
lock_unpoison(&self.inner.active_by_session).len()
}
/// The opaque transport binding metadata captured when the shell was
/// opened, if the transport reported any. Used by callers to validate that
/// the bindings an open shell depends on are still current (e.g. a named
/// SSH resource at the configuration generation it was opened against).
pub fn metadata(
&self,
shell_id: &str,
workflow_session_id: &str,
runtime_project_id: &str,
) -> Result<Option<TransportMetadata>, ShellError> {
let entry = self.lookup(shell_id, workflow_session_id, runtime_project_id)?;
let metadata = lock_unpoison(&entry.metadata).clone();
Ok(metadata)
}
pub fn sweep_idle(&self) -> usize {
let idle = Duration::from_secs(self.inner.idle_timeout_secs.load(Ordering::SeqCst));
let entries = lock_unpoison(&self.inner.entries)
.values()
.cloned()
.collect::<Vec<_>>();
let mut closed = 0;
for entry in entries {
self.refresh_exit(&entry);
let should_attempt_close = lock_unpoison(&entry.state).is_active()
&& lock_unpoison(&entry.last_activity_instant).elapsed() >= idle;
if !should_attempt_close {
continue;
}
if entry
.busy
.compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
.is_err()
{
continue;
}
let should_close = lock_unpoison(&entry.state).is_active()
&& lock_unpoison(&entry.last_activity_instant).elapsed() >= idle;
if !should_close {
entry.busy.store(false, Ordering::SeqCst);
continue;
}
self.transition_terminal(
&entry,
ShellState::Closed,
None,
Some("idle_timeout".to_string()),
);
entry.process.shutdown();
closed += 1;
}
closed
}
pub(super) fn lookup(
&self,
shell_id: &str,
workflow_session_id: &str,
runtime_project_id: &str,
) -> Result<Arc<ShellEntry>, ShellError> {
let entry = lock_unpoison(&self.inner.entries)
.get(shell_id)
.cloned()
.ok_or_else(|| {
ShellError::new(
"persistent_shell_not_found",
"persistent shell was not found or belongs to another runtime",
)
})?;
entry.validate_identity(workflow_session_id, runtime_project_id)?;
Ok(entry)
}
pub(super) fn ensure_idle_sweeper(&self) {
if !self.inner.sweeper_started.swap(true, Ordering::SeqCst) {
spawn_idle_sweeper(Arc::downgrade(&self.inner));
}
}
pub(super) fn refresh_exit(&self, entry: &Arc<ShellEntry>) {
if !lock_unpoison(&entry.state).is_active() {
return;
}
if let Some(status) = entry.process.try_wait() {
self.transition_terminal(
entry,
ShellState::Exited,
status.code(),
Some("shell_process_exited".to_string()),
);
entry.process.terminate_remaining_group_after_exit();
}
}
pub(super) fn transition_terminal(
&self,
entry: &Arc<ShellEntry>,
state: ShellState,
exit_code: Option<i32>,
reason: Option<String>,
) {
{
let mut current = lock_unpoison(&entry.state);
if !current.is_active() && *current != ShellState::Opening {
return;
}
*current = state;
}
entry.busy.store(false, Ordering::SeqCst);
*lock_unpoison(&entry.exit_code) = exit_code;
*lock_unpoison(&entry.close_reason) = reason;
// A terminal shell can never be reused; release its transport-binding
// metadata so a stale binding cannot keep validating a dead entry.
*lock_unpoison(&entry.metadata) = None;
entry.touch();
let mut active = lock_unpoison(&self.inner.active_by_session);
if active
.get(&entry.identity.workflow_session_id)
.is_some_and(|id| id == &entry.identity.shell_id)
{
active.remove(&entry.identity.workflow_session_id);
}
drop(active);
let mut order = lock_unpoison(&self.inner.terminal_order);
if !order.iter().any(|id| id == &entry.identity.shell_id) {
order.push_back(entry.identity.shell_id.clone());
}
drop(order);
self.prune_terminal_records();
}
pub(super) fn prune_terminal_records(&self) {
let limit = self.inner.max_terminal_records.load(Ordering::SeqCst);
loop {
let remove = {
let mut order = lock_unpoison(&self.inner.terminal_order);
(order.len() > limit).then(|| order.pop_front()).flatten()
};
let Some(shell_id) = remove else {
break;
};
let mut entries = lock_unpoison(&self.inner.entries);
if entries
.get(&shell_id)
.is_some_and(|entry| !lock_unpoison(&entry.state).is_active())
{
entries.remove(&shell_id);
}
}
}
}
fn spawn_idle_sweeper(weak: Weak<ManagerInner>) {
let _ = thread::Builder::new()
.name("wc-persistent-shell-idle".to_string())
.spawn(move || loop {
let Some(inner) = weak.upgrade() else {
break;
};
if inner.stop_sweeper.load(Ordering::SeqCst) {
break;
}
let interval = Duration::from_secs(
(inner.idle_timeout_secs.load(Ordering::SeqCst) / 4).clamp(1, 30),
);
drop(inner);
thread::sleep(interval);
let Some(inner) = weak.upgrade() else {
break;
};
if inner.stop_sweeper.load(Ordering::SeqCst) {
break;
}
PersistentShellManager { inner }.sweep_idle();
});
}