Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

g1t.sh
2,224 linesCodeBlameRaw
1//! The runner in the app: g1t's self-hosted runner, started for the
2//! signed-in person when g1t opens and stopped when it quits, so their
3//! computer runs their workspace's `runs-on: self-hosted` jobs with
4//! nothing to install, register or start by hand
5//! (docs.g1t.sh/guides/desktop/#the-runner).
6//!
7//! - **When.** On by default; settings turn it off ("Run jobs on this
8//! computer while g1t is open"), and Stop in settings or the tray stops
9//! it until Start, or until g1t starts again. It starts a few seconds
10//! after the window, never before.
11//! - **How.** The backend settings choose (backend.rs): Sandboxed, the
12//! default, where nothing a job does reaches this computer; Docker; or
13//! directly on this computer. The two that are not sandboxes are only
14//! ever chosen by the person, and nothing falls back to them. Sandboxed
15//! is `g1t-runner sandbox` (launcher.rs) on every OS: `up` a few seconds
16//! after the window, `status` every few seconds while it runs, `down`
17//! as g1t quits, and `down --drain` when another backend is chosen.
18//! Each kind of job it runs (Linux jobs, Windows jobs) starts and stops
19//! on its own too: settings keep which are on (`runnerCapabilities`),
20//! every `up` names them (`--only`), and stopping one is `down --only`
21//! it, which leaves the other running. One that should run and doesn't
22//! while the other runs is tried again by itself, every few minutes, and
23//! every five while the newest release has no sandbox images.
24//! - **Registering.** The first time, and when the workspace or backend
25//! changes, the app needs a registration token for the workspace. Only
26//! the site can make one, for an owner who is signed in: the app asks
27//! the site's page (`g1t-runner-wanted`), the page asks the server
28//! (`POST /-/desktop/runner-token`) and hands the token to the app
29//! (`runner_register` in bridge.rs), which gives it to `g1t-runner`
30//! once: in its environment, or to the sandbox's first `up`. It is never
31//! shown or written to the log. The registration is kept
32//! (registration.json and the runner's own folder; the sandbox keeps its
33//! own, and sandbox-registration.json says whose it is) and used on
34//! every later start.
35//! - **Up to date.** Soon after it starts and every hour, and when the
36//! person asks ("Check for runner updates"), the app looks for a newer
37//! release on a thread of its own: its manifest only when g1t's release
38//! key signed it (native.rs), its program only when its SHA-256 is the
39//! manifest's. What it finds waits, downloaded, until each kind of job
40//! is idle, and goes in kind by kind, never in the middle of a job: the
41//! sandbox's Linux jobs take its image (`sandbox update`, which drains
42//! them itself) once they have no job; its Windows jobs, whose runner is
43//! the program itself, are drained (`down --only windows --drain`), the
44//! program is replaced, and they start again; a runner in Docker or on
45//! this computer is stopped between jobs and started on the new one.
46//! "Download updates by themselves" off, it looks only when asked.
47//! - **Seen.** What it is doing, and when it is not running why and how to
48//! fix it (diagnosis.rs), is in the tray, in settings, and on the site's
49//! Runners page (`g1t-runner-status`), read from what its log says
50//! (log.rs), or for the sandbox from its status. The whole log is kept
51//! in `runner/runner.log`.
52//!
53//! All of it runs on a thread of its own, which owns the runner's process
54//! and is told what happened through one channel.
55
56mod backend;
57pub mod diagnosis;
58mod launcher;
59mod log;
60mod native;
61
62use std::collections::VecDeque;
63use std::fs::{File, OpenOptions};
64use std::io::{BufRead, BufReader, Read, Write};
65use std::path::PathBuf;
66use std::process::{Child, Stdio};
67use std::sync::Mutex;
68use std::sync::mpsc::{self, Receiver, RecvTimeoutError, Sender};
69use std::time::{Duration, Instant, SystemTime};
70
71use serde::{Deserialize, Serialize};
72use tauri::{AppHandle, Emitter, EventTarget, Manager};
73use url::Url;
74
75pub use backend::Backend;
76use backend::{Engine, Missing};
77use diagnosis::{Code, Diagnosis, Facts};
78use launcher::{Down, State, Status};
79use native::Fault;
80
81use crate::{config, frame, settings, tray};
82
83/// What settings, the tray and the site listen for: the runner's status changed.
84pub const STATUS_EVENT: &str = "g1t-runner-status";
85/// What the site's page listens for: the app needs a registration token for a workspace.
86pub const WANTED_EVENT: &str = "g1t-runner-wanted";
87
88/// How long after the window the runner starts.
89const AFTER_START: Duration = Duration::from_secs(4);
90/// How often the thread looks at the runner when nothing has happened.
91const TICK: Duration = Duration::from_secs(5);
92/// How long after starting the first look for an update is, then how often.
93const FIRST_UPDATE: Duration = Duration::from_secs(90);
94const UPDATE_EVERY: Duration = Duration::from_secs(60 * 60);
95/// How often a backend that cannot start is looked at again.
96const LOOK_AGAIN: Duration = Duration::from_secs(30);
97/// How long the app waits for the site to hand over a token before asking again.
98const ASK_AGAIN: Duration = Duration::from_secs(60);
99/// The longest the app waits before starting a runner that stopped by itself again.
100const MOST_BACKOFF: Duration = Duration::from_secs(10 * 60);
101/// The runner says when a poll fails, and nothing when one works: this
102/// long without another failure, it is back (its longest wait is 60s).
103const BACK_ONLINE: Duration = Duration::from_secs(75);
104/// The runner's log is started over when it is bigger than this.
105const LOG_LIMIT: u64 = 5 * 1024 * 1024;
106/// The last lines of the log settings show.
107const TAIL: usize = 40;
108
109/// A workspace the site says the person may add runners to: those they own.
110#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
111pub struct Offered {
112 pub slug: String,
113 #[serde(default)]
114 pub name: Option<String>,
115}
116
117/// What the runner is doing, as the thread last said.
118#[derive(Clone, Debug, Default, PartialEq)]
119struct Now {
120 code: Code,
121 detail: Option<String>,
122 exit_code: Option<i32>,
123 wait_seconds: Option<u64>,
124 job: Option<String>,
125 version: Option<String>,
126 /// What the sandbox says to do, for a cause in its own words.
127 fix: Option<String>,
128 /// How the sandbox is, while jobs run sandboxed.
129 sandbox: Option<Status>,
130 /// A sandbox run is up, or on its way.
131 live: bool,
132 /// The kinds of job a launcher command is starting now.
133 starting: Vec<String>,
134 /// How far the sandbox's image has downloaded, while it does.
135 download: Option<launcher::Download>,
136}
137
138/// How the runner's updates stand, as the thread last said.
139#[derive(Clone, Debug, Default, PartialEq)]
140struct Updates {
141 /// When the releases last answered.
142 checked_at: Option<SystemTime>,
143 checking: bool,
144 /// A newer version, downloaded and checked, waiting.
145 ready: Option<String>,
146 /// Its program is still to be put in place.
147 program: bool,
148 /// The sandbox's image is still to take it.
149 image: bool,
150 /// The kinds of job taking it now.
151 installing: Vec<String>,
152}
153
154/// What a look at the releases found.
155struct Found {
156 version: String,
157 /// Newer than the program here.
158 newer: bool,
159 /// Its program, downloaded and checked: None when it isn't newer, or is
160 /// already waiting.
161 program: Option<Vec<u8>>,
162 /// The release has the sandbox's images.
163 images: bool,
164}
165
166/// What the site last said about the person.
167#[derive(Clone, Debug, Default, PartialEq)]
168struct Site {
169 /// Unknown until the site's first page loads.
170 signed_in: Option<bool>,
171 owned: Vec<Offered>,
172 current: Option<String>,
173}
174
175/// What the rest of the app shares with the thread.
176#[derive(Default)]
177pub struct Shared {
178 now: Mutex<Now>,
179 /// The workspace a token is wanted for.
180 wanted: Mutex<Option<String>>,
181 site: Mutex<Site>,
182 /// Stopped by the person until Start, or until g1t starts again.
183 paused: Mutex<bool>,
184 tail: Mutex<VecDeque<String>>,
185 tx: Mutex<Option<Sender<Ask>>>,
186 updates: Mutex<Updates>,
187}
188
189/// What settings, the tray and the site are shown.
190#[derive(Clone, Debug, PartialEq, Serialize)]
191#[serde(rename_all = "camelCase")]
192pub struct View {
193 pub diagnosis: Diagnosis,
194 /// What the site says of this computer once g1t is closed on it.
195 pub closed: Diagnosis,
196 pub running: bool,
197 pub version: Option<String>,
198 pub backend: Backend,
199 pub workspace: Option<String>,
200 /// The workspaces it may run for, from the site.
201 pub workspaces: Vec<Offered>,
202 /// The setting: run jobs while g1t is open.
203 pub enabled: bool,
204 pub paused: bool,
205 /// The workspace's Runners page.
206 pub link: Option<String>,
207 /// This computer's name, which the runner has on g1t.
208 pub machine: String,
209 pub platform: String,
210 /// The last lines of the runner's log.
211 pub log: Vec<String>,
212}
213
214/// What the thread is told.
215enum Ask {
216 /// Something changed: settings, Start or Stop, Retry.
217 Look,
218 /// A kind of job was started: start what should run now, stopping nothing.
219 Soon,
220 Retry,
221 /// "Check for runner updates": look now, and install what is found
222 /// even with "Download updates by themselves" off.
223 Check,
224 /// What a look at the releases found.
225 Checked(Result<Found, Fault>),
226 Token { workspace: String, token: String },
227 Refused { workspace: String, allowed: bool, why: String },
228 /// A line of the runner's output, or the launcher's; true from its standard output.
229 Line(u64, String, bool),
230 Exited(u64),
231 /// What `sandbox status` said.
232 Sandbox(u64, Result<Box<Status>, String>),
233 Quit(Sender<()>),
234}
235
236/// The registration the app made, besides what the runner keeps itself.
237#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
238struct Registration {
239 server: String,
240 workspace: String,
241 backend: Backend,
242}
243
244/// Whose the sandbox's registration is. The sandbox keeps its own, and
245/// keeps it while another backend runs, so choosing Sandboxed again needs
246/// no new one; this says which workspace it is for.
247#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
248struct SandboxRegistration {
249 server: String,
250 workspace: String,
251 /// False from the `up` that was given a token until it registered.
252 registered: bool,
253}
254
255impl SandboxRegistration {
256 fn path(app: &AppHandle) -> PathBuf {
257 folder(app).join("sandbox-registration.json")
258 }
259 fn load(app: &AppHandle) -> Option<SandboxRegistration> {
260 std::fs::read(Self::path(app)).ok().and_then(|bytes| serde_json::from_slice(&bytes).ok())
261 }
262 fn save(&self, app: &AppHandle) {
263 let _ = std::fs::create_dir_all(folder(app));
264 let _ = std::fs::write(Self::path(app), serde_json::to_vec_pretty(self).unwrap_or_default());
265 }
266 fn clear(app: &AppHandle) {
267 let _ = std::fs::remove_file(Self::path(app));
268 }
269}
270
271fn folder(app: &AppHandle) -> PathBuf {
272 app.path().app_data_dir().unwrap_or_else(|_| std::env::temp_dir().join("sh.g1t.desktop")).join("runner")
273}
274
275impl Registration {
276 fn path(app: &AppHandle) -> PathBuf {
277 folder(app).join("registration.json")
278 }
279 fn load(app: &AppHandle) -> Option<Registration> {
280 std::fs::read(Self::path(app)).ok().and_then(|bytes| serde_json::from_slice(&bytes).ok())
281 }
282 fn save(&self, app: &AppHandle) {
283 let _ = std::fs::create_dir_all(folder(app));
284 let _ = std::fs::write(Self::path(app), serde_json::to_vec_pretty(self).unwrap_or_default());
285 }
286 fn clear(app: &AppHandle) {
287 let _ = std::fs::remove_file(Self::path(app));
288 }
289}
290
291/// Where a native runner of a backend keeps its configuration.
292fn native_dir(app: &AppHandle, backend: Backend) -> PathBuf {
293 folder(app).join(match backend {
294 Backend::Docker => "docker",
295 Backend::Host => "host",
296 Backend::Sandboxed => launcher::FOLDER,
297 })
298}
299
300/// The workspace the runner is for: the one settings name, else the one it
301/// is registered with, else the one the person is in on the site, else the
302/// first they own.
303fn workspace(app: &AppHandle) -> Option<String> {
304 let settings = config::settings(app);
305 if let Some(chosen) = settings.runner_workspace.filter(|w| !w.is_empty()) {
306 return Some(chosen);
307 }
308 let server = config::server(app).to_string();
309 if let Some(registration) = Registration::load(app).filter(|r| r.server == server) {
310 return Some(registration.workspace);
311 }
312 let site = app.state::<Shared>().site.lock().unwrap().clone();
313 site.current.filter(|slug| site.owned.iter().any(|o| &o.slug == slug)).or_else(|| site.owned.first().map(|o| o.slug.clone()))
314}
315
316/// The kinds of job a sandbox can have.
317const KINDS: [&str; 3] = ["linux", "windows", "macos"];
318
319fn facts(app: &AppHandle, now: &Now) -> Facts {
320 let server = config::server(app);
321 let settings = config::settings(app);
322 let backend = settings.runner_backend;
323 Facts {
324 workspace: workspace(app),
325 backend: backend.label(),
326 machine: backend::machine_name(),
327 job: now.job.clone(),
328 detail: now.detail.clone(),
329 exit_code: now.exit_code,
330 wait_seconds: now.wait_seconds,
331 server: server.host_str().unwrap_or("g1t").to_owned(),
332 site: server.as_str().trim_end_matches('/').to_owned(),
333 platform: std::env::consts::OS.into(),
334 fix: now.fix.clone(),
335 sandbox: now.sandbox.clone().filter(|_| backend == Backend::Sandboxed),
336 off: KINDS.iter().filter(|kind| !settings.runner_capabilities.on(kind)).map(|kind| (*kind).to_owned()).collect(),
337 live: now.live,
338 starting: now.starting.clone(),
339 download: now.download.clone(),
340 held: !settings.runner || *app.state::<Shared>().paused.lock().unwrap(),
341 update: update_facts(app, now, backend),
342 }
343}
344
345/// How the runner's updates stand, for the words: what is still to take a
346/// waiting version. The sandbox's Windows jobs (any kind but Linux) take
347/// its program while they run; a runner with no kinds of job takes it
348/// itself.
349fn update_facts(app: &AppHandle, now: &Now, backend: Backend) -> diagnosis::Update {
350 let updates = app.state::<Shared>().updates.lock().unwrap().clone();
351 let sandbox = now.sandbox.as_ref().filter(|_| backend == Backend::Sandboxed);
352 let mut waiting = Vec::new();
353 if updates.image {
354 waiting.push("linux".to_owned());
355 }
356 if updates.program {
357 match sandbox {
358 Some(status) => waiting.extend(native_kinds(status).into_iter().map(|c| c.id.clone())),
359 None => waiting.push("runner".to_owned()),
360 }
361 }
362 diagnosis::Update {
363 installed: native::installed(&folder(app)),
364 checked_at: updates.checked_at.map(rfc3339),
365 checked_ago: updates.checked_at.and_then(|at| at.elapsed().ok()).map(|ago| ago.as_secs()),
366 checking: updates.checking,
367 ready: updates.ready,
368 waiting,
369 installing: updates.installing,
370 }
371}
372
373/// The kinds of job running on the program itself, which replacing it
374/// stops: the sandbox's Windows jobs (not Linux jobs, which run in its VM).
375fn native_kinds(status: &Status) -> Vec<&launcher::Capability> {
376 status.capabilities.iter().filter(|c| c.id != "linux" && !c.off() && matches!(c.state, State::Running | State::Starting)).collect()
377}
378
379/// A time as RFC 3339, in UTC to the second: `2026-10-12T12:04:05Z`.
380fn rfc3339(at: SystemTime) -> String {
381 let seconds = at.duration_since(SystemTime::UNIX_EPOCH).map_or(0, |since| since.as_secs());
382 let (days, rest) = (seconds / 86_400, seconds % 86_400);
383 // Days since 1970 to a date in the proleptic Gregorian calendar (Howard Hinnant's civil_from_days).
384 let z = days as i64 + 719_468;
385 let era = z.div_euclid(146_097);
386 let day_of_era = z.rem_euclid(146_097);
387 let year_of_era = (day_of_era - day_of_era / 1460 + day_of_era / 36_524 - day_of_era / 146_096) / 365;
388 let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100);
389 let month_index = (5 * day_of_year + 2) / 153;
390 let day = day_of_year - (153 * month_index + 2) / 5 + 1;
391 let month = if month_index < 10 { month_index + 3 } else { month_index - 9 };
392 let year = year_of_era + era * 400 + i64::from(month <= 2);
393 format!("{year:04}-{month:02}-{day:02}T{:02}:{:02}:{:02}Z", rest / 3600, rest % 3600 / 60, rest % 60)
394}
395
396pub fn view(app: &AppHandle) -> View {
397 let shared = app.state::<Shared>();
398 let now = shared.now.lock().unwrap().clone();
399 let settings = config::settings(app);
400 let facts = facts(app, &now);
401 let server = config::server(app);
402 View {
403 diagnosis: diagnosis::diagnose(now.code, &facts),
404 closed: diagnosis::closed(&facts),
405 running: matches!(now.code, Code::Idle | Code::Busy | Code::Offline) || facts.sandbox.as_ref().is_some_and(Status::running),
406 version: now.version,
407 backend: settings.runner_backend,
408 link: facts.workspace.as_ref().and_then(|w| server.join(&format!("{w}/-/runners")).ok()).map(|u| u.to_string()),
409 workspace: facts.workspace,
410 workspaces: shared.site.lock().unwrap().owned.clone(),
411 enabled: settings.runner,
412 paused: *shared.paused.lock().unwrap(),
413 machine: facts.machine,
414 platform: facts.platform,
415 log: shared.tail.lock().unwrap().iter().cloned().collect(),
416 }
417}
418
419/// Whether the runner is running a job: what restarts g1t waits for it,
420/// since restarting stops the runner and would cut the job short.
421pub fn busy(app: &AppHandle) -> bool {
422 let now = app.state::<Shared>().now.lock().unwrap().clone();
423 now.code == Code::Busy || now.sandbox.as_ref().is_some_and(|status| status.job().is_some())
424}
425
426/// The tray's line: `Runner: Waiting for jobs`, at most 60 characters.
427pub fn headline(view: &View) -> String {
428 let line = format!("Runner: {}", view.diagnosis.title);
429 if line.chars().count() > 60 { line.chars().take(59).collect::<String>() + "…" } else { line }
430}
431
432/// Tells everyone what the runner is doing now.
433fn publish(app: &AppHandle) {
434 let view = view(app);
435 tray::runner_changed(app, &view);
436 let _ = app.emit_to(settings::SETTINGS, STATUS_EVENT, view.clone());
437 let _ = app.emit_to(EventTarget::webview(frame::SITE), STATUS_EVENT, view);
438}
439
440fn ask(app: &AppHandle, ask: Ask) {
441 if let Some(tx) = app.state::<Shared>().tx.lock().unwrap().as_ref() {
442 let _ = tx.send(ask);
443 }
444}
445
446/// Starts the thread that keeps the runner, as the app starts.
447pub fn install(app: &AppHandle) {
448 let (tx, rx) = mpsc::channel();
449 *app.state::<Shared>().tx.lock().unwrap() = Some(tx.clone());
450 let handle = app.clone();
451 let spawned = std::thread::Builder::new().name("g1t-runner".into()).spawn(move || Keeper::new(handle, tx).keep(rx));
452 if let Err(error) = spawned {
453 eprintln!("g1t: the runner's thread could not start: {error}");
454 }
455}
456
457/// Settings changed: the thread looks again.
458pub fn settings_changed(app: &AppHandle) {
459 ask(app, Ask::Look);
460 publish(app);
461}
462
463/// Start or Stop, from settings or the tray.
464pub fn set_paused(app: &AppHandle, paused: bool) {
465 *app.state::<Shared>().paused.lock().unwrap() = paused;
466 // Start with the setting off turns the setting on: that is what was asked.
467 if !paused && !config::settings(app).runner {
468 let mut settings = config::settings(app);
469 settings.runner = true;
470 let _ = settings::apply(app, settings);
471 }
472 settings_changed(app);
473}
474
475/// Start or Stop on one kind of job's card: kept in settings, and the
476/// thread starts or stops that kind alone. Starting one with the runner
477/// off or stopped starts the runner too: that is what was asked.
478pub fn set_kind(app: &AppHandle, kind: &str, on: bool) -> Result<(), String> {
479 let mut settings = config::settings(app);
480 if !settings.runner_capabilities.set(kind, on) {
481 return Err(format!("g1t runs no {kind} jobs"));
482 }
483 if on {
484 settings.runner = true;
485 *app.state::<Shared>().paused.lock().unwrap() = false;
486 }
487 settings::apply(app, settings)?;
488 if on {
489 ask(app, Ask::Soon);
490 }
491 settings_changed(app);
492 Ok(())
493}
494
495/// The tray's Start or Stop row.
496pub fn toggle(app: &AppHandle) {
497 let view = view(app);
498 set_paused(app, view.enabled && !view.paused);
499}
500
501/// Retry and Restart: now, rather than when the wait is over.
502pub fn retry(app: &AppHandle) {
503 *app.state::<Shared>().paused.lock().unwrap() = false;
504 ask(app, Ask::Retry);
505}
506
507/// What the site says about the person, on every page: whether they are
508/// signed in, the workspaces they own, and the one they are in.
509pub fn offer(app: &AppHandle, signed_in: bool, owned: Vec<Offered>, current: Option<String>) {
510 let next = Site { signed_in: Some(signed_in), owned, current: current.map(|c| c.to_ascii_lowercase()) };
511 let changed = {
512 let shared = app.state::<Shared>();
513 let mut site = shared.site.lock().unwrap();
514 let changed = *site != next;
515 *site = next;
516 changed
517 };
518 if changed {
519 settings_changed(app);
520 }
521}
522
523/// The site handed over a registration token, or said why it could not:
524/// `allowed` is false when the person may not add runners to the workspace.
525pub fn register(app: &AppHandle, workspace: String, token: Option<String>, allowed: bool, error: Option<String>) {
526 match token {
527 Some(token) if token.starts_with("g1trt_") => ask(app, Ask::Token { workspace, token }),
528 _ => ask(app, Ask::Refused { workspace, allowed, why: error.unwrap_or_else(|| "g1t didn't make a registration token.".into()) }),
529 }
530}
531
532/// A diagnosis's button, or Start and Stop: from the settings window, or
533/// from the site's card for this computer. With a kind of job (`linux`,
534/// `windows`), Start and Stop are that kind's alone.
535pub fn act(app: &AppHandle, action: &str, kind: Option<&str>) -> Result<(), String> {
536 use tauri_plugin_opener::OpenerExt;
537 let browse = |url: &str| app.opener().open_url(url, None::<&str>).map_err(|e| e.to_string());
538 if let ("start" | "stop", Some(kind)) = (action, kind) {
539 return set_kind(app, kind, action == "start");
540 }
541 match action {
542 "start" => {
543 set_paused(app, false);
544 retry(app);
545 }
546 "stop" => set_paused(app, true),
547 "retry" | "restart" => retry(app),
548 "check_updates" => ask(app, Ask::Check),
549 "sign_in" => crate::links::go(app, "/login"),
550 "new_workspace" => crate::links::go(app, "/workspaces/new"),
551 "view_runners" => match workspace(app) {
552 Some(workspace) => crate::links::go(app, &format!("/{workspace}/-/runners")),
553 None => crate::links::go(app, "/"),
554 },
555 "choose_workspace" | "choose_backend" => settings::open(app),
556 "view_log" => {
557 let path = log_file(app);
558 if !path.exists() {
559 return Err("The runner hasn't written a log yet.".into());
560 }
561 app.opener().open_path(path.to_string_lossy(), None::<&str>).map_err(|e| e.to_string())?;
562 }
563 "open_docker" => open_docker(app)?,
564 "copy_wsl_install" => copy(launcher::WSL_INSTALL)?,
565 "copy_vfkit_install" => copy(launcher::VFKIT_INSTALL)?,
566 "windows_features" => windows_features()?,
567 "get_docker" => browse("https://docs.docker.com/get-started/get-docker/")?,
568 "install_sysbox" => browse("https://github.com/nestybox/sysbox#installation")?,
569 "learn_more" => browse("https://docs.g1t.sh/guides/desktop/#how-jobs-run")?,
570 "report" => browse(config::server(app).join("support").map_err(|e| e.to_string())?.as_str())?,
571 other => return Err(format!("the runner has no action called {other}")),
572 }
573 Ok(())
574}
575
576/// Starts Docker Desktop, where it is installed in its usual place.
577fn open_docker(app: &AppHandle) -> Result<(), String> {
578 use tauri_plugin_opener::OpenerExt;
579 if cfg!(target_os = "macos") {
580 return app.opener().open_path("/Applications/Docker.app", None::<&str>).map_err(|e| e.to_string());
581 }
582 if cfg!(windows) {
583 let program = std::env::var_os("ProgramFiles").map(PathBuf::from).unwrap_or_else(|| PathBuf::from(r"C:\Program Files"));
584 let desktop = program.join("Docker").join("Docker").join("Docker Desktop.exe");
585 if desktop.exists() {
586 return app.opener().open_path(desktop.to_string_lossy(), None::<&str>).map_err(|e| e.to_string());
587 }
588 return Err("Docker Desktop isn't installed in its usual place. Get Docker to install it.".into());
589 }
590 Err("Start Docker with your system's service manager, such as `sudo systemctl start docker`.".into())
591}
592
593/// Puts a command on the clipboard, for the person to paste where it runs.
594fn copy(text: &str) -> Result<(), String> {
595 let tools: &[(&str, &[&str])] = if cfg!(windows) {
596 &[("clip", &[])]
597 } else if cfg!(target_os = "macos") {
598 &[("pbcopy", &[])]
599 } else {
600 &[("wl-copy", &[]), ("xclip", &["-selection", "clipboard"]), ("xsel", &["--clipboard", "--input"])]
601 };
602 for (program, args) in tools {
603 let Ok(mut child) = backend::quiet(program).args(*args).stdin(Stdio::piped()).stdout(Stdio::null()).stderr(Stdio::null()).spawn() else { continue };
604 if let Some(mut input) = child.stdin.take() {
605 let _ = input.write_all(text.as_bytes());
606 }
607 if child.wait().is_ok_and(|status| status.success()) {
608 return Ok(());
609 }
610 }
611 Err(format!("It couldn't be copied. The command is: {text}"))
612}
613
614/// Windows' "Turn Windows features on or off", where Windows Sandbox and
615/// Virtual Machine Platform are.
616fn windows_features() -> Result<(), String> {
617 if !cfg!(windows) {
618 return Err("Windows features are only on Windows.".into());
619 }
620 backend::quiet("optionalfeatures.exe").spawn().map(|_| ()).map_err(|e| format!("Windows features couldn't be opened: {e}"))
621}
622
623/// The workspace a token is wanted for now, if one is: the site asks as it loads.
624pub fn wanted(app: &AppHandle) -> Option<String> {
625 app.state::<Shared>().wanted.lock().unwrap().clone()
626}
627
628/// The runner's log, for the settings window to open.
629pub fn log_file(app: &AppHandle) -> PathBuf {
630 folder(app).join("runner.log")
631}
632
633/// Stops the runner as g1t quits or restarts, and waits for it.
634pub fn shutdown(app: &AppHandle) {
635 let tx = app.state::<Shared>().tx.lock().unwrap().take();
636 if let Some(tx) = tx {
637 let (done, wait) = mpsc::channel();
638 if tx.send(Ask::Quit(done)).is_ok() {
639 let _ = wait.recv_timeout(Duration::from_secs(30));
640 }
641 }
642}
643
644/// How the runner is started this time.
645enum Process {
646 /// `g1t-runner`, a child of the app, whose output is the runner's log.
647 Child(Child),
648 /// The sandbox, which runs on its own once `up` is done; the launcher
649 /// command the app is waiting for, while there is one.
650 Sandbox(Option<(Step, Child)>),
651}
652
653/// A launcher command that takes a while.
654#[derive(Clone, Debug, PartialEq, Eq)]
655enum Step {
656 Up,
657 Update,
658 /// `up --only <kind>`: one kind of job, started while the rest run.
659 Start(String),
660 /// `down --only <kind>`: one kind of job, stopped, the rest running on.
661 Stop(String),
662 /// `down --only <kind> --drain`: one kind of job, stopped after its
663 /// job, so the program its runner is can be replaced.
664 Drain(String),
665}
666
667impl Step {
668 fn name(&self) -> &'static str {
669 match self {
670 Step::Up | Step::Start(_) => "up",
671 Step::Update => "update",
672 Step::Stop(_) | Step::Drain(_) => "down",
673 }
674 }
675}
676
677struct Run {
678 generation: u64,
679 engine: Engine,
680 backend: Backend,
681 workspace: String,
682 process: Process,
683 /// Registered and polling.
684 listening: bool,
685 busy: bool,
686 /// When a poll last failed to reach g1t.
687 offline_at: Option<Instant>,
688 /// Started with a registration token, which a container keeps in its configuration until replaced.
689 with_token: bool,
690 forgotten: bool,
691 token_refused: bool,
692 error: Option<String>,
693 /// The status a launcher command is printing, line by line.
694 json: Option<Vec<String>>,
695 /// Asking the sandbox how it is, and when it was last asked.
696 polling: bool,
697 polled_at: Option<Instant>,
698 /// A poll asked before a launcher command ended, whose answer is old.
699 stale: bool,
700}
701
702impl Run {
703 fn new(generation: u64, engine: Engine, backend: Backend, workspace: String, process: Process, with_token: bool) -> Run {
704 Run {
705 generation,
706 engine,
707 backend,
708 workspace,
709 process,
710 listening: false,
711 busy: false,
712 offline_at: None,
713 with_token,
714 forgotten: false,
715 token_refused: false,
716 error: None,
717 json: None,
718 polling: false,
719 polled_at: None,
720 stale: false,
721 }
722 }
723
724 fn step(&self) -> Option<Step> {
725 match &self.process {
726 Process::Sandbox(Some((step, _))) => Some(step.clone()),
727 _ => None,
728 }
729 }
730}
731
732struct Keeper {
733 app: AppHandle,
734 tx: Sender<Ask>,
735 run: Option<Run>,
736 generation: u64,
737 token: Option<(String, String)>,
738 not_before: Instant,
739 failures: u32,
740 next_update: Instant,
741 log: Option<File>,
742 /// Why the last update failed (`UpdateFailed`, or `UpdateRefused` for
743 /// a release g1t didn't sign), said while the runner is idle, until
744 /// the next one works.
745 update_failed: Option<(Code, String)>,
746 /// A newer program, downloaded and checked, waiting for the kinds of
747 /// job that run on it to be idle: its version and its bytes.
748 program: Option<(String, Vec<u8>)>,
749 /// The person asked for this update: it installs with "Download
750 /// updates by themselves" off.
751 asked: bool,
752 /// When g1t last looked, as settings last said it.
753 said_checked: String,
754 /// A kind of job that should run and doesn't, while another runs:
755 /// when it is tried again, and how many times it has been.
756 retry: Vec<(String, Instant, u32)>,
757}
758
759/// What a set of the state carries besides its code.
760#[derive(Default)]
761struct Extra {
762 detail: Option<String>,
763 exit_code: Option<i32>,
764 wait_seconds: Option<u64>,
765 fix: Option<String>,
766}
767
768fn detail(text: impl Into<String>) -> Extra {
769 Extra { detail: Some(text.into()), ..Extra::default() }
770}
771
772impl Keeper {
773 fn new(app: AppHandle, tx: Sender<Ask>) -> Keeper {
774 let now = Instant::now();
775 Keeper {
776 app,
777 tx,
778 run: None,
779 generation: 0,
780 token: None,
781 not_before: now + AFTER_START,
782 failures: 0,
783 next_update: now + FIRST_UPDATE,
784 log: None,
785 update_failed: None,
786 program: None,
787 asked: false,
788 said_checked: String::new(),
789 retry: Vec::new(),
790 }
791 }
792
793 fn keep(mut self, rx: Receiver<Ask>) {
794 self.reap();
795 self.set(Code::Starting, detail("Starting after g1t's window."));
796 loop {
797 match rx.recv_timeout(TICK) {
798 Ok(Ask::Quit(done)) => {
799 self.halt();
800 let _ = done.send(());
801 return;
802 }
803 Ok(ask) => self.handle(ask),
804 Err(RecvTimeoutError::Timeout) => {}
805 Err(RecvTimeoutError::Disconnected) => {
806 self.halt();
807 return;
808 }
809 }
810 self.look();
811 self.said_when();
812 }
813 }
814
815 /// Settings say when g1t last looked for a newer runner, in minutes
816 /// and hours: said again as that changes.
817 fn said_when(&mut self) {
818 let (checked_at, checking) = self.with_updates(|updates| (updates.checked_at, updates.checking));
819 let checked_ago = checked_at.and_then(|at| at.elapsed().ok()).map(|ago| ago.as_secs());
820 let checked = diagnosis::checked(&diagnosis::Update { checked_ago, checking, ..diagnosis::Update::default() });
821 if checked != self.said_checked {
822 self.said_checked = checked;
823 publish(&self.app);
824 }
825 }
826
827 fn with_updates<R>(&self, change: impl FnOnce(&mut Updates) -> R) -> R {
828 change(&mut self.app.state::<Shared>().updates.lock().unwrap())
829 }
830
831 /// What an idle runner shows: why its last update failed, until one works.
832 fn idle(&self, code: Code) -> (Code, Extra) {
833 match (&self.update_failed, code) {
834 (Some((failed, why)), Code::Idle) => (*failed, detail(why.clone())),
835 _ => (code, Extra::default()),
836 }
837 }
838
839 fn set(&mut self, code: Code, extra: Extra) {
840 let changed = {
841 let shared = self.app.state::<Shared>();
842 let mut now = shared.now.lock().unwrap();
843 let job = if code == Code::Busy { now.job.clone() } else { None };
844 let next = Now {
845 code,
846 detail: extra.detail,
847 exit_code: extra.exit_code,
848 wait_seconds: extra.wait_seconds,
849 job,
850 fix: extra.fix,
851 live: self.run.as_ref().is_some_and(|run| run.engine == Engine::Launcher),
852 ..now.clone()
853 };
854 let changed = *now != next;
855 *now = next;
856 changed
857 };
858 if changed {
859 publish(&self.app);
860 }
861 }
862
863 fn code(&self) -> Code {
864 self.app.state::<Shared>().now.lock().unwrap().code
865 }
866
867 fn note(&mut self, job: Option<String>, version: Option<String>) {
868 let shared = self.app.state::<Shared>();
869 let mut now = shared.now.lock().unwrap();
870 if job.is_some() {
871 now.job = job;
872 }
873 if version.is_some() {
874 now.version = version;
875 }
876 }
877
878 /// A line of the runner's log, into the log file and the tail settings show.
879 fn write_log(&mut self, line: &str) {
880 {
881 let shared = self.app.state::<Shared>();
882 let mut tail = shared.tail.lock().unwrap();
883 if tail.len() == TAIL {
884 tail.pop_front();
885 }
886 tail.push_back(line.to_owned());
887 }
888 let path = log_file(&self.app);
889 if self.log.is_none() {
890 let _ = std::fs::create_dir_all(folder(&self.app));
891 if std::fs::metadata(&path).is_ok_and(|m| m.len() > LOG_LIMIT) {
892 let _ = std::fs::rename(&path, path.with_extension("log.old"));
893 }
894 self.log = OpenOptions::new().create(true).append(true).open(&path).ok();
895 }
896 if let Some(file) = self.log.as_mut() {
897 let _ = writeln!(file, "{line}");
898 }
899 }
900
901 fn handle(&mut self, ask: Ask) {
902 match ask {
903 Ask::Look | Ask::Quit(_) => {}
904 Ask::Soon => {
905 self.not_before = Instant::now();
906 for (_, at, _) in &mut self.retry {
907 *at = Instant::now();
908 }
909 }
910 Ask::Retry => {
911 self.not_before = Instant::now();
912 self.failures = 0;
913 // Each kind of job that waits to be tried again is tried now.
914 for (_, at, _) in &mut self.retry {
915 *at = Instant::now();
916 }
917 match self.code() {
918 // The runner is fine: only the update is tried again.
919 Code::UpdateFailed | Code::UpdateRefused if self.run.is_some() => self.check(true),
920 Code::Offline => self.halt(),
921 _ if self.run.as_ref().is_some_and(|run| !run.listening) => self.halt(),
922 _ => {}
923 }
924 }
925 Ask::Check => self.check(true),
926 Ask::Checked(found) => self.checked(found),
927 Ask::Token { workspace, token } => {
928 let shared = self.app.state::<Shared>();
929 let mut wanted = shared.wanted.lock().unwrap();
930 if wanted.as_deref() == Some(workspace.as_str()) {
931 *wanted = None;
932 self.token = Some((workspace, token));
933 self.not_before = Instant::now();
934 }
935 }
936 Ask::Refused { workspace, allowed, why } => {
937 let matched = {
938 let shared = self.app.state::<Shared>();
939 let mut wanted = shared.wanted.lock().unwrap();
940 let matched = wanted.as_deref() == Some(workspace.as_str());
941 if matched {
942 *wanted = None;
943 }
944 matched
945 };
946 if matched {
947 self.not_before = Instant::now() + MOST_BACKOFF;
948 self.set(if allowed { Code::RegisterFailed } else { Code::NotAllowed }, detail(why));
949 }
950 }
951 Ask::Line(generation, line, out) => self.line(generation, &line, out),
952 Ask::Exited(generation) => self.exited(generation),
953 Ask::Sandbox(generation, status) => self.polled(generation, status),
954 }
955 }
956
957 fn line(&mut self, generation: u64, line: &str, out: bool) {
958 let Some(run) = self.run.as_mut().filter(|run| run.generation == generation) else { return };
959 if run.engine == Engine::Launcher {
960 // The launcher's log, then its status, which is read when it ends.
961 if out && (run.json.is_some() || launcher::starts_status(line)) {
962 run.json.get_or_insert_with(Vec::new).push(line.to_owned());
963 } else {
964 if let Some(download) = launcher::download(line) {
965 let changed = {
966 let shared = self.app.state::<Shared>();
967 let mut now = shared.now.lock().unwrap();
968 let changed = now.download.as_ref() != Some(&download);
969 now.download = Some(download);
970 changed
971 };
972 if changed {
973 publish(&self.app);
974 }
975 }
976 self.write_log(line);
977 }
978 return;
979 }
980 let mut became = None;
981 let mut job = None;
982 let mut version = None;
983 match log::read(line) {
984 log::Event::Listening { version: v } => {
985 run.listening = true;
986 version = Some(v);
987 became = Some(Code::Idle);
988 }
989 log::Event::Took { name, repo } => {
990 run.busy = true;
991 run.offline_at = None;
992 job = Some(format!("{name} ({repo})"));
993 became = Some(Code::Busy);
994 }
995 log::Event::Ended => {
996 run.busy = false;
997 became = Some(Code::Idle);
998 }
999 log::Event::Offline => {
1000 run.offline_at = Some(Instant::now());
1001 if !run.busy {
1002 became = Some(Code::Offline);
1003 }
1004 }
1005 log::Event::Forgotten => run.forgotten = true,
1006 log::Event::TokenRefused => run.token_refused = true,
1007 log::Event::Failed(error) => run.error = Some(error),
1008 log::Event::Other => {}
1009 }
1010 let registered = version.is_some().then(|| Registration {
1011 server: config::server(&self.app).to_string(),
1012 workspace: run.workspace.clone(),
1013 backend: run.backend,
1014 });
1015 self.write_log(line);
1016 if let Some(registration) = registered {
1017 registration.save(&self.app);
1018 self.failures = 0;
1019 }
1020 self.note(job, version);
1021 if let Some(code) = became {
1022 let (code, extra) = self.idle(code);
1023 self.set(code, extra);
1024 }
1025 }
1026
1027 fn exited(&mut self, generation: u64) {
1028 if self.run.as_ref().is_none_or(|run| run.generation != generation) {
1029 return;
1030 }
1031 if self.run.as_ref().is_some_and(|run| run.engine == Engine::Launcher) {
1032 return self.launched(generation);
1033 }
1034 let mut run = self.run.take().expect("checked above");
1035 let exit_code = match &mut run.process {
1036 Process::Child(child) => child.wait().ok().and_then(|status| status.code()),
1037 Process::Sandbox(_) => None,
1038 };
1039 self.stop(&mut run, Down::Now);
1040 if run.forgotten || run.token_refused {
1041 // The registration is no more, or was never made: the next start asks for a token.
1042 self.forget();
1043 self.not_before = Instant::now();
1044 return;
1045 }
1046 self.failures += 1;
1047 let wait = Duration::from_secs(15 * 2u64.pow(self.failures.min(6))).min(MOST_BACKOFF);
1048 self.not_before = Instant::now() + wait;
1049 let offline = run.error.as_deref().is_some_and(|e| e.contains("could not reach g1t"));
1050 if offline {
1051 self.set(Code::Offline, Extra::default());
1052 } else if !run.listening && Registration::load(&self.app).is_none() {
1053 self.set(Code::RegisterFailed, Extra { detail: run.error, ..Extra::default() });
1054 } else {
1055 self.set(Code::Crashed, Extra { detail: run.error, exit_code, wait_seconds: Some(wait.as_secs()), ..Extra::default() });
1056 }
1057 }
1058
1059 /// Stops a run's process, the way it was started: the sandbox `how` says.
1060 fn stop(&mut self, run: &mut Run, how: Down) {
1061 match (&mut run.process, run.engine) {
1062 (Process::Sandbox(step), _) => {
1063 // A launcher command still going is ended first, then the sandbox stopped.
1064 if let Some((_, child)) = step.as_mut() {
1065 native::stop(child);
1066 }
1067 let binary = native::binary(&folder(&self.app));
1068 // How it is once stopped, so each kind of job's card says so.
1069 let stopped = match launcher::down(&binary, &native_dir(&self.app, Backend::Sandboxed), how) {
1070 Ok(status) => Some(status),
1071 Err(why) => {
1072 self.write_log(&format!("g1t: the sandbox didn't stop cleanly: {why}"));
1073 None
1074 }
1075 };
1076 let shared = self.app.state::<Shared>();
1077 let mut now = shared.now.lock().unwrap();
1078 now.sandbox = stopped.filter(|_| how != Down::Delete);
1079 now.starting.clear();
1080 now.download = None;
1081 }
1082 (Process::Child(child), Engine::Launcher) => native::stop(child),
1083 (Process::Child(child), Engine::Native { docker }) => {
1084 native::stop(child);
1085 // A job's container outlives the runner killed under it: removed now, not at its next start.
1086 if docker {
1087 native::sweep(&native_dir(&self.app, run.backend));
1088 }
1089 }
1090 }
1091 let _ = std::fs::remove_file(folder(&self.app).join("pid"));
1092 }
1093
1094 /// Stops whatever runs now.
1095 fn halt(&mut self) {
1096 self.halt_as(Down::Now);
1097 }
1098
1099 /// Stops whatever runs: the sandbox now, after its job, or for good.
1100 fn halt_as(&mut self, how: Down) {
1101 self.generation += 1;
1102 if let Some(mut run) = self.run.take() {
1103 self.stop(&mut run, how);
1104 }
1105 }
1106
1107 /// A runner an earlier g1t started and did not stop: it ended without quitting.
1108 fn reap(&self) {
1109 if let Some(pid) = std::fs::read_to_string(folder(&self.app).join("pid")).ok().and_then(|text| text.trim().parse().ok()) {
1110 native::reap(pid);
1111 let _ = std::fs::remove_file(folder(&self.app).join("pid"));
1112 }
1113 }
1114
1115 /// Unregisters the runner the app registered, whatever backend it was.
1116 fn forget(&mut self) {
1117 let app = self.app.clone();
1118 let Some(registration) = Registration::load(&app) else { return };
1119 let binary = native::binary(&folder(&app));
1120 match registration.backend {
1121 // The sandbox keeps its registration for when it is chosen
1122 // again (sandbox-registration.json says whose): it only stops,
1123 // after its job.
1124 Backend::Sandboxed => {
1125 let _ = launcher::down(&binary, &native_dir(&app, Backend::Sandboxed), Down::Drain);
1126 }
1127 backend => native::forget(&binary, &native_dir(&app, backend)),
1128 }
1129 Registration::clear(&app);
1130 }
1131
1132 /// Removes the sandbox and its registration: it was for another
1133 /// workspace, or g1t no longer knows its runners.
1134 fn forget_sandbox(&mut self) {
1135 let binary = native::binary(&folder(&self.app));
1136 if let Err(why) = launcher::down(&binary, &native_dir(&self.app, Backend::Sandboxed), Down::Delete) {
1137 self.write_log(&format!("g1t: the sandbox couldn't be removed: {why}"));
1138 }
1139 SandboxRegistration::clear(&self.app);
1140 if Registration::load(&self.app).is_some_and(|r| r.backend == Backend::Sandboxed) {
1141 Registration::clear(&self.app);
1142 }
1143 }
1144
1145 /// Whether what a native runner keeps of a registration is still here.
1146 fn kept(&self, backend: Backend) -> bool {
1147 native_dir(&self.app, backend).join("config.json").exists()
1148 }
1149
1150 /// Asks the site for a registration token for `workspace`.
1151 fn want(&mut self, workspace: &str) {
1152 *self.app.state::<Shared>().wanted.lock().unwrap() = Some(workspace.to_owned());
1153 let _ = self.app.emit_to(EventTarget::webview(frame::SITE), WANTED_EVENT, workspace.to_owned());
1154 self.not_before = Instant::now() + ASK_AGAIN;
1155 let signed_out = self.app.state::<Shared>().site.lock().unwrap().signed_in == Some(false);
1156 self.set(if signed_out { Code::SignedOut } else { Code::Registering }, Extra::default());
1157 }
1158
1159 /// The thread's look round: what should run, and whether it does.
1160 fn look(&mut self) {
1161 let settings = config::settings(&self.app);
1162 let paused = *self.app.state::<Shared>().paused.lock().unwrap();
1163 if !settings.runner || paused {
1164 if self.run.is_some() {
1165 self.halt();
1166 }
1167 *self.app.state::<Shared>().wanted.lock().unwrap() = None;
1168 self.set(if paused { Code::Stopped } else { Code::Off }, Extra::default());
1169 return;
1170 }
1171 // Looking needs a runner here to compare with; the first comes with the first start.
1172 if Instant::now() >= self.next_update && native::installed(&folder(&self.app)).is_some() {
1173 self.check(false);
1174 }
1175 let workspace = workspace(&self.app);
1176 if let Some(run) = &self.run {
1177 let moved = run.backend != settings.runner_backend || Some(&run.workspace) != workspace.as_ref();
1178 let back = run.offline_at.is_some_and(|at| at.elapsed() >= BACK_ONLINE);
1179 let (busy, stepping) = (run.busy, run.step().is_some());
1180 if back {
1181 if let Some(run) = self.run.as_mut() {
1182 run.offline_at = None;
1183 }
1184 self.set(Code::Idle, Extra::default());
1185 }
1186 // A change of backend or workspace waits for the job to end, as an update does.
1187 if moved && !busy && !stepping {
1188 self.halt_as(Down::Drain);
1189 self.not_before = Instant::now();
1190 } else if !moved && !stepping && !self.install() {
1191 self.kinds(workspace.as_deref());
1192 }
1193 self.poll();
1194 return;
1195 }
1196 if Instant::now() < self.not_before {
1197 return;
1198 }
1199 // Nothing runs: a waiting program goes in before anything starts.
1200 if self.program.is_some()
1201 && self.allowed()
1202 && let Some((version, bytes)) = self.program.take()
1203 {
1204 self.put(version, &bytes);
1205 }
1206 self.start(settings.runner_backend, workspace);
1207 }
1208
1209 fn start(&mut self, backend: Backend, workspace: Option<String>) {
1210 let app = self.app.clone();
1211 let engine = match backend::engine(backend) {
1212 Ok(engine) => engine,
1213 Err(missing) => {
1214 self.not_before = Instant::now() + LOOK_AGAIN;
1215 let code = match missing {
1216 Missing::Docker => Code::NoDocker,
1217 };
1218 self.set(code, Extra::default());
1219 return;
1220 }
1221 };
1222 let Some(workspace) = workspace else {
1223 self.not_before = Instant::now() + LOOK_AGAIN;
1224 let code = match app.state::<Shared>().site.lock().unwrap().signed_in {
1225 Some(false) => Code::SignedOut,
1226 Some(true) => Code::NoWorkspace,
1227 None => Code::Starting,
1228 };
1229 self.set(code, detail("Waiting for g1t to open."));
1230 return;
1231 };
1232 if engine == Engine::Launcher {
1233 return self.start_sandbox(workspace);
1234 }
1235 let server = config::server(&app);
1236 let registration = Registration::load(&app);
1237 let ours = registration.as_ref().is_some_and(|r| r.server == server.as_str() && r.workspace == workspace && r.backend == backend) && self.kept(backend);
1238 let mut token = None;
1239 if !ours {
1240 if registration.is_some() {
1241 self.set(Code::Starting, detail("Removing this computer's earlier registration."));
1242 self.forget();
1243 }
1244 match self.token.take() {
1245 Some((for_workspace, given)) if for_workspace == workspace => token = Some(given),
1246 _ => return self.want(&workspace),
1247 }
1248 }
1249 self.hold_workspace(&workspace);
1250 self.set(Code::Starting, Extra::default());
1251 let envs = environment(&server, backend, token.as_deref());
1252 let generation = self.generation + 1;
1253 self.generation = generation;
1254 let binary = match self.ensure_binary(&server) {
1255 Ok(binary) => binary,
1256 Err(fault) => return self.not_installed(workspace, token, fault),
1257 };
1258 let started = native::spawn(&binary, &native_dir(&app, backend), &envs).map_err(|e| format!("g1t-runner could not start: {e}")).inspect(|child| {
1259 let _ = std::fs::write(folder(&app).join("pid"), child.id().to_string());
1260 });
1261 match started {
1262 Ok(mut child) => {
1263 self.follow(generation, &mut child);
1264 self.run = Some(Run::new(generation, engine, backend, workspace, Process::Child(child), token.is_some()));
1265 self.set(Code::Starting, Extra::default());
1266 }
1267 Err(why) => self.not_started(workspace, token, why),
1268 }
1269 }
1270
1271 /// The runner couldn't be started: the token is kept for the next try,
1272 /// while it lasts, and it is tried again in a while.
1273 fn not_started(&mut self, workspace: String, token: Option<String>, why: String) {
1274 if let Some(given) = token {
1275 self.token = Some((workspace, given));
1276 }
1277 self.failures += 1;
1278 self.not_before = Instant::now() + LOOK_AGAIN;
1279 let offline = why.contains("error sending request") || why.contains("dns") || why.contains("connect");
1280 self.write_log(&format!("g1t: the runner could not start: {why}"));
1281 if offline {
1282 self.set(Code::Offline, Extra::default());
1283 } else {
1284 self.set(Code::Crashed, Extra { detail: Some(why), wait_seconds: Some(LOOK_AGAIN.as_secs()), ..Extra::default() });
1285 }
1286 }
1287
1288 /// The setting names the workspace from now on, so browsing another one never moves the runner.
1289 fn hold_workspace(&self, workspace: &str) {
1290 if config::settings(&self.app).runner_workspace.as_deref() != Some(workspace) {
1291 let mut settings = config::settings(&self.app);
1292 settings.runner_workspace = Some(workspace.to_owned());
1293 let _ = config::set_settings(&self.app, settings);
1294 }
1295 }
1296
1297 /// Reads the child's output, a line at a time, onto the thread's channel.
1298 fn follow(&self, generation: u64, child: &mut Child) {
1299 fn pipe(tx: Sender<Ask>, generation: u64, from: impl Read + Send + 'static, last: bool) {
1300 std::thread::spawn(move || {
1301 for line in BufReader::new(from).lines().map_while(Result::ok) {
1302 if tx.send(Ask::Line(generation, line, last)).is_err() {
1303 return;
1304 }
1305 }
1306 if last {
1307 let _ = tx.send(Ask::Exited(generation));
1308 }
1309 });
1310 }
1311 if let Some(err) = child.stderr.take() {
1312 pipe(self.tx.clone(), generation, err, false);
1313 }
1314 if let Some(out) = child.stdout.take() {
1315 pipe(self.tx.clone(), generation, out, true);
1316 }
1317 }
1318
1319 /// `g1t-runner` installed, from the newest release when it is not:
1320 /// one g1t signed, as every update is.
1321 fn ensure_binary(&mut self, server: &Url) -> Result<PathBuf, Fault> {
1322 let folder = folder(&self.app);
1323 let binary = native::binary(&folder);
1324 if native::installed(&folder).is_some() {
1325 return Ok(binary);
1326 }
1327 self.set(Code::Updating, detail("Downloading g1t-runner."));
1328 let manifest = native::latest(server)?;
1329 let bytes = native::download(server, &manifest).map_err(Fault::Failed)?;
1330 native::put(&folder, &bytes, &manifest.version).map_err(Fault::Failed)?;
1331 self.note(None, Some(manifest.version));
1332 self.with_updates(|updates| updates.checked_at = Some(SystemTime::now()));
1333 Ok(binary)
1334 }
1335
1336 /// There is no `g1t-runner` here, and none could be had: a release g1t
1337 /// didn't sign is refused, and asked for again in a while.
1338 fn not_installed(&mut self, workspace: String, token: Option<String>, fault: Fault) {
1339 match fault {
1340 Fault::Failed(why) => self.not_started(workspace, token, why),
1341 Fault::Unsigned(why) => {
1342 if let Some(given) = token {
1343 self.token = Some((workspace, given));
1344 }
1345 self.write_log(&format!("g1t: g1t-runner wasn't installed: {why}"));
1346 self.not_before = Instant::now() + MOST_BACKOFF;
1347 self.set(Code::UpdateRefused, detail(why));
1348 }
1349 }
1350 }
1351
1352 // --- Updates ------------------------------------------------------------------
1353
1354 /// Looks for a newer release on a thread of its own, so the runner is
1355 /// looked after meanwhile: its signed manifest, and its program when it
1356 /// is newer than the one here. `asked` is the person asking, which
1357 /// works with "Download updates by themselves" off.
1358 fn check(&mut self, asked: bool) {
1359 self.next_update = Instant::now() + UPDATE_EVERY;
1360 if !asked && !config::settings(&self.app).auto_update {
1361 return;
1362 }
1363 if self.with_updates(|updates| std::mem::replace(&mut updates.checking, true)) {
1364 return;
1365 }
1366 self.asked |= asked;
1367 publish(&self.app);
1368 let (server, folder) = (config::server(&self.app), folder(&self.app));
1369 let waiting = self.program.as_ref().map(|(version, _)| version.clone());
1370 let tx = self.tx.clone();
1371 let spawned = std::thread::Builder::new().name("g1t-runner-releases".into()).spawn(move || {
1372 let _ = tx.send(Ask::Checked(look_for_release(&server, &folder, waiting.as_deref())));
1373 });
1374 if spawned.is_err() {
1375 self.with_updates(|updates| updates.checking = false);
1376 }
1377 }
1378
1379 /// What the look found: a newer program waits for its kinds of job to
1380 /// be idle, and so does the sandbox's image when it is older than the
1381 /// release's. A release g1t didn't sign, or one that didn't download,
1382 /// leaves the runner as it is, and is said while it is idle.
1383 fn checked(&mut self, found: Result<Found, Fault>) {
1384 self.with_updates(|updates| updates.checking = false);
1385 let found = match found {
1386 Ok(found) => found,
1387 Err(fault) => {
1388 let failed = match fault {
1389 Fault::Unsigned(why) => (Code::UpdateRefused, why),
1390 Fault::Failed(why) => (Code::UpdateFailed, format!("g1t-runner couldn't be updated: {why}")),
1391 };
1392 self.write_log(&format!("g1t: {}", failed.1));
1393 self.update_failed = Some(failed);
1394 self.asked = false;
1395 if matches!(self.code(), Code::Idle | Code::UpdateFailed | Code::UpdateRefused) {
1396 let (code, extra) = self.idle(Code::Idle);
1397 self.set(code, extra);
1398 }
1399 return publish(&self.app);
1400 }
1401 };
1402 if let Some(bytes) = found.program {
1403 self.program = Some((found.version.clone(), bytes));
1404 }
1405 if !found.newer {
1406 self.program = None;
1407 }
1408 // The sandbox's image is the release's version; one older takes the newest.
1409 let sandbox = self.app.state::<Shared>().now.lock().unwrap().sandbox.clone();
1410 let launcher = self.run.as_ref().is_some_and(|run| run.engine == Engine::Launcher);
1411 let image = launcher
1412 && found.images
1413 && sandbox.is_some_and(|status| status.linux().state != State::Absent && status.image_version.as_deref().is_some_and(|have| native::newer(&found.version, have)));
1414 let program = self.program.is_some();
1415 self.with_updates(|updates| {
1416 updates.checked_at = Some(SystemTime::now());
1417 updates.program = program;
1418 updates.image = image;
1419 updates.ready = (program || image).then(|| found.version.clone());
1420 });
1421 if program || image {
1422 self.write_log(&format!("g1t: g1t-runner {} is ready, and installs as each kind of job is idle.", found.version));
1423 } else {
1424 self.asked = false;
1425 }
1426 // It worked: what the last failure said goes.
1427 if self.update_failed.take().is_some() && matches!(self.code(), Code::UpdateFailed | Code::UpdateRefused) {
1428 self.set(Code::Idle, Extra::default());
1429 }
1430 publish(&self.app);
1431 }
1432
1433 /// Whether a waiting update may go in: "Download updates by
1434 /// themselves" is on, or the person asked for it. Turned off since it
1435 /// downloaded, it no longer waits.
1436 fn allowed(&mut self) -> bool {
1437 if self.asked || config::settings(&self.app).auto_update {
1438 return true;
1439 }
1440 self.program = None;
1441 self.with_updates(|updates| {
1442 updates.program = false;
1443 updates.image = false;
1444 updates.ready = None;
1445 });
1446 false
1447 }
1448
1449 /// Puts a waiting update in, kind by kind, as each kind of job is idle:
1450 /// true when it started something, which is waited for. Never in the
1451 /// middle of a job.
1452 fn install(&mut self) -> bool {
1453 let image = self.with_updates(|updates| updates.image);
1454 if (self.program.is_none() && !image) || !self.allowed() {
1455 return false;
1456 }
1457 let Some((engine, busy, listening)) = self.run.as_ref().map(|run| (run.engine, run.busy, run.listening)) else { return false };
1458 if engine != Engine::Launcher {
1459 // A runner in Docker or on this computer is the program: stopped between jobs, started on the new one.
1460 if busy || !listening {
1461 return false;
1462 }
1463 let Some((version, bytes)) = self.program.take() else { return false };
1464 self.set(Code::Updating, detail(format!("Updating g1t-runner to {version}.")));
1465 self.halt();
1466 self.put(version, &bytes);
1467 self.not_before = Instant::now();
1468 return true;
1469 }
1470 let Some(status) = self.app.state::<Shared>().now.lock().unwrap().sandbox.clone() else { return false };
1471 if self.program.is_some() {
1472 let running = native_kinds(&status);
1473 if running.is_empty() {
1474 // Nothing runs on the program: it is replaced now, and
1475 // kinds() starts what was drained on the new one first,
1476 // before the image, which takes minutes, is updated.
1477 let (version, bytes) = self.program.take().expect("checked above");
1478 self.put(version, &bytes);
1479 return false;
1480 } else if !status.per_kind() {
1481 // A launcher from before kinds of job stopped on their own: the whole sandbox stops between jobs.
1482 if busy {
1483 return false;
1484 }
1485 let (version, bytes) = self.program.take().expect("checked above");
1486 self.set(Code::Updating, detail(format!("Updating g1t-runner to {version}.")));
1487 self.halt_as(Down::Drain);
1488 self.put(version, &bytes);
1489 self.with_updates(|updates| updates.image = true);
1490 self.not_before = Instant::now();
1491 return true;
1492 } else if let Some(idle) = running.iter().find(|kind| kind.job.is_none()) {
1493 // Stopped after any job it takes meanwhile; replaced once it has.
1494 let kind = idle.id.clone();
1495 self.with_updates(|updates| updates.installing = vec![kind.clone()]);
1496 self.kind_step(Step::Drain(kind), None);
1497 return true;
1498 }
1499 }
1500 // Linux jobs take the image once they have no job (`update` drains them itself too).
1501 let image = self.with_updates(|updates| updates.image);
1502 if image && status.linux().job.is_none() {
1503 self.with_updates(|updates| updates.installing.push("linux".into()));
1504 self.sandbox_step(Step::Update);
1505 return true;
1506 }
1507 false
1508 }
1509
1510 /// Puts a downloaded program in place, with nothing running on it.
1511 fn put(&mut self, version: String, bytes: &[u8]) {
1512 match native::put(&folder(&self.app), bytes, &version) {
1513 Ok(()) => {
1514 self.write_log(&format!("g1t: g1t-runner {version} is in place."));
1515 if self.run.as_ref().is_none_or(|run| run.engine != Engine::Launcher) {
1516 self.note(None, Some(version));
1517 }
1518 }
1519 Err(why) => {
1520 let why = format!("g1t-runner {version} couldn't be put in place: {why}");
1521 self.write_log(&format!("g1t: {why}"));
1522 self.update_failed = Some((Code::UpdateFailed, why));
1523 }
1524 }
1525 self.done_with(|updates| {
1526 updates.program = false;
1527 updates.installing.retain(|kind| kind == "linux");
1528 });
1529 }
1530
1531 /// Part of an update is done: what waits is said again.
1532 fn done_with(&mut self, change: impl FnOnce(&mut Updates)) {
1533 let left = self.with_updates(|updates| {
1534 change(updates);
1535 if !updates.program && !updates.image {
1536 updates.ready = None;
1537 }
1538 updates.program || updates.image
1539 });
1540 if !left {
1541 self.asked = false;
1542 }
1543 publish(&self.app);
1544 }
1545
1546 // --- The sandbox (launcher.rs) ------------------------------------------------
1547
1548 /// Starts the sandbox for `workspace`: registered with it the first
1549 /// time, with a token from the site; from its own registration after.
1550 fn start_sandbox(&mut self, workspace: String) {
1551 let app = self.app.clone();
1552 let server = config::server(&app);
1553 let binary = match self.ensure_binary(&server) {
1554 Ok(binary) => binary,
1555 Err(fault) => return self.not_installed(workspace, None, fault),
1556 };
1557 let dir = native_dir(&app, Backend::Sandboxed);
1558 let mut status = match launcher::status(&binary, &dir) {
1559 Ok(status) => status,
1560 Err(why) => return self.no_status(why),
1561 };
1562 self.seen(&status);
1563 // Nothing here can be sandboxed: why, and how to fix it; looked at again in a while.
1564 if !status.anything_available() {
1565 self.not_before = Instant::now() + LOOK_AGAIN;
1566 let (code, extra) = shown(&status);
1567 return self.set(code, extra);
1568 }
1569 // Another backend's registration goes; the sandbox's own is kept for its workspace.
1570 if Registration::load(&app).is_some_and(|r| r.backend != Backend::Sandboxed) {
1571 self.set(Code::Starting, detail("Removing this computer's earlier registration."));
1572 self.forget();
1573 }
1574 let kept = SandboxRegistration::load(&app);
1575 let ours = kept.as_ref().is_some_and(|k| k.server == server.as_str() && k.workspace == workspace);
1576 let made = status.capabilities.iter().any(|c| c.state != State::Absent) || status.state != State::Absent;
1577 if !ours && made && kept.is_none() {
1578 // Made by hand (`g1t-runner sandbox up`), and there is one per computer: it is the person's to remove.
1579 self.not_before = Instant::now() + LOOK_AGAIN;
1580 return self.set(Code::SandboxTaken, Extra::default());
1581 }
1582 if !ours && made {
1583 // The app's sandbox, registered with another workspace or server.
1584 self.set(Code::Starting, detail("Removing this computer's sandbox for another workspace."));
1585 self.forget_sandbox();
1586 status = match launcher::status(&binary, &dir) {
1587 Ok(status) => status,
1588 Err(why) => return self.no_status(why),
1589 };
1590 }
1591 // The kinds of job the person stopped are stopped here too, before
1592 // anything starts (the launcher keeps them off); with every kind
1593 // stopped, nothing starts.
1594 let kinds = config::settings(&app).runner_capabilities;
1595 if status.per_kind() {
1596 let stopping: Vec<String> = status.capabilities.iter().filter(|c| !kinds.on(&c.id) && !c.off()).map(|c| c.id.clone()).collect();
1597 for kind in &stopping {
1598 if let Err(why) = launcher::down_only(&binary, &dir, kind) {
1599 self.write_log(&format!("g1t: {} couldn't be stopped: {why}", diagnosis::kind_label(kind)));
1600 }
1601 }
1602 if !stopping.is_empty() {
1603 status = match launcher::status(&binary, &dir) {
1604 Ok(status) => status,
1605 Err(why) => return self.no_status(why),
1606 };
1607 self.seen(&status);
1608 }
1609 }
1610 let wanted: Vec<String> = status.capabilities.iter().filter(|c| c.available && kinds.on(&c.id)).map(|c| c.id.clone()).collect();
1611 if status.capabilities.iter().any(|c| c.available) && wanted.is_empty() {
1612 self.not_before = Instant::now() + LOOK_AGAIN;
1613 return self.set(Code::KindsStopped, Extra::default());
1614 }
1615 let mut token = None;
1616 if !ours || !kept.as_ref().is_some_and(|k| k.registered) || status.unregistered() {
1617 match self.token.take() {
1618 Some((for_workspace, given)) if for_workspace == workspace => token = Some(given),
1619 _ => return self.want(&workspace),
1620 }
1621 SandboxRegistration { server: server.to_string(), workspace: workspace.clone(), registered: false }.save(&app);
1622 }
1623 self.hold_workspace(&workspace);
1624 let generation = self.generation + 1;
1625 self.generation = generation;
1626 self.run = Some(Run::new(generation, Engine::Launcher, Backend::Sandboxed, workspace.clone(), Process::Sandbox(None), token.is_some()));
1627 // A new release's image first, then the sandbox starts again.
1628 let image = self.with_updates(|updates| std::mem::take(&mut updates.image));
1629 let step = if image && status.linux().state != State::Absent { Step::Update } else { Step::Up };
1630 if step == Step::Update {
1631 self.with_updates(|updates| updates.installing.push("linux".into()));
1632 } else if image {
1633 self.done_with(|_| {});
1634 }
1635 let name = backend::machine_name();
1636 let url = server.as_str().trim_end_matches('/').to_owned();
1637 let up = launcher::Up { url: &url, name: &name, labels: "desktop", token: token.as_deref(), only: wanted.iter().map(String::as_str).collect() };
1638 let (args, starting) = match step {
1639 Step::Update => (launcher::update_args(&dir), vec![]),
1640 _ => (launcher::up_args(&dir, &up), wanted.clone()),
1641 };
1642 if let Err(why) = self.spawn_step(step, args, up.token) {
1643 self.run = None;
1644 return self.not_started(workspace, token, why);
1645 }
1646 self.now_starting(starting);
1647 self.set(Code::Starting, detail(STARTING_SANDBOX));
1648 }
1649
1650 /// Says which kinds of job a launcher command is starting now.
1651 fn now_starting(&self, kinds: Vec<String>) {
1652 let changed = {
1653 let shared = self.app.state::<Shared>();
1654 let mut now = shared.now.lock().unwrap();
1655 let changed = now.starting != kinds;
1656 now.starting = kinds;
1657 changed
1658 };
1659 if changed {
1660 publish(&self.app);
1661 }
1662 }
1663
1664 /// Each kind of job as the person has it: one they stopped is stopped
1665 /// (`down --only`), one they started is started (`up --only`), and one
1666 /// that should run and doesn't while another runs is tried again in a
1667 /// while: every five minutes while the release has no sandbox images.
1668 /// One launcher command at a time, each leaving the others as they are.
1669 fn kinds(&mut self, workspace: Option<&str>) {
1670 if !self.run.as_ref().is_some_and(|run| run.engine == Engine::Launcher) {
1671 return;
1672 }
1673 let Some(status) = self.app.state::<Shared>().now.lock().unwrap().sandbox.clone() else { return };
1674 if !status.per_kind() {
1675 return;
1676 }
1677 let on = config::settings(&self.app).runner_capabilities;
1678 // A kind of job drained for a new program starts on the new one, once it is in.
1679 let replacing = self.program.is_some();
1680 for capability in &status.capabilities {
1681 if !on.on(&capability.id) && !capability.off() {
1682 return self.kind_step(Step::Stop(capability.id.clone()), None);
1683 }
1684 if on.on(&capability.id) && capability.off() && capability.available && !(replacing && capability.id != "linux") {
1685 return self.start_kind(capability, workspace);
1686 }
1687 }
1688 // Nothing runs: the sandbox as a whole starts again (sandbox_stopped).
1689 if !status.running() {
1690 self.retry.clear();
1691 return;
1692 }
1693 let now = Instant::now();
1694 for capability in &status.capabilities {
1695 let id = capability.id.as_str();
1696 let wanting = on.on(id) && !capability.off() && capability.available && matches!(capability.state, State::Error | State::Stopped | State::Absent);
1697 if !wanting {
1698 self.retry.retain(|(kind, _, _)| kind != id);
1699 continue;
1700 }
1701 let images = id == "linux" && status.trouble().is_some_and(|(cause, _)| launcher::why(&cause) == launcher::Why::NoImages);
1702 match self.retry.iter().position(|(kind, _, _)| kind == id) {
1703 // Just seen not running: tried again in a while.
1704 None => self.retry.push((id.to_owned(), now + again(images, 1), 1)),
1705 Some(at) if now >= self.retry[at].1 => {
1706 let tries = self.retry[at].2 + 1;
1707 self.retry[at] = (id.to_owned(), now + again(images, tries), tries);
1708 self.write_log(&format!("g1t: trying {} again.", diagnosis::kind_label(id)));
1709 return self.start_kind(capability, workspace);
1710 }
1711 Some(_) => {}
1712 }
1713 }
1714 }
1715
1716 /// Starts one kind of job; one never registered needs a token from the
1717 /// site first, which is asked for, and it starts once it is here.
1718 fn start_kind(&mut self, capability: &launcher::Capability, workspace: Option<&str>) {
1719 let mut token = None;
1720 if capability.state == State::Absent {
1721 let Some(workspace) = workspace else { return };
1722 match self.token.take() {
1723 Some((for_workspace, given)) if for_workspace == workspace => token = Some(given),
1724 other => {
1725 self.token = other;
1726 if wanted(&self.app).is_none() {
1727 *self.app.state::<Shared>().wanted.lock().unwrap() = Some(workspace.to_owned());
1728 let _ = self.app.emit_to(EventTarget::webview(frame::SITE), WANTED_EVENT, workspace.to_owned());
1729 }
1730 return;
1731 }
1732 }
1733 }
1734 self.kind_step(Step::Start(capability.id.clone()), token);
1735 }
1736
1737 /// `up --only` or `down --only` one kind of job, while the rest run on.
1738 fn kind_step(&mut self, step: Step, token: Option<String>) {
1739 let dir = native_dir(&self.app, Backend::Sandboxed);
1740 let (args, starting, said) = match &step {
1741 Step::Start(kind) => {
1742 let url = config::server(&self.app).as_str().trim_end_matches('/').to_owned();
1743 let name = backend::machine_name();
1744 let up = launcher::Up { url: &url, name: &name, labels: "desktop", token: token.as_deref(), only: vec![kind.as_str()] };
1745 (launcher::up_args(&dir, &up), vec![kind.clone()], format!("starting {}", diagnosis::kind_label(kind)))
1746 }
1747 Step::Stop(kind) => (launcher::down_only_args(&dir, kind), vec![], format!("stopping {}", diagnosis::kind_label(kind))),
1748 Step::Drain(kind) => (launcher::drain_only_args(&dir, kind), vec![], format!("stopping {} after their job, to update g1t-runner", diagnosis::kind_label(kind))),
1749 Step::Up | Step::Update => return,
1750 };
1751 self.write_log(&format!("g1t: {said}."));
1752 match self.spawn_step(step, args, token.as_deref()) {
1753 Ok(()) => self.now_starting(starting),
1754 Err(why) => self.write_log(&format!("g1t: {why}")),
1755 }
1756 }
1757
1758 /// `sandbox status` didn't answer: a `g1t-runner` from before its
1759 /// launcher, which a newer release replaces, or one that failed.
1760 fn no_status(&mut self, why: String) {
1761 self.write_log(&format!("g1t: the sandbox's status: {why}"));
1762 self.failures += 1;
1763 if why.contains("older than its sandbox launcher") {
1764 let (server, folder) = (config::server(&self.app), folder(&self.app));
1765 if let Ok(Found { version, program: Some(bytes), .. }) = look_for_release(&server, &folder, None)
1766 && native::put(&folder, &bytes, &version).is_ok()
1767 {
1768 self.note(None, Some(version));
1769 self.not_before = Instant::now();
1770 return;
1771 }
1772 self.not_before = Instant::now() + MOST_BACKOFF;
1773 return self.set(Code::NoLauncher, detail(why));
1774 }
1775 self.not_before = Instant::now() + LOOK_AGAIN;
1776 self.set(Code::SandboxFailed, Extra { detail: Some(why), wait_seconds: Some(LOOK_AGAIN.as_secs()), ..Extra::default() });
1777 }
1778
1779 /// Runs a launcher command for the sandbox's run, reading its output.
1780 fn spawn_step(&mut self, step: Step, args: Vec<std::ffi::OsString>, token: Option<&str>) -> Result<(), String> {
1781 let binary = native::binary(&folder(&self.app));
1782 let mut command = launcher::command(&binary, args);
1783 launcher::with_token(&mut command, token);
1784 let mut child = command.spawn().map_err(|e| format!("g1t-runner sandbox {} could not start: {e}", step.name()))?;
1785 let _ = std::fs::write(folder(&self.app).join("pid"), child.id().to_string());
1786 let Some(generation) = self.run.as_ref().map(|run| run.generation) else {
1787 native::stop(&mut child);
1788 return Ok(());
1789 };
1790 self.follow(generation, &mut child);
1791 let run = self.run.as_mut().expect("checked above");
1792 run.json = None;
1793 run.process = Process::Sandbox(Some((step, child)));
1794 Ok(())
1795 }
1796
1797 /// `update` on the running sandbox, while it has no job.
1798 fn sandbox_step(&mut self, step: Step) {
1799 if self.run.as_ref().is_none_or(|run| run.step().is_some()) {
1800 return;
1801 }
1802 let dir = native_dir(&self.app, Backend::Sandboxed);
1803 if let Err(why) = self.spawn_step(step, launcher::update_args(&dir), None) {
1804 self.write_log(&format!("g1t: {why}"));
1805 }
1806 }
1807
1808 /// Asks the sandbox how it is, every few seconds, on a thread of its own.
1809 fn poll(&mut self) {
1810 let Some(run) = self.run.as_mut().filter(|run| run.engine == Engine::Launcher) else { return };
1811 if run.polling || run.polled_at.is_some_and(|at| at.elapsed() < launcher::STATUS_EVERY) {
1812 return;
1813 }
1814 run.polling = true;
1815 run.polled_at = Some(Instant::now());
1816 let (tx, generation) = (self.tx.clone(), run.generation);
1817 let binary = native::binary(&folder(&self.app));
1818 let dir = native_dir(&self.app, Backend::Sandboxed);
1819 let spawned = std::thread::Builder::new().name("g1t-sandbox-status".into()).spawn(move || {
1820 let _ = tx.send(Ask::Sandbox(generation, launcher::status(&binary, &dir).map(Box::new)));
1821 });
1822 if spawned.is_err() {
1823 run.polling = false;
1824 }
1825 }
1826
1827 /// What a poll said.
1828 fn polled(&mut self, generation: u64, status: Result<Box<Status>, String>) {
1829 let Some(run) = self.run.as_mut().filter(|run| run.generation == generation) else { return };
1830 run.polling = false;
1831 if std::mem::take(&mut run.stale) {
1832 return;
1833 }
1834 match status {
1835 Ok(status) => self.sandbox_now(*status),
1836 // Once is nothing: the next poll asks again.
1837 Err(why) => self.write_log(&format!("g1t: the sandbox's status: {why}")),
1838 }
1839 }
1840
1841 /// Keeps what a status says for the tray, settings and the site.
1842 fn seen(&mut self, status: &Status) {
1843 let shared = self.app.state::<Shared>();
1844 let mut now = shared.now.lock().unwrap();
1845 now.sandbox = Some(status.clone());
1846 if let Some(version) = status.version() {
1847 now.version = Some(version);
1848 }
1849 now.job = status.job().map(launcher::Job::said);
1850 }
1851
1852 /// The sandbox as it is now, while it is the run.
1853 fn sandbox_now(&mut self, status: Status) {
1854 let had = self.app.state::<Shared>().now.lock().unwrap().job.clone();
1855 let Some(run) = self.run.as_mut() else { return };
1856 let step = run.step();
1857 let job = status.job().map(launcher::Job::said);
1858 let was = had.filter(|_| run.busy);
1859 run.busy = job.is_some();
1860 run.listening = status.running();
1861 let update_failed = self.update_failed.clone();
1862 if was != job {
1863 if let Some(ended) = &was {
1864 self.write_log(&format!("g1t: {ended} ended."));
1865 }
1866 if let Some(took) = &job {
1867 self.write_log(&format!("g1t: Took {took}."));
1868 }
1869 }
1870 self.seen(&status);
1871 match &step {
1872 Some(Step::Up) => return self.set(Code::Starting, detail(STARTING_SANDBOX)),
1873 Some(Step::Update) if status.state == State::Updating => return self.set(Code::Updating, detail("Updating the sandbox to the newest image.")),
1874 _ => {}
1875 }
1876 let (code, extra) = shown(&status);
1877 // Every kind of job stopped by the person: nothing to start again.
1878 if step.is_none() && status.all_off() && !status.running() {
1879 self.run = None;
1880 self.generation += 1;
1881 self.retry.clear();
1882 self.not_before = Instant::now() + LOOK_AGAIN;
1883 return self.set(Code::KindsStopped, Extra::default());
1884 }
1885 if step.is_none() && !status.running() && !matches!(status.state, State::Starting | State::Updating) {
1886 return self.sandbox_stopped(&status, code, extra);
1887 }
1888 match (code, update_failed) {
1889 (Code::Idle, Some((failed, why))) => self.set(failed, detail(why)),
1890 (code, _) => self.set(code, extra),
1891 }
1892 }
1893
1894 /// The sandbox stopped by itself, or never got going: started again,
1895 /// waiting longer each time, or registered again when that is why.
1896 fn sandbox_stopped(&mut self, status: &Status, code: Code, extra: Extra) {
1897 let error = status.error.clone().or_else(|| extra.detail.clone()).unwrap_or_default();
1898 if self.registration_trouble(&error) {
1899 return;
1900 }
1901 self.run = None;
1902 self.generation += 1;
1903 self.failures += 1;
1904 // A release without the sandbox's images is looked at again every few minutes.
1905 let wait = if code == Code::WaitingForImage { launcher::IMAGES_AGAIN } else { again(false, self.failures) };
1906 self.not_before = Instant::now() + wait;
1907 let (code, extra) = match code {
1908 Code::Idle | Code::Busy | Code::Starting | Code::Updating => (Code::SandboxFailed, Extra { detail: status.error.clone().or_else(|| some_text("The sandbox stopped by itself.")), ..Extra::default() }),
1909 code => (code, extra),
1910 };
1911 self.set(code, Extra { wait_seconds: Some(wait.as_secs()), ..extra });
1912 }
1913
1914 /// When the sandbox's trouble is its registration: true when the app
1915 /// has seen to it, and the next start registers again.
1916 fn registration_trouble(&mut self, error: &str) -> bool {
1917 match launcher::why(error) {
1918 launcher::Why::TokenRefused | launcher::Why::Unregistered => {
1919 if let Some(mut kept) = SandboxRegistration::load(&self.app) {
1920 kept.registered = false;
1921 kept.save(&self.app);
1922 }
1923 self.halt();
1924 self.not_before = Instant::now();
1925 true
1926 }
1927 launcher::Why::Forgotten => {
1928 self.halt();
1929 self.forget_sandbox();
1930 self.not_before = Instant::now();
1931 true
1932 }
1933 _ => false,
1934 }
1935 }
1936
1937 /// A launcher command ended: what it printed last is the sandbox's status.
1938 fn launched(&mut self, generation: u64) {
1939 let Some(run) = self.run.as_mut().filter(|run| run.generation == generation) else { return };
1940 let Process::Sandbox(slot) = &mut run.process else { return };
1941 let Some((step, mut child)) = slot.take() else { return };
1942 let worked = child.wait().is_ok_and(|status| status.success());
1943 let _ = std::fs::remove_file(folder(&self.app).join("pid"));
1944 let printed = run.json.take().unwrap_or_default().join("\n");
1945 run.stale = run.polling;
1946 let with_token = run.with_token;
1947 let status = launcher::parse(&printed);
1948 {
1949 let shared = self.app.state::<Shared>();
1950 let mut now = shared.now.lock().unwrap();
1951 now.starting.clear();
1952 now.download = None;
1953 }
1954 let error = match (&status, worked) {
1955 (Ok(status), true) => status.error.clone().filter(|_| !status.running()),
1956 (Ok(status), false) => Some(status.error.clone().unwrap_or_else(|| format!("g1t-runner sandbox {} failed.", step.name()))),
1957 (Err(why), _) => Some(why.clone()),
1958 };
1959 let status = status.ok();
1960 let running = status.as_ref().is_some_and(Status::running);
1961 if let Some(error) = &error {
1962 self.write_log(&format!("g1t: sandbox {}: {error}", step.name()));
1963 }
1964 // Drained for a new program, which goes in on the next look; one
1965 // that didn't stop leaves the update for the next one, rather than
1966 // being asked again and again.
1967 if let (Step::Drain(_), Some(error)) = (&step, &error) {
1968 self.program = None;
1969 self.update_failed = Some((Code::UpdateFailed, format!("g1t-runner couldn't be updated: {error}")));
1970 self.done_with(|updates| {
1971 updates.program = false;
1972 updates.installing.retain(|kind| kind == "linux");
1973 });
1974 }
1975 // One kind of job started or stopped: how the sandbox is now says
1976 // the rest, and one that didn't start is tried again in a while.
1977 if matches!(step, Step::Start(_) | Step::Stop(_) | Step::Drain(_)) {
1978 if let Some(status) = status {
1979 self.sandbox_now(status);
1980 }
1981 return;
1982 }
1983 // Registered: from now on it starts with no token.
1984 if step == Step::Up && with_token && (error.is_none() || running) {
1985 let app = self.app.clone();
1986 if let Some(run) = self.run.as_mut() {
1987 run.with_token = false;
1988 let registration = Registration { server: config::server(&app).to_string(), workspace: run.workspace.clone(), backend: Backend::Sandboxed };
1989 SandboxRegistration { server: registration.server.clone(), workspace: registration.workspace.clone(), registered: true }.save(&app);
1990 registration.save(&app);
1991 }
1992 }
1993 // The image is taken, or failed to be: the next look says whether another waits.
1994 if step == Step::Update {
1995 self.done_with(|updates| {
1996 updates.image = false;
1997 updates.installing.retain(|kind| kind != "linux");
1998 });
1999 }
2000 if let Some(error) = error.clone() {
2001 if self.registration_trouble(&error) {
2002 return;
2003 }
2004 if step == Step::Update {
2005 self.update_failed = Some((Code::UpdateFailed, format!("The sandbox's image couldn't be updated: {error}")));
2006 } else if !running {
2007 // Nothing runs: tidied away, and tried again in a while;
2008 // a release without the sandbox's images every few minutes.
2009 self.halt();
2010 self.failures += 1;
2011 let (code, cause, fix) = status
2012 .as_ref()
2013 .and_then(diagnosis::sandbox_trouble)
2014 .unwrap_or_else(|| (diagnosis::sandbox_code(launcher::why(&error)), error.clone(), None));
2015 let wait = if code == Code::WaitingForImage { launcher::IMAGES_AGAIN } else { again(false, self.failures) };
2016 self.not_before = Instant::now() + wait;
2017 // The cards say why from what `up` said, not from the stopped sandbox.
2018 if let Some(failed) = status {
2019 self.app.state::<Shared>().now.lock().unwrap().sandbox = Some(failed);
2020 }
2021 return self.set(code, Extra { detail: Some(cause), fix, wait_seconds: Some(wait.as_secs()), ..Extra::default() });
2022 }
2023 } else if step == Step::Up {
2024 self.failures = 0;
2025 } else {
2026 self.update_failed = None;
2027 }
2028 if step == Step::Update && !running {
2029 // Updated while stopped: it starts again with the new image.
2030 self.run = None;
2031 self.generation += 1;
2032 self.not_before = Instant::now();
2033 return;
2034 }
2035 if let Some(status) = status {
2036 self.sandbox_now(status);
2037 }
2038 }
2039}
2040
2041/// What the tray says while the sandbox starts.
2042const STARTING_SANDBOX: &str = "Starting the sandbox. The first time, it downloads and checks the sandbox's image, which takes a few minutes.";
2043
2044/// The newest release, signed, and its program when it is newer than the
2045/// one in `folder` and isn't already `waiting`: downloaded and checked
2046/// against its SHA-256. On the update thread, never the runner's.
2047fn look_for_release(server: &Url, folder: &std::path::Path, waiting: Option<&str>) -> Result<Found, Fault> {
2048 let manifest = native::latest(server)?;
2049 let newer = native::installed(folder).is_none_or(|have| native::newer(&manifest.version, &have));
2050 let program = if newer && waiting != Some(manifest.version.as_str()) {
2051 Some(native::download(server, &manifest).map_err(|why| Fault::Failed(format!("{} didn't download: {why}", manifest.version)))?)
2052 } else {
2053 None
2054 };
2055 Ok(Found { images: manifest.has_images(), version: manifest.version, newer, program })
2056}
2057
2058fn some_text(text: &str) -> Option<String> {
2059 Some(text.to_owned())
2060}
2061
2062/// How long before trying again, the `tries`th time: every few minutes
2063/// while the release has no sandbox images, else longer each time.
2064fn again(images: bool, tries: u32) -> Duration {
2065 if images { launcher::IMAGES_AGAIN } else { Duration::from_secs(15 * 2u64.pow(tries.min(6))).min(MOST_BACKOFF) }
2066}
2067
2068/// What the app shows for how the sandbox is, with no launcher command
2069/// running: what its runners do while one runs (each kind of job's card
2070/// says the rest), else what is wrong with Linux jobs.
2071fn shown(status: &Status) -> (Code, Extra) {
2072 if status.job().is_some() {
2073 return (Code::Busy, Extra::default());
2074 }
2075 if status.running() {
2076 return (Code::Idle, Extra::default());
2077 }
2078 if let Some((code, cause, fix)) = diagnosis::sandbox_trouble(status) {
2079 return (code, Extra { detail: Some(cause), fix, ..Extra::default() });
2080 }
2081 match status.linux().state {
2082 State::Starting => (Code::Starting, detail(STARTING_SANDBOX)),
2083 State::Updating => (Code::Updating, detail("Updating the sandbox to the newest image.")),
2084 _ if status.all_off() => (Code::KindsStopped, Extra::default()),
2085 _ => (Code::SandboxFailed, detail("The sandbox isn't running.")),
2086 }
2087}
2088
2089/// What `register-and-run` reads: where g1t is, the runner's name and
2090/// labels, and the token, the first time only. The app updates the runner,
2091/// so the runner does not update itself; registering again takes the
2092/// place of this computer's earlier runner of the same name.
2093fn environment(server: &Url, backend: Backend, token: Option<&str>) -> Vec<(&'static str, String)> {
2094 let mut envs = vec![
2095 ("G1T_RUNNER_URL", server.as_str().trim_end_matches('/').to_owned()),
2096 ("G1T_RUNNER_NAME", backend::machine_name()),
2097 ("G1T_RUNNER_LABELS", "desktop".to_owned()),
2098 ("G1T_RUNNER_REPLACE", "1".to_owned()),
2099 ("G1T_RUNNER_NO_AUTO_UPDATE", "1".to_owned()),
2100 ];
2101 if backend == Backend::Host {
2102 envs.push(("G1T_RUNNER_NO_DOCKER", "1".to_owned()));
2103 }
2104 if let Some(token) = token {
2105 envs.push(("G1T_RUNNER_TOKEN", token.to_owned()));
2106 }
2107 envs
2108}
2109
2110#[cfg(test)]
2111mod tests {
2112 use super::*;
2113
2114 fn view(code: Code, job: Option<&str>) -> View {
2115 let facts = Facts { workspace: Some("acme".into()), backend: "sandboxed", machine: "box".into(), job: job.map(str::to_owned), ..Facts::default() };
2116 View {
2117 diagnosis: diagnosis::diagnose(code, &facts),
2118 closed: diagnosis::closed(&facts),
2119 running: code == Code::Idle,
2120 version: None,
2121 backend: Backend::Sandboxed,
2122 workspace: Some("acme".into()),
2123 workspaces: vec![],
2124 enabled: true,
2125 paused: false,
2126 link: None,
2127 machine: "box".into(),
2128 platform: "windows".into(),
2129 log: vec![],
2130 }
2131 }
2132
2133 #[test]
2134 fn the_tray_says_what_the_runner_is_doing() {
2135 assert_eq!(headline(&view(Code::Idle, None)), "Runner: Waiting for jobs");
2136 assert_eq!(headline(&view(Code::Busy, Some("Build (acme/web)"))), "Runner: Running Build (acme/web)");
2137 assert_eq!(headline(&view(Code::NoDocker, None)), "Runner: Not running: Docker isn't running");
2138 assert_eq!(headline(&view(Code::Busy, Some(&"x".repeat(200)))).chars().count(), 60);
2139 }
2140
2141 #[test]
2142 fn the_runner_is_told_where_and_who_and_the_token_only_once() {
2143 let server = Url::parse("https://g1t.sh/").unwrap();
2144 let envs = environment(&server, Backend::Host, Some("g1trt_abc"));
2145 let get = |name: &str| envs.iter().find(|(n, _)| *n == name).map(|(_, v)| v.as_str());
2146 assert_eq!(get("G1T_RUNNER_URL"), Some("https://g1t.sh"));
2147 assert_eq!(get("G1T_RUNNER_LABELS"), Some("desktop"));
2148 assert_eq!(get("G1T_RUNNER_NO_DOCKER"), Some("1"));
2149 assert_eq!(get("G1T_RUNNER_NO_AUTO_UPDATE"), Some("1"));
2150 assert_eq!(get("G1T_RUNNER_TOKEN"), Some("g1trt_abc"));
2151 let again = environment(&server, Backend::Docker, None);
2152 assert!(!again.iter().any(|(name, _)| *name == "G1T_RUNNER_TOKEN" || *name == "G1T_RUNNER_NO_DOCKER"));
2153 }
2154
2155 fn status(json: &str) -> Status {
2156 launcher::parse(json).unwrap()
2157 }
2158
2159 #[test]
2160 fn the_sandbox_shows_what_its_runners_do_and_linux_trouble_first() {
2161 let running = status(r#"{"schema":1,"state":"running","capabilities":[{"id":"linux","available":true,"state":"running"},{"id":"windows","available":false,"state":"absent","cause":"Windows Sandbox is not turned on, or this edition of Windows has none (Home does not)."}]}"#);
2162 assert_eq!(shown(&running).0, Code::Idle);
2163 let busy = status(r#"{"schema":1,"state":"running","job":{"kind":"workflow","id":"j","name":"build","repo":"acme/web"}}"#);
2164 assert_eq!(shown(&busy).0, Code::Busy);
2165 let starting = status(r#"{"schema":1,"state":"starting","capabilities":[{"id":"linux","available":true,"state":"starting"}]}"#);
2166 assert_eq!(shown(&starting).0, Code::Starting);
2167 let updating = status(r#"{"schema":1,"state":"updating","capabilities":[{"id":"linux","available":true,"state":"updating"}]}"#);
2168 assert_eq!(shown(&updating).0, Code::Updating);
2169 // Windows jobs run while WSL is missing: the runner waits for jobs,
2170 // and the Linux card says what is wrong (diagnosis.rs).
2171 let no_wsl = status(r#"{"schema":1,"state":"error","error":"WSL is not installed, and the sandbox for Linux jobs is a WSL distribution of its own.","capabilities":[
2172 {"id":"linux","available":false,"state":"error","cause":"WSL is not installed, and the sandbox for Linux jobs is a WSL distribution of its own.","fix":"Open a terminal as administrator and run `wsl --install --no-distribution`."},
2173 {"id":"windows","available":true,"state":"running"}]}"#);
2174 assert_eq!(shown(&no_wsl).0, Code::Idle);
2175 let (code, extra) = shown(&status(r#"{"schema":1,"state":"error","error":"WSL is not installed, and the sandbox for Linux jobs is a WSL distribution of its own.","capabilities":[
2176 {"id":"linux","available":false,"state":"error","cause":"WSL is not installed, and the sandbox for Linux jobs is a WSL distribution of its own."},
2177 {"id":"windows","available":false,"state":"absent"}]}"#));
2178 assert_eq!(code, Code::NoWsl);
2179 assert!(extra.detail.unwrap().starts_with("WSL is not installed"));
2180 // 0.3.0 without its images, on Home: waiting for them, not failing.
2181 let images = status(r#"{"schema":1,"state":"error","error":"release 0.3.0 has no sandbox images","capabilities":[{"id":"linux","available":true,"state":"error","turned_off":false},{"id":"windows","available":false,"state":"absent","turned_off":false}]}"#);
2182 assert_eq!(shown(&images).0, Code::WaitingForImage);
2183 assert_eq!(again(true, 5), launcher::IMAGES_AGAIN);
2184 assert_eq!(again(false, 1), Duration::from_secs(30));
2185 assert_eq!(again(false, 9), MOST_BACKOFF);
2186 // Every kind of job stopped by the person.
2187 let off = status(r#"{"schema":1,"state":"stopped","capabilities":[{"id":"linux","available":true,"state":"stopped","turned_off":true},{"id":"windows","available":true,"state":"stopped","turned_off":true}]}"#);
2188 assert_eq!(shown(&off).0, Code::KindsStopped);
2189 let failed = status(r#"{"schema":1,"state":"error","error":"the sandbox did not start within 180 seconds","capabilities":[{"id":"linux","available":true,"state":"error"}]}"#);
2190 let (code, extra) = shown(&failed);
2191 assert_eq!((code, extra.detail.as_deref()), (Code::SandboxFailed, Some("the sandbox did not start within 180 seconds")));
2192 let stopped = status(r#"{"schema":1,"state":"stopped","capabilities":[{"id":"linux","available":true,"state":"stopped"},{"id":"windows","available":true,"state":"stopped"}]}"#);
2193 assert_eq!(shown(&stopped).0, Code::SandboxFailed);
2194 }
2195
2196 #[test]
2197 fn when_is_said_as_the_site_reads_it() {
2198 let at = |seconds: u64| rfc3339(SystemTime::UNIX_EPOCH + Duration::from_secs(seconds));
2199 assert_eq!(at(0), "1970-01-01T00:00:00Z");
2200 assert_eq!(at(1_791_806_645), "2026-10-12T12:04:05Z");
2201 // The day after February 29th of a leap year.
2202 assert_eq!(at(1_709_251_199 + 1), "2024-03-01T00:00:00Z");
2203 assert_eq!(at(951_782_400), "2000-02-29T00:00:00Z");
2204 }
2205
2206 /// What is running on the program itself, which an update drains first:
2207 /// Windows jobs running or starting, not Linux jobs, nor ones stopped.
2208 #[test]
2209 fn an_update_drains_only_what_runs_on_the_program() {
2210 let kinds = |json: &str| native_kinds(&status(json)).iter().map(|c| c.id.clone()).collect::<Vec<_>>();
2211 assert_eq!(
2212 kinds(r#"{"schema":1,"state":"running","capabilities":[{"id":"linux","available":true,"state":"running","turned_off":false},{"id":"windows","available":true,"state":"running","turned_off":false}]}"#),
2213 ["windows"]
2214 );
2215 assert!(kinds(r#"{"schema":1,"state":"running","capabilities":[{"id":"linux","available":true,"state":"running","turned_off":false},{"id":"windows","available":true,"state":"stopped","turned_off":true}]}"#).is_empty());
2216 assert!(kinds(r#"{"schema":1,"state":"running","capabilities":[{"id":"linux","available":true,"state":"running"},{"id":"windows","available":false,"state":"absent"}]}"#).is_empty());
2217 }
2218
2219 #[test]
2220 fn the_site_names_workspaces_in_its_own_words() {
2221 let offered: Vec<Offered> = serde_json::from_str(r#"[{"slug":"acme","name":"Acme"},{"slug":"solo"}]"#).unwrap();
2222 assert_eq!(offered[1], Offered { slug: "solo".into(), name: None });
2223 }
2224}