Skip to main content

appcore_contracts/policy/
job.rs

1// =============================================================================
2//        #######
3//     ###       ###     F: job.rs
4//    ##   ## ##   ##    P: AppCore-Runtime
5//         ## ##
6//                       C: 2026/07/22 15:41:18 by dnettoRaw
7//    ##   ## ##   ##    U: 2026/07/22 15:41:18 by dnettoRaw
8//      ###########      S: 1.0.1-rc.8
9// =============================================================================
10
11use super::*;
12
13/// Policy for jobs declared by an application.
14#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
15pub struct JobPolicy {
16    enabled: bool,
17    max_concurrency: u32,
18    retry_limit: u32,
19}
20
21impl JobPolicy {
22    /// Creates a job policy.
23    pub fn new(enabled: bool, max_concurrency: u32, retry_limit: u32) -> ContractResult<Self> {
24        if enabled && max_concurrency == 0 {
25            return Err(ContractError::InvalidValue {
26                field: "jobs.max_concurrency",
27                reason: "must be greater than zero when jobs are enabled",
28            });
29        }
30        Ok(Self {
31            enabled,
32            max_concurrency,
33            retry_limit,
34        })
35    }
36
37    /// Returns a policy that disables distributed jobs.
38    pub fn disabled() -> Self {
39        Self {
40            enabled: false,
41            max_concurrency: 0,
42            retry_limit: 0,
43        }
44    }
45
46    /// Reports whether jobs are enabled.
47    pub fn is_enabled(&self) -> bool {
48        self.enabled
49    }
50
51    /// Returns the maximum number of concurrent jobs.
52    pub fn max_concurrency(&self) -> u32 {
53        self.max_concurrency
54    }
55
56    /// Returns the retry limit for failed jobs.
57    pub fn retry_limit(&self) -> u32 {
58        self.retry_limit
59    }
60
61    pub(crate) fn validate(&self) -> ContractResult<()> {
62        if self.enabled && self.max_concurrency == 0 {
63            return Err(ContractError::InvalidValue {
64                field: "jobs.max_concurrency",
65                reason: "must be greater than zero when jobs are enabled",
66            });
67        }
68        Ok(())
69    }
70}