stackless_core/state/
journal.rs1use super::error::StateError;
5use super::row::Row;
6use super::store::Store;
7
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub struct Checkpoint {
10 pub instance: String,
11 pub step_id: String,
12 pub resource_kind: String,
13 pub resource_id: String,
14 pub payload: String,
16 pub recorded_at: i64,
17}
18
19impl TryFrom<&Row> for Checkpoint {
20 type Error = StateError;
21
22 fn try_from(row: &Row) -> Result<Self, Self::Error> {
23 Ok(Self {
24 instance: row.get_string(0)?,
25 step_id: row.get_string(1)?,
26 resource_kind: row.get_string(2)?,
27 resource_id: row.get_string(3)?,
28 payload: row.get_string(4)?,
29 recorded_at: row.get_i64(5)?,
30 })
31 }
32}
33
34impl Store {
35 pub fn record_checkpoint(
36 &self,
37 instance: &str,
38 step_id: &str,
39 resource_kind: &str,
40 resource_id: &str,
41 payload: &str,
42 ) -> Result<(), StateError> {
43 let owner = self
44 .instance(instance)?
45 .ok_or_else(|| StateError::InstanceNotFound {
46 name: instance.into(),
47 })?;
48 self.remember_payload_secrets(&owner.instance_id, payload)?;
49 self.execute(
50 "INSERT INTO checkpoints (instance, step_id, resource_kind, resource_id, payload, recorded_at)
51 VALUES (?1, ?2, ?3, ?4, ?5, ?6)
52 ON CONFLICT(instance, step_id) DO UPDATE SET
53 resource_kind = excluded.resource_kind,
54 resource_id = excluded.resource_id,
55 payload = excluded.payload,
56 recorded_at = excluded.recorded_at",
57 &[
58 instance.into(),
59 step_id.into(),
60 resource_kind.into(),
61 resource_id.into(),
62 payload.into(),
63 Self::now().into(),
64 ],
65 )?;
66 Ok(())
67 }
68
69 pub fn checkpoint(
70 &self,
71 instance: &str,
72 step_id: &str,
73 ) -> Result<Option<Checkpoint>, StateError> {
74 self.query_row(
75 "SELECT instance, step_id, resource_kind, resource_id, payload, recorded_at
76 FROM checkpoints WHERE instance = ?1 AND step_id = ?2",
77 &[instance.into(), step_id.into()],
78 |row| Checkpoint::try_from(row),
79 )
80 }
81
82 pub fn checkpoints(&self, instance: &str) -> Result<Vec<Checkpoint>, StateError> {
85 self.query_map(
86 "SELECT instance, step_id, resource_kind, resource_id, payload, recorded_at
87 FROM checkpoints WHERE instance = ?1 ORDER BY rowid",
88 &[instance.into()],
89 |row| Checkpoint::try_from(row),
90 )
91 }
92
93 pub fn remove_checkpoint(&self, instance: &str, step_id: &str) -> Result<(), StateError> {
95 self.execute(
96 "DELETE FROM checkpoints WHERE instance = ?1 AND step_id = ?2",
97 &[instance.into(), step_id.into()],
98 )?;
99 Ok(())
100 }
101}