Periodic Workflow Vs Cron Workflow

Hi,

I was going through periodic workflow and Cron workflow, and I have some doubts in how they are different.

  1. In both cases - will a new workflow be initiated irrespective of the previous workflow failed or completed ( I don’t think This will be the case for periodic)

  2. In case of periodic workflow, why are we doing:

           while (true) {
          // Print reason of failure of the previous run, before restarting.
          if (execution != null) {
            WorkflowStub workflow = client.newUntypedWorkflowStub(execution, Optional.empty());
            try {
              workflow.getResult(Void.class);
            } catch (WorkflowException e) {
              System.out.println("Previous instance failed:\n" + Throwables.getStackTraceAsString(e));
            }
          }
          // New stub instance should be created for each new workflow start.
          GreetingWorkflow workflow =
              client.newWorkflowStub(
                  GreetingWorkflow.class,
                  // At most one instance.
                  WorkflowOptions.newBuilder()
                      .setWorkflowId(PERIODIC_WORKFLOW_ID)
                      .setTaskQueue(TASK_QUEUE)
                      .setWorkflowRunTimeout(Duration.ofMillis(1))
                      .build());
          try {
            execution = WorkflowClient.start(workflow::greetPeriodically, "World");
            System.out.println("Started " + execution);
          } catch (WorkflowExecutionAlreadyStarted e) {
            System.out.println("Still running as " + e.getExecution());
          } catch (Throwable e) {
            e.printStackTrace();
            System.exit(1);
          }
          // This value is so low just for the sample purpose. In production workflow
          // it is usually much higher.
          Thread.sleep(10000);
        }

when we can do:

    @Override
    public void greetPeriodically(String name) {
        activities.greet("Hello " + name + "! Sleeping for " + 10000 + " milliseconds.");
        Workflow.sleep(10000);
      continueAsNew.greetPeriodically(name);// ==> Also what is the use of this line, if we anyhow initiate workflows in a loop 
      // unreachable line
}

Why are we initiating the workflows in a loop? is this because, if the prev initiated workflow fails, then the line continueAsNew.greetPeriodically(name); will never be reached hence there will be no Next workflow, ultimately losing its periodicity?

  1. How do I stop a Cron/Periodic workflow already started?

  2. Suppose There is some code change in my periodic/cron workflow already in use. How can I ensure that the already running instances will start using the new code, Will versioning solve this problem?

  3. What would be a good use-case for using Cron vs Periodic, if you could give an example ?

The periodic sample is outdated. It was written before workflow retries were implemented. Currently if there is a need to ensure that a workflow is restarted even in case of its code failing a retry options should be used.

  1. How do I stop a Cron/Periodic workflow already started?

Terminate it.

  1. Suppose There is some code change in my periodic/cron workflow already in use. How can I ensure that the already running instances will start using the new code, Will versioning solve this problem?

Yes, versioning is the safest way to deploy code changes while workflows are running.

  1. What would be a good use-case for using Cron vs Periodic, if you could give an example ?

Cron is very simple and doesn’t give you any control over schedule. Periodic gives you the full control. For example, you can change when the next execution happens through a signal. See updatableTimer sample.

1 Like

Thank you!

Is it Possible to Provide a Sample Implementation of Periodic workflow Since the current Implementation is out dated?

The implementation is fine. The only outdated part is that loop that starts the instance.

1 Like

Could you please tell me what will I not have if I use Cron instead for Periodic workflow? I am not clear on the advantages.

If you just need periodic execution of some logic cron is fine. Periodic workflow gives you full control over scheduling logic. For example you can choose different sleep interval for each invocation if needed.

1 Like