holochain_zome_types/
schedule.rs

1use crate::prelude::*;
2use std::time::Duration;
3
4/// Tick the scheduler every this many millis.
5pub const SCHEDULER_INTERVAL: Duration = Duration::from_millis(100);
6
7/// Expire persisted schedules after this long.
8pub const PERSISTED_TIMEOUT: Duration = Duration::from_millis(20000);
9
10/// Scheduling errors.
11#[derive(Debug, thiserror::Error)]
12pub enum ScheduleError {
13    /// Something went wrong, probably parsing a cron tab.
14    #[error("{0}")]
15    Cron(String),
16    /// Timestamp issues.
17    #[error(transparent)]
18    Timestamp(crate::timestamp::TimestampError),
19}
20
21/// Defines either a persisted or ephemeral schedule for a schedule function.
22/// Persisted schedules survive a conductor reboot, ephemeral will not.
23/// Persisted schedules continue beyond irrecoverable errors, ephemeral do not.
24#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
25pub enum Schedule {
26    /// Persisted schedules are defined by a crontab syntax string.
27    Persisted(String),
28    /// Ephemeral schedules are defined by a Duration.
29    Ephemeral(Duration),
30}
31
32impl From<String> for Schedule {
33    fn from(cron: String) -> Self {
34        Self::Persisted(cron)
35    }
36}
37
38impl From<Duration> for Schedule {
39    fn from(timeout: Duration) -> Self {
40        Self::Ephemeral(timeout)
41    }
42}
43
44/// A fully qualified scheduled function.
45#[derive(Debug, Clone, PartialEq)]
46pub struct ScheduledFn(ZomeName, FunctionName);
47
48impl ScheduledFn {
49    /// Constructor.
50    pub fn new(zome_name: ZomeName, fn_name: FunctionName) -> Self {
51        Self(zome_name, fn_name)
52    }
53
54    /// ZomeName accessor.
55    pub fn zome_name(&self) -> &ZomeName {
56        &self.0
57    }
58
59    /// Function name accessor.
60    pub fn fn_name(&self) -> &FunctionName {
61        &self.1
62    }
63}