1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use runledger_core::jobs::{WorkflowDagBuilder, WorkflowRunEnqueue};
use serde_json::Value;
use uuid::Uuid;
use super::{CatalogError, JobCatalog};
/// Workflow DAG builder that validates job types against a [`JobCatalog`].
#[derive(Debug, Clone)]
pub struct CatalogWorkflowDagBuilder<'a, 'catalog> {
pub(super) catalog: &'catalog JobCatalog,
pub(super) inner: WorkflowDagBuilder<'a>,
}
impl JobCatalog {
/// Starts a workflow DAG builder that validates step job types against the catalog.
#[must_use]
pub fn workflow_dag<'a>(
&self,
workflow_type: &'a str,
metadata: &'a Value,
) -> CatalogWorkflowDagBuilder<'a, '_> {
CatalogWorkflowDagBuilder {
catalog: self,
inner: WorkflowDagBuilder::new(workflow_type, metadata),
}
}
}
impl<'a, 'catalog> CatalogWorkflowDagBuilder<'a, 'catalog> {
/// Sets the organization scope for the workflow run and its steps by default.
#[must_use]
pub fn organization_id(mut self, organization_id: Uuid) -> Self {
self.inner = self.inner.organization_id(organization_id);
self
}
/// Clears the workflow-level organization scope.
#[must_use]
pub fn clear_organization_id(mut self) -> Self {
self.inner = self.inner.clear_organization_id();
self
}
/// Sets the workflow idempotency key.
#[must_use]
pub fn idempotency_key(mut self, idempotency_key: &'a str) -> Self {
self.inner = self.inner.idempotency_key(idempotency_key);
self
}
/// Clears the workflow idempotency key.
#[must_use]
pub fn clear_idempotency_key(mut self) -> Self {
self.inner = self.inner.clear_idempotency_key();
self
}
/// Adds a job step after validating `job_type_name` against enabled catalog entries.
///
/// # Errors
/// Returns [`CatalogError`] when the job type is unknown or disabled, or when
/// the underlying workflow builder rejects the step.
pub fn job(
mut self,
step_key: &'a str,
job_type_name: &str,
payload: &'a Value,
) -> Result<Self, CatalogError> {
let job_type = self
.catalog
.require_catalog_enabled_job_type(job_type_name)?;
self.inner = self
.inner
.job(step_key, job_type.as_str(), payload)
.map_err(CatalogError::WorkflowBuild)?;
Ok(self)
}
/// Adds success dependencies to an existing workflow step.
///
/// # Errors
/// Returns [`CatalogError::WorkflowBuild`] when the underlying workflow
/// builder rejects the dependency edge.
pub fn after_success<I>(self, step_key: &'a str, prerequisites: I) -> Result<Self, CatalogError>
where
I: IntoIterator<Item = &'a str>,
{
self.inner
.after_success(step_key, prerequisites)
.map_err(CatalogError::WorkflowBuild)
.map(|inner| Self {
catalog: self.catalog,
inner,
})
}
/// Adds terminal-state dependencies to an existing workflow step.
///
/// # Errors
/// Returns [`CatalogError::WorkflowBuild`] when the underlying workflow
/// builder rejects the dependency edge.
pub fn after_terminal<I>(
self,
step_key: &'a str,
prerequisites: I,
) -> Result<Self, CatalogError>
where
I: IntoIterator<Item = &'a str>,
{
self.inner
.after_terminal(step_key, prerequisites)
.map_err(CatalogError::WorkflowBuild)
.map(|inner| Self {
catalog: self.catalog,
inner,
})
}
/// Builds the workflow enqueue payload.
///
/// This is an alias for [`Self::try_build`].
///
/// # Errors
/// Returns [`CatalogError::WorkflowBuild`] when final workflow validation fails.
pub fn build(self) -> Result<WorkflowRunEnqueue<'a>, CatalogError> {
self.try_build()
}
/// Builds the workflow enqueue payload.
///
/// # Errors
/// Returns [`CatalogError::WorkflowBuild`] when final workflow validation fails.
pub fn try_build(self) -> Result<WorkflowRunEnqueue<'a>, CatalogError> {
self.inner.try_build().map_err(CatalogError::WorkflowBuild)
}
}