worker
worker copied to clipboard
Arbitrary functions for cron patterns
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
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
- 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.
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:
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 ?
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.
Fixed in #272