-
Notifications
You must be signed in to change notification settings - Fork 0
feat(job): add result passing and unified notification router #60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d71c0e4
feat(job): add result passing via CurrentJob::set_result
bodymindarts dcfeadd
fix(job): add missing result arg to maybe_schedule_retry test call
bodymindarts 98a74f9
fix(tests): use unique job types for await_completion tests
bodymindarts 290a02e
fix(tests): use unique job types for all tests to prevent cross-proce…
bodymindarts 5298db4
feat(job): change set_result from write-once to incremental overwrite
bodymindarts a43f225
refactor(job): add CouldNotSerializeResult error variant and JobResul…
bodymindarts 6046742
refactor(job): persist set_result to DB immediately instead of holdin…
bodymindarts 7e741f9
fix(job): address PR review comments on result-passing
bodymindarts 7687577
fix(job): address PR 60 review comments round 3
bodymindarts File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,3 +15,4 @@ target/ | |
| .env | ||
| .bacon-locations | ||
| .claude/settings.local.json | ||
| .mcp.json | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,72 @@ use es_entity::{context::TracingContext, *}; | |
|
|
||
| use crate::{JobId, error::JobError}; | ||
|
|
||
| /// Newtype wrapper around a raw JSON value representing the result produced by a | ||
| /// job runner via [`CurrentJob::set_result`](crate::CurrentJob::set_result). | ||
| /// | ||
| /// Using a dedicated type instead of bare `serde_json::Value` gives call sites | ||
| /// semantic clarity and prevents accidental mix-ups with other JSON payloads | ||
| /// (config, execution state, etc.). | ||
| #[derive(Debug, Clone, Serialize, Deserialize)] | ||
| #[serde(transparent)] | ||
| pub struct JobResult(serde_json::Value); | ||
|
|
||
| impl JobResult { | ||
| /// Wrap a raw JSON value. | ||
| pub(crate) fn new(value: serde_json::Value) -> Self { | ||
| Self(value) | ||
| } | ||
|
|
||
| /// Consume the wrapper and return the inner JSON value. | ||
| pub fn into_inner(self) -> serde_json::Value { | ||
| self.0 | ||
| } | ||
|
|
||
| /// Return a reference to the inner JSON value. | ||
| pub fn as_value(&self) -> &serde_json::Value { | ||
| &self.0 | ||
| } | ||
|
|
||
| /// Deserialize the result into a typed struct. | ||
| pub fn deserialize<T: serde::de::DeserializeOwned>(&self) -> Result<T, serde_json::Error> { | ||
| serde_json::from_value(self.0.clone()) | ||
| } | ||
| } | ||
|
|
||
| /// Outcome returned by [`Jobs::await_completion`](crate::Jobs::await_completion), | ||
| /// carrying both the terminal state and an optional result value. | ||
| #[derive(Debug, Clone)] | ||
| pub struct JobCompletionResult { | ||
| state: JobTerminalState, | ||
| result: Option<JobResult>, | ||
| } | ||
|
|
||
| impl JobCompletionResult { | ||
| pub(crate) fn new(state: JobTerminalState, result: Option<JobResult>) -> Self { | ||
| Self { state, result } | ||
| } | ||
|
|
||
| /// The terminal state the job reached. | ||
| pub fn state(&self) -> JobTerminalState { | ||
| self.state | ||
| } | ||
|
|
||
| /// Returns the result wrapper, if any. | ||
| pub fn result(&self) -> Option<&JobResult> { | ||
|
||
| self.result.as_ref() | ||
| } | ||
|
|
||
| /// Deserialize the result value into a typed struct. | ||
| pub fn typed_result<T: serde::de::DeserializeOwned>( | ||
| &self, | ||
| ) -> Result<Option<T>, serde_json::Error> { | ||
| match &self.result { | ||
| Some(r) => r.deserialize().map(Some), | ||
| None => Ok(None), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Terminal outcome of a job lifecycle. | ||
| #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| pub enum JobTerminalState { | ||
|
|
@@ -79,6 +145,9 @@ pub enum JobEvent { | |
| ExecutionErrored { | ||
| error: String, | ||
| }, | ||
| ResultUpdated { | ||
| result: JobResult, | ||
| }, | ||
| JobCompleted, | ||
| Cancelled, | ||
| AttemptCounterReset, | ||
|
|
@@ -223,6 +292,29 @@ impl Job { | |
| } | ||
| } | ||
|
|
||
| /// Returns the result value attached to this job, if any. | ||
| /// | ||
| /// Scans for the latest `ResultUpdated` event (last write wins). | ||
| pub fn result(&self) -> Option<&JobResult> { | ||
| self.events.iter_all().rev().find_map(|event| { | ||
| if let JobEvent::ResultUpdated { result } = event { | ||
| Some(result) | ||
| } else { | ||
| None | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| /// Deserialize the result value into a typed struct. | ||
| pub fn typed_result<T: serde::de::DeserializeOwned>( | ||
| &self, | ||
| ) -> Result<Option<T>, serde_json::Error> { | ||
| match self.result() { | ||
| Some(r) => r.deserialize().map(Some), | ||
| None => Ok(None), | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn inject_tracing_parent(&self) { | ||
| if let JobEvent::Initialized { | ||
| tracing_context: Some(tracing_context), | ||
|
|
@@ -292,6 +384,20 @@ impl Job { | |
| self.events.push(JobEvent::JobCompleted); | ||
| } | ||
|
|
||
| /// Attach or overwrite the result value for this job. | ||
| /// | ||
| /// Returns [`Idempotent::AlreadyApplied`] when the new value is identical | ||
| /// to the current one, allowing callers to skip the DB round-trip. | ||
| pub(crate) fn update_result(&mut self, result: JobResult) -> es_entity::Idempotent<()> { | ||
| if let Some(existing) = self.result() | ||
|
||
| && *existing.as_value() == *result.as_value() | ||
| { | ||
| return es_entity::Idempotent::AlreadyApplied; | ||
| } | ||
| self.events.push(JobEvent::ResultUpdated { result }); | ||
| es_entity::Idempotent::Executed(()) | ||
| } | ||
|
|
||
| pub(super) fn maybe_schedule_retry( | ||
| &mut self, | ||
| now: DateTime<Utc>, | ||
|
|
@@ -362,6 +468,7 @@ impl TryFromEvents<JobEvent> for Job { | |
| JobEvent::ExecutionCompleted => {} | ||
| JobEvent::ExecutionAborted { .. } => {} | ||
| JobEvent::ExecutionErrored { .. } => {} | ||
| JobEvent::ResultUpdated { .. } => {} | ||
| JobEvent::JobCompleted => {} | ||
| JobEvent::Cancelled => {} | ||
| JobEvent::AttemptCounterReset => {} | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't this be job_result = JobResult::try_from(T)?