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

Support for seconds (possibly milliseconds) #1

Closed
rossipedia opened this issue Jun 29, 2014 · 4 comments
Closed

Support for seconds (possibly milliseconds) #1

rossipedia opened this issue Jun 29, 2014 · 4 comments

Comments

@rossipedia
Copy link
Contributor

Seconds

Proposed keywords: s, sec, seconds, secondofminute, secondsofminute (for consistency).

This would allow describing a repeating task at sub-second resolution, rather than the default smallest unit being minutes, which could be useful if you wanted a repeating task say on the minute and at :30 seconds (twice a minute).

Also, milliseconds could possibly be useful, although when you need sub-1s resolution, I'm not sure that aligning the events to a specific millisecond is really useful.

What do you think?

@bretcope
Copy link
Member

I completely agree, and was thinking about that today. Those are definitely the right aliases. Also, implementation should be really simple.

Milliseconds seems like too much and if you actually need a task to run at an exact millisecond, JavaScript may not be the best language to help you out with that anyway.

I was also thinking about adding an interval syntax similar to the cron */2 syntax which would run every two of a time unit (i.e. every two minutes). Or 5-15/2 which runs every two minutes after the fifth minute until the 15th minute.

I already used the / for defining dates, and I don't really want to overload that character, so we need to pick something else. The first idea which comes to mind is % since it's essentially a modulus operation anyway. The syntax would be:

  • minutes(5-15%2) runs at 5,7,9,11,13,15
  • minutes(5%2) runs at 5,7,9, ... 57, 59
  • minutes(%2) runs at 0, 2, 4, 6, ... 56, 58

Thoughts?

@rossipedia
Copy link
Contributor Author

👍 on not overloading the / character, keeping the grammar as context free as possible is definitely the right way to go. I'm all for the % op instead.

I really like the syntax, as I think it makes things very terse yet readable. I haven't dug too far into the internals, would the implementation be all that difficult?

@bretcope
Copy link
Member

I actually don't think it'll be too difficult to implement. Internally, I turn all arguments into a range. So, minutes(1) actually expands to minutes(1-1) because it simplifies the comparison algorithms. I'll add a modulus property to ranges in the AST, and then the function which does comparison will just have an extra check if the modulus is present. Essentially amounting to: if (current - lowBound % modulusValue === 0)

@bretcope
Copy link
Member

Seconds is implemented. That was easy, as expected.

Implementing modulus turned out to be a tremendous pain in the ass, specifically because it has to account for split intervals (such as fri-mon). The way I had originally implemented split intervals (by literally just splitting them into two intervals) wasn't going to work with modulus because it wouldn't have the context of the earlier interval in order to base its count-since-start off of. So I had to refactor the range code and all of the comparison code to handle this situation.

The upside is that once split intervals were generalized (instead of only supported on days of week), it meant that enabling split intervals on everything else was trivial. So stuff like minutes(40-20) is supported now.

Anyway, modulus is now implemented and documented in the readme. It could probably use some more examples, but I don't really feel like adding any right this moment.

Published as 0.3.0.

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

No branches or pull requests

2 participants