Skip to content

Latest commit

 

History

History

cron-workflows

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Cron Workflows

This example demonstrates a working Cron workflow. Note the limitations and caveats listed in the docs.

Differences from the hello world demo:

  • The Workflow is started with the cronSchedule: '* * * * *', option: src/execute-workflow.ts.
  • The Activity actually prints a log, instead of returning a string.
  • The Workflow runs forever, so if we want it to stop, we have to cancel it. In our execute-workflow.ts script, we cancel it using the handle (when Ctrl/Cmd-C is hit). Usually, we'd use the Workflow ID to cancel—for example:
const handle = client.getHandle('1e793a6c-31e2-41c9-8139-53d114293a9e');
await handle.cancel();

Note that when we're changing code and restarting Workers, unless we cancel all previous Workflows, they may get picked up by our Worker (since we likely didn't change our Workflow name or task queue), and their output may conflict/mix with new Workflows we're starting. We can check what is still running in Temporal Web (localhost:8088 in case we need to kill all previous Workflows.

Running this sample

  1. Make sure Temporal Server is running locally (see the quick install guide).
  2. npm install to install dependencies.
  3. npm run start.watch to start the Worker.
  4. In another shell, npm run workflow to run the Workflow.

Example Worker output:

Hello from 075f6172-9151-43d8-9848-b8ddb615a3a5, Temporal!
Workflow time:  1636333860201
Activity time: 1636333860241
Hello from 075f6172-9151-43d8-9848-b8ddb615a3a5, Temporal!
Workflow time:  1636333920319
Activity time: 1636333920340

The difference between "Workflow time" and "Activity time" reflects the latency between scheduling an Activity and actually starting it.

Each new Workflow is continuedAsNew under the hood:

image

And you can see the details in the event history:

image