runnerd-core 0.1.0

Config, domain logic and API types shared by runnerd and its clients
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
//! The writer thread: applies writes in the order they were sent, closes job records once both GitHub and the
//! engine are done with them, and announces what changed. A change and the durable events it announces commit in
//! one transaction and are broadcast only after, so a subscriber that reads the stored events after subscribing
//! misses none, and a crash never keeps a change without its event.

use std::collections::BTreeMap;
use std::sync::{Arc, Mutex};

use rusqlite::{Connection, OptionalExtension, Transaction, TransactionBehavior, params};
use tokio::sync::{broadcast, mpsc, oneshot};

use super::read::{self, secs};
use super::{
    Downtime, HOST_SAMPLE_BATCH, LogPosition, MirrorPosition, Resolution, RunnerRecord,
    SampleRecord, Write, logged,
};
use crate::api::{self, Event, EventEnvelope};
use crate::github::{JobDetails, JobStep, QueuedJob};
use crate::record;
use crate::sync::lock;
use crate::time;

/// Makes an event its repository's latest in `announced`.
const ANNOUNCE_REPOSITORY: &str =
    "INSERT OR REPLACE INTO announced (repository, event) VALUES (?1, ?2)";
/// Makes an event its profile's latest in `announced_profiles`.
const ANNOUNCE_PROFILE: &str =
    "INSERT OR REPLACE INTO announced_profiles (profile, event) VALUES (?1, ?2)";

pub(super) fn run(
    db: Connection,
    downtime: Option<Downtime>,
    mut queue: mpsc::UnboundedReceiver<Write>,
    events: &broadcast::Sender<Arc<EventEnvelope>>,
    held: &Mutex<Vec<api::HostSample>>,
) {
    let writer = Writer {
        db,
        downtime,
        events,
        held,
    };
    while let Some(write) = queue.blocking_recv() {
        writer.apply(write);
    }
    writer.store_host_samples();
}

struct Writer<'store> {
    db: Connection,
    /// The time before this daemon when no runnerd ran, which a job queued before it first records waited through.
    downtime: Option<Downtime>,
    events: &'store broadcast::Sender<Arc<EventEnvelope>>,
    /// `Store::held`.
    held: &'store Mutex<Vec<api::HostSample>>,
}

/// The events a change announces once it commits.
type Announced = Vec<Event>;

fn timestamp(text: Option<&str>) -> Option<u64> {
    text.and_then(time::parse_ms)
}

/// How `records.removed_by` stores a removal: its name in the API.
fn removal_name(by: api::RecordRemoval) -> String {
    serde_json::to_value(by)
        .ok()
        .and_then(|value| value.as_str().map(str::to_owned))
        .unwrap_or_default()
}

impl Writer<'_> {
    #[allow(
        clippy::too_many_lines,
        reason = "one arm per kind of write, each a call; split, it would only move the match"
    )]
    fn apply(&self, write: Write) {
        match write {
            Write::Queued {
                repository,
                job,
                seen_at,
            } => {
                self.change("record a queued job", |announced| {
                    self.queued(&repository, &job, seen_at, announced)
                });
            }
            Write::RunnerStarted(runner) => self.runner_started(&runner),
            Write::RunnerAdmission { runner, admission } => self.admission(&runner, admission),
            Write::RunnerOomKilled(runner) => self.runner_oom_killed(&runner),
            Write::RunnerStoppedFor { runner, reason } => self.runner_stopped_for(&runner, reason),
            Write::RunnerCancelBy { runner, at } => self.runner_cancel_by(&runner, at),
            Write::HostWaited { jobs, ms } => self.host_waited(&jobs, ms),
            Write::SlotWaited { jobs, ms } => self.slot_waited(&jobs, ms),
            Write::BuildCache { runner, counts } => self.build_cache(&runner, &counts),
            Write::RunnerRemoved { runner, at } => {
                self.change("record a removed runner", |announced| {
                    self.runner_removed(&runner, at, announced)
                });
            }
            Write::RunnersGone { running, at } => {
                self.change("record removed runners", |announced| {
                    self.runners_gone(&running, at, announced)
                });
            }
            Write::Claimed {
                job,
                run_id,
                runner,
                at,
            } => {
                self.change("record a claimed job", |announced| {
                    self.claimed(job, run_id, &runner, at, announced)
                });
            }
            Write::Details { details } => {
                self.change("record a job's progress", |announced| {
                    self.details(&details, announced)
                });
            }
            Write::Elsewhere { details } => {
                self.change("close a job that ran elsewhere", |announced| {
                    self.elsewhere(&details, announced)
                });
            }
            Write::Unanswered { job, at } => {
                self.change("close a job GitHub no longer answers for", |announced| {
                    self.unanswered(job, at, announced)
                });
            }
            Write::Lost { job, at } => {
                self.change("close a lost job", |announced| {
                    self.close(job, "lost", Some(at), announced)
                });
            }
            Write::Log {
                job,
                lines,
                position,
                max_bytes,
                _backlog,
            } => {
                self.change("store log lines", |announced| {
                    self.log(job, lines, position, max_bytes, announced)
                });
            }
            Write::Samples(samples) => {
                self.change("record samples", |announced| {
                    self.samples(&samples, announced)
                });
            }
            Write::HostSample(sample) => {
                // Live: no ID to wait for.
                let _ = self.events.send(Arc::new(EventEnvelope {
                    id: None,
                    at: secs(time::now_ms()),
                    event: Event::HostSample { sample },
                }));
                if lock(self.held).len() >= HOST_SAMPLE_BATCH {
                    self.store_host_samples();
                }
            }
            Write::MirrorBytes { bytes, until, done } => {
                let _ = done.send(self.mirror_bytes(&bytes, until));
            }
            Write::CacheSizes { at, sizes } => self.cache_sizes(at, sizes),
            Write::Event(event) => {
                self.change("store an event", |announced| {
                    announced.push(*event);
                    Ok(())
                });
            }
            Write::RecordMade(record) => self.record_made(record),
            Write::RecordSkipped { job, why } => self.record_skipped(job, &why),
            Write::RecordRemoved { job, at, by } => self.record_removed(job, at, by),
            Write::Visibility {
                repository,
                visibility,
                at,
            } => {
                self.change("record a repository's visibility", |_| {
                    self.db.execute(
                        "INSERT INTO repositories (name, visibility, checked_at) VALUES (?1, ?2, ?3) \
                         ON CONFLICT (name) DO UPDATE SET visibility = ?2, checked_at = ?3",
                        params![repository, visibility_text(visibility), at],
                    )?;
                    Ok(())
                });
            }
            Write::Paused { repositories, done } => {
                let _ = done.send(self.paused(&repositories));
            }
            Write::SizingIntent { change, done } => {
                let _ = done.send(self.sizing_intent(&change));
            }
            Write::SizingResolved {
                id,
                resolution,
                done,
            } => self.sizing_resolved(id, resolution, done),
            Write::Notify { change, done } => self.notify(change, done),
            Write::RunnerNamed { secs, done } => {
                let _ = done.send(self.runner_named(secs));
            }
            Write::Flush(done) => {
                self.store_host_samples();
                let _ = done.send(());
            }
        }
    }

    fn record_made(&self, record: api::Record) {
        self.change("record a job's record", |announced| {
            self.db.execute(
                "INSERT OR REPLACE INTO records (job, created_at, bytes) VALUES (?1, ?2, ?3)",
                params![record.job, record.created_at * 1000, record.bytes],
            )?;
            announced.push(Event::RecordCreated { record });
            Ok(())
        });
    }

    fn record_skipped(&self, job: u64, why: &str) {
        logged(
            "record a skipped record",
            self.db.execute(
                "INSERT OR REPLACE INTO records (job, skipped) VALUES (?1, ?2)",
                params![job, why],
            ),
        );
    }

    fn record_removed(&self, job: u64, at: u64, by: api::RecordRemoval) {
        self.change("record a removed record", |announced| {
            self.db.execute(
                "INSERT INTO records (job, removed_at, removed_by) VALUES (?1, ?2, ?3) \
                 ON CONFLICT (job) DO UPDATE SET removed_at = ?2, removed_by = ?3",
                params![job, at, removal_name(by)],
            )?;
            announced.push(Event::RecordDeleted {
                job,
                removed_by: by,
            });
            Ok(())
        });
    }

    /// Stores the held host samples in one transaction, then lets go of them. If that fails they are held for the
    /// next try, ten batches at most: older ones are dropped.
    fn store_host_samples(&self) {
        let samples = lock(self.held).clone();
        if samples.is_empty() {
            return;
        }
        let stored = self.change("record host samples", |_| {
            for sample in &samples {
                self.db.execute(
                    "INSERT OR REPLACE INTO host_samples (at, cpu, memory) VALUES (?1, ?2, ?3)",
                    params![sample.at * 1000, sample.cpu, sample.memory],
                )?;
            }
            Ok(())
        });
        let mut held = lock(self.held);
        let done = if stored {
            samples.len()
        } else {
            held.len().saturating_sub(HOST_SAMPLE_BATCH * 10)
        };
        let len = held.len();
        held.drain(..done.min(len));
    }

    fn runner_started(&self, runner: &RunnerRecord) {
        logged(
            "record a runner",
            self.db.execute(
                "INSERT OR IGNORE INTO runners (name, repository, profile, slot, container, started_at, \
                 memory_cap, cpu_cap, admission, memory_measure, purpose) \
                 VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, 'without_page_cache', ?10)",
                params![
                    runner.name,
                    runner.repository,
                    runner.profile,
                    runner.slot,
                    runner.container,
                    runner.started_at,
                    runner.memory_cap,
                    runner.cpu_cap,
                    runner
                        .admission
                        .and_then(|admission| serde_json::to_string(&admission).ok()),
                    runner.purpose
                ],
            ),
        );
    }

    fn cache_sizes(&self, at: u64, sizes: BTreeMap<String, u64>) {
        for (name, bytes) in sizes {
            logged(
                "record a cache size",
                self.db.execute(
                    "INSERT OR REPLACE INTO cache_sizes (at, name, bytes) VALUES (?1, ?2, ?3)",
                    params![at, name, bytes],
                ),
            );
        }
    }

    fn admission(&self, runner: &str, admission: api::Admission) {
        logged(
            "record what a runner reserves",
            self.db.execute(
                "UPDATE runners SET admission = ?2 WHERE name = ?1",
                params![runner, serde_json::to_string(&admission).ok()],
            ),
        );
    }

    fn runner_oom_killed(&self, runner: &str) {
        logged(
            "record a kill for memory",
            self.db.execute(
                "UPDATE runners SET oom_kills = max(coalesce(oom_kills, 0), 1) WHERE name = ?1",
                [runner],
            ),
        );
    }

    fn runner_stopped_for(&self, runner: &str, reason: &str) {
        logged(
            "record why a runner stopped",
            self.db.execute(
                "UPDATE runners SET stopped_for = ?2 WHERE name = ?1",
                params![runner, reason],
            ),
        );
    }

    fn runner_cancel_by(&self, runner: &str, at: u64) {
        logged(
            "record a cancelled runner's deadline",
            self.db.execute(
                "UPDATE runners SET cancel_by = ?2 WHERE name = ?1",
                params![runner, at],
            ),
        );
    }

    fn host_waited(&self, jobs: &[u64], ms: u64) {
        for job in jobs {
            logged(
                "record a wait for the host's memory",
                self.db.execute(
                    "UPDATE jobs SET host_waited = host_waited + ?2 WHERE id = ?1",
                    params![job, ms],
                ),
            );
        }
    }

    fn slot_waited(&self, jobs: &[u64], ms: u64) {
        for job in jobs {
            logged(
                "record a wait for a slot",
                self.db.execute(
                    "UPDATE jobs SET slot_waited = slot_waited + ?2 WHERE id = ?1",
                    params![job, ms],
                ),
            );
        }
    }

    fn build_cache(&self, runner: &str, counts: &api::BuildCacheCounts) {
        logged(
            "record the build cache's counts",
            self.db.execute(
                "UPDATE runners SET build_cache_hits = build_cache_hits + ?2, \
                 build_cache_misses = build_cache_misses + ?3, \
                 build_cache_read_bytes = build_cache_read_bytes + ?4, \
                 build_cache_written_bytes = build_cache_written_bytes + ?5, \
                 build_cache_refused = build_cache_refused + ?6, \
                 build_cache_writer = max(coalesce(build_cache_writer, 0), ?7) WHERE name = ?1",
                params![
                    runner,
                    counts.hits,
                    counts.misses,
                    counts.read_bytes,
                    counts.written_bytes,
                    counts.refused,
                    counts.writer
                ],
            ),
        );
    }

    /// Makes a change and stores the durable events it announces in one transaction, then sends them, with their
    /// IDs, to every subscriber. On any error nothing is kept or sent. Returns whether it committed.
    fn change(
        &self,
        what: &str,
        change: impl FnOnce(&mut Announced) -> rusqlite::Result<()>,
    ) -> bool {
        let result = (|| {
            // Immediate, so a busy database is waited for: a deferred transaction that read first fails at once.
            let transaction = Transaction::new_unchecked(&self.db, TransactionBehavior::Immediate)?;
            let mut announced = Vec::new();
            change(&mut announced)?;
            let envelopes = announced
                .into_iter()
                .map(|event| self.store_event(event))
                .collect::<rusqlite::Result<Vec<_>>>()?;
            transaction.commit()?;
            Ok(envelopes)
        })();
        let Some(envelopes) = logged(what, result) else {
            return false;
        };
        for envelope in envelopes {
            // No subscriber is not an error.
            let _ = self.events.send(Arc::new(envelope));
        }
        true
    }

    /// Stores an event if it is durable, giving it its ID. A repository's or profile's event becomes
    /// its latest in `announced` or `announced_profiles`.
    fn store_event(&self, event: Event) -> rusqlite::Result<EventEnvelope> {
        let at = time::now_ms();
        let id = if event.is_durable() {
            let body = serde_json::to_string(&event)
                .map_err(|error| rusqlite::Error::ToSqlConversionFailure(Box::new(error)))?;
            self.db.execute(
                "INSERT INTO events (at, body) VALUES (?1, ?2)",
                params![at, body],
            )?;
            let id = self.db.last_insert_rowid();
            let latest = match &event {
                Event::RepositoryChanged { repository } => {
                    Some((ANNOUNCE_REPOSITORY, &repository.name))
                }
                Event::RepositoryRemoved { repository } => Some((ANNOUNCE_REPOSITORY, repository)),
                Event::ProfileChanged { profile } => Some((ANNOUNCE_PROFILE, &profile.name)),
                Event::ProfileRemoved { profile } => Some((ANNOUNCE_PROFILE, profile)),
                _ => None,
            };
            if let Some((upsert, name)) = latest {
                self.db.execute(upsert, params![name, id])?;
            }
            u64::try_from(id).ok()
        } else {
            None
        };
        Ok(EventEnvelope {
            id,
            at: secs(at),
            event,
        })
    }

    fn queued(
        &self,
        repository: &str,
        job: &QueuedJob,
        seen_at: u64,
        announced: &mut Announced,
    ) -> rusqlite::Result<()> {
        let labels = serde_json::to_string(&job.labels).unwrap_or_else(|_| "[]".to_owned());
        let queued_at = if job.created_at == 0 {
            time::now_ms()
        } else {
            job.created_at
        };
        let unseen = self.downtime.map_or(0, |downtime| downtime.of(queued_at));
        let inserted = self.db.execute(
            "INSERT INTO jobs (id, repository, run_id, run_attempt, workflow, name, branch, url, labels, state, \
             queued_at, unseen, seen_at) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, 'queued', ?10, ?11, ?12) \
             ON CONFLICT (id) DO NOTHING",
            params![
                job.id,
                repository,
                job.run_id,
                job.run_attempt,
                job.workflow,
                job.name,
                job.branch,
                job.url,
                labels,
                queued_at,
                unseen,
                seen_at
            ],
        )?;
        if inserted == 0 {
            // A runner's capture may have claimed it first: the listing still saw it queued.
            self.db.execute(
                "UPDATE jobs SET seen_at = ?2 WHERE id = ?1 AND seen_at IS NULL \
                 AND (started_at IS NULL OR started_at >= ?2)",
                params![job.id, seen_at],
            )?;
        } else if let Some(job) = read::summary(&self.db, job.id)? {
            announced.push(Event::JobQueued { job });
        }
        Ok(())
    }

    fn claimed(
        &self,
        job: u64,
        run_id: u64,
        runner: &str,
        at: u64,
        announced: &mut Announced,
    ) -> rusqlite::Result<()> {
        // A job the daemon never saw queued: GitHub's listing lagged, or it was queued before the daemon started.
        let inserted = self.db.execute(
            "INSERT INTO jobs (id, repository, run_id, run_attempt, name, labels, state, runner, queued_at, \
             started_at) SELECT ?1, repository, ?2, 1, '', '[]', 'running', name, ?3, ?3 FROM runners \
             WHERE name = ?4 ON CONFLICT (id) DO NOTHING",
            params![job, run_id, at, runner],
        )?;
        // A runner here took a job `unanswered` closed, as one GitHub refused while its token lost the repository and
        // queued again once it was restored: the job is this machine's (ADR 0109).
        let updated = self.db.execute(
            "UPDATE jobs SET state = 'running', runner = ?2, started_at = coalesce(started_at, ?3), \
             finished_at = NULL WHERE id = ?1 AND (state = 'queued' \
             OR (state = 'elsewhere' AND conclusion IS NULL AND started_at IS NULL \
             AND finished_at IS NOT NULL))",
            params![job, runner, at],
        )?;
        if (inserted == 1 || updated == 1)
            && let Some(job) = read::summary(&self.db, job)?
        {
            announced.push(Event::JobStarted { job });
        }
        Ok(())
    }

    fn details(&self, details: &JobDetails, announced: &mut Announced) -> rusqlite::Result<()> {
        let completed = details.status == "completed";
        let labels = serde_json::to_string(&details.labels).unwrap_or_else(|_| "[]".to_owned());
        let started_at = timestamp(details.started_at.as_deref());
        // A runner that outlived the last daemon may have started it while none ran: what the downtime gave it
        // after that start was no wait. Once, when GitHub's start first says so.
        let after_start = match (self.downtime, started_at) {
            (Some(downtime), Some(started_at)) if started_at < downtime.until => self
                .db
                .query_row(
                    "SELECT queued_at, started_at FROM jobs WHERE id = ?1",
                    [details.id],
                    |row| Ok((row.get::<_, u64>(0)?, row.get::<_, Option<u64>>(1)?)),
                )
                .optional()?
                .filter(|(_, known)| known.is_none_or(|known| known >= downtime.until))
                .map_or(0, |(queued_at, _)| downtime.after(queued_at, started_at)),
            _ => 0,
        };
        let updated = self.db.execute(
            "UPDATE jobs SET run_id = ?2, run_attempt = ?3, name = ?4, workflow = ?5, branch = ?6, url = ?7, \
             labels = ?8, queued_at = coalesce(?9, queued_at), started_at = coalesce(?10, started_at), \
             finished_at = CASE WHEN ?11 THEN coalesce(?12, finished_at) ELSE finished_at END, \
             conclusion = ?13, github_completed = ?11, unseen = max(unseen - ?14, 0) \
             WHERE id = ?1 AND state = 'running'",
            params![
                details.id,
                details.run_id,
                details.run_attempt,
                details.name,
                details.workflow_name,
                details.head_branch,
                details.html_url,
                labels,
                timestamp(details.created_at.as_deref()),
                started_at,
                completed,
                timestamp(details.completed_at.as_deref()),
                details.conclusion,
                after_start,
            ],
        )?;
        if updated != 1 {
            return Ok(());
        }
        for step in &details.steps {
            if let Some(step) = self.step(details.id, step)? {
                announced.push(Event::JobStep {
                    job: details.id,
                    step,
                });
            }
        }
        self.close_if_done(details.id, announced)
    }

    /// Stores a step; returns it if it is new or changed.
    fn step(&self, job: u64, step: &JobStep) -> rusqlite::Result<Option<api::Step>> {
        let stored = api::Step {
            number: step.number,
            name: step.name.clone(),
            status: step.status.clone(),
            conclusion: step.conclusion.clone(),
            started_at: timestamp(step.started_at.as_deref()).map(secs),
            completed_at: timestamp(step.completed_at.as_deref()).map(secs),
        };
        let changed = self.db.execute(
            "INSERT INTO steps (job, number, name, status, conclusion, started_at, completed_at) \
             VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7) ON CONFLICT (job, number) DO UPDATE SET \
             name = excluded.name, status = excluded.status, conclusion = excluded.conclusion, \
             started_at = excluded.started_at, completed_at = excluded.completed_at \
             WHERE (name, status, conclusion, started_at, completed_at) IS NOT \
             (excluded.name, excluded.status, excluded.conclusion, excluded.started_at, excluded.completed_at)",
            params![
                job,
                step.number,
                step.name,
                step.status,
                step.conclusion,
                timestamp(step.started_at.as_deref()),
                timestamp(step.completed_at.as_deref()),
            ],
        )?;
        Ok((changed == 1).then_some(stored))
    }

    fn runner_removed(
        &self,
        runner: &str,
        at: u64,
        announced: &mut Announced,
    ) -> rusqlite::Result<()> {
        self.db.execute(
            "UPDATE runners SET removed_at = ?2 WHERE name = ?1 AND removed_at IS NULL",
            params![runner, at],
        )?;
        let job: Option<u64> = self
            .db
            .query_row(
                "SELECT id FROM jobs WHERE runner = ?1 AND state = 'running'",
                [runner],
                |row| row.get(0),
            )
            .optional()?;
        match job {
            Some(job) => self.close_if_done(job, announced),
            None => Ok(()),
        }
    }

    fn runners_gone(
        &self,
        running: &[String],
        at: u64,
        announced: &mut Announced,
    ) -> rusqlite::Result<()> {
        let open: Vec<String> = self
            .db
            .prepare_cached("SELECT name FROM runners WHERE removed_at IS NULL")?
            .query_map([], |row| row.get(0))?
            .collect::<rusqlite::Result<_>>()?;
        for runner in open.iter().filter(|runner| !running.contains(runner)) {
            self.runner_removed(runner, at, announced)?;
        }
        Ok(())
    }

    /// Closes a running job once GitHub reports it completed and its runner is removed.
    /// A job GitHub completed closes once its runner is gone. A runner the history never recorded, as Docker failed to
    /// inspect it, leaves no removal to wait for.
    fn close_if_done(&self, job: u64, announced: &mut Announced) -> rusqlite::Result<()> {
        let done = self
            .db
            .query_row(
                "SELECT j.github_completed AND (r.name IS NULL OR r.removed_at IS NOT NULL) FROM jobs j \
                 LEFT JOIN runners r ON r.name = j.runner WHERE j.id = ?1 AND j.state = 'running'",
                [job],
                |row| row.get::<_, bool>(0),
            )
            .optional()?;
        if done == Some(true) {
            self.close(job, "completed", None, announced)?;
        }
        Ok(())
    }

    /// Keeps a job's metrics and makes its record final.
    fn close(
        &self,
        job: u64,
        state: &str,
        finished_at: Option<u64>,
        announced: &mut Announced,
    ) -> rusqlite::Result<()> {
        let metrics = read::metrics(&self.db, job)?;
        let closed = self.db.execute(
            "UPDATE jobs SET state = ?2, finished_at = coalesce(finished_at, ?3), cpu_time = ?4, \
             cpu_peak = ?5, cpu_average = ?6, memory_peak = ?7, memory_average = ?8, \
             network_received = ?9, network_sent = ?10, cpu_periods = ?11, cpu_throttled = ?12, \
             cpu_stalled = ?13 WHERE id = ?1 AND state = 'running'",
            params![
                job,
                state,
                finished_at,
                metrics.cpu_time,
                metrics.cpu_peak,
                metrics.cpu_average,
                metrics.memory_peak,
                metrics.memory_average,
                metrics.network_received,
                metrics.network_sent,
                metrics.cpu_periods,
                metrics.cpu_throttled,
                metrics.cpu_stalled,
            ],
        )?;
        if closed == 1 {
            self.read_runner_fault(job)?;
        }
        if closed == 1
            && let Some(job) = read::summary(&self.db, job)?
        {
            announced.push(Event::JobFinished { job });
        }
        Ok(())
    }

    /// Keeps whether a job that failed was failed by its runner (`record::runner_fault`), from its failed step's
    /// log, which is complete once the record is final.
    fn read_runner_fault(&self, job: u64) -> rusqlite::Result<()> {
        let Some(job) = read::job(&self.db, job)? else {
            return Ok(());
        };
        let summary = &job.summary;
        if summary.state == api::JobState::Completed
            && summary.is_failure()
            && record::runner_fault(&job, &read::log_text(&self.db, summary.id)?)
        {
            self.db.execute(
                "UPDATE jobs SET runner_fault = 1 WHERE id = ?1",
                [summary.id],
            )?;
        }
        Ok(())
    }

    fn unanswered(&self, job: u64, at: u64, announced: &mut Announced) -> rusqlite::Result<()> {
        let closed = self.db.execute(
            "UPDATE jobs SET state = 'elsewhere', finished_at = ?2 WHERE id = ?1 AND state = 'queued'",
            params![job, at],
        )?;
        if closed == 1
            && let Some(job) = read::summary(&self.db, job)?
        {
            announced.push(Event::JobFinished { job });
        }
        Ok(())
    }

    fn elsewhere(&self, details: &JobDetails, announced: &mut Announced) -> rusqlite::Result<()> {
        // GitHub gives a job no runner took, as one a newer push cancelled while queued, a start time: its creation.
        // Its runner's name is null, or empty for a job GitHub skipped.
        let started_at = details
            .runner_name
            .as_ref()
            .filter(|name| !name.is_empty())
            .and_then(|_| timestamp(details.started_at.as_deref()));
        let closed = self.db.execute(
            "UPDATE jobs SET state = 'elsewhere', conclusion = ?2, started_at = ?3, finished_at = ?4 \
             WHERE id = ?1 AND state = 'queued'",
            params![
                details.id,
                details.conclusion,
                started_at,
                timestamp(details.completed_at.as_deref()),
            ],
        )?;
        if closed == 1
            && let Some(job) = read::summary(&self.db, details.id)?
        {
            announced.push(Event::JobFinished { job });
        }
        Ok(())
    }

    fn log(
        &self,
        job: u64,
        lines: Vec<String>,
        position: Option<LogPosition>,
        max_bytes: u64,
        announced: &mut Announced,
    ) -> rusqlite::Result<()> {
        let (mut count, mut bytes, mut truncated): (u64, u64, bool) = self.db.query_row(
            "SELECT log_lines, log_bytes, log_truncated FROM jobs WHERE id = ?1",
            [job],
            |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?)),
        )?;
        for line in lines {
            if truncated {
                break;
            }
            let size = line.len() as u64 + 1;
            if bytes + size > max_bytes {
                truncated = true;
                break;
            }
            self.db.execute(
                "INSERT OR IGNORE INTO log_lines (job, line, text) VALUES (?1, ?2, ?3)",
                params![job, count, line],
            )?;
            announced.push(Event::LogLine {
                job,
                line: count,
                text: line,
            });
            count += 1;
            bytes += size;
        }
        self.db.execute(
            "UPDATE jobs SET log_lines = ?2, log_bytes = ?3, log_truncated = ?4, \
             log_page = coalesce(?5, log_page), log_offset = coalesce(?6, log_offset) WHERE id = ?1",
            params![
                job,
                count,
                bytes,
                truncated,
                position.map(|position| position.page),
                position.map(|position| position.offset)
            ],
        )?;
        Ok(())
    }

    fn samples(&self, samples: &[SampleRecord], announced: &mut Announced) -> rusqlite::Result<()> {
        for sample in samples {
            self.db.execute(
                "INSERT OR REPLACE INTO samples (runner, at, cpu, cpu_usage, memory, network_received, \
                 network_sent, cpu_periods, cpu_throttled, cpu_stalled) \
                 VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10)",
                params![
                    sample.runner,
                    sample.at,
                    sample.cpu,
                    sample.cpu_usage,
                    sample.memory,
                    sample.network_received,
                    sample.network_sent,
                    sample.cpu_held.periods,
                    sample.cpu_held.throttled,
                    sample.cpu_held.stalled,
                ],
            )?;
            if let Some(ooms) = sample.events.oom {
                self.db.execute(
                    "UPDATE runners SET ooms = max(coalesce(ooms, 0), ?2) WHERE name = ?1",
                    params![sample.runner, ooms],
                )?;
            }
            if let Some(ooms) = sample.events.local_oom {
                self.db.execute(
                    "UPDATE runners SET local_ooms = max(coalesce(local_ooms, 0), ?2) WHERE name = ?1",
                    params![sample.runner, ooms],
                )?;
            }
            if let Some(throttled) = sample.events.sock_throttled {
                self.db.execute(
                    "UPDATE runners SET sock_throttled = max(coalesce(sock_throttled, 0), ?2) WHERE name = ?1",
                    params![sample.runner, throttled],
                )?;
            }
            if let Some(kills) = sample.events.oom_kill.filter(|kills| *kills > 0) {
                self.db.execute(
                    "UPDATE runners SET oom_kills = max(coalesce(oom_kills, 0), ?2) WHERE name = ?1",
                    params![sample.runner, kills],
                )?;
            }
            let job: Option<u64> = self
                .db
                .query_row(
                    "SELECT id FROM jobs WHERE runner = ?1 AND state = 'running'",
                    [&sample.runner],
                    |row| row.get(0),
                )
                .optional()?;
            if let Some(job) = job {
                announced.push(Event::JobSample {
                    job,
                    sample: api::Sample {
                        at: secs(sample.at),
                        cpu: sample.cpu,
                        memory: sample.memory,
                        network_received: sample.network_received,
                        network_sent: sample.network_sent,
                    },
                });
            }
        }
        Ok(())
    }

    /// Replaces the paused repositories.
    fn paused(&self, repositories: &[String]) -> bool {
        self.change("record the paused repositories", |_| {
            self.db.execute("DELETE FROM paused", [])?;
            for repository in repositories {
                self.db
                    .execute("INSERT INTO paused (repository) VALUES (?1)", [repository])?;
            }
            Ok(())
        })
    }

    /// Records what the notifier did, and sends whether it committed.
    fn notify(&self, change: super::NotifyChange, done: oneshot::Sender<bool>) {
        let committed = match change {
            super::NotifyChange::Record {
                target,
                set,
                remove,
            } => self.notify_record(&target, &set, &remove),
            super::NotifyChange::Keep(targets) => self.notify_keep(&targets),
        };
        let _ = done.send(committed);
    }

    fn notify_record(&self, target: &str, set: &[(String, u64)], remove: &[String]) -> bool {
        self.change("record what was notified", |_| {
            for (key, value) in set {
                self.db.execute(
                    "INSERT OR REPLACE INTO notify (target, key, value) VALUES (?1, ?2, ?3)",
                    params![target, key, value],
                )?;
            }
            for key in remove {
                self.db.execute(
                    "DELETE FROM notify WHERE target = ?1 AND key = ?2",
                    params![target, key],
                )?;
            }
            Ok(())
        })
    }

    fn notify_keep(&self, targets: &[String]) -> bool {
        self.change("forget removed notification targets", |_| {
            let kept: Vec<String> = self
                .db
                .prepare_cached("SELECT DISTINCT target FROM notify")?
                .query_map([], |row| row.get(0))?
                .collect::<rusqlite::Result<_>>()?;
            for gone in kept.iter().filter(|target| !targets.contains(target)) {
                self.db
                    .execute("DELETE FROM notify WHERE target = ?1", [gone])?;
            }
            Ok(())
        })
    }

    /// Runs `write` with its commits synced to disk (`synchronous = FULL`), for what must outlast a power failure:
    /// an auto sizing change recorded before the config file is written and synced, what became of it, and the second
    /// a runner was named for. `None` when the database will not sync them.
    fn durably<T>(&self, write: impl FnOnce() -> T) -> Option<T> {
        logged(
            "sync commits to disk",
            self.db.pragma_update(None, "synchronous", "full"),
        )?;
        let written = write();
        logged(
            "stop syncing every commit",
            self.db.pragma_update(None, "synchronous", "normal"),
        );
        Some(written)
    }

    fn sizing_intent(&self, change: &api::SizingChange) -> Option<i64> {
        self.durably(|| self.record_intent(change)).flatten()
    }

    fn record_intent(&self, change: &api::SizingChange) -> Option<i64> {
        let mut id = None;
        self.change("record an auto sizing change", |_| {
            self.db.execute(
                "INSERT INTO auto_sizing (profile, at, from_memory, from_slots, to_memory, to_slots, reason, state, \
                 from_cpus, to_cpus) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, 'pending', ?8, ?9)",
                params![
                    change.profile,
                    change.at.saturating_mul(1000),
                    change.from.memory,
                    change.from.slots,
                    change.to.memory,
                    change.to.slots,
                    change.reason,
                    change.from.cpus,
                    change.to.cpus
                ],
            )?;
            id = Some(self.db.last_insert_rowid());
            Ok(())
        })
        .then_some(id)
        .flatten()
    }

    fn sizing_resolved(&self, id: i64, resolution: Resolution, done: oneshot::Sender<bool>) {
        let resolved = self
            .durably(|| self.resolve(id, resolution))
            .unwrap_or(false);
        let _ = done.send(resolved);
    }

    fn resolve(&self, id: i64, resolution: Resolution) -> bool {
        self.change("resolve an auto sizing change", |announced| {
            let state = match resolution {
                Resolution::Done => "done",
                Resolution::Failed => "failed",
                Resolution::Uncertain => "uncertain",
            };
            self.db.execute(
                "UPDATE auto_sizing SET state = ?2 WHERE id = ?1 AND state = 'pending'",
                params![id, state],
            )?;
            if resolution == Resolution::Done {
                let change = self.db.query_row(
                    "SELECT * FROM auto_sizing WHERE id = ?1",
                    [id],
                    read::sizing_change,
                )?;
                announced.push(Event::SizingChanged { change });
            }
            Ok(())
        })
    }

    /// Synced to disk: a name a power failure took back would be given again after it.
    fn runner_named(&self, secs: u64) -> bool {
        self.durably(|| {
            self.change("record a runner's name", |_| {
                self.db.execute(
                    "INSERT OR REPLACE INTO meta (key, value) VALUES ('runner_named', ?1)",
                    [secs],
                )?;
                Ok(())
            })
        })
        .unwrap_or(false)
    }

    /// Counts a batch of the mirror's log and where it ends, together: counting part of it, or counting it without
    /// moving past it, would count it again after a restart.
    fn mirror_bytes(&self, bytes: &[(String, u64)], until: MirrorPosition) -> bool {
        self.change("count mirror bytes", |_| {
            for (runner, bytes) in bytes {
                self.db.execute(
                    "UPDATE runners SET mirror_bytes = mirror_bytes + ?2 WHERE name = ?1",
                    params![runner, bytes],
                )?;
            }
            self.db.execute(
                "INSERT OR REPLACE INTO meta (key, value) VALUES ('mirror_log_until', ?1), ('mirror_log_seen', ?2)",
                [until.at, until.seen],
            )?;
            Ok(())
        })
    }
}

/// A visibility as the `repositories` table keeps it, GitHub's word for it.
fn visibility_text(visibility: api::Visibility) -> &'static str {
    match visibility {
        api::Visibility::Public => "public",
        api::Visibility::Private => "private",
        api::Visibility::Internal => "internal",
    }
}

#[cfg(test)]
#[allow(
    clippy::unwrap_used,
    reason = "tests: a database that cannot open fails the test"
)]
mod tests {
    use std::sync::Mutex;

    use rusqlite::Connection;
    use tokio::sync::broadcast;

    use super::Writer;
    use crate::testdir::TestDir;

    fn synchronous(db: &Connection) -> u8 {
        db.pragma_query_value(None, "synchronous", |row| row.get(0))
            .unwrap()
    }

    #[test]
    fn a_durable_write_syncs_its_commits_and_the_writer_goes_back_to_normal_after() {
        let dir = TestDir::new("store-write");
        let db = Connection::open(dir.0.join("runnerd.db")).unwrap();
        super::super::configure(&db).unwrap();
        let (events, _) = broadcast::channel(1);
        let held = Mutex::new(Vec::new());
        let writer = Writer {
            db,
            downtime: None,
            events: &events,
            held: &held,
        };
        // 2 is FULL, 1 NORMAL.
        assert_eq!(writer.durably(|| synchronous(&writer.db)), Some(2));
        assert_eq!(synchronous(&writer.db), 1);
    }
}