New infinitic.io Website!

v0.15.0

Playbook

Building A Workflow Scheduler

The code examples for pages in this section are available on Github.

In many applications, there's a need to execute workflows on a recurring basis, following a specific schedule.

This guide demonstrates how to implement a RecurringWorkflowScheduler that dispatches a RecurringWorkflow at regular intervals based on a specified cron expression.

For this example, we will consider this contract for the recurring workflow:

The @Name annotation is used by Infinitic for Workflow and Service identification.

We'll start by defining a RecurringWorkflowScheduler interface with a schedule method. This method takes two parameters:

  • cronExpr: A string representing the cron expression (e.g., "30 * * * *")
  • input: The RecurringWorkflowInput data that will be used for each RecurringWorkflow instance dispatched.

Here is an example of implementation of a fully functional recurring workflow scheduler:

This implementation utilizes the cron-utils package to determine the timing of recurring executions based on cron expressions. Here's a breakdown:

The RecurringWorkflowSchedulerImpl class:

  • Extends the Workflow class provided by Infinitic and implements the RecurringWorkflowScheduler contract
  • Initializes a CronParser for Unix-style cron expressions (the @Ignore annotation prevents Infinitic from serializing the parser field).

The schedule method handles the scheduling logic:

  1. Cron Expression Parsing: The provided cron expression is parsed into a Cron object.

  2. Current Time Retrieval: Uses Infinitic's inline() function to get the current time, ensuring proper handling of non-deterministic operations.

  3. Next Execution Calculation: Determines the next execution time based on the parsed cron expression and current time.

  4. Execution Flow: If a next execution time exists, the method:

    a. Uses the workflow's timer function to wait until the next execution time.

    b. Dispatches the recurring workflow once the timer completes.

    c. Recursively schedules the next occurrence by dispatching a new method call on the same workflow instance.

The recursive scheduling approach (step 4c above) is crucial for efficient workflow history management: By dispatching a new method on the same workflow instance for each occurrence, the workflow history doesn't accumulate indefinitely, as Infinitic deletes the history for each completed method execution.

To start the scheduler:

To stop the scheduler:

A typical output (from the worker console) should be something like:

17:14:00 - Instance: 0190980f-06c0-7f98-8189-a5dcd989c862
17:15:00 - Instance: 0190980f-f120-7d25-95f7-1201ee776fbf
17:16:00 - Instance: 01909810-db80-7c8c-af00-225fcb7177cb
17:17:00 - Instance: 01909811-c5e0-7cea-9e99-6085ab762239
17:18:00 - Instance: 01909812-b040-76f7-9cf3-8e39775c78ef
17:19:00 - Instance: 01909813-9aa0-79cc-9841-409b15f21f6c
17:20:00 - Instance: 01909814-8500-79c9-b651-ed1596dee3e1
Previous
Features