leptoskit_rpc 0.3.0

The app's operations: leptos server functions with retries and error classification, queries and mutations that run on the database, one client for every platform, and publishing it all to generated 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
//! The workers: claiming jobs, running them on a lease, recording how they
//! went, and the chores around them: `on_failure`, cron, pruning, and
//! waking up when a job is enqueued.

use std::collections::HashMap;
use std::collections::HashSet;
use std::hash::BuildHasher;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::PoisonError;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::AtomicI64;
use std::sync::atomic::Ordering;
use std::time::Duration;

use leptoskit_db::Actor;
use leptoskit_db::Connection;
use leptoskit_db::Db;
use leptoskit_db::DbError;
use leptoskit_db::Json;
use leptoskit_db::Table;
use leptoskit_db::Timestamp;
use tokio::sync::Notify;

use super::FailureRun;
use super::JobCx;
use super::JobRegistration;
use super::JobRow;
use super::JobRun;
use super::JobState;
use super::cron::CronRegistration;
use super::cx::Progress;
use super::cx::RunState;
use super::duration;
use super::job_registration;
use super::owner_actor;
use super::owner_context;
use crate::Error;
use crate::server::Identity;

/// The time workers go by: the system clock, or, in tests, the system clock
/// moved forward by hand.
#[derive(Clone, Default)]
pub(crate) struct Clock {
	offset: Option<Arc<AtomicI64>>,
}

impl Clock {
	/// A clock tests move forward.
	pub(crate) fn manual() -> Self {
		Self {
			offset: Some(Arc::default()),
		}
	}

	pub(crate) fn now(&self) -> Timestamp {
		let offset = self
			.offset
			.as_ref()
			.map_or(0, |offset| offset.load(Ordering::SeqCst));
		leptoskit_db::now() + chrono::Duration::milliseconds(offset)
	}

	pub(crate) fn advance(&self, by: Duration) {
		if let Some(offset) = &self.offset {
			offset.fetch_add(
				i64::try_from(by.as_millis()).unwrap_or(i64::MAX),
				Ordering::SeqCst,
			);
		}
	}
}

/// Faults a test injects: a worker that dies inside a step.
#[derive(Default)]
pub(crate) struct Faults {
	kill_in: Mutex<HashSet<String>>,
}

impl Faults {
	/// Kills the next run of step `name` after its work, before its result
	/// is saved.
	pub(crate) fn kill_in(&self, name: &str) {
		self.kill_in
			.lock()
			.unwrap_or_else(PoisonError::into_inner)
			.insert(name.to_owned());
	}

	/// Whether step `name` dies now; each fault fires once.
	pub(crate) fn kills(&self, name: &str) -> bool {
		self.kill_in
			.lock()
			.unwrap_or_else(PoisonError::into_inner)
			.remove(name)
	}
}

/// How workers claim and run jobs.
#[derive(Clone)]
pub(crate) struct Settings {
	/// How long a worker holds a job it isn't heard from.
	pub lease: Duration,
	/// Each queue's concurrency, across every instance; queues not named
	/// have none but the workers'.
	pub queues: Arc<HashMap<String, usize>>,
	pub clock: Clock,
	pub faults: Option<Arc<Faults>>,
}

impl Settings {
	pub(crate) fn new(clock: Clock) -> Self {
		Self {
			lease: Duration::from_secs(60),
			queues: Arc::default(),
			clock,
			faults: None,
		}
	}
}

/// How long writes of a job's progress are spread apart.
const PROGRESS_EVERY: Duration = Duration::from_millis(250);

/// The workers that run a database's jobs, and the change log's background
/// work beside them.
///
/// ```ignore
/// let jobs = rpc::server::Jobs::new(db.clone())
/// 	.workers(4)
/// 	.queue("chain", 2)
/// 	.retention(Duration::from_secs(7 * 24 * 60 * 60))
/// 	.start();
/// // …and at shutdown, after the server stops taking requests:
/// jobs.shutdown().await;
/// ```
///
/// Starting them also starts the change log's upkeep, which prunes it to
/// [`live_retention`](Self::live_retention) and keeps its Postgres
/// partitions ahead, and, with the `postgres` feature, replication capture,
/// which records changes when the migrations capture them by logical
/// replication and otherwise only waits.
pub struct Jobs {
	db: Db,
	workers: usize,
	poll: Duration,
	retention: Option<Duration>,
	cron: bool,
	live_retention: Option<Duration>,
	capture: bool,
	settings: Settings,
}

impl Jobs {
	/// Workers for `db`'s jobs: four at a time, checking every five seconds
	/// as well as whenever a job is enqueued, with a one-minute lease each,
	/// keeping finished jobs for a week, and enqueuing `#[rpc::cron]` jobs.
	pub fn new(db: Db) -> Self {
		Self {
			db,
			workers: 4,
			poll: Duration::from_secs(5),
			retention: Some(Duration::from_hours(168)),
			cron: true,
			live_retention: Some(Duration::from_mins(15)),
			capture: true,
			settings: Settings::new(Clock::default()),
		}
	}

	/// Workers configured from `[rpc.jobs]`, keeping the change log for
	/// `[db] live_retention_minutes`.
	///
	/// ```ignore
	/// let jobs = rpc::server::Jobs::from_configs(db.clone(), &config.rpc.jobs, &config.db).start();
	/// ```
	pub fn from_configs(
		db: Db,
		jobs: &leptoskit_config::RpcJobsConfig,
		database: &leptoskit_config::DbConfig,
	) -> Self {
		Self::from_config(db, jobs).live_retention(database.live_retention())
	}

	/// Workers configured from `[rpc.jobs]` in `leptoskit.toml`.
	///
	/// ```ignore
	/// let config = LeptoskitConfig::from_toml(include_str!("../../leptoskit.toml"), Path::new("my_app"))?;
	/// let jobs = rpc::server::Jobs::from_config(db.clone(), &config.rpc.jobs).start();
	/// ```
	pub fn from_config(db: Db, config: &leptoskit_config::RpcJobsConfig) -> Self {
		let mut jobs = Self::new(db)
			.workers(usize::try_from(config.workers).unwrap_or(usize::MAX))
			.poll(Duration::from_secs(config.poll_seconds.into()))
			.retention(
				(config.retention_days > 0)
					.then(|| Duration::from_secs(u64::from(config.retention_days) * 24 * 60 * 60)),
			);

		for (name, queue) in &config.queues {
			jobs = jobs.queue(
				name,
				usize::try_from(queue.concurrency).unwrap_or(usize::MAX),
			);
		}

		jobs
	}

	/// How many jobs this instance runs at once.
	#[must_use]
	pub fn workers(mut self, workers: usize) -> Self {
		self.workers = workers.max(1);
		self
	}

	/// The same as [`workers`](Self::workers).
	#[must_use]
	pub fn concurrency(self, concurrency: usize) -> Self {
		self.workers(concurrency)
	}

	/// How often idle workers look for jobs that are due, besides being
	/// woken whenever one is enqueued, on any instance.
	#[must_use]
	pub fn poll(mut self, poll: Duration) -> Self {
		self.poll = poll.max(Duration::from_millis(10));
		self
	}

	/// How long a worker holds a job before another may take it over. A
	/// running job renews its lease as it goes, so this is how soon a job
	/// whose worker died runs again. A job's own `timeout` lengthens it.
	#[must_use]
	pub fn lease(mut self, lease: Duration) -> Self {
		self.settings.lease = lease.max(Duration::from_millis(100));
		self
	}

	/// Runs at most `concurrency` jobs of `queue` at once, across every
	/// instance that runs workers for the same database. Give each instance
	/// the same queues.
	#[must_use]
	pub fn queue(mut self, queue: impl Into<String>, concurrency: usize) -> Self {
		Arc::make_mut(&mut self.settings.queues).insert(queue.into(), concurrency.max(1));
		self
	}

	/// How long finished and cancelled jobs are kept; `None` keeps them.
	/// Failed jobs stay until someone retries them.
	#[must_use]
	pub fn retention(mut self, retention: Option<Duration>) -> Self {
		self.retention = retention;
		self
	}

	/// Whether this instance enqueues `#[rpc::cron]` jobs. Every instance
	/// may: each tick's job is enqueued once, whichever gets there first.
	#[must_use]
	pub fn cron(mut self, cron: bool) -> Self {
		self.cron = cron;
		self
	}

	/// How long the change log is kept, so a client that reconnects within
	/// it catches up rather than refetching everything; `None` keeps it all.
	/// Every instance may prune: they take turns. Fifteen minutes unless set.
	#[must_use]
	pub fn live_retention(mut self, retention: Option<Duration>) -> Self {
		self.live_retention = retention;
		self
	}

	/// Whether this instance takes part in replication capture: on by
	/// default, and idle unless the migrations capture by replication. Turn
	/// it off on instances that shouldn't hold a replication connection.
	#[must_use]
	pub fn capture(mut self, capture: bool) -> Self {
		self.capture = capture;
		self
	}

	/// Starts the workers, which run until the returned handle stops them.
	pub fn start(self) -> JobsHandle {
		let shutdown = Arc::new(AtomicBool::new(false));
		let wake = Arc::new(Notify::new());
		let mut tasks = Vec::with_capacity(self.workers + 2);
		let maintenance = self
			.live_retention
			.map(|retention| self.db.live_maintenance(retention, Duration::from_mins(1)));
		#[cfg(feature = "postgres")]
		let capture = self.capture.then(|| self.db.capture_changes());
		#[cfg(not(feature = "postgres"))]
		let _ = self.capture;

		tasks.push(tokio::spawn(waker(
			self.db.clone(),
			Arc::clone(&wake),
			Arc::clone(&shutdown),
		)));

		if self.cron {
			tasks.push(tokio::spawn(scheduler(
				self.db.clone(),
				self.settings.clock.clone(),
				Arc::clone(&wake),
				Arc::clone(&shutdown),
			)));
		}

		for index in 0..self.workers {
			tasks.push(tokio::spawn(worker(
				self.db.clone(),
				self.settings.clone(),
				Arc::clone(&shutdown),
				Arc::clone(&wake),
				self.poll,
				// One worker prunes, so instances don't race each other more
				// than they must.
				self.retention.filter(|_| index == 0),
			)));
		}

		JobsHandle {
			shutdown,
			wake,
			tasks,
			maintenance,
			#[cfg(feature = "postgres")]
			capture,
		}
	}
}

/// A running set of workers.
pub struct JobsHandle {
	shutdown: Arc<AtomicBool>,
	wake: Arc<Notify>,
	tasks: Vec<tokio::task::JoinHandle<()>>,
	maintenance: Option<leptoskit_db::live::Maintenance>,
	#[cfg(feature = "postgres")]
	capture: Option<leptoskit_db::live::Capture>,
}

impl JobsHandle {
	/// Asks the workers to stop: they take no more jobs, and finish the
	/// ones they hold. The change log's upkeep and capture stop too.
	pub fn stop(&self) {
		self.shutdown.store(true, Ordering::SeqCst);
		self.wake.notify_waiters();

		if let Some(maintenance) = &self.maintenance {
			maintenance.stop();
		}

		#[cfg(feature = "postgres")]
		if let Some(capture) = &self.capture {
			capture.stop();
		}
	}

	/// What this instance's replication capture has done, with the
	/// `postgres` feature.
	#[cfg(feature = "postgres")]
	pub fn capture_stats(&self) -> Option<leptoskit_db::live::CaptureStats> {
		self.capture
			.as_ref()
			.map(leptoskit_db::live::Capture::stats)
	}

	/// Stops the workers and waits for the jobs they hold to finish.
	pub async fn shutdown(self) {
		self.stop();

		for task in self.tasks {
			let _ = task.await;
		}

		if let Some(maintenance) = self.maintenance {
			maintenance.shutdown().await;
		}

		#[cfg(feature = "postgres")]
		if let Some(capture) = self.capture {
			capture.shutdown().await;
		}
	}
}

/// Wakes idle workers when the jobs table changes on any instance: an
/// insert there is a job to run. The table is live, so the change arrives
/// over Postgres `NOTIFY`, or in process on SQLite.
async fn waker(db: Db, wake: Arc<Notify>, shutdown: Arc<AtomicBool>) {
	let Ok(mut feed) = db.system("wake job workers").changes().await else {
		// Without a feed the workers poll, which is slower but complete.
		return;
	};

	let table = match JobRow::NAMESPACE {
		Some(namespace) => format!("{namespace}.{}", JobRow::NAME),
		None => JobRow::NAME.to_owned(),
	};

	while !shutdown.load(Ordering::SeqCst) {
		let changed = tokio::select! {
			changed = feed.next() => changed,
			() = tokio::time::sleep(Duration::from_secs(1)) => continue,
		};

		let wakes = match changed {
			Ok(leptoskit_db::live::Invalidation::Rows(rows)) => {
				rows.iter().any(|rows| rows.table == table)
			}
			Ok(leptoskit_db::live::Invalidation::Everything) | Err(_) => true,
		};

		if wakes {
			wake.notify_waiters();
		}
	}
}

async fn worker(
	db: Db,
	settings: Settings,
	shutdown: Arc<AtomicBool>,
	wake: Arc<Notify>,
	poll: Duration,
	retention: Option<Duration>,
) {
	let mut pruned_at: Option<std::time::Instant> = None;

	while !shutdown.load(Ordering::SeqCst) {
		// Ask to be woken before looking, so an enqueue between the look
		// and the wait isn't missed.
		let woken = wake.notified();
		tokio::pin!(woken);
		woken.as_mut().enable();

		match run_one(&db, &settings, true).await {
			Ok(true) => continue,
			Ok(false) => {}
			Err(_) => {
				tokio::time::sleep(Duration::from_secs(1)).await;
				continue;
			}
		}

		let _ = sweep_failures(&db).await;

		if let Some(retention) = retention
			&& pruned_at.is_none_or(|at| at.elapsed() >= Duration::from_hours(1))
		{
			pruned_at = Some(std::time::Instant::now());
			let _ = prune(&db, retention).await;
		}

		if shutdown.load(Ordering::SeqCst) {
			break;
		}

		tokio::select! {
			() = woken => {}
			() = tokio::time::sleep(poll) => {}
		}
	}
}

/// Claims one due job and runs it. Whether one ran.
pub(crate) async fn run_one(
	db: &Db,
	settings: &Settings,
	heartbeat: bool,
) -> Result<bool, DbError> {
	let Some(row) = claim(db, settings).await? else {
		return Ok(false);
	};
	execute(db, settings, row, heartbeat).await;
	Ok(true)
}

/// The lease a job of `name` gets: the workers' own, or its timeout and a
/// margin, whichever is longer.
fn lease_for(settings: &Settings, name: &str) -> Duration {
	let timeout = job_registration(name).and_then(|registration| registration.options.timeout);
	timeout.map_or(settings.lease, |timeout| {
		settings.lease.max(timeout + Duration::from_secs(30))
	})
}

/// Takes the first due job this worker may run: waiting ones whose time has
/// come, and running ones whose worker's lease ran out.
async fn claim(db: &Db, settings: &Settings) -> Result<Option<JobRow>, DbError> {
	let system = db.system("claim jobs");
	let now = settings.clock.now();
	let candidates = JobRow::query()
		.filter(|job| {
			(job.state.eq(JobState::Pending) & job.run_at.lte(now))
				| (job.state.eq(JobState::Running) & job.locked_until.lt(now))
		})
		.order_by(|job| job.priority.desc())
		.order_by(|job| job.run_at.asc())
		.limit(32)
		.all(&system)
		.await?;

	for row in candidates {
		let lease = lease_for(settings, &row.name);
		let limit = settings.queues.get(&row.queue).copied();
		let taken = if limit.is_none() && row.serial_key.is_none() {
			take(&system, &row, now, lease).await?
		} else {
			match guarded_take(&system, &row, now, lease, limit).await {
				Ok(taken) => taken,
				// Another worker claimed in the same queue or series at the
				// same moment; one of the two wins, and this tries the next.
				Err(error) if error.is_contended() => false,
				Err(error) => return Err(error),
			}
		};

		if taken {
			let mut claimed = row;
			claimed.state = JobState::Running;
			claimed.attempts += 1;
			claimed.started_at = Some(now);
			claimed.locked_until = Some(now + duration(lease));

			return Ok(Some(claimed));
		}
	}

	Ok(None)
}

/// Marks `row` running for this worker, if nobody else did first: the
/// attempt count is the fence, so a worker that lost its lease can't record
/// over the one that took the job over.
async fn take(
	connection: &impl Connection,
	row: &JobRow,
	now: Timestamp,
	lease: Duration,
) -> Result<bool, DbError> {
	let (id, state, attempts) = (row.id, row.state, row.attempts);
	let changed = JobRow::update()
		.filter(|job| job.id.eq(id) & job.state.eq(state) & job.attempts.eq(attempts))
		.set(|job| job.state, JobState::Running)
		.set(|job| job.attempts, attempts + 1)
		.set(|job| job.started_at, now)
		.set(|job| job.locked_until, now + duration(lease))
		.run(connection)
		.await?;
	Ok(changed == 1)
}

/// Takes `row` only if its queue is under `limit` and no other job in its
/// series runs, in one serializable transaction: two workers that each see
/// room and each take a job conflict, and Postgres makes one of them fail,
/// so the limit holds across instances. SQLite has one writer, so it holds
/// there already.
async fn guarded_take(
	system: &Db,
	row: &JobRow,
	now: Timestamp,
	lease: Duration,
	limit: Option<usize>,
) -> Result<bool, DbError> {
	let tx = system.begin_serializable().await?;
	let id = row.id;

	let room = match limit {
		Some(limit) => {
			let queue = row.queue.clone();
			let running = JobRow::query()
				.filter(|job| {
					job.queue.eq(queue)
						& job.state.eq(JobState::Running)
						& job.locked_until.gte(now)
						& job.id.ne(id)
				})
				.count(&tx)
				.await?;
			usize::try_from(running).unwrap_or(usize::MAX) < limit
		}
		None => true,
	};

	let free = match (&row.serial_key, room) {
		(Some(serial), true) => {
			let serial = serial.clone();
			!JobRow::query()
				.filter(|job| {
					job.serial_key.eq(serial)
						& job.state.eq(JobState::Running)
						& job.locked_until.gte(now)
						& job.id.ne(id)
				})
				.exists(&tx)
				.await?
		}
		(_, room) => room,
	};

	if !free || !take(&tx, row, now, lease).await? {
		tx.rollback().await?;

		return Ok(false);
	}

	tx.commit().await?;
	Ok(true)
}

/// How one run ended.
enum Outcome {
	Done(serde_json::Value),
	/// Its worker died, as far as anyone can tell: leave the row leased.
	Abandoned,
	Cancelled(String),
	Failed {
		error: String,
		permanent: bool,
	},
}

/// Runs a claimed job and records how it went.
async fn execute(db: &Db, settings: &Settings, row: JobRow, heartbeat: bool) {
	let owner = owner_actor(&row.owner);
	let context = owner_context(owner);
	let system = db.with_context(context.clone()).system("run jobs");
	let identity = Identity::new(context).with_caller(format!("job:{}", row.id));
	let now = settings.clock.now();
	let state = Arc::new(RunState::new(
		row.steps.0.clone(),
		row.cancel_requested,
		Progress {
			text: row.progress.clone(),
			fraction: row.fraction,
			dirty: false,
		},
		settings.faults.clone(),
	));
	let registration = job_registration(&row.name);

	let outcome = if let Some(outcome) = skip_reason(&row, now) {
		outcome
	} else if let Some(registration) = registration {
		run_handler(
			registration,
			ClaimedJob {
				system: system.clone(),
				identity,
				row: &row,
				owner,
				settings,
				heartbeat,
				state: &state,
			},
		)
		.await
	} else {
		// Another instance, on a newer build, may know it.
		Outcome::Failed {
			error: format!("no job is named `{}`; is its crate linked?", row.name),
			permanent: false,
		}
	};

	let _ = record(&system, settings, &row, &state, outcome).await;
}

/// Why a claimed job doesn't run, if it doesn't: it was cancelled, it
/// passed its deadline, or it ran its last attempt.
fn skip_reason(row: &JobRow, now: Timestamp) -> Option<Outcome> {
	if row.cancel_requested {
		return Some(Outcome::Cancelled("the job was cancelled".to_owned()));
	}

	if row.deadline_at.is_some_and(|deadline| deadline < now) {
		return Some(Outcome::Failed {
			error: "the job passed its deadline".to_owned(),
			permanent: true,
		});
	}

	if row.attempts > row.max_attempts {
		return Some(Outcome::Failed {
			error: row
				.error
				.clone()
				.unwrap_or_else(|| "the worker running its last attempt stopped".to_owned()),
			permanent: true,
		});
	}

	None
}

/// Everything one claimed job's run needs.
struct ClaimedJob<'a> {
	system: Db,
	identity: Identity,
	row: &'a JobRow,
	owner: Option<Actor>,
	settings: &'a Settings,
	heartbeat: bool,
	state: &'a Arc<RunState>,
}

/// Runs a claimed job's handler, with its timeout and heartbeat, and says
/// how it went.
async fn run_handler(registration: &'static JobRegistration, job: ClaimedJob<'_>) -> Outcome {
	let cx = JobCx::for_run(
		job.system.clone(),
		job.identity,
		job.row.id,
		job.row.attempts,
		job.owner,
		Arc::clone(job.state),
	);

	let running = (registration.handler)(JobRun {
		cx,
		arguments: job.row.arguments.0.clone(),
	});
	let beat = job.heartbeat.then(|| {
		tokio::spawn(beat(
			job.system.clone(),
			job.row.id,
			job.row.attempts,
			lease_for(job.settings, &job.row.name),
			job.settings.clock.clone(),
			Arc::clone(job.state),
		))
	});

	let result = match registration.options.timeout {
		Some(timeout) => {
			tokio::time::timeout(timeout, running)
				.await
				.unwrap_or_else(|_| {
					Err(Error::retry(format!(
						"the job ran longer than its timeout of {timeout:?}"
					)))
				})
		}
		None => running.await,
	};

	if let Some(beat) = beat {
		beat.abort();
	}

	match result {
		_ if job.state.is_killed() => Outcome::Abandoned,
		Ok(value) => Outcome::Done(value),

		Err(error) if job.state.is_cancelled() => Outcome::Cancelled(error.message().to_owned()),
		Err(error) => {
			Outcome::Failed {
				permanent: error.is_permanent(),
				error: error.message().to_owned(),
			}
		}
	}
}

/// Keeps a running job's lease, writes its progress a few times a second,
/// and notices when someone cancels it.
async fn beat(
	db: Db,
	id: leptoskit_db::Id<JobRow>,
	attempt: u32,
	lease: Duration,
	clock: Clock,
	state: Arc<RunState>,
) {
	let renew_every = (lease / 3).max(PROGRESS_EVERY);
	let mut renewed = std::time::Instant::now();

	loop {
		tokio::time::sleep(PROGRESS_EVERY).await;
		let renew = renewed.elapsed() >= renew_every;
		let progress = state.take_progress();

		if !renew && progress.is_none() {
			continue;
		}

		let mut update = JobRow::update().filter(|job| {
			job.id.eq(id) & job.state.eq(JobState::Running) & job.attempts.eq(attempt)
		});

		if renew {
			renewed = std::time::Instant::now();
			update = update.set(|job| job.locked_until, clock.now() + duration(lease));
		}

		if let Some(progress) = progress {
			update = update
				.set(|job| job.progress, progress.text)
				.set(|job| job.fraction, progress.fraction);
		}

		let _ = update.run(&db).await;

		if renew
			&& let Ok(true) = JobRow::query()
				.filter(|job| job.id.eq(id) & job.cancel_requested.eq(true))
				.exists(&db)
				.await
		{
			state.cancel();
		}
	}
}

/// Writes how a run went, if this worker still holds the job.
async fn record(
	system: &Db,
	settings: &Settings,
	row: &JobRow,
	state: &RunState,
	outcome: Outcome,
) -> Result<(), DbError> {
	let now = settings.clock.now();
	let (id, attempt) = (row.id, row.attempts);
	let progress = state.progress();
	let update = JobRow::update()
		.filter(|job| job.id.eq(id) & job.state.eq(JobState::Running) & job.attempts.eq(attempt))
		.set(|job| job.locked_until, None::<Timestamp>)
		.set(|job| job.progress, progress.text)
		.set(|job| job.fraction, progress.fraction);

	let update = match outcome {
		Outcome::Abandoned => return Ok(()),
		Outcome::Done(value) => {
			update
				.set(|job| job.state, JobState::Done)
				.set(|job| job.result, Some(Json(value)))
				.set(|job| job.error, None::<String>)
				.set(|job| job.finished_at, now)
		}
		Outcome::Cancelled(error) => {
			update
				.set(|job| job.state, JobState::Cancelled)
				.set(|job| job.error, truncate(error))
				.set(|job| job.finished_at, now)
		}
		Outcome::Failed { error, permanent } => {
			let backoff = job_registration(&row.name)
				.map(|registration| registration.options.backoff)
				.unwrap_or_default();
			let random =
				std::collections::hash_map::RandomState::new().hash_one(row.id.to_string());
			let retry_at = now + duration(backoff.delay(row.attempts, random));
			let gives_up = permanent
				|| row.attempts >= row.max_attempts
				|| row.deadline_at.is_some_and(|deadline| retry_at > deadline);
			if gives_up {
				let on_failure = job_registration(&row.name)
					.is_some_and(|registration| registration.on_failure.is_some());
				update
					.set(|job| job.state, JobState::Failed)
					.set(|job| job.error, truncate(error))
					.set(|job| job.finished_at, now)
					.set(|job| job.failure_pending, on_failure)
			} else {
				update
					.set(|job| job.state, JobState::Pending)
					.set(|job| job.error, truncate(error))
					.set(|job| job.run_at, retry_at)
			}
		}
	};

	update.run(system).await.map(|_| ())
}

/// An error short enough for its column.
fn truncate(mut error: String) -> String {
	if error.len() > 2000 {
		let mut end = 2000;

		while !error.is_char_boundary(end) {
			end -= 1;
		}

		error.truncate(end);
	}

	error
}

/// Runs the `on_failure` mutation of jobs that gave up. Each runs once:
/// its receipt is keyed by the job, so two sweeps at once apply it once.
/// How many ran.
pub(crate) async fn sweep_failures(db: &Db) -> Result<usize, DbError> {
	let system = db.system("run jobs' on_failure");
	let failed = JobRow::query()
		.filter(|job| job.state.eq(JobState::Failed) & job.failure_pending.eq(true))
		.limit(16)
		.all(&system)
		.await?;
	let mut ran = 0;

	for row in failed {
		let id = row.id;
		let handler = job_registration(&row.name).and_then(|registration| registration.on_failure);

		let done = match handler {
			None => true,
			Some(handler) => {
				let owner = owner_actor(&row.owner);
				let context = owner_context(owner);
				let result = handler(FailureRun {
					db: db
						.with_context(context.clone())
						.system("run a job's on_failure"),
					identity: Identity::new(context).with_caller(format!("job:{id}")),
					id: id.to_string(),
					arguments: row.arguments.0,
					error: row.error.unwrap_or_default(),
					attempts: row.attempts,
				})
				.await;
				ran += 1;
				// A mutation that can't succeed, such as one whose arguments no
				// longer fit, isn't tried forever.
				result.is_ok()
					|| result.as_ref().is_err_and(|error| {
						error.is_permanent() || matches!(error, Error::Invalid(_))
					})
			}
		};

		if done {
			JobRow::update()
				.filter(|job| job.id.eq(id) & job.failure_pending.eq(true))
				.set(|job| job.failure_pending, false)
				.run(&system)
				.await?;
		}
	}

	Ok(ran)
}

/// Deletes finished and cancelled jobs that finished more than `age` ago,
/// freeing their keys. Failed jobs stay until someone retries them. How
/// many it deleted.
///
/// # Errors
///
/// When the database fails.
pub async fn prune(db: &Db, age: Duration) -> Result<u64, DbError> {
	prune_at(db, leptoskit_db::now(), age).await
}

pub(crate) async fn prune_at(db: &Db, now: Timestamp, age: Duration) -> Result<u64, DbError> {
	let cutoff = now
		.checked_sub_signed(duration(age))
		.unwrap_or(chrono::DateTime::<chrono::Utc>::MIN_UTC);
	JobRow::query()
		.filter(|job| {
			(job.state.eq(JobState::Done) | job.state.eq(JobState::Cancelled))
				& job.finished_at.lt(cutoff)
		})
		.delete(&db.system("prune finished jobs"))
		.await
}

/// The most ticks of one schedule enqueued at once, so a clock that jumped
/// far doesn't flood the queue.
const MOST_TICKS: usize = 1_000;

/// Enqueues each `#[rpc::cron]` job for every tick in `(after, until]`,
/// keyed by the tick, so however many instances do the same, each tick's
/// job is enqueued once. How many this call enqueued.
pub(crate) async fn enqueue_cron(
	db: &Db,
	after: Timestamp,
	until: Timestamp,
) -> crate::Result<usize> {
	let system = db.system("enqueue cron jobs");
	let mut enqueued = 0;

	for registration in inventory::iter::<CronRegistration>() {
		let mut from = after;

		for _ in 0..MOST_TICKS {
			let Some(tick) = registration.schedule.next_after(from) else {
				break;
			};

			if tick > until {
				break;
			}

			from = tick;
			let key = format!("cron:{}:{}", registration.name, tick.to_rfc3339());

			if (registration.enqueue)(system.clone(), key, tick).await? {
				enqueued += 1;
			}
		}
	}

	Ok(enqueued)
}

/// Enqueues cron jobs as their ticks come.
async fn scheduler(db: Db, clock: Clock, wake: Arc<Notify>, shutdown: Arc<AtomicBool>) {
	if inventory::iter::<CronRegistration>().next().is_none() {
		return;
	}

	let mut last = clock.now();

	while !shutdown.load(Ordering::SeqCst) {
		let next = inventory::iter::<CronRegistration>()
			.filter_map(|registration| registration.schedule.next_after(last))
			.min();
		let wait = next
			.and_then(|next| (next - clock.now()).to_std().ok())
			.unwrap_or_default()
			.min(Duration::from_secs(1));
		tokio::time::sleep(wait).await;
		let now = clock.now();

		if enqueue_cron(&db, last, now).await.is_ok() {
			last = now;
			wake.notify_waiters();
		}
	}
}