Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simultaneous Cron Alarms #83

Merged
merged 3 commits into from
Feb 10, 2025
Merged

Simultaneous Cron Alarms #83

merged 3 commits into from
Feb 10, 2025

Conversation

Brayden
Copy link
Member

@Brayden Brayden commented Feb 3, 2025

Purpose

This enables StarbaseDB instances to be able to declare hooks in the /src/index.ts file to execute custom code at defined crontab intervals. By default Durable Objects can support 1 Alarm at a time (and Workers can support 3 cron jobs) but we need the ability to expand it so any number of events can signup to execute code at any interval. See below example for implementation.

Currently there is a hardcoded limit of supporting 10 simultaneous events to trigger at any given interval which can easily be updated by adding more WHEN cases to the query. This was a technical decision for the moment to prevent accidental setups of recursively added calls but can easily be updated to support N events at any time.

Tasks

  • When a request comes in, ensure an alarm is set if a task is defined in tmp_cron_tasks table
  • If multiple tasks should happen at the same time, both should emit an event individually
  • Setup retries for failed alarms

Verify

Each task should have an entry in the tmp_cron_tasks table of your StarbaseDB instance. An example row might look like:

{
    "name": "Even minutes",
    "cron_tab": "*/2 * * * *",
    "payload": "",
    "callback_host": "https://starbasedb-{MY-IDENTIFIER}.workers.dev"
}

Then your code in your /src/index.ts would implement the plugin setup like below:

import { CronPlugin } from '../plugins/cron'

// ....
// ....

const cronPlugin = new CronPlugin()
cronPlugin.onEvent(({ name, cron_tab, payload }) => {
    console.log('CRON EVENT: ', name, cron_tab, payload)

    if (name === 'Even minutes') {
        console.log('Payload: ', JSON.stringify(payload))
    }
}, ctx)

const plugins = [
    // ...
    cronPlugin,
] satisfies StarbasePlugin[]

Before

After

@Brayden Brayden self-assigned this Feb 3, 2025
@Brayden Brayden added the enhancement New feature or request label Feb 3, 2025
Copy link

github-actions bot commented Feb 3, 2025

Coverage Report

Status Category Percentage Covered / Total
🔴 Lines 2.02% (🎯 75%) 21 / 1039
🔴 Statements 1.93% (🎯 75%) 21 / 1086
🔴 Functions 2.89% (🎯 75%) 5 / 173
🔴 Branches 0% (🎯 75%) 0 / 600
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
src/do.ts 0% 0% 0% 0% 21-324
src/index.ts 0% 0% 0% 0% 17-297
Generated in workflow #64 for commit ea30a34 by the Vitest Coverage Report Action

@Brayden Brayden merged commit 3554b6f into main Feb 10, 2025
1 check passed
@Brayden Brayden deleted the bwilmoth/cron-alarms branch February 10, 2025 16:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant