w4c-workflows-api/Services/Execution/ExecutionResult.cs

47 lines
1.7 KiB
C#

namespace w4c_workflows.Services.Execution;
/// <summary>
/// The result of executing one task. <see cref="Output"/> is always valid JSON
/// (a serialized value) or <c>null</c> when the task produced no output; the
/// control plane stores it directly into <c>task_runs.output jsonb</c>.
/// </summary>
public sealed record ExecutionResult(
bool Success,
int ExitCode,
string Stdout,
string Stderr,
string? Output,
string? Error,
TimeSpan Duration);
/// <summary>
/// Serializes an <see cref="ExecutionResult"/> into the flat <c>task.result</c>
/// event fields the worker publishes back to the control plane.
/// </summary>
public static class TaskResultMessage
{
public const string TypeValue = "task.result";
public static IReadOnlyDictionary<string, string> ToFields(TaskInvocation invocation, ExecutionResult result)
{
var fields = new Dictionary<string, string>
{
["type"] = TypeValue,
["run_id"] = invocation.RunId,
["task_id"] = invocation.TaskId,
["attempt"] = invocation.Attempt.ToString(System.Globalization.CultureInfo.InvariantCulture),
["tenant_id"] = invocation.TenantId,
["status"] = result.Success ? "succeeded" : "failed",
["exit_code"] = result.ExitCode.ToString(System.Globalization.CultureInfo.InvariantCulture),
["duration_ms"] = ((long)result.Duration.TotalMilliseconds).ToString(System.Globalization.CultureInfo.InvariantCulture),
};
if (result.Output != null)
fields["output"] = result.Output;
if (result.Error != null)
fields["error"] = result.Error;
return fields;
}
}