# Temporal Cron Questions

**URL:** <https://community.temporal.io/t/temporal-cron-questions/201>\
**Category:** Community Support\
**Tags:** java-sdk\
**Created:** [July 16, 2020, 12:30am UTC](https://community.temporal.io/t/temporal-cron-questions/201 "2020-07-16T00:30:53Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![xwang](https://avatars.discourse-cdn.com/v4/letter/x/ac8455/32.png) [@xwang](https://community.temporal.io/u/xwang)\
**Post date:** [July 16, 2020, 12:30am UTC](https://community.temporal.io/t/temporal-cron-questions/201/1 "2020-07-16T00:30:53Z")

</div>

Hi,

I’m planning to use a cron workflow to schedule tasks periodically. The task runs every 15 minutes and would fan out more workflows consumed by another service.

I have the following questions regarding how to set up the cron:

1. If I want this cron triggered by only 1 instance at a time and running in a dedicated service. Should I just trigger the cron workflow during service start up time?
2. If yes to Q1, when I redeploy the service each time, this would mean a new cron workflow will be triggered, if it is using the same workflow id, will this cause failures?
3. If during the redeployment, I change some config of the workflow, like the cron schedule from 15 min to 5 min, How will Temporal pick up the change and make sure all subsequent runs will execute with the latest version? (I dont care existing scheduled run)?
4. Is there a good way to stop or pause the cron workflow?

Thanks!  
Bill

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [July 16, 2020, 1:00am UTC](https://community.temporal.io/t/temporal-cron-questions/201/3 "2020-07-16T01:00:57Z")

</div>

1. There is no problem triggering workflow by more than one instance. Temporal ensures the uniqueness of workflow executions by their WorkflowId. So if your triggering code is using the same ID then there will be only one workflow execution running. The trigger code should ignore the WorkflowExecutionAlreadyStarted exception which is thrown in the case of duplicated starts.
2. No problem here.
3. Currently, there is no way to update a cron workflow schedule directly without stopping the cron workflow. The workarounds are:  
3.1. Triggering code can query already running the workflow for its cron schedule and then terminate and start it again if it changed.  
3.2. Do not use “cron” workflow feature and implement your own periodic workflow that can check if cron schedule has changed using activity and sleep accordingly. Here is a [periodic workflow](https://github.com/temporalio/java-samples/blob/master/src/main/java/io/temporal/samples/hello/HelloPeriodic.java) example. And [here is an example](https://github.com/temporalio/java-samples/tree/master/src/main/java/io/temporal/samples/updatabletimer) of updating timer on the signal which is also relevant.

---

<div class="post-metadata">

**Author:** ![xwang](https://avatars.discourse-cdn.com/v4/letter/x/ac8455/32.png) [@xwang](https://community.temporal.io/u/xwang)\
**Post date:** [July 16, 2020, 2:33am UTC](https://community.temporal.io/t/temporal-cron-questions/201/4 "2020-07-16T02:33:59Z")

</div>

Thanks a lot for the reply! @maxim

---

<div class="post-metadata">

**Author:** ![RobZienert](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/robzienert/32/469_2.png) [@RobZienert](https://community.temporal.io/u/RobZienert)\
**Post date:** [June 22, 2021, 12:27am UTC](https://community.temporal.io/t/temporal-cron-questions/201/5 "2021-06-22T00:27:43Z")

</div>

Would calling `continueAsNew` on the workflow also pick up the new cron schedule?

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [June 22, 2021, 1:13am UTC](https://community.temporal.io/t/temporal-cron-questions/201/6 "2021-06-22T01:13:38Z")

</div>

I believe it does pick it up. But please test.

---

<div class="post-metadata">

**Author:** ![blynch](https://avatars.discourse-cdn.com/v4/letter/b/eada6e/32.png) [@blynch](https://community.temporal.io/u/blynch)\
**Post date:** [June 14, 2022, 9:22pm UTC](https://community.temporal.io/t/temporal-cron-questions/201/8 "2022-06-14T21:22:36Z")

</div>

> [@maxim](#):
>
> 3.1. Triggering code can query already running the workflow for its cron schedule and then terminate and start it again if it changed.

@maxim Is there an example for how to do this (with the Java SDK)? I can’t seem to find the API for querying an existing cron schedule.

`Workflow.getInfo().getCronSchedule()` exposes this, but how can I query it from outside of the workflow?

---

<div class="post-metadata">

**Author:** ![tihomir](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/tihomir/32/6580_2.png) [@tihomir](https://community.temporal.io/u/tihomir)\
**Post date:** [June 14, 2022, 10:29pm UTC](https://community.temporal.io/t/temporal-cron-questions/201/9 "2022-06-14T22:29:30Z")

</div>

> Would calling `continueAsNew` on the workflow also pick up the new cron schedule?

No, currently if a cron workflow calls continueAsNew the created new run loses cronSchedule prop.

---

<div class="post-metadata">

**Author:** ![tihomir](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/tihomir/32/6580_2.png) [@tihomir](https://community.temporal.io/u/tihomir)\
**Post date:** [June 14, 2022, 10:40pm UTC](https://community.temporal.io/t/temporal-cron-questions/201/10 "2022-06-14T22:40:48Z")

</div>

You would need to get the workflow history, and and get the cronSchedule from the first event.  
Example:

```auto
public static String getCronSchedule(WorkflowExecution wfExec) {
     GetWorkflowExecutionHistoryRequest req = GetWorkflowExecutionHistoryRequest.newBuilder()
                .setNamespace(client.getOptions().getNamespace())
                .setExecution(wfExec)
                .build();
     GetWorkflowExecutionHistoryResponse res =
                service.blockingStub().getWorkflowExecutionHistory(req);
     HistoryEvent firstEvent = res.getHistory().getEvents(0);
     return firstEvent.getWorkflowExecutionStartedEventAttributes().getCronSchedule();
}

```

You can invoke it with:

```auto
getCronSchedule(WorkflowExecution.newBuilder()
                .setWorkflowId("<wokflow_id>")
                .setRunId("<workflow_run_id>")
                .build())

```

---

<div class="post-metadata">

**Author:** ![blynch](https://avatars.discourse-cdn.com/v4/letter/b/eada6e/32.png) [@blynch](https://community.temporal.io/u/blynch)\
**Post date:** [June 14, 2022, 11:12pm UTC](https://community.temporal.io/t/temporal-cron-questions/201/11 "2022-06-14T23:12:08Z")

</div>

> [@tihomir](#):
>
> You would need to get the workflow history, and and get the cronSchedule from the first event.

Thank you @tihomir. After writing my question, I also found that we can use an untyped workflow stub: `WorkflowStub#getOptions()#getCronSchedule()`. This should work as well?

---

<div class="post-metadata">

**Author:** ![tihomir](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/tihomir/32/6580_2.png) [@tihomir](https://community.temporal.io/u/tihomir)\
**Post date:** [June 14, 2022, 11:20pm UTC](https://community.temporal.io/t/temporal-cron-questions/201/12 "2022-06-14T23:20:20Z")

</div>

Yes, if you have a running execution you could do something like:

```auto
// create new wf stub for existing exec
MyWorkflow workflow = client.newWorkflowStub(MyWorkflow.class, "<workflow_id>");
// convert to untyped
WorkflowStub untyped = WorkflowStub.fromTyped(workflow);
String cron = untyped.getOptions().get().getCronSchedule();

```

or use untyped stub directly

```auto
WorkflowStub stub = client.newUntypedWorkflowStub("<workflow_id>", Optional.empty(), Optional.empty());
stub.getOptions().get().getCronSchedule();

```
