coven_database/
internal_schema_retry_effect.rs1use crate::sqlite::StoreSql;
23use crate::stored_millis::{since_epoch, stored_time, Round};
24use crate::DbError;
25use std::time::SystemTime;
26
27pub(crate) const RETRY_EFFECT: &str = "coven_retry_effect";
29
30pub(super) const TABLE: &str = "CREATE TABLE coven_retry_effect (
32 singleton INTEGER PRIMARY KEY NOT NULL CHECK(singleton=1),
33 syncing INTEGER NOT NULL CHECK(syncing IN (0,1)),
34 held INTEGER NOT NULL CHECK(held IN (0,1)),
35 cooling_until INTEGER,
36 effect TEXT NOT NULL GENERATED ALWAYS AS (CASE
37 WHEN syncing=0 THEN 'not_syncing'
38 WHEN held=1 THEN 'failed'
39 WHEN cooling_until IS NOT NULL THEN 'cooling_down'
40 ELSE 'pass' END) STORED,
41 CHECK(syncing=1 OR cooling_until IS NULL)
42 ) STRICT;
43 INSERT INTO coven_retry_effect(singleton,syncing,held,cooling_until) VALUES(1,0,0,NULL);";
44
45#[derive(Clone, Copy, Debug, PartialEq, Eq)]
47pub struct RetryState {
48 pub sync: RetrySync,
51 pub held: bool,
54}
55
56#[derive(Clone, Copy, Debug, PartialEq, Eq)]
58pub enum RetrySync {
59 Stopped,
61 Started {
63 cooling_until: Option<SystemTime>,
66 },
67}
68
69impl RetryState {
70 pub(crate) const OPENED: Self = Self { sync: RetrySync::Stopped, held: false };
73}
74
75fn millis(time: SystemTime) -> Result<i64, DbError> {
79 since_epoch(time, Round::Up).ok_or(DbError::Invariant {
80 what: "a cooldown ends between the Unix epoch and SQLite's last millisecond",
81 })
82}
83
84pub(crate) fn read(db: &StoreSql) -> Result<RetryState, DbError> {
86 let (syncing, held, cooling_until) = db.query_row(
87 "SELECT syncing,held,cooling_until FROM coven_retry_effect WHERE singleton=1",
88 [],
89 |row| Ok((row.get::<_, bool>(0)?, row.get::<_, bool>(1)?, row.get::<_, Option<i64>>(2)?)),
90 )?;
91 Ok(RetryState { sync: sync(syncing, cooling_until)?, held })
92}
93
94pub(crate) fn sync(syncing: bool, cooling_until: Option<i64>) -> Result<RetrySync, DbError> {
97 Ok(if syncing {
98 RetrySync::Started { cooling_until: cooling_until.map(stored_time).transpose()? }
99 } else {
100 RetrySync::Stopped
101 })
102}
103
104pub(crate) fn save(db: &StoreSql, state: RetryState) -> Result<(), DbError> {
107 let held = state.held;
108 let (syncing, cooling_until) = match state.sync {
109 RetrySync::Stopped => (false, None),
110 RetrySync::Started { cooling_until } => (true, cooling_until.map(millis).transpose()?),
111 };
112 db.execute(
113 "UPDATE coven_retry_effect SET syncing=?1,held=?2,cooling_until=?3
114 WHERE singleton=1 AND (syncing IS NOT ?1 OR held IS NOT ?2 OR cooling_until IS NOT ?3)",
115 sqlive::rusqlite::params![syncing, held, cooling_until],
116 )?;
117 Ok(())
118}
119
120pub(crate) fn stored(state: RetryState) -> Result<RetryState, DbError> {
123 let sync = match state.sync {
124 RetrySync::Stopped => RetrySync::Stopped,
125 RetrySync::Started { cooling_until } => RetrySync::Started {
126 cooling_until: cooling_until
127 .map(|until| millis(until).and_then(stored_time))
128 .transpose()?,
129 },
130 };
131 Ok(RetryState { sync, ..state })
132}
133
134#[cfg(test)]
135#[path = "internal_schema_retry_effect_tests.rs"]
136mod tests;