Skip to main content

Creating tasks

This section provides information on how to create new tasks.

API reference:

Required scopes:

  • Taskurai.Task.Create

Prerequisites

Creating a new task

In your application, you can create one or more tasks to offload workloads to asynchronous commands. You can store the returned task ID to provide updates to the end-user about the progress and outcome of the task.

var serializerOptions = new JsonSerializerOptions()
{
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault,
Converters ={
new JsonStringEnumConverter()
}
};

// Custom task payload
var myData = new
{
Name = "Test",
Value = "Some value",
Properties = new List<string>()
{
"Some property",
"Another property"
}
};

var createTaskInput = new TaskConfig("testCommand")
{
ExternalId = "my-id-123456",
Arguments = new List<TaskArgument>()
{
new TaskArgument("arg1")
{
Value = "abc-123"
}
},
Category = "Console app",
Subject = "Samples",
Description = "This is a test task",
Data = BinaryData.FromObjectAsJson(myData)
};

var createdTask = await taskurai.CreateTaskAsync(createTaskInput, cancellationToken);

Console.WriteLine(JsonSerializer.Serialize(createdTask, serializerOptions));

Next, run the console application to validate:

dotnet run

The expected output should be:

{
"Id": "5e7f3b48-8914-478e-8b6c-35c8d5924c7a",
"Config": {
"Command": "testCommand",
"Arguments": [
{
"Name": "arg1",
"Value": "abc-123"
}
],
"Category": "Console app",
"Subject": "Samples",
"Description": "This is a test task",
"ExternalId": "my-id-123456",
"IsolationMode": true,
"LocalIdentifier": "6d63b7cf-a0f9-4448-ac8a-5a6edd8bdf7a",
"Id": "5e7f3b48-8914-478e-8b6c-35c8d5924c7a",
"Data": {"Name":"Test","Value":"Some value","Properties":["Some property","Another property"]}
},
"Created": "2023-06-15T17:51:25.7657589+00:00",
"Status": "Created",
"ProvisioningState": "Created",
"Archived": false,
"Postponed": false,
"Cancelled": false
}

Waiting for a task to be completed

In most cases, the task is created and is not awaited for. Updates are provided after the task is handled by other means, such as sending notifications to the user application or sending messages via email, etc.

While not recommended as the main use-case, Taskurai does offer a construct to wait for an async task to be completed. This can be useful in services that do not have direct user interaction. By using this construct, the task works as a direct command, and the result is returned after the task is completed.

var myData = new {
Name = "Task to wait for",
Value = "some value",
SomeValues = new List<string>()
{
"Line1",
"Line2"
}
};

var taskConfigWait = new TaskConfig("testAction")
{
ExternalId = "1234567",
Arguments = new List<TaskArgument>()
{
new TaskArgument("id")
{
Value = "abc-xxx"
}
},
Category = "Console app",
Subject = "Test and wait for it",
Description = "This is a test task",
Data = BinaryData.FromObjectAsJson(myData)
};

var operation = await taskurai.CreateTaskAndWaitAsync(taskConfigWait, cancellationToken);

Console.WriteLine("Waiting for task to be completed...");

var response = await operation.WaitForCompletionResponseAsync(cancellationToken);

var createdAndWaitTask = operation.Value;

Console.WriteLine("Awaited task:");
Console.WriteLine( JsonSerializer.Serialize(createdAndWaitTask));

Next, run the console application to validate:

dotnet run

The expected output should be:

{
"Id":"bee61f65-10af-4bf7-b13f-7f2d54883d29",
"Config":{
"Command":"testCommand",
"Arguments":[
{
"Name":"id",
"Value":"abc-xxx"
}
],
"Category":"Console app",
"Subject":"Test and wait for it",
"Description":"This is a test task",
"ExternalId":"1234567",
"AccountId":null,
"UserId":null,
"IsolationMode":null,
"LocalIdentifier":null,
"Unlisted":null,
"Id":"bee61f65-10af-4bf7-b13f-7f2d54883d29",
"Data":{
"Name":"Task to wait for",
"Value":"some value",
"SomeValues":[
"Line1",
"Line2"
]
}
},
"Created":"2023-06-28T16:28:49.8595064+00:00",
"Modified":"2023-06-28T16:29:05.3693397+00:00",
"Started":"2023-06-28T16:29:05.3693397+00:00",
"RunStarted":"2023-06-28T16:29:05.3693397+00:00",
"Stopped":"2023-06-28T16:29:05.3693397+00:00",
"Status": "Succeeded",
"ProvisioningState":"Succeeded",
"StatusCode":200,
"RunCount":1,
"Progress":{
"Progress":100,
"Message":"Task finished."
},
"Result":{
"Summary":null,
"Details":[

],
"Output":"https://storage.co/id",
"OutputDetails":[

],
"Data":null
},
"Error":null,
"Archived":false,
"Postponed":false,
"Cancelled":false
}