Skip to content

How to run a job using the SDK

The job service is a very generic service which allows clients to execute various kinds of custom made job on the cloud infrastructure. This sections provides examples on how to run a job using the SDK. For description of the job service and concepts around it, please refer to the Jobs section of the platform documentation.

The entry point for using the Compute service is the IComputeClient interface. It can be resolved from DI container (after using AddPlatformClients extension) or constructed manually.

var collection = new ServiceCollection();
collection.AddPlatformClients(o =>
{
    o.ConfigureOpenApiKey("my-open-api-key");
    o.PlatformEnvironment = PlatformEnvironment.Dev;
});

using var provider = collection.BuildServiceProvider();

var computeClient = provider.GetRequiredService<IComputeClient>();

The example below executes a very basic container with instruction to print 'hi' and then wait for 10 seconds. It demonstrates one of the simplest jobs possible and how to wait until the job has finished.

    var containerName = "mcr.microsoft.com/dotnet/aspnet:5.0-alpine";
    var arguments = new string[] { "/bin/sh", "-c", "echo 'hi'; sleep 10" };

    // variable for detecting if job has finished
    var jobHasFinished = false;

    var jobBuilder = _computClient.CreateJob(projectId, containerName, arguments);

    // provide a job life cycle message handler
    jobBuilder.WithMessageHandler(m => {
            jobHasFinished = m.Status == JobStateType.Finished;
            return Task.CompletedTask;
        }
    );

    // subscribe to events by using the monitor object
    await using var monitor = await jobBuilder.ExecuteAndMonitorAsync();

    // wait for job to finish
    while (!jobHasFinished)
    {
        await Task.Delay(2000);
    }

The following example demonstrates slightly more advanced usage where input and output properties are defined and message handlers for progress and error messages are provided. The example also shows how to provide environment variables for the running container.

    var containerName = "mycontainerimage";
    var arguments = new string[] { "myprogram", "123"};
    var environmentVariables = new Dictionary<string, string> { { "FOO", "bar" } };
    var cpuCores = 4.0f;
    var memoryMB = 4000;

    var jobHasFinished = false; // variable for detecting if job has finished

    var jobBuilder = _computClient.CreateJob(projectId, containerName, arguments, cpuCores, memoryMB, environmentVariables)
        .WithPlatformInput(projectId, localPath: "input/data")
        .WithPlatformOutput("output", "")
        .WithMessageHandler(m =>
            {
                jobHasFinished = m.Status == JobStateType.Finished;
                return Task.CompletedTask;
            },
            p =>
            {
                Console.WriteLine($"Progress: {p.Message}");
                return Task.CompletedTask;
            },
            e =>
            {
                Console.WriteLine($"Error during job execution {e.Message}");
                return Task.CompletedTask;
            }
        );

    // subscribe to events by using the monitor object
    await using var monitor = await jobBuilder.ExecuteAndMonitorAsync();

    // wait for job to finish
    while (!jobHasFinished)
    {
        await Task.Delay(2000);
    }

This page demonstrated how to execute one off jobs. The job service provides also the possibility to schedule jobs for regular repeated execution. Please refer to the Jobs section of the Platform documentation for more details.