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
//! How a session's turns are recorded in the job registry once it is added.
use super::{stopping, writing};
use crate::app::daemon_host::jobs::{JobAgent, JobRecord, JobRegistry, RegisterError, SessionJob};
use rho_protocol::{ErrorCode, RpcError};
use rho_sdk::SessionId;
/// What a session's job records of it as the session opens.
pub(super) struct JobOpening {
/// The agent it runs as.
pub(super) agent: JobAgent,
/// The title it opens with, its saved one, if it has one.
pub(super) title: Option<String>,
/// The session it reports to, if it reports to its starter.
pub(super) reports_to: Option<SessionId>,
}
/// How a session's turns are recorded once it is added.
pub(super) enum SessionJobRecord {
/// On a new job, registered as the session is added.
Register(JobRecord),
/// On the session's last job, which an earlier daemon or an earlier load
/// recorded.
Reuse(JobRecord),
}
impl SessionJobRecord {
pub(super) fn job(&self) -> &JobRecord {
match self {
Self::Register(record) | Self::Reuse(record) => record,
}
}
/// Records the job in `jobs` as the one of `session`'s turns, which
/// open as `opening` says, refusing it when the registry no longer agrees, as
/// when another job began writing the session while it reopened
/// (#1661).
pub(super) fn claim(
self,
jobs: &JobRegistry,
session: &SessionId,
opening: JobOpening,
) -> Result<(), RpcError> {
let JobOpening {
agent,
title,
reports_to,
} = opening;
match (self, jobs.session_job(session)) {
(Self::Register(record), SessionJob::New) => {
let record = JobRecord {
agent: Some(agent),
title,
reports_to,
..record
};
jobs.register(record).map_err(|error| match error {
// Stopping, or restarting for an upgrade.
RegisterError::Closed => stopping(),
RegisterError::Io(error) => RpcError::new(
ErrorCode::InternalError,
format!("could not record the session's job: {error}"),
),
})
}
(Self::Reuse(record), SessionJob::Last(last)) if last.id == record.id => {
// The session reopens with the grant its environment
// carries, held to the job's limit, so its record says the
// same.
jobs.set_grant(&record.id, record.grant);
jobs.set_agent(&record.id, agent);
jobs.set_reports_to(&record.id, reports_to.as_ref());
if let Some(title) = title {
jobs.set_title(&record.id, &title);
}
Ok(())
}
(_, SessionJob::Busy(job)) => Err(writing(&job, session)),
(Self::Register(_) | Self::Reuse(_), SessionJob::New | SessionJob::Last(_)) => {
Err(RpcError::new(
ErrorCode::SessionBusy,
format!("session {session}'s job changed while the daemon opened it"),
))
}
}
}
}