Click or drag to resize

Distributed CRON

Cadence allows you to execute workflows on a recurring schedule, like a traditional Linux CRON job. All you need to do is initialize a WorkflowOptions instance, setting the CronSchedule string to the schedule. This string is formatted just like a Linux CRON schedule. This handy calculator shows how this works: https://crontab.guru

Here's how you can start a CRON workflow:

C#
[WorkflowInterface(TaskList = "my-tasks")]
public interface ICronWorkflow : IWorkflow
{
    [WorkflowMethod(Name = "backup")]
    Task BackupAsync();
}

public static class Program
{
    public static async Task Main(string[] args)
    {
        // Connect to Cadence

        var settings = new CadenceSettings()
        {
            DefaultDomain = "my-domain",
            CreateDomain  = true,
            Servers       = new List<string>() { "cadence://localhost:7933" }
        };

        using (var client = await CadenceClient.ConnectAsync(settings))
        {
            var stub = client.NewWorkflowFutureStub<ICronWorkflow>(
                "backup",
                new WorkflowOptions()
                {
                    // Run the workflow every day at 1:00am UTC:
                    CronSchedule = "0 1 * * *"
                }); ;

            await stub.StartAsync();
        }
    }
}

This example assumes that a workflow service implementing the ICronWorkflow is already running somewhere. The program connects to to Cadence and then calls NewWorkflowFutureStubTWorkflowInterface passing the workflow method name. In this example, the name is backup which matches the name specified by the WorkflowMethodAttribute tagging the interface method.

Note Note

Workflow methods that are not tagged with a name, can be specified by passing null or Empty.x

NewWorkflowFutureStubTWorkflowInterface returns an untyped workflow method stub that doesn't immediately wait for the workflow to complete. This is useful for situations like starting a CRON workflow or for running multiple workflows in parallel.

See Also

Reference