Skip to content

Commit c547568

Browse files
committed
API changes.
1 parent 6447294 commit c547568

File tree

6 files changed

+67
-29
lines changed

6 files changed

+67
-29
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ version = "0.1.0-alpha.1"
44
edition = "2021"
55

66
[dependencies]
7+
anyhow = "1"
78
ctrlc-async = "3"
89
derive_builder = "0.20"
910
envy = "0.4"

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ enum ClientTlStrategy {
1616

1717
pub use client::Client;
1818
pub use worker::{Worker, WorkerBuilder};
19-
pub use workflow::{Workflow, WorkflowBuilder};
19+
pub use workflow::{Step, StepBuilder, Workflow, WorkflowBuilder};
2020

2121
#[cfg(test)]
2222
mod tests;

src/worker/listener.rs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -73,30 +73,32 @@ where
7373
.map_err(crate::Error::CouldNotDecodeActionPayload)?;
7474

7575
// FIXME: Obviously, run this asynchronously rather than blocking the main listening loop.
76-
match tokio::task::spawn_local(async move { action_callable(input).await }).await {
77-
Ok(output_value) => {
78-
dispatcher
79-
.send_step_action_event(step_action_event(
80-
worker_id,
81-
&action,
82-
StepActionEventType::StepEventTypeCompleted,
83-
serde_json::to_string(&output_value).expect("must succeed"),
84-
))
85-
.await?
86-
.into_inner();
87-
}
88-
Err(join_error) => {
89-
dispatcher
90-
.send_step_action_event(step_action_event(
91-
worker_id,
92-
&action,
93-
StepActionEventType::StepEventTypeFailed,
94-
join_error.to_string(),
95-
))
96-
.await?
97-
.into_inner();
98-
}
99-
}
76+
let action_event =
77+
match tokio::task::spawn_local(async move { action_callable(input).await }).await {
78+
Ok(Ok(output_value)) => step_action_event(
79+
worker_id,
80+
&action,
81+
StepActionEventType::StepEventTypeCompleted,
82+
serde_json::to_string(&output_value).expect("must succeed"),
83+
),
84+
Ok(Err(error)) => step_action_event(
85+
worker_id,
86+
&action,
87+
StepActionEventType::StepEventTypeFailed,
88+
error.to_string(),
89+
),
90+
Err(join_error) => step_action_event(
91+
worker_id,
92+
&action,
93+
StepActionEventType::StepEventTypeFailed,
94+
join_error.to_string(),
95+
),
96+
};
97+
98+
dispatcher
99+
.send_step_action_event(action_event)
100+
.await?
101+
.into_inner();
100102

101103
Ok(())
102104
}

src/worker/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ async fn construct_endpoint(
111111
}
112112

113113
impl<'a> Worker<'a> {
114-
pub async fn register_workflow(&mut self, workflow: crate::workflow::Workflow) {
114+
pub fn register_workflow(&mut self, workflow: crate::workflow::Workflow) {
115115
self.workflows.push(workflow);
116116
}
117117

src/workflow.rs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
use std::sync::Arc;
22

3-
pub(crate) type StepFunction =
4-
dyn Fn(serde_json::Value) -> futures_util::future::BoxFuture<'static, serde_json::Value>;
3+
type StepFunction =
4+
dyn Fn(
5+
serde_json::Value,
6+
) -> futures_util::future::LocalBoxFuture<'static, anyhow::Result<serde_json::Value>>;
57

68
#[derive(derive_builder::Builder)]
79
#[builder(pattern = "owned")]
8-
pub(crate) struct Step {
10+
pub struct Step {
11+
#[builder(setter(into))]
912
pub(crate) name: String,
13+
#[builder(setter(custom))]
1014
function: Arc<StepFunction>,
1115
#[builder(default)]
1216
pub(crate) retries: usize,
@@ -16,12 +20,33 @@ pub(crate) struct Step {
1620
pub(crate) timeout: std::time::Duration,
1721
}
1822

23+
impl StepBuilder {
24+
pub fn function<I, O, Fut, F>(mut self, function: &'static F) -> Self
25+
where
26+
I: serde::de::DeserializeOwned,
27+
O: serde::ser::Serialize,
28+
Fut: std::future::Future<Output = anyhow::Result<O>> + 'static,
29+
F: Fn(I) -> Fut,
30+
{
31+
use futures_util::FutureExt;
32+
self.function = Some(Arc::new(|value| {
33+
let result = function(serde_json::from_value(value).expect("must succeed"));
34+
async { Ok(serde_json::to_value(result.await?).expect("must succeed")) }.boxed_local()
35+
}));
36+
self
37+
}
38+
}
39+
1940
#[derive(derive_builder::Builder)]
2041
#[builder(pattern = "owned")]
2142
pub struct Workflow {
43+
#[builder(setter(into))]
2244
pub(crate) name: String,
45+
#[builder(setter(into))]
2346
pub(crate) description: String,
47+
#[builder(default, setter(into))]
2448
pub(crate) version: String,
49+
#[builder(default, setter(custom))]
2550
pub(crate) steps: Vec<Step>,
2651
#[builder(default)]
2752
pub(crate) on_events: Vec<String>,
@@ -31,6 +56,15 @@ pub struct Workflow {
3156
pub(crate) schedule_timeout: std::time::Duration,
3257
}
3358

59+
impl WorkflowBuilder {
60+
pub fn step(mut self, step: Step) -> Self {
61+
let mut steps = self.steps.take().unwrap_or_default();
62+
steps.push(step);
63+
self.steps = Some(steps);
64+
self
65+
}
66+
}
67+
3468
impl Workflow {
3569
pub(crate) fn actions<'a>(
3670
&'a self,

0 commit comments

Comments
 (0)