BatchClient.CreateTask Method

Definition

Overloads

CreateTask(String, BatchTaskCreateContent, Nullable<Int32>, Nullable<DateTimeOffset>, CancellationToken)

Creates a Task to the specified Job.

CreateTask(String, RequestContent, Nullable<Int32>, Nullable<DateTimeOffset>, RequestContext)

[Protocol Method] Creates a Task to the specified Job.

CreateTask(String, BatchTaskCreateContent, Nullable<Int32>, Nullable<DateTimeOffset>, CancellationToken)

Source:
BatchClient.cs

Creates a Task to the specified Job.

public virtual Azure.Response CreateTask (string jobId, Azure.Compute.Batch.BatchTaskCreateContent task, int? timeOutInSeconds = default, DateTimeOffset? ocpdate = default, System.Threading.CancellationToken cancellationToken = default);
abstract member CreateTask : string * Azure.Compute.Batch.BatchTaskCreateContent * Nullable<int> * Nullable<DateTimeOffset> * System.Threading.CancellationToken -> Azure.Response
override this.CreateTask : string * Azure.Compute.Batch.BatchTaskCreateContent * Nullable<int> * Nullable<DateTimeOffset> * System.Threading.CancellationToken -> Azure.Response
Public Overridable Function CreateTask (jobId As String, task As BatchTaskCreateContent, Optional timeOutInSeconds As Nullable(Of Integer) = Nothing, Optional ocpdate As Nullable(Of DateTimeOffset) = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Response

Parameters

jobId
String

The ID of the Job to which the Task is to be created.

task
BatchTaskCreateContent

The Task to be created.

timeOutInSeconds
Nullable<Int32>

The maximum time that the server can spend processing the request, in seconds. The default is 30 seconds. If the value is larger than 30, the default will be used instead.".

ocpdate
Nullable<DateTimeOffset>

The time the request was issued. Client libraries typically set this to the current system clock time; set it explicitly if you are calling the REST API directly.

cancellationToken
CancellationToken

The cancellation token to use.

Returns

Exceptions

jobId or task is null.

jobId is an empty string, and was expected to be non-empty.

Examples

This sample shows how to call CreateTask.

Uri endpoint = new Uri("<https://my-service.azure.com>");
TokenCredential credential = new DefaultAzureCredential();
BatchClient client = new BatchClient(endpoint, credential);

BatchTaskCreateContent task = new BatchTaskCreateContent("<id>", "<commandLine>");
Response response = client.CreateTask("<jobId>", task);

This sample shows how to call CreateTask with all parameters.

Uri endpoint = new Uri("<https://my-service.azure.com>");
TokenCredential credential = new DefaultAzureCredential();
BatchClient client = new BatchClient(endpoint, credential);

BatchTaskCreateContent task = new BatchTaskCreateContent("<id>", "<commandLine>")
{
    DisplayName = "<displayName>",
    ExitConditions = new ExitConditions
    {
        ExitCodes = {new ExitCodeMapping(1234, new ExitOptions
        {
            JobAction = BatchJobAction.None,
            DependencyAction = DependencyAction.Satisfy,
        })},
        ExitCodeRanges = { new ExitCodeRangeMapping(1234, 1234, default) },
        PreProcessingError = default,
        FileUploadError = default,
        Default = default,
    },
    ContainerSettings = new BatchTaskContainerSettings("<imageName>")
    {
        ContainerRunOptions = "<containerRunOptions>",
        Registry = new ContainerRegistryReference
        {
            Username = "<username>",
            Password = "<password>",
            RegistryServer = "<registryServer>",
            IdentityReference = new BatchNodeIdentityReference
            {
                ResourceId = "<resourceId>",
            },
        },
        WorkingDirectory = ContainerWorkingDirectory.TaskWorkingDirectory,
    },
    ResourceFiles = {new ResourceFile
    {
        AutoStorageContainerName = "<autoStorageContainerName>",
        StorageContainerUrl = "<storageContainerUrl>",
        HttpUrl = "<httpUrl>",
        BlobPrefix = "<blobPrefix>",
        FilePath = "<filePath>",
        FileMode = "<fileMode>",
        IdentityReference = default,
    }},
    OutputFiles = {new OutputFile("<filePattern>", new OutputFileDestination
    {
        Container = new OutputFileBlobContainerDestination("<containerUrl>")
        {
            Path = "<path>",
            IdentityReference = default,
            UploadHeaders = {new HttpHeader("<name>")
            {
                Value = "<value>",
            }},
        },
    }, new OutputFileUploadConfig(OutputFileUploadCondition.TaskSuccess))},
    EnvironmentSettings = {new EnvironmentSetting("<name>")
    {
        Value = "<value>",
    }},
    AffinityInfo = new AffinityInfo("<affinityId>"),
    Constraints = new BatchTaskConstraints
    {
        MaxWallClockTime = XmlConvert.ToTimeSpan("PT1H23M45S"),
        RetentionTime = XmlConvert.ToTimeSpan("PT1H23M45S"),
        MaxTaskRetryCount = 1234,
    },
    RequiredSlots = 1234,
    UserIdentity = new UserIdentity
    {
        Username = "<username>",
        AutoUser = new AutoUserSpecification
        {
            Scope = AutoUserScope.Task,
            ElevationLevel = ElevationLevel.NonAdmin,
        },
    },
    MultiInstanceSettings = new MultiInstanceSettings("<coordinationCommandLine>")
    {
        NumberOfInstances = 1234,
        CommonResourceFiles = { default },
    },
    DependsOn = new BatchTaskDependencies
    {
        TaskIds = { "<taskIds>" },
        TaskIdRanges = { new BatchTaskIdRange(1234, 1234) },
    },
    ApplicationPackageReferences = {new BatchApplicationPackageReference("<applicationId>")
    {
        Version = "<version>",
    }},
    AuthenticationTokenSettings = new AuthenticationTokenSettings
    {
        Access = { AccessScope.Job },
    },
};
Response response = client.CreateTask("<jobId>", task, timeOutInSeconds: 1234, ocpdate: DateTimeOffset.Parse("Tue, 10 May 2022 18:57:31 GMT"));

Remarks

The maximum lifetime of a Task from addition to completion is 180 days. If a Task has not completed within 180 days of being added it will be terminated by the Batch service and left in whatever state it was in at that time.

Applies to

CreateTask(String, RequestContent, Nullable<Int32>, Nullable<DateTimeOffset>, RequestContext)

Source:
BatchClient.cs

[Protocol Method] Creates a Task to the specified Job.

public virtual Azure.Response CreateTask (string jobId, Azure.Core.RequestContent content, int? timeOutInSeconds = default, DateTimeOffset? ocpdate = default, Azure.RequestContext context = default);
abstract member CreateTask : string * Azure.Core.RequestContent * Nullable<int> * Nullable<DateTimeOffset> * Azure.RequestContext -> Azure.Response
override this.CreateTask : string * Azure.Core.RequestContent * Nullable<int> * Nullable<DateTimeOffset> * Azure.RequestContext -> Azure.Response
Public Overridable Function CreateTask (jobId As String, content As RequestContent, Optional timeOutInSeconds As Nullable(Of Integer) = Nothing, Optional ocpdate As Nullable(Of DateTimeOffset) = Nothing, Optional context As RequestContext = Nothing) As Response

Parameters

jobId
String

The ID of the Job to which the Task is to be created.

content
RequestContent

The content to send as the body of the request.

timeOutInSeconds
Nullable<Int32>

The maximum time that the server can spend processing the request, in seconds. The default is 30 seconds. If the value is larger than 30, the default will be used instead.".

ocpdate
Nullable<DateTimeOffset>

The time the request was issued. Client libraries typically set this to the current system clock time; set it explicitly if you are calling the REST API directly.

context
RequestContext

The request context, which can override default behaviors of the client pipeline on a per-call basis.

Returns

The response returned from the service.

Exceptions

jobId or content is null.

jobId is an empty string, and was expected to be non-empty.

Service returned a non-success status code.

Examples

This sample shows how to call CreateTask.

Uri endpoint = new Uri("<https://my-service.azure.com>");
TokenCredential credential = new DefaultAzureCredential();
BatchClient client = new BatchClient(endpoint, credential);

using RequestContent content = RequestContent.Create(new
{
    id = "<id>",
    commandLine = "<commandLine>",
});
Response response = client.CreateTask("<jobId>", content);

Console.WriteLine(response.Status);

This sample shows how to call CreateTask with all parameters and request content.

Uri endpoint = new Uri("<https://my-service.azure.com>");
TokenCredential credential = new DefaultAzureCredential();
BatchClient client = new BatchClient(endpoint, credential);

using RequestContent content = RequestContent.Create(new
{
    id = "<id>",
    displayName = "<displayName>",
    exitConditions = new Dictionary<string, object>
    {
        ["exitCodes"] = new object[]
        {
            new
            {
                code = 1234,
                exitOptions = new
                {
                    jobAction = "none",
                    dependencyAction = "satisfy",
                },
            }
        },
        ["exitCodeRanges"] = new object[]
        {
            new
            {
                start = 1234,
                end = 1234,
            }
        },
        ["preProcessingError"] = null,
        ["fileUploadError"] = null,
        ["default"] = null
    },
    commandLine = "<commandLine>",
    containerSettings = new
    {
        containerRunOptions = "<containerRunOptions>",
        imageName = "<imageName>",
        registry = new
        {
            username = "<username>",
            password = "<password>",
            registryServer = "<registryServer>",
            identityReference = new
            {
                resourceId = "<resourceId>",
            },
        },
        workingDirectory = "taskWorkingDirectory",
    },
    resourceFiles = new object[]
    {
        new
        {
            autoStorageContainerName = "<autoStorageContainerName>",
            storageContainerUrl = "<storageContainerUrl>",
            httpUrl = "<httpUrl>",
            blobPrefix = "<blobPrefix>",
            filePath = "<filePath>",
            fileMode = "<fileMode>",
        }
    },
    outputFiles = new object[]
    {
        new
        {
            filePattern = "<filePattern>",
            destination = new
            {
                container = new
                {
                    path = "<path>",
                    containerUrl = "<containerUrl>",
                    uploadHeaders = new object[]
                    {
                        new
                        {
                            name = "<name>",
                            value = "<value>",
                        }
                    },
                },
            },
            uploadOptions = new
            {
                uploadCondition = "tasksuccess",
            },
        }
    },
    environmentSettings = new object[]
    {
        new
        {
            name = "<name>",
            value = "<value>",
        }
    },
    affinityInfo = new
    {
        affinityId = "<affinityId>",
    },
    constraints = new
    {
        maxWallClockTime = "PT1H23M45S",
        retentionTime = "PT1H23M45S",
        maxTaskRetryCount = 1234,
    },
    requiredSlots = 1234,
    userIdentity = new
    {
        username = "<username>",
        autoUser = new
        {
            scope = "task",
            elevationLevel = "nonadmin",
        },
    },
    multiInstanceSettings = new
    {
        numberOfInstances = 1234,
        coordinationCommandLine = "<coordinationCommandLine>",
        commonResourceFiles = new object[]
        {
            null
        },
    },
    dependsOn = new
    {
        taskIds = new object[]
        {
            "<taskIds>"
        },
        taskIdRanges = new object[]
        {
            new
            {
                start = 1234,
                end = 1234,
            }
        },
    },
    applicationPackageReferences = new object[]
    {
        new
        {
            applicationId = "<applicationId>",
            version = "<version>",
        }
    },
    authenticationTokenSettings = new
    {
        access = new object[]
        {
            "job"
        },
    },
});
Response response = client.CreateTask("<jobId>", content, timeOutInSeconds: 1234, ocpdate: DateTimeOffset.Parse("Tue, 10 May 2022 18:57:31 GMT"));

Console.WriteLine(response.Status);

Applies to