Skip to main content

coil_runtime/jobs/
request.rs

1use super::super::*;
2use super::RuntimeJobsError;
3use super::helpers::validate_runtime_identifier;
4
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub struct JobDispatchRequest {
7    pub job_name: String,
8    pub payload_description: String,
9    pub scheduled_for: Option<JobInstant>,
10    pub idempotency_key: Option<String>,
11}
12
13impl JobDispatchRequest {
14    pub fn new(
15        job_name: impl Into<String>,
16        payload_description: impl Into<String>,
17    ) -> Result<Self, RuntimeJobsError> {
18        let job_name = validate_runtime_identifier("job_name", job_name.into())?;
19        let payload_description =
20            validate_runtime_identifier("payload_description", payload_description.into())?;
21
22        Ok(Self {
23            job_name,
24            payload_description,
25            scheduled_for: None,
26            idempotency_key: None,
27        })
28    }
29
30    pub fn scheduled_for(mut self, instant: JobInstant) -> Self {
31        self.scheduled_for = Some(instant);
32        self
33    }
34
35    pub fn with_idempotency_key(
36        mut self,
37        key: impl Into<String>,
38    ) -> Result<Self, RuntimeJobsError> {
39        self.idempotency_key = Some(validate_runtime_identifier("idempotency_key", key.into())?);
40        Ok(self)
41    }
42}
43
44#[derive(Debug, Clone, PartialEq, Eq)]
45pub struct DomainEventDispatchRequest {
46    pub event_type: String,
47    pub aggregate_kind: String,
48    pub aggregate_id: String,
49    pub payload_description: String,
50    pub correlation_id: Option<String>,
51    pub causation_id: Option<String>,
52}
53
54impl DomainEventDispatchRequest {
55    pub fn new(
56        event_type: impl Into<String>,
57        aggregate_kind: impl Into<String>,
58        aggregate_id: impl Into<String>,
59        payload_description: impl Into<String>,
60    ) -> Result<Self, RuntimeJobsError> {
61        Ok(Self {
62            event_type: validate_runtime_identifier("event_type", event_type.into())?,
63            aggregate_kind: validate_runtime_identifier("aggregate_kind", aggregate_kind.into())?,
64            aggregate_id: validate_runtime_identifier("aggregate_id", aggregate_id.into())?,
65            payload_description: validate_runtime_identifier(
66                "payload_description",
67                payload_description.into(),
68            )?,
69            correlation_id: None,
70            causation_id: None,
71        })
72    }
73
74    pub fn with_correlation_id(
75        mut self,
76        correlation_id: impl Into<String>,
77    ) -> Result<Self, RuntimeJobsError> {
78        self.correlation_id = Some(validate_runtime_identifier(
79            "correlation_id",
80            correlation_id.into(),
81        )?);
82        Ok(self)
83    }
84
85    pub fn with_causation_id(
86        mut self,
87        causation_id: impl Into<String>,
88    ) -> Result<Self, RuntimeJobsError> {
89        self.causation_id = Some(validate_runtime_identifier(
90            "causation_id",
91            causation_id.into(),
92        )?);
93        Ok(self)
94    }
95}