Managing state in commands
This section provides information on how to access state in commands and to store intermediate and final state in the worker application.
Within a task context, using the State property, there is easy access to the state context.
In tasks, it is recommended to use the TaskuraiStateContext instead of the TaskuraiStateClient. The state context is aware of the context of the task and cancellation tokens.
API reference:
Required scopes:
Data/Buildby.Taskurai/state/createData/Buildby.Taskurai/state/readData/Buildby.Taskurai/state/delete
Optional scopes:
Data/Buildby.Taskurai/sensitive/read: Can return sensitive data.
Prerequisites
You have completed:
Managing state in a command
In a previous section, Storing initial state, an initial large json file was uploaded to the default state store and a task was created that accepts a state reference as argument.
The following sample of a command would process the daily sales JSON file and aggregate the sales, upload to another system:
- C#
[Command]
[Version(1)]
public async Task<TaskCommandResult> UploadDailySalesSAP(TaskuraiTaskContext context, CancellationToken cancellationToken, DateTimeOffset date, StateReference dailySales)
{
Logger.LogInformation($"Generating aggregate of daily sales, run count = {context.Task.RunCount}.");
// Download file
StateDownloadResponse stateDownloadResponse = await context.State.GetBlobStateContentAsync(dailySales);
await context.ProgressAsync(10, "Downloaded daily sales.");
// Aggregate file
await Task.Delay(5000);
await context.ProgressAsync(60, "Aggregated daily sales.");
// Write aggregated file
var aggregatedState = await context.State.SaveBlobStateAsync(
new StateInput(context.Task, "dailySalesAggregate")
{
HasBlob = true,
Ttl = 30 * 24 * 60 * 60, // one month
ContentType = "application/json"
},
stateDownloadResponse.BlobDownloadResult.Content,
overwrite: true
);
// Simulate upload to SAP
await Task.Delay(5000);
return Succeeded(
progress: new Progress()
{
Percentage = 100,
Message = "Successfully aggregated daily sales."
},
result: new ResultResponse()
{
Messages = [
new ResultMessage()
{
Code = "200",
Message = "Successfully aggregated daily sales and uploaded to SAP."
}
],
Outputs = [
new ResultOutput()
{
Name = "dailySalesAggregate",
StateReference = aggregatedState.StateReference
}
]
}
);
}