fp/packages/temporal/README.md

2.2 KiB

Cron Workflows

DEPRECATED: use Schedules instead.

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/client.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 client.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. temporal server start-dev to start Temporal Server.
  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 my-schedule, Temporal!
Workflow time:  1636333860201
Activity time: 1636333860241
Hello from my-schedule, 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