worker icon indicating copy to clipboard operation
worker copied to clipboard

Arbitrary functions for cron patterns

Open wokalski opened this issue 4 years ago • 5 comments

Feature description

Instead of just passing a cron pattern string we could also enable users to pass a function in the same place (i.e. "* * * * *" would be a shorthand for cron("* * * * *") where the function has to follow the TimeDigest -> boolean interface.

Motivating example

One thing this solves in my specific use case are timezone-dependent patterns. I want to run a job in a given timezone, and make it work consistently even if the time offset changes due to daylight saving.

Supporting development

  • [x] am interested in building this feature myself

wokalski avatar Sep 13 '21 11:09 wokalski

This actually aligns with another feature I've been considering (JIT-ing cron expressions) so yes I'm open to this. Let's see if we can do it as a small non-breaking change to start with, I'd rather do all the breaking changes in one fell-swoop as we ready for a 1.0 release (i.e. later). Try and keep the diff small.

benjie avatar Sep 13 '21 13:09 benjie

@benjie

  1. What name convention would you prefer for it? I thought about allowing both:
pattern: "* * * * *"

as well as

pattern: cron("* * * * *")

Where cron is a function which parses the cron expression.

Alternatively since the string literal representation is a shorthand for it, I wouldn't have anything against naming this function

cronExpression

If we name it cronExpression I wouldn't have anything against creating a dedicated file for it and moving a bunch of stuff over from crontab.ts.

wokalski avatar Sep 13 '21 15:09 wokalski

If we use match it should make sense for both a pattern and a function:

match: string | ((digest: /* readonly? */ TimestampDigest) => boolean)

Let's do that, but fall back to pattern for backwards compatibility. You can mark it @deprecated in TypeScript and rename it in the README. :+1:

benjie avatar Sep 13 '21 17:09 benjie

Ok, good idea. And how about the function of which creates the cron pattern matcher specifically? Should I name it cron or cronExpression something else ?

wokalski avatar Sep 14 '21 05:09 wokalski

I'm not sure we need that function - just pass a string in that case and we'll handle it in the exact same way that we currently do.

benjie avatar Sep 14 '21 08:09 benjie

Fixed in #272

benjie avatar Oct 24 '23 10:10 benjie