pub struct TaskResult { /* private fields */ }Expand description
A result envelope: the flat map of string fields a worker adds to a reply stream.
A result carries task_id and status, exactly success or error; an entry without them
isn’t a result. A worker also writes model_type, model_id, task_name and model_name,
echoed from the task, and on an error error, a message, and error_type, a code in lowercase
snake case such as unsupported_model; it should write worker_id and processing_ms, who ran
the task and for how long, and may write requested_model_name. A reader requires none of
those: error types are an open set, and a reader treats an unknown or missing one as a failure
that won’t pass on a retry. Every other field, such as embedding or result, is the output,
which this crate keeps in order and never reads. A result parsed from a stream and written back
gives the same fields in the same order.
Debug shows the identity and status fields and the names of the others, never their values.
use legit_streams::{Status, TaskResult};
let result = TaskResult::success("app_01J0000000000000000000000A")
.worker_id("worker-1")
.processing_ms(12)
.field("result", "{}")
.build()?;
assert_eq!(result.status(), Status::Success);
assert_eq!(result.processing_ms(), Some(12));
assert_eq!(TaskResult::from_fields(result.fields().to_vec())?, result);Implementations§
Source§impl TaskResult
impl TaskResult
Sourcepub fn success(task_id: impl Into<String>) -> ResultBuilder
pub fn success(task_id: impl Into<String>) -> ResultBuilder
Start a success result for a task.
use legit_streams::TaskResult;
assert!(TaskResult::success("app_1").build()?.is_success());Sourcepub fn failure(
task_id: impl Into<String>,
message: impl Into<String>,
) -> ResultBuilder
pub fn failure( task_id: impl Into<String>, message: impl Into<String>, ) -> ResultBuilder
Start an error result for a task, with the message for its error field.
A worker must set ResultBuilder::error_type on every error it writes, as this crate’s
workers do. The builder still builds an error without one: a reader must accept such a
result, from an older worker, and its tests need one to read.
use legit_streams::TaskResult;
let result = TaskResult::failure("app_1", "no such model").error_type("unsupported_model").build()?;
assert_eq!(result.error(), Some("no such model"));
assert_eq!(result.error_type(), Some("unsupported_model"));Sourcepub fn from_entry(entry: &Entry) -> Result<Self>
pub fn from_entry(entry: &Entry) -> Result<Self>
Read a result from a stream entry, within the default Limits.
§Errors
The errors of TaskResult::from_raw.
use legit_streams::{Entry, EntryId, TaskResult};
let entry = Entry::new(EntryId::ZERO, vec![(b"task_id".to_vec(), b"app_1".to_vec())]);
assert!(TaskResult::from_entry(&entry).is_err());Sourcepub fn from_raw(raw: &[(Vec<u8>, Vec<u8>)], limits: &Limits) -> Result<Self>
pub fn from_raw(raw: &[(Vec<u8>, Vec<u8>)], limits: &Limits) -> Result<Self>
Read a result from raw stream fields.
§Errors
Error::TooLarge for an entry over limits, Error::InvalidUtf8 for a name or value
that isn’t UTF-8, Error::DuplicateField for a name given twice,
Error::MissingField without task_id or status, Error::EmptyField for an empty
task_id, and Error::InvalidValue for a status other than exactly success or
error. Nothing else is required: not error or error_type on an error, nor
worker_id or processing_ms.
use legit_streams::{Error, Limits, TaskResult};
let raw = vec![(b"task_id".to_vec(), b"app_1".to_vec()), (b"status".to_vec(), b"done".to_vec())];
assert!(matches!(TaskResult::from_raw(&raw, &Limits::default()), Err(Error::InvalidValue { .. })));Sourcepub fn from_fields(fields: Vec<(String, String)>) -> Result<Self>
pub fn from_fields(fields: Vec<(String, String)>) -> Result<Self>
Read a result from decoded fields, within the default Limits.
§Errors
The errors of TaskResult::from_raw that apply to strings.
use legit_streams::{Error, TaskResult};
let fields = vec![("task_id".to_owned(), "app_1".to_owned()), ("status".to_owned(), "Error".to_owned())];
assert!(matches!(TaskResult::from_fields(fields), Err(Error::InvalidValue { .. })));
let fields = vec![("task_id".to_owned(), "app_1".to_owned()), ("status".to_owned(), "error".to_owned())];
assert_eq!(TaskResult::from_fields(fields)?.error_type(), None);Sourcepub fn task_id(&self) -> &str
pub fn task_id(&self) -> &str
The id of the task this is the result of.
use legit_streams::TaskResult;
assert_eq!(TaskResult::success("app_1").build()?.task_id(), "app_1");Sourcepub fn status(&self) -> Status
pub fn status(&self) -> Status
Whether the task succeeded.
use legit_streams::{Status, TaskResult};
assert_eq!(TaskResult::failure("app_1", "failed").build()?.status(), Status::Error);Sourcepub fn is_success(&self) -> bool
pub fn is_success(&self) -> bool
Whether the status is success.
use legit_streams::TaskResult;
assert!(!TaskResult::failure("app_1", "failed").build()?.is_success());Sourcepub fn error(&self) -> Option<&str>
pub fn error(&self) -> Option<&str>
The error message, on an error.
use legit_streams::TaskResult;
assert_eq!(TaskResult::success("app_1").build()?.error(), None);Sourcepub fn error_type(&self) -> Option<&str>
pub fn error_type(&self) -> Option<&str>
The error code, such as unsupported_model, when the worker gave one.
use legit_streams::TaskResult;
assert_eq!(TaskResult::failure("app_1", "failed").build()?.error_type(), None);Sourcepub fn model_type(&self) -> Option<&str>
pub fn model_type(&self) -> Option<&str>
The task’s model_type, echoed.
use legit_streams::TaskResult;
assert_eq!(TaskResult::success("app_1").model_type("post").build()?.model_type(), Some("post"));Sourcepub fn model_id(&self) -> Option<&str>
pub fn model_id(&self) -> Option<&str>
The task’s model_id, echoed.
use legit_streams::TaskResult;
assert_eq!(TaskResult::success("app_1").model_id("42").build()?.model_id(), Some("42"));Sourcepub fn task_name(&self) -> Option<&str>
pub fn task_name(&self) -> Option<&str>
The task’s task_name, echoed.
use legit_streams::TaskResult;
assert_eq!(TaskResult::success("app_1").build()?.task_name(), None);Sourcepub fn model_name(&self) -> Option<&str>
pub fn model_name(&self) -> Option<&str>
The model the worker names: the task’s model_name echoed, or the worker’s own name for
what ran.
use legit_streams::TaskResult;
let result = TaskResult::success("app_1").model_name("example-model").build()?;
assert_eq!(result.model_name(), Some("example-model"));Sourcepub fn worker_id(&self) -> Option<&str>
pub fn worker_id(&self) -> Option<&str>
The consumer name of the worker that ran the task.
use legit_streams::TaskResult;
assert_eq!(TaskResult::success("app_1").worker_id("w-1").build()?.worker_id(), Some("w-1"));Sourcepub fn processing_ms(&self) -> Option<u64>
pub fn processing_ms(&self) -> Option<u64>
How long the worker spent on the task, in milliseconds, from taking the entry to writing
the result; None when the result doesn’t say, or says it with something other than a
whole number.
use legit_streams::TaskResult;
assert_eq!(TaskResult::success("app_1").build()?.processing_ms(), None);Sourcepub fn get(&self, name: &str) -> Option<&str>
pub fn get(&self, name: &str) -> Option<&str>
The value of a field, the envelope’s own or the worker’s output.
use legit_streams::TaskResult;
let result = TaskResult::success("app_1").field("embedding_format", "base64_fp32").build()?;
assert_eq!(result.get("embedding_format"), Some("base64_fp32"));Sourcepub fn fields(&self) -> &[(String, String)]
pub fn fields(&self) -> &[(String, String)]
Every field, in order.
use legit_streams::TaskResult;
assert_eq!(TaskResult::success("app_1").build()?.fields().len(), 2);Sourcepub fn into_fields(self) -> Vec<(String, String)>
pub fn into_fields(self) -> Vec<(String, String)>
The fields, in order, consuming the result.
use legit_streams::TaskResult;
let fields = TaskResult::success("app_1").build()?.into_fields();
assert_eq!(fields[0], ("task_id".to_owned(), "app_1".to_owned()));