denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
97.12k stars 5.36k forks source link

Feature Request: Enhance `Deno.cron` to Include Seconds in Cron Patterns #21561

Open Hexagon opened 10 months ago

Hexagon commented 10 months ago

Deno.cron uses the saffron crate for parsing cron patterns, which doesn't support seconds. I propose to include seconds in the cron pattern syntax. While not commonly available in "system cron", seconds is vital when dealing with in-application scheduling which Deno.cron is used for.

A list of possible use cases for second granularity scheduling with Deno.cron:

Example:

Deno.cron("example", "*/10 * 10,22 * * *", () => {
    console.log("This function will run every 10 seconds at 10AM and 10PM.");
});

Possible Solution:

I suggest checking out my drop in replacement for saffron called croner. This extends the existing cron pattern syntax to include seconds. It's designed to set seconds to 0 by default if omitted in a 5-part pattern, ensuring backward compatibility. It also include other improvements such as time-zone support and POSIX compatible cron syntax.

Hexagon commented 10 months ago

From version 2.0.0, croner uses minute granularity by default, but can optionally enable seconds or require them by using the builder methods .with_seconds_optional() and .with_seconds_required(). Therefore, technically, second granularity could be an option for Deno.cron().

zadeviggers commented 10 months ago

This would be helpful for our use-case of tracking the connection status of our sensor network - we currently use setInterval to go through every 15 seconds and mark sensors that we haven't received a packet from recently as offline. However setInterval has it's own problems, one of which is clarity on how often it runs. Supporting seconds in Deno.cron would help with this.

There is also a good example of cron syntax that supports seconds here - just type in 6 *s.

image