You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@kvrocks.apache.org by "kinoute (via GitHub)" <gi...@apache.org> on 2024/06/09 16:21:05 UTC
[PR] Add support for "*/n" interval cronjob syntax [kvrocks]
kinoute opened a new pull request, #2360:
URL: https://github.com/apache/kvrocks/pull/2360
Currently KVRocks only supports integers and * in crontab format. I thought it would be nice to add support for the `*/n` interval syntax. I understand that we can already add multiple crontab entries for the same feature like this:
```
bgsave-cron 0 3 * * * 0 6 * * * (and more...)
```
But it would convenient if we could do something like this instead:
```
bgsave-cron 0 */3 * * *
```
Again, I'm new to C++ so no offense taken if the code provided is not good at all for your standards.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(cron): add support for "*/n" interval cronjob syntax [kvrocks]
Posted by "PragmaTwice (via GitHub)" <gi...@apache.org>.
PragmaTwice commented on code in PR #2360:
URL: https://github.com/apache/kvrocks/pull/2360#discussion_r1633213770
##########
src/common/cron.cc:
##########
@@ -84,20 +99,30 @@ StatusOr<Scheduler> Cron::convertToScheduleTime(const std::string &minute, const
const std::string &wday) {
Scheduler st;
- st.minute = GET_OR_RET(convertParam(minute, 0, 59));
- st.hour = GET_OR_RET(convertParam(hour, 0, 23));
- st.mday = GET_OR_RET(convertParam(mday, 1, 31));
- st.month = GET_OR_RET(convertParam(month, 1, 12));
- st.wday = GET_OR_RET(convertParam(wday, 0, 6));
+ st.minute = GET_OR_RET(convertParam(minute, 0, 59, st.minute_interval));
+ st.hour = GET_OR_RET(convertParam(hour, 0, 23, st.hour_interval));
+ st.mday = GET_OR_RET(convertParam(mday, 1, 31, st.mday_interval));
+ st.month = GET_OR_RET(convertParam(month, 1, 12, st.month_interval));
+ st.wday = GET_OR_RET(convertParam(wday, 0, 6, st.wday_interval));
return st;
}
-StatusOr<int> Cron::convertParam(const std::string ¶m, int lower_bound, int upper_bound) {
+StatusOr<int> Cron::convertParam(const std::string ¶m, int lower_bound, int upper_bound, bool &is_interval) {
if (param == "*") {
return -1;
}
+ // Check for interval syntax (*/n)
+ if (util::HasPrefix(param, "*/")) {
+ auto s = ParseInt<int>(param.substr(2), {lower_bound, upper_bound}, 10);
+ if (!s) {
+ return std::move(s).Prefixed(fmt::format("malformed cron token `{}`", param));
+ }
Review Comment:
```suggestion
if (!s || *s == 0) {
return std::move(s).Prefixed(fmt::format("malformed cron token `{}`", param));
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(cron): add support for "*/n" interval cronjob syntax [kvrocks]
Posted by "PragmaTwice (via GitHub)" <gi...@apache.org>.
PragmaTwice commented on code in PR #2360:
URL: https://github.com/apache/kvrocks/pull/2360#discussion_r1634929136
##########
src/common/cron.cc:
##########
@@ -57,10 +62,21 @@ bool Cron::IsTimeMatch(const tm *tm) {
tm->tm_mon == last_tm_.tm_mon && tm->tm_wday == last_tm_.tm_wday) {
return false;
}
+
+ auto match = [](int current, int val, bool interval) {
+ if (val == -1) return true;
+ if (interval) return current % val == 0;
+ return val == current;
+ };
Review Comment:
```suggestion
auto match = [](int current, int val, bool interval, int min_val) {
if (val == -1) return true;
if (interval) return (current - min_val) % val == 0;
return val == current;
};
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(cron): add support for "*/n" interval cronjob syntax [kvrocks]
Posted by "PragmaTwice (via GitHub)" <gi...@apache.org>.
PragmaTwice commented on code in PR #2360:
URL: https://github.com/apache/kvrocks/pull/2360#discussion_r1634929136
##########
src/common/cron.cc:
##########
@@ -57,10 +62,21 @@ bool Cron::IsTimeMatch(const tm *tm) {
tm->tm_mon == last_tm_.tm_mon && tm->tm_wday == last_tm_.tm_wday) {
return false;
}
+
+ auto match = [](int current, int val, bool interval) {
+ if (val == -1) return true;
+ if (interval) return current % val == 0;
+ return val == current;
+ };
Review Comment:
```suggestion
auto match = [](int current, int val, bool interval, int min_val) {
if (val == -1) return true;
if (interval) return (current - min_val) % (val - min_val) == 0;
return val == current;
};
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(cron): add support for "*/n" interval cronjob syntax [kvrocks]
Posted by "kinoute (via GitHub)" <gi...@apache.org>.
kinoute commented on code in PR #2360:
URL: https://github.com/apache/kvrocks/pull/2360#discussion_r1637642191
##########
src/common/cron.cc:
##########
@@ -57,10 +62,21 @@ bool Cron::IsTimeMatch(const tm *tm) {
tm->tm_mon == last_tm_.tm_mon && tm->tm_wday == last_tm_.tm_wday) {
return false;
}
+
+ auto match = [](int current, int val, bool interval, int interval_offset) {
+ if (val == -1) return true;
+ if (interval) return (current - interval_offset) % val == 0;
+ return val == current;
+ };
+
for (const auto &st : schedulers_) {
- if ((st.minute == -1 || tm->tm_min == st.minute) && (st.hour == -1 || tm->tm_hour == st.hour) &&
- (st.mday == -1 || tm->tm_mday == st.mday) && (st.month == -1 || (tm->tm_mon + 1) == st.month) &&
- (st.wday == -1 || tm->tm_wday == st.wday)) {
+ bool minute_match = match(tm->tm_min, st.minute, st.minute_interval, 0);
+ bool hour_match = match(tm->tm_hour, st.hour, st.hour_interval, 0);
+ bool mday_match = match(tm->tm_mday, st.mday, st.mday_interval, 0);
Review Comment:
@PragmaTwice Fixed
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(cron): add support for "*/n" interval cronjob syntax [kvrocks]
Posted by "PragmaTwice (via GitHub)" <gi...@apache.org>.
PragmaTwice commented on code in PR #2360:
URL: https://github.com/apache/kvrocks/pull/2360#discussion_r1632521567
##########
src/common/cron.cc:
##########
@@ -58,9 +63,21 @@ bool Cron::IsTimeMatch(const tm *tm) {
return false;
}
for (const auto &st : schedulers_) {
- if ((st.minute == -1 || tm->tm_min == st.minute) && (st.hour == -1 || tm->tm_hour == st.hour) &&
- (st.mday == -1 || tm->tm_mday == st.mday) && (st.month == -1 || (tm->tm_mon + 1) == st.month) &&
- (st.wday == -1 || tm->tm_wday == st.wday)) {
+ bool minuteMatch = (st.minute == -1 || tm->tm_min == st.minute ||
+ (st.minute > 0 && st.minute <= 59 && tm->tm_min % st.minute == 0) ||
+ (st.minute == 0 && tm->tm_min == 0));
Review Comment:
`st.minute == 0 && tm->tm_min == 0` seems useless since it's covered in `tm->tm_min == st.minute`.
`st.minute > 0 && st.minute <= 59 && tm->tm_min % st.minute == 0`: from my understanding, I think we need to check `st.minute_interval` to see if it's `true`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(cron): add support for "*/n" interval cronjob syntax [kvrocks]
Posted by "PragmaTwice (via GitHub)" <gi...@apache.org>.
PragmaTwice commented on code in PR #2360:
URL: https://github.com/apache/kvrocks/pull/2360#discussion_r1634931860
##########
src/common/cron.cc:
##########
@@ -57,10 +62,21 @@ bool Cron::IsTimeMatch(const tm *tm) {
tm->tm_mon == last_tm_.tm_mon && tm->tm_wday == last_tm_.tm_wday) {
return false;
}
+
+ auto match = [](int current, int val, bool interval) {
+ if (val == -1) return true;
+ if (interval) return current % val == 0;
+ return val == current;
+ };
+
for (const auto &st : schedulers_) {
- if ((st.minute == -1 || tm->tm_min == st.minute) && (st.hour == -1 || tm->tm_hour == st.hour) &&
- (st.mday == -1 || tm->tm_mday == st.mday) && (st.month == -1 || (tm->tm_mon + 1) == st.month) &&
- (st.wday == -1 || tm->tm_wday == st.wday)) {
+ bool minute_match = match(tm->tm_min, st.minute, st.minute_interval);
+ bool hour_match = match(tm->tm_hour, st.hour, st.hour_interval);
+ bool mday_match = match(tm->tm_mday, st.mday, st.mday_interval);
+ bool month_match = match(tm->tm_mon - 1, st.month, st.month_interval);
+ bool wday_match = match(tm->tm_wday, st.wday, st.wday_interval);
Review Comment:
```suggestion
bool minute_match = match(tm->tm_min, st.minute, st.minute_interval, 0);
bool hour_match = match(tm->tm_hour, st.hour, st.hour_interval, 0);
bool mday_match = match(tm->tm_mday, st.mday, st.mday_interval, 1);
bool month_match = match(tm->tm_mon + 1, st.month, st.month_interval, 1);
bool wday_match = match(tm->tm_wday, st.wday, st.wday_interval, 0);
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(cron): add support for "*/n" interval cronjob syntax [kvrocks]
Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #2360:
URL: https://github.com/apache/kvrocks/pull/2360#issuecomment-2157689067
## [](https://sonarcloud.io/dashboard?id=apache_kvrocks&pullRequest=2360) **Quality Gate passed**
Issues
 [0 New issues](https://sonarcloud.io/project/issues?id=apache_kvrocks&pullRequest=2360&resolved=false&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_kvrocks&pullRequest=2360&resolutions=WONTFIX)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_kvrocks&pullRequest=2360&resolved=false&sinceLeakPeriod=true)
 No data about Coverage
 No data about Duplication
[See analysis details on SonarCloud](https://sonarcloud.io/dashboard?id=apache_kvrocks&pullRequest=2360)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(cron): add support for "*/n" interval cronjob syntax [kvrocks]
Posted by "PragmaTwice (via GitHub)" <gi...@apache.org>.
PragmaTwice commented on code in PR #2360:
URL: https://github.com/apache/kvrocks/pull/2360#discussion_r1634929136
##########
src/common/cron.cc:
##########
@@ -57,10 +62,21 @@ bool Cron::IsTimeMatch(const tm *tm) {
tm->tm_mon == last_tm_.tm_mon && tm->tm_wday == last_tm_.tm_wday) {
return false;
}
+
+ auto match = [](int current, int val, bool interval) {
+ if (val == -1) return true;
+ if (interval) return current % val == 0;
+ return val == current;
+ };
Review Comment:
```suggestion
auto match = [](int current, int val, bool interval, int min_val) {
if (val == -1) return true;
if (interval) return (current - min_val) % val == 0;
return val == current;
};
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(cron): add support for "*/n" interval cronjob syntax [kvrocks]
Posted by "kinoute (via GitHub)" <gi...@apache.org>.
kinoute commented on PR #2360:
URL: https://github.com/apache/kvrocks/pull/2360#issuecomment-2160045229
@PragmaTwice Thanks, updated again the code
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(cron): add support for "*/n" interval cronjob syntax [kvrocks]
Posted by "git-hulk (via GitHub)" <gi...@apache.org>.
git-hulk merged PR #2360:
URL: https://github.com/apache/kvrocks/pull/2360
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(cron): add support for "*/n" interval cronjob syntax [kvrocks]
Posted by "PragmaTwice (via GitHub)" <gi...@apache.org>.
PragmaTwice commented on code in PR #2360:
URL: https://github.com/apache/kvrocks/pull/2360#discussion_r1632518925
##########
kvrocks.conf:
##########
@@ -515,14 +515,14 @@ compaction-checker-range 0-7
# force-compact-file-min-deleted-percentage 10
# Bgsave scheduler, auto bgsave at scheduled time
-# time expression format is the same as crontab(currently only support * and int)
+# Time expression format is the same as crontab (currently only support *, int and */n)
Review Comment:
```suggestion
# Time expression format is the same as crontab (currently only support *, int and */int)
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(cron): add support for "*/n" interval cronjob syntax [kvrocks]
Posted by "kinoute (via GitHub)" <gi...@apache.org>.
kinoute commented on PR #2360:
URL: https://github.com/apache/kvrocks/pull/2360#issuecomment-2157472827
@PragmaTwice Thanks for your suggestions, I updated the code
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(cron): add support for "*/n" interval cronjob syntax [kvrocks]
Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #2360:
URL: https://github.com/apache/kvrocks/pull/2360#issuecomment-2165800943
## [](https://sonarcloud.io/dashboard?id=apache_kvrocks&pullRequest=2360) **Quality Gate passed**
Issues
 [0 New issues](https://sonarcloud.io/project/issues?id=apache_kvrocks&pullRequest=2360&resolved=false&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_kvrocks&pullRequest=2360&resolutions=WONTFIX)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_kvrocks&pullRequest=2360&resolved=false&sinceLeakPeriod=true)
 No data about Coverage
 No data about Duplication
[See analysis details on SonarCloud](https://sonarcloud.io/dashboard?id=apache_kvrocks&pullRequest=2360)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(cron): add support for "*/n" interval cronjob syntax [kvrocks]
Posted by "PragmaTwice (via GitHub)" <gi...@apache.org>.
PragmaTwice commented on code in PR #2360:
URL: https://github.com/apache/kvrocks/pull/2360#discussion_r1632524525
##########
src/common/cron.cc:
##########
@@ -84,20 +101,33 @@ StatusOr<Scheduler> Cron::convertToScheduleTime(const std::string &minute, const
const std::string &wday) {
Scheduler st;
- st.minute = GET_OR_RET(convertParam(minute, 0, 59));
- st.hour = GET_OR_RET(convertParam(hour, 0, 23));
- st.mday = GET_OR_RET(convertParam(mday, 1, 31));
- st.month = GET_OR_RET(convertParam(month, 1, 12));
- st.wday = GET_OR_RET(convertParam(wday, 0, 6));
+ st.minute = GET_OR_RET(convertParam(minute, 0, 59, st.minute_interval));
+ st.hour = GET_OR_RET(convertParam(hour, 0, 23, st.hour_interval));
+ st.mday = GET_OR_RET(convertParam(mday, 1, 31, st.mday_interval));
+ st.month = GET_OR_RET(convertParam(month, 1, 12, st.month_interval));
+ st.wday = GET_OR_RET(convertParam(wday, 0, 6, st.wday_interval));
return st;
}
-StatusOr<int> Cron::convertParam(const std::string ¶m, int lower_bound, int upper_bound) {
+StatusOr<int> Cron::convertParam(const std::string ¶m, int lower_bound, int upper_bound, bool &is_interval) {
if (param == "*") {
return -1;
}
+ // Check for interval syntax (*/n)
+ std::regex interval_regex(R"(\*/(\d+))");
+ std::smatch match;
+ if (std::regex_match(param, match, interval_regex)) {
+ int interval = std::stoi(match[1].str());
+ if (interval >= lower_bound && interval <= upper_bound) {
+ is_interval = true;
+ return interval;
+ } else {
+ return {Status::NotOK, "interval value out of bounds"};
+ }
+ }
Review Comment:
```suggestion
if (HasPrefix(param, "*/")) {
auto s = ParseInt<int>(param.substr(2), {lower_bound, upper_bound}, 10);
if (!s) {
return std::move(s).Prefixed(fmt::format("malformed cron token `{}`", param));
}
is_interval = true;
return *s;
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(cron): add support for "*/n" interval cronjob syntax [kvrocks]
Posted by "PragmaTwice (via GitHub)" <gi...@apache.org>.
PragmaTwice commented on code in PR #2360:
URL: https://github.com/apache/kvrocks/pull/2360#discussion_r1633231573
##########
src/common/cron.cc:
##########
@@ -84,20 +99,30 @@ StatusOr<Scheduler> Cron::convertToScheduleTime(const std::string &minute, const
const std::string &wday) {
Scheduler st;
- st.minute = GET_OR_RET(convertParam(minute, 0, 59));
- st.hour = GET_OR_RET(convertParam(hour, 0, 23));
- st.mday = GET_OR_RET(convertParam(mday, 1, 31));
- st.month = GET_OR_RET(convertParam(month, 1, 12));
- st.wday = GET_OR_RET(convertParam(wday, 0, 6));
+ st.minute = GET_OR_RET(convertParam(minute, 0, 59, st.minute_interval));
+ st.hour = GET_OR_RET(convertParam(hour, 0, 23, st.hour_interval));
+ st.mday = GET_OR_RET(convertParam(mday, 1, 31, st.mday_interval));
+ st.month = GET_OR_RET(convertParam(month, 1, 12, st.month_interval));
+ st.wday = GET_OR_RET(convertParam(wday, 0, 6, st.wday_interval));
return st;
}
-StatusOr<int> Cron::convertParam(const std::string ¶m, int lower_bound, int upper_bound) {
+StatusOr<int> Cron::convertParam(const std::string ¶m, int lower_bound, int upper_bound, bool &is_interval) {
if (param == "*") {
return -1;
}
+ // Check for interval syntax (*/n)
+ if (util::HasPrefix(param, "*/")) {
+ auto s = ParseInt<int>(param.substr(2), {lower_bound, upper_bound}, 10);
+ if (!s) {
+ return std::move(s).Prefixed(fmt::format("malformed cron token `{}`", param));
+ }
Review Comment:
the integer cannot be 0 here to prevent division by zero.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(cron): add support for "*/n" interval cronjob syntax [kvrocks]
Posted by "PragmaTwice (via GitHub)" <gi...@apache.org>.
PragmaTwice commented on code in PR #2360:
URL: https://github.com/apache/kvrocks/pull/2360#discussion_r1634933935
##########
src/common/cron.cc:
##########
@@ -57,10 +62,21 @@ bool Cron::IsTimeMatch(const tm *tm) {
tm->tm_mon == last_tm_.tm_mon && tm->tm_wday == last_tm_.tm_wday) {
return false;
}
+
+ auto match = [](int current, int val, bool interval) {
+ if (val == -1) return true;
+ if (interval) return current % val == 0;
+ return val == current;
+ };
+
for (const auto &st : schedulers_) {
- if ((st.minute == -1 || tm->tm_min == st.minute) && (st.hour == -1 || tm->tm_hour == st.hour) &&
- (st.mday == -1 || tm->tm_mday == st.mday) && (st.month == -1 || (tm->tm_mon + 1) == st.month) &&
- (st.wday == -1 || tm->tm_wday == st.wday)) {
+ bool minute_match = match(tm->tm_min, st.minute, st.minute_interval);
+ bool hour_match = match(tm->tm_hour, st.hour, st.hour_interval);
+ bool mday_match = match(tm->tm_mday, st.mday, st.mday_interval);
+ bool month_match = match(tm->tm_mon - 1, st.month, st.month_interval);
+ bool wday_match = match(tm->tm_wday, st.wday, st.wday_interval);
Review Comment:
You can try other algorithm.
But be sure to make it correct for all cases.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(cron): add support for "*/n" interval cronjob syntax [kvrocks]
Posted by "PragmaTwice (via GitHub)" <gi...@apache.org>.
PragmaTwice commented on code in PR #2360:
URL: https://github.com/apache/kvrocks/pull/2360#discussion_r1633222493
##########
src/common/cron.cc:
##########
@@ -58,9 +63,19 @@ bool Cron::IsTimeMatch(const tm *tm) {
return false;
}
for (const auto &st : schedulers_) {
- if ((st.minute == -1 || tm->tm_min == st.minute) && (st.hour == -1 || tm->tm_hour == st.hour) &&
- (st.mday == -1 || tm->tm_mday == st.mday) && (st.month == -1 || (tm->tm_mon + 1) == st.month) &&
- (st.wday == -1 || tm->tm_wday == st.wday)) {
+ bool minute_match = (st.minute == -1 || tm->tm_min == st.minute ||
+ (st.minute > 0 && st.minute <= 59 && tm->tm_min % st.minute == 0 && st.minute_interval));
Review Comment:
```suggestion
auto match = [](int current, int val, bool interval) {
if (val == -1) return true;
if (interval) return current % val == 0;
return val == current;
};
bool minute_match = match(tm->tm_min, st.minute, st.minute_interval);
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(cron): add support for "*/n" interval cronjob syntax [kvrocks]
Posted by "kinoute (via GitHub)" <gi...@apache.org>.
kinoute commented on code in PR #2360:
URL: https://github.com/apache/kvrocks/pull/2360#discussion_r1634738272
##########
src/common/cron.cc:
##########
@@ -57,10 +62,21 @@ bool Cron::IsTimeMatch(const tm *tm) {
tm->tm_mon == last_tm_.tm_mon && tm->tm_wday == last_tm_.tm_wday) {
return false;
}
+
+ auto match = [](int current, int val, bool interval) {
+ if (val == -1) return true;
+ if (interval) return current % val == 0;
+ return val == current;
+ };
+
for (const auto &st : schedulers_) {
- if ((st.minute == -1 || tm->tm_min == st.minute) && (st.hour == -1 || tm->tm_hour == st.hour) &&
- (st.mday == -1 || tm->tm_mday == st.mday) && (st.month == -1 || (tm->tm_mon + 1) == st.month) &&
- (st.wday == -1 || tm->tm_wday == st.wday)) {
+ bool minute_match = match(tm->tm_min, st.minute, st.minute_interval);
+ bool hour_match = match(tm->tm_hour, st.hour, st.hour_interval);
+ bool mday_match = match(tm->tm_mday, st.mday, st.mday_interval);
+ bool month_match = match(tm->tm_mon - 1, st.month, st.month_interval);
Review Comment:
But If I do that, my tests for the month day will fail. So I guess my tests are bad then? According to crontab.guru, a crontab like `0 0 1 */4 *` will result in the following:
```
next at 2024-09-01 00:00:00
then at 2025-01-01 00:00:00
then at 2025-05-01 00:00:00
then at 2025-09-01 00:00:00
then at 2026-01-01 00:00:00
```
https://crontab.guru/#0_0_1_*/4_*
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(cron): add support for "*/n" interval cronjob syntax [kvrocks]
Posted by "PragmaTwice (via GitHub)" <gi...@apache.org>.
PragmaTwice commented on code in PR #2360:
URL: https://github.com/apache/kvrocks/pull/2360#discussion_r1636528359
##########
src/common/cron.cc:
##########
@@ -57,10 +62,21 @@ bool Cron::IsTimeMatch(const tm *tm) {
tm->tm_mon == last_tm_.tm_mon && tm->tm_wday == last_tm_.tm_wday) {
return false;
}
+
+ auto match = [](int current, int val, bool interval, int interval_offset) {
+ if (val == -1) return true;
+ if (interval) return (current - interval_offset) % val == 0;
+ return val == current;
+ };
+
for (const auto &st : schedulers_) {
- if ((st.minute == -1 || tm->tm_min == st.minute) && (st.hour == -1 || tm->tm_hour == st.hour) &&
- (st.mday == -1 || tm->tm_mday == st.mday) && (st.month == -1 || (tm->tm_mon + 1) == st.month) &&
- (st.wday == -1 || tm->tm_wday == st.wday)) {
+ bool minute_match = match(tm->tm_min, st.minute, st.minute_interval, 0);
+ bool hour_match = match(tm->tm_hour, st.hour, st.hour_interval, 0);
+ bool mday_match = match(tm->tm_mday, st.mday, st.mday_interval, 0);
Review Comment:
```suggestion
bool mday_match = match(tm->tm_mday, st.mday, st.mday_interval, 1);
```
Should it be 1?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(cron): add support for "*/n" interval cronjob syntax [kvrocks]
Posted by "kinoute (via GitHub)" <gi...@apache.org>.
kinoute commented on PR #2360:
URL: https://github.com/apache/kvrocks/pull/2360#issuecomment-2162137686
I added more tests for non interval syntax and renamed the `min_val` parameter
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(cron): add support for "*/n" interval cronjob syntax [kvrocks]
Posted by "PragmaTwice (via GitHub)" <gi...@apache.org>.
PragmaTwice commented on code in PR #2360:
URL: https://github.com/apache/kvrocks/pull/2360#discussion_r1634555169
##########
src/common/cron.cc:
##########
@@ -57,10 +62,21 @@ bool Cron::IsTimeMatch(const tm *tm) {
tm->tm_mon == last_tm_.tm_mon && tm->tm_wday == last_tm_.tm_wday) {
return false;
}
+
+ auto match = [](int current, int val, bool interval) {
+ if (val == -1) return true;
+ if (interval) return current % val == 0;
+ return val == current;
+ };
+
for (const auto &st : schedulers_) {
- if ((st.minute == -1 || tm->tm_min == st.minute) && (st.hour == -1 || tm->tm_hour == st.hour) &&
- (st.mday == -1 || tm->tm_mday == st.mday) && (st.month == -1 || (tm->tm_mon + 1) == st.month) &&
- (st.wday == -1 || tm->tm_wday == st.wday)) {
+ bool minute_match = match(tm->tm_min, st.minute, st.minute_interval);
+ bool hour_match = match(tm->tm_hour, st.hour, st.hour_interval);
+ bool mday_match = match(tm->tm_mday, st.mday, st.mday_interval);
+ bool month_match = match(tm->tm_mon - 1, st.month, st.month_interval);
Review Comment:
```suggestion
bool month_match = match(tm->tm_mon + 1, st.month, st.month_interval);
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(cron): add support for "*/n" interval cronjob syntax [kvrocks]
Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #2360:
URL: https://github.com/apache/kvrocks/pull/2360#issuecomment-2159985510
## [](https://sonarcloud.io/dashboard?id=apache_kvrocks&pullRequest=2360) **Quality Gate passed**
Issues
 [0 New issues](https://sonarcloud.io/project/issues?id=apache_kvrocks&pullRequest=2360&resolved=false&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_kvrocks&pullRequest=2360&resolutions=WONTFIX)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_kvrocks&pullRequest=2360&resolved=false&sinceLeakPeriod=true)
 No data about Coverage
 No data about Duplication
[See analysis details on SonarCloud](https://sonarcloud.io/dashboard?id=apache_kvrocks&pullRequest=2360)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(cron): add support for "*/n" interval cronjob syntax [kvrocks]
Posted by "PragmaTwice (via GitHub)" <gi...@apache.org>.
PragmaTwice commented on code in PR #2360:
URL: https://github.com/apache/kvrocks/pull/2360#discussion_r1634913714
##########
src/common/cron.cc:
##########
@@ -57,10 +62,21 @@ bool Cron::IsTimeMatch(const tm *tm) {
tm->tm_mon == last_tm_.tm_mon && tm->tm_wday == last_tm_.tm_wday) {
return false;
}
+
+ auto match = [](int current, int val, bool interval) {
+ if (val == -1) return true;
+ if (interval) return current % val == 0;
+ return val == current;
+ };
+
for (const auto &st : schedulers_) {
- if ((st.minute == -1 || tm->tm_min == st.minute) && (st.hour == -1 || tm->tm_hour == st.hour) &&
- (st.mday == -1 || tm->tm_mday == st.mday) && (st.month == -1 || (tm->tm_mon + 1) == st.month) &&
- (st.wday == -1 || tm->tm_wday == st.wday)) {
+ bool minute_match = match(tm->tm_min, st.minute, st.minute_interval);
+ bool hour_match = match(tm->tm_hour, st.hour, st.hour_interval);
+ bool mday_match = match(tm->tm_mday, st.mday, st.mday_interval);
+ bool month_match = match(tm->tm_mon - 1, st.month, st.month_interval);
Review Comment:
For `tm->tm_mon - 1`, the non-interval case will be wrong.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(cron): add support for "*/n" interval cronjob syntax [kvrocks]
Posted by "PragmaTwice (via GitHub)" <gi...@apache.org>.
PragmaTwice commented on code in PR #2360:
URL: https://github.com/apache/kvrocks/pull/2360#discussion_r1633222493
##########
src/common/cron.cc:
##########
@@ -58,9 +63,19 @@ bool Cron::IsTimeMatch(const tm *tm) {
return false;
}
for (const auto &st : schedulers_) {
- if ((st.minute == -1 || tm->tm_min == st.minute) && (st.hour == -1 || tm->tm_hour == st.hour) &&
- (st.mday == -1 || tm->tm_mday == st.mday) && (st.month == -1 || (tm->tm_mon + 1) == st.month) &&
- (st.wday == -1 || tm->tm_wday == st.wday)) {
+ bool minute_match = (st.minute == -1 || tm->tm_min == st.minute ||
+ (st.minute > 0 && st.minute <= 59 && tm->tm_min % st.minute == 0 && st.minute_interval));
Review Comment:
```suggestion
auto match = [](int current, int val, bool interval) {
if (val == -1) return true;
if (interval) return current % val == 0;
return val == current;
}
bool minute_match = match(tm->tm_min, st.minute, st.minute_interval);
```
##########
src/common/cron.cc:
##########
@@ -58,9 +63,19 @@ bool Cron::IsTimeMatch(const tm *tm) {
return false;
}
for (const auto &st : schedulers_) {
- if ((st.minute == -1 || tm->tm_min == st.minute) && (st.hour == -1 || tm->tm_hour == st.hour) &&
- (st.mday == -1 || tm->tm_mday == st.mday) && (st.month == -1 || (tm->tm_mon + 1) == st.month) &&
- (st.wday == -1 || tm->tm_wday == st.wday)) {
+ bool minute_match = (st.minute == -1 || tm->tm_min == st.minute ||
+ (st.minute > 0 && st.minute <= 59 && tm->tm_min % st.minute == 0 && st.minute_interval));
+ bool hour_match = (st.hour == -1 || tm->tm_hour == st.hour ||
+ (st.hour > 0 && st.hour <= 23 && tm->tm_hour % st.hour == 0 && st.hour_interval));
+ bool mday_match = (st.mday == -1 || tm->tm_mday == st.mday ||
+ (st.mday > 0 && st.mday <= 31 && tm->tm_mday % st.mday == 0 && st.mday_interval) ||
+ (st.mday == 0 && tm->tm_mday == 1));
+ bool month_match = (st.month == -1 || tm->tm_mon == st.month ||
+ (st.month > 0 && st.month <= 12 && (tm->tm_mon - 1) % st.month == 0 && st.month_interval));
+ bool wday_match = (st.wday == -1 || tm->tm_wday == st.wday ||
+ (st.wday >= 0 && st.wday <= 6 && tm->tm_wday % st.wday == 0 && st.wday_interval));
Review Comment:
ditto
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(cron): add support for "*/n" interval cronjob syntax [kvrocks]
Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #2360:
URL: https://github.com/apache/kvrocks/pull/2360#issuecomment-2162927973
## [](https://sonarcloud.io/dashboard?id=apache_kvrocks&pullRequest=2360) **Quality Gate passed**
Issues
 [0 New issues](https://sonarcloud.io/project/issues?id=apache_kvrocks&pullRequest=2360&resolved=false&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_kvrocks&pullRequest=2360&resolutions=WONTFIX)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_kvrocks&pullRequest=2360&resolved=false&sinceLeakPeriod=true)
 No data about Coverage
 No data about Duplication
[See analysis details on SonarCloud](https://sonarcloud.io/dashboard?id=apache_kvrocks&pullRequest=2360)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(cron): add support for "*/n" interval cronjob syntax [kvrocks]
Posted by "kinoute (via GitHub)" <gi...@apache.org>.
kinoute commented on code in PR #2360:
URL: https://github.com/apache/kvrocks/pull/2360#discussion_r1634738272
##########
src/common/cron.cc:
##########
@@ -57,10 +62,21 @@ bool Cron::IsTimeMatch(const tm *tm) {
tm->tm_mon == last_tm_.tm_mon && tm->tm_wday == last_tm_.tm_wday) {
return false;
}
+
+ auto match = [](int current, int val, bool interval) {
+ if (val == -1) return true;
+ if (interval) return current % val == 0;
+ return val == current;
+ };
+
for (const auto &st : schedulers_) {
- if ((st.minute == -1 || tm->tm_min == st.minute) && (st.hour == -1 || tm->tm_hour == st.hour) &&
- (st.mday == -1 || tm->tm_mday == st.mday) && (st.month == -1 || (tm->tm_mon + 1) == st.month) &&
- (st.wday == -1 || tm->tm_wday == st.wday)) {
+ bool minute_match = match(tm->tm_min, st.minute, st.minute_interval);
+ bool hour_match = match(tm->tm_hour, st.hour, st.hour_interval);
+ bool mday_match = match(tm->tm_mday, st.mday, st.mday_interval);
+ bool month_match = match(tm->tm_mon - 1, st.month, st.month_interval);
Review Comment:
But If I do that, my tests for the month day will fail. So I guess my tests are bad then? According to crontab.guru, a crontab like `0 0 1 */4 *` will result in the following:
```
next at 2024-09-01 00:00:00
then at 2025-01-01 00:00:00
then at 2025-05-01 00:00:00
then at 2025-09-01 00:00:00
then at 2026-01-01 00:00:00
```
https://crontab.guru/#0_0_1_*/4_*
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org