-
Notifications
You must be signed in to change notification settings - Fork 207
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 possibility to schedule jobs by interval in seconds #235
Conversation
784cb47
to
cd6af1b
Compare
Thanks for the review! |
I like the options but having thoughts on the interface for setting the interval. Currently we re-implement parsing seconds supporting syntax like 5 seconds and suggesting to users that this is an interval. I think that is a nice idea to leverage how intervals work and peoples familiarity with it but taking it a step further could be better and actually simplify how it works and reduce cases where people get surprised (ie. '3s' and '3 sec' are both valid intervals). In PG you can overload functions, so add cron.schedule taking text (cron definition) and cron.schedule taking actual interval. postgres=# \df
List of functions
Schema | Name | Result data type | Argument data types | Type
--------+------+------------------+---------------------+------
public | test | void | p_schedule interval | func
public | test | void | p_schedule text | func
(2 rows)
postgres=# select test('* * * *');
NOTICE: Text with * * * *
test
------
(1 row)
postgres=# select test('1 s'::interval);
NOTICE: Interval with 00:00:01
test
------
(1 row) This way users can have the parsing be done by PostgreSQL, and you can simply discard out of range intervals. Alternatively you could take it further and try to support casting INTERVAL to a cron schedule where possible. This way you could allow using the interval syntax to define jobs that happen every N days, hours, minutes and convert that to their cron counterparts. So that |
cd6af1b
to
57becc9
Compare
57becc9
to
c15004f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool 🎉
I think this change broke some valid cron syntax, such as |
Hmz, that's bad. I'll look into it |
By popular demand, fixes #6.
Extends the schedule syntax to allow defining jobs based on an interval in seconds that starts when pg_cron learns about the job, shortly after scheduling or server boot. The interval is measured from the start of the last run. If a run takes longer than the interval, the next run starts immediately after, which resets the interval timer.
We opted to define a job purely based on an interval because adding another part to the cron schedule would make the schedule hard to read, incompatible with typical crontab tools, and the level of control over when in the week the "per x second" job happens seems unnecessary. Moreover, we usually cannot guarantee when a job happens down to the second on busy servers, so we'll make no attempt to do so.
Syntax is as follows, inspired by
interval
syntax, but only positive whole seconds are supported.