Udostępnij za pośrednictwem


BatchClient.CreateTaskAsync Method

Definition

Overloads

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

Creates a Task to the specified Job.

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

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

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

Source:
BatchClientCustom.cs

Creates a Task to the specified Job.

public virtual System.Threading.Tasks.Task<Azure.Response> CreateTaskAsync(string jobId, Azure.Compute.Batch.BatchTaskCreateContent task, int? timeOutInSeconds = default, DateTimeOffset? ocpdate = default, System.Threading.CancellationToken cancellationToken = default);
abstract member CreateTaskAsync : string * Azure.Compute.Batch.BatchTaskCreateContent * Nullable<int> * Nullable<DateTimeOffset> * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Azure.Response>
override this.CreateTaskAsync : string * Azure.Compute.Batch.BatchTaskCreateContent * Nullable<int> * Nullable<DateTimeOffset> * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Azure.Response>
Public Overridable Function CreateTaskAsync (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 Task(Of 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 CreateTaskAsync.

Uri endpoint = new Uri("<endpoint>");
TokenCredential credential = new DefaultAzureCredential();
BatchClient client = new BatchClient(endpoint, credential);

BatchTaskCreateContent task = new BatchTaskCreateContent("task1", "cmd /c echo task1");
Response response = await client.CreateTaskAsync("jobId", task);

This sample shows how to call CreateTaskAsync.

Uri endpoint = new Uri("<endpoint>");
TokenCredential credential = new DefaultAzureCredential();
BatchClient client = new BatchClient(endpoint, credential);

BatchTaskCreateContent task = new BatchTaskCreateContent("taskId", "bash -c 'echo hello'")
{
    ContainerSettings = new BatchTaskContainerSettings("ubuntu")
    {
        ContainerRunOptions = "--rm",
    },
    UserIdentity = new UserIdentity
    {
        AutoUser = new AutoUserSpecification
        {
            Scope = AutoUserScope.Task,
            ElevationLevel = ElevationLevel.NonAdmin,
        },
    },
};
Response response = await client.CreateTaskAsync("jobId", task);

This sample shows how to call CreateTaskAsync.

Uri endpoint = new Uri("<endpoint>");
TokenCredential credential = new DefaultAzureCredential();
BatchClient client = new BatchClient(endpoint, credential);

BatchTaskCreateContent task = new BatchTaskCreateContent("taskId", "bash -c 'echo hello'")
{
    ContainerSettings = new BatchTaskContainerSettings("ubuntu")
    {
        ContainerHostBatchBindMounts = {new ContainerHostBatchBindMountEntry
        {
            Source = ContainerHostDataPath.Task,
            IsReadOnly = true,
        }},
    },
    UserIdentity = new UserIdentity
    {
        AutoUser = new AutoUserSpecification
        {
            Scope = AutoUserScope.Task,
            ElevationLevel = ElevationLevel.NonAdmin,
        },
    },
};
Response response = await client.CreateTaskAsync("jobId", task);

This sample shows how to call CreateTaskAsync.

Uri endpoint = new Uri("<endpoint>");
TokenCredential credential = new DefaultAzureCredential();
BatchClient client = new BatchClient(endpoint, credential);

BatchTaskCreateContent task = new BatchTaskCreateContent("taskId", "bash -c 'echo hello'")
{
    ContainerSettings = new BatchTaskContainerSettings("ubuntu")
    {
        ContainerHostBatchBindMounts = {new ContainerHostBatchBindMountEntry
        {
            Source = ContainerHostDataPath.Task,
            IsReadOnly = true,
        }, new ContainerHostBatchBindMountEntry
        {
            Source = ContainerHostDataPath.Task,
            IsReadOnly = true,
        }},
    },
    UserIdentity = new UserIdentity
    {
        AutoUser = new AutoUserSpecification
        {
            Scope = AutoUserScope.Task,
            ElevationLevel = ElevationLevel.NonAdmin,
        },
    },
};
Response response = await client.CreateTaskAsync("jobId", task);

This sample shows how to call CreateTaskAsync.

Uri endpoint = new Uri("<endpoint>");
TokenCredential credential = new DefaultAzureCredential();
BatchClient client = new BatchClient(endpoint, credential);

BatchTaskCreateContent task = new BatchTaskCreateContent("taskId", "cmd /c exit 3")
{
    ExitConditions = new ExitConditions
    {
        ExitCodeRanges = {new ExitCodeRangeMapping(2, 4, new ExitOptions
        {
            JobAction = BatchJobAction.Terminate,
        })},
    },
    UserIdentity = new UserIdentity
    {
        AutoUser = new AutoUserSpecification
        {
            Scope = AutoUserScope.Task,
            ElevationLevel = ElevationLevel.NonAdmin,
        },
    },
};
Response response = await client.CreateTaskAsync("jobId", task);

This sample shows how to call CreateTaskAsync.

Uri endpoint = new Uri("<endpoint>");
TokenCredential credential = new DefaultAzureCredential();
BatchClient client = new BatchClient(endpoint, credential);

BatchTaskCreateContent task = new BatchTaskCreateContent("task1", "cmd /c echo task1")
{
    RequiredSlots = 2,
};
Response response = await client.CreateTaskAsync("jobId", task);

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

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

Source:
BatchClientCustom.cs

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

public virtual System.Threading.Tasks.Task<Azure.Response> CreateTaskAsync(string jobId, Azure.Core.RequestContent content, int? timeOutInSeconds = default, DateTimeOffset? ocpdate = default, Azure.RequestContext context = default);
abstract member CreateTaskAsync : string * Azure.Core.RequestContent * Nullable<int> * Nullable<DateTimeOffset> * Azure.RequestContext -> System.Threading.Tasks.Task<Azure.Response>
override this.CreateTaskAsync : string * Azure.Core.RequestContent * Nullable<int> * Nullable<DateTimeOffset> * Azure.RequestContext -> System.Threading.Tasks.Task<Azure.Response>
Public Overridable Function CreateTaskAsync (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 Task(Of 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 CreateTaskAsync.

Uri endpoint = new Uri("<endpoint>");
TokenCredential credential = new DefaultAzureCredential();
BatchClient client = new BatchClient(endpoint, credential);

using RequestContent content = RequestContent.Create(new
{
    id = "task1",
    commandLine = "cmd /c echo task1",
});
Response response = await client.CreateTaskAsync("jobId", content);

Console.WriteLine(response.Status);

This sample shows how to call CreateTaskAsync.

Uri endpoint = new Uri("<endpoint>");
TokenCredential credential = new DefaultAzureCredential();
BatchClient client = new BatchClient(endpoint, credential);

using RequestContent content = RequestContent.Create(new
{
    id = "taskId",
    commandLine = "bash -c 'echo hello'",
    containerSettings = new
    {
        imageName = "ubuntu",
        containerRunOptions = "--rm",
    },
    userIdentity = new
    {
        autoUser = new
        {
            scope = "task",
            elevationLevel = "nonadmin",
        },
    },
});
Response response = await client.CreateTaskAsync("jobId", content);

Console.WriteLine(response.Status);

This sample shows how to call CreateTaskAsync.

Uri endpoint = new Uri("<endpoint>");
TokenCredential credential = new DefaultAzureCredential();
BatchClient client = new BatchClient(endpoint, credential);

using RequestContent content = RequestContent.Create(new
{
    id = "taskId",
    commandLine = "bash -c 'echo hello'",
    containerSettings = new
    {
        imageName = "ubuntu",
        containerHostBatchBindMounts = new object[]
        {
            new
            {
                source = "Task",
                isReadOnly = true,
            }
        },
    },
    userIdentity = new
    {
        autoUser = new
        {
            scope = "task",
            elevationLevel = "nonadmin",
        },
    },
});
Response response = await client.CreateTaskAsync("jobId", content);

Console.WriteLine(response.Status);

This sample shows how to call CreateTaskAsync.

Uri endpoint = new Uri("<endpoint>");
TokenCredential credential = new DefaultAzureCredential();
BatchClient client = new BatchClient(endpoint, credential);

using RequestContent content = RequestContent.Create(new
{
    id = "taskId",
    commandLine = "bash -c 'echo hello'",
    containerSettings = new
    {
        imageName = "ubuntu",
        containerHostBatchBindMounts = new object[]
        {
            new
            {
                source = "Task",
                isReadOnly = true,
            },
            new
            {
                source = "Task",
                isReadOnly = true,
            }
        },
    },
    userIdentity = new
    {
        autoUser = new
        {
            scope = "task",
            elevationLevel = "nonadmin",
        },
    },
});
Response response = await client.CreateTaskAsync("jobId", content);

Console.WriteLine(response.Status);

This sample shows how to call CreateTaskAsync.

Uri endpoint = new Uri("<endpoint>");
TokenCredential credential = new DefaultAzureCredential();
BatchClient client = new BatchClient(endpoint, credential);

using RequestContent content = RequestContent.Create(new
{
    id = "taskId",
    commandLine = "cmd /c exit 3",
    exitConditions = new
    {
        exitCodeRanges = new object[]
        {
            new
            {
                start = 2,
                end = 4,
                exitOptions = new
                {
                    jobAction = "terminate",
                },
            }
        },
    },
    userIdentity = new
    {
        autoUser = new
        {
            scope = "task",
            elevationLevel = "nonadmin",
        },
    },
});
Response response = await client.CreateTaskAsync("jobId", content);

Console.WriteLine(response.Status);

This sample shows how to call CreateTaskAsync.

Uri endpoint = new Uri("<endpoint>");
TokenCredential credential = new DefaultAzureCredential();
BatchClient client = new BatchClient(endpoint, credential);

using RequestContent content = RequestContent.Create(new
{
    id = "task1",
    requiredSlots = 2,
    commandLine = "cmd /c echo task1",
});
Response response = await client.CreateTaskAsync("jobId", content);

Console.WriteLine(response.Status);

Applies to