Hi,
I was going through periodic workflow and Cron workflow, and I have some doubts in how they are different.
-
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)
-
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?
-
How do I stop a Cron/Periodic workflow already started?
-
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?
-
What would be a good use-case for using Cron vs Periodic, if you could give an example ?