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
//! Telling a session, once after the daemon starts, of each session that
//! reports to it, completed its last turn, and waits on an unmerged pull
//! request it watches.
//!
//! Such a session owes its starter nothing: its starter merges what it
//! reported, and pull request news wakes the session to report again,
//! through the opening the idle unload kept (see `pull_request_wake`). That
//! opening lives only in memory, so once the daemon ends, nothing wakes the
//! session until it is prompted. Its starter, told of the sessions a
//! daemon's end stopped (see `peer_wake`), would hear nothing of it and
//! never prompt it.
//!
//! So the daemon notes, as it starts, each session whose job records that it
//! reports to a starter (`JobRecord::reports_to`) and whose last turn
//! completed. As a starter loads to hear, it is told of each such session
//! that is still unloaded, with nothing else to wake it, and watches a pull
//! request no read found merged or closed: one notice each, through its
//! inbox, all at once. A session the starter is told anything else of, as a
//! stop, a turn's end, or a question, is told that instead: this notice is
//! left out, or a later one replaces it. Each session is told once per
//! daemon start: forgotten as its notice is delivered, or as the starter's
//! load finds it no longer waits. A starter that unloads before taking a
//! notice is told again as it next loads. Nothing starts a turn of the
//! waiting session: its starter decides whether to prompt it.
use std::{
collections::HashMap,
sync::{Arc, Mutex, MutexGuard, PoisonError},
};
use rho_sdk::SessionId;
use super::{super::jobs::Reporter, Sessions};
use crate::app::peer_sessions::{PeerInbox, Receipt};
/// The sessions noted as the daemon started, by the session they report to,
/// that their starter has yet to be told of.
#[derive(Default)]
pub(super) struct RestartNotices(Arc<Mutex<Noted>>);
type Noted = HashMap<SessionId, Vec<Reporter>>;
impl RestartNotices {
fn lock(&self) -> MutexGuard<'_, Noted> {
lock(&self.0)
}
/// What runs once the notice of `session` reaches `starter`.
fn receipt(&self, starter: &SessionId, session: &SessionId) -> Receipt {
let noted = Arc::downgrade(&self.0);
let (starter, session) = (starter.clone(), session.clone());
Receipt::new(move || {
if let Some(noted) = noted.upgrade() {
forget(&mut lock(¬ed), &starter, &session);
}
})
}
}
fn lock(noted: &Mutex<Noted>) -> MutexGuard<'_, Noted> {
// Each change is one insert or removal.
noted.lock().unwrap_or_else(PoisonError::into_inner)
}
/// `starter` needs no telling of `session` any more.
fn forget(noted: &mut Noted, starter: &SessionId, session: &SessionId) {
if let Some(sessions) = noted.get_mut(starter) {
sessions.retain(|reporter| reporter.session != *session);
if sessions.is_empty() {
noted.remove(starter);
}
}
}
impl Sessions {
/// Notes each session that reports to its starter and whose last turn
/// completed, to tell its starter as it loads should the session wait
/// on a pull request. Call once, as the daemon starts, before any
/// session loads.
pub(in crate::app::daemon_host) fn note_restart_notices(&self) {
let mut noted = self.restart_notices.lock();
for reporter in self.jobs.reporters() {
let starter = reporter.starter.clone();
noted.entry(starter).or_default().push(reporter);
}
}
/// Tells `starter`, just loaded to hear in `inbox`, of each session
/// noted for it that still waits: not one `wakes` names, as one loaded,
/// and watching a pull request unmerged, unless a notice of it waits
/// already, which tells more of it. Call holding no lock, once
/// `starter` was told the rest (see `peer_wake`).
pub(super) fn tell_restarted(
&self,
starter: &SessionId,
inbox: &PeerInbox,
wakes: impl Fn(&SessionId) -> bool,
) {
let noted = self.restart_notices.lock().get(starter).cloned();
for Reporter { session, title, .. } in noted.unwrap_or_default() {
let unmerged = if wakes(&session) {
Vec::new()
} else {
self.pull_requests.service().watched_unmerged(&session)
};
let told = !unmerged.is_empty() && {
let receipt = self.restart_notices.receipt(starter, &session);
inbox.tell_restarted(session.as_str(), title, unmerged, receipt)
};
if !told {
forget(&mut self.restart_notices.lock(), starter, &session);
}
}
}
}
#[cfg(test)]
#[path = "restart_notice_tests.rs"]
mod tests;