Skip to content

Simple cronjobs

Defining a simple cron job (scheduled job)

The POST/api/process/cronjob endpoint can be used for defining a cron job. It is very similar to creating a single running job, but there is an extra property:

  • schedule: Defines how often should the job be executed. See Schedule
Click to show simple example to define a cron job
{
    "Schedule": "0 * * * *",
    "Runtime": {
        "type": "ContainerRuntimeSpec",
        "Containers": [
            {
                "Image": "busybox",
                "Command": ["/bin/sh", "-c", "ls"]
            }
        ] 
    },
}
var cronJobBuilder = _computeClient
        .CreateJob(_projectId, "busybox",new string [] {"/bin/sh", "-c", "ls"]});
await cronJobBuilder.ScheduleAsync("0 * * * *");

Console.WriteLine($"Cron job id {cronJob.CronJobId}");
Click to show example with platform data input/output
{
    "Schedule": "0 * * * *",
    "Runtime": {
        "type": "ContainerRuntimeSpec",
        "Containers": [
            {
                "Image": "busybox",
                "Command": ["/bin/sh", "-c", "cd /work/input/; ls > /work/output/workls.txt"]
            }
        ]
    },
    "InputData": {
        "Locations": [
            {
                "type": "PlatformInputLocation",
                "ProjectId": "00000000-0000-0000-0000-000000000000",
                "LocalPath": "input",
                "ExcludedFiles": ["somefile.txt"]
            }
        ]
    },
    "OutputData": {
        "Locations": [
            {
                "type": "PlatformOutputLocation",
                "LocalPath": "output",
                "RelativePlatformPath": ""
            }
        ]
    }
}
var cronJob = _computeClient
        .CreateCronJob(_projectId, "busybox",new string [] {"/bin/sh", "-c", "cd /work/input/; ls > /work/output/workls.txt"]})
        .WithPlatformInput(_projectId,"input");
        .WithPlatformOutput("output",string.Empty)
        .ScheduleAsync("0 * * * *");

Console.WriteLine($"CronJob id {cronJob.CronJobId}");

Subscribing to job created events

When a cron job is created, typically no new jobs are executed immedately. New jobs are executed according to the schedule. In order to get details and monitor individual job executions, you can subscribe to an event that indicates that a the cron job has executed a new job instance.

Show SDK example subscribing to job events.
var jobMonitor = _computeClient
          .CreateJob(_projectId, "busybox",new string [] {"/bin/sh", "-c", "l.txt"]})
          .WithMessageHandler(onData)
          .SechduleAndMonitorAsync("*/2 * * * *");
...
Task onData(JobMessage message)
{
    //process message
    return Task.CompletedTask;
}

Note that IDs of executed job instances are also recorded in array RelatedJobIds in the cron job details you get get by The GET/api/process/cronjob/{id}. This array contains IDs of up to 10 latest job instances. You can use these IDs with job endpoints as any other job ID.

Deleting a cron job

The DELETE/api/process/cronjob/{id} endpoint can be used to delete a cron job. Deleting a cron job will also delete all currently running instances of the cron job and no further instances will be executed.