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

Add Cronjob Extension #196

Merged
merged 1 commit into from
Nov 17, 2024
Merged

Add Cronjob Extension #196

merged 1 commit into from
Nov 17, 2024

Conversation

lmangani
Copy link
Contributor

@lmangani lmangani commented Nov 17, 2024

DuckDB CronJob Extension

The DuckDB Cron extension adds support for scheduled query execution within DuckDB. It allows you to schedule SQL queries to run periodically using standard cron expressions while your DuckDB process is active.

⚠️ The initial version lacks any persistence. Contributions welcome!

Experimental: USE AT YOUR OWN RISK!

Cron Runner

-- Every 15 seconds during hours 1-4
SELECT cron('SELECT now()', '*/15 * 1-4 * * *');

-- Every 2 hours (at minute 0, second 0) during hours 1-4
SELECT cron('SELECT version()', '0 0 */2 1-4 * *');

-- Every Monday through Friday at 7:00:00 AM
SELECT cron('SELECT cleanup()', '0 0 7 ? * MON-FRI');

The function returns a job ID that can be used to manage the scheduled task.

┌──────────┐
│  job_id  │
│  varchar │
├──────────┤
│ task_0   │
└──────────┘

Supported Expressions

The extension uses six-field cron expressions:

┌───────────── second (0 - 59)
│ ┌───────────── minute (0 - 59)
│ │ ┌───────────── hour (0 - 23)
│ │ │ ┌───────────── day of month (1 - 31)
│ │ │ │ ┌───────────── month (1 - 12)
│ │ │ │ │ ┌───────────── day of week (0 - 6) (Sunday to Saturday)
│ │ │ │ │ │
* * * * * *
Special characters:
  • *: any value
  • ?: no specific value (used in day-of-month/day-of-week)
  • ,: value list separator
  • -: range of values
  • /: step values
  • MON-SUN: named weekdays (case sensitive)
Common Patterns:
0 0 * * * *: Top of every hour
0 */15 * * * *: Every 15 minutes
0 0 0 * * *: Once per day at midnight
0 0 12 ? * MON-FRI: Weekdays at noon
0 0 0 1 * ?: First day of every month

Listing Jobs

Use the cron_jobs() table function to view all scheduled jobs and their status:

SELECT * FROM cron_jobs();

┌─────────┬──────────────────┬────────────────┬──────────────────────────┬─────────┬──────────────────────────┬─────────────┐
│ job_id  │      query       │    schedule    │         next_run         │ status  │         last_run         │ last_result │
│ varcharvarcharvarcharvarcharvarcharvarcharvarchar   │
├─────────┼──────────────────┼────────────────┼──────────────────────────┼─────────┼──────────────────────────┼─────────────┤
│ task_0  │ SELECT version() │ */10 * * * * * │ Fri Nov 15 20:44:20 2024 │ Active  │ Fri Nov 15 20:44:10 2024 │ Success     │
└─────────┴──────────────────┴────────────────┴──────────────────────────┴─────────┴──────────────────────────┴─────────────┘

Returns:

  • job_id: Unique identifier for the job
  • query: The SQL query to execute
  • schedule: The cron expression
  • next_run: Next scheduled execution time
  • status: Current status (Active/Inactive)
  • last_run: Last execution timestamp
  • last_result`: Result of the last execution

Deleting Jobs

To delete a scheduled job:

SELECT cron_delete('task_0');

@lmangani lmangani marked this pull request as ready for review November 17, 2024 12:54
@carlopi carlopi merged commit 29499e2 into duckdb:main Nov 17, 2024
37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants