Skip to content

Commit

Permalink
Fix bug that causes some cron schedules to get incorrectly parsed
Browse files Browse the repository at this point in the history
  • Loading branch information
marcocitus committed Feb 9, 2023
1 parent 77c03c2 commit 37b5687
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### pg_cron v1.5.1 (February 9, 2023) ###

* Fixes a bug that caused incorrect parsing of some crons schedules

### pg_cron v1.5.0 (February 7, 2023) ###

* Adds the possibility of scheduling a job with a 1-59 second interval
Expand Down
4 changes: 2 additions & 2 deletions src/entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ parse_cron_entry(char *schedule)
* minutes hours doms months dows USERNAME cmd\n
*/

ecode_e ecode = e_none;
ecode_e ecode = e_none;
entry *e = (entry *) calloc(sizeof(entry), sizeof(char));
int ch = 0;
char cmd[MAX_COMMAND];
Expand Down Expand Up @@ -231,7 +231,7 @@ parse_cron_entry(char *schedule)
return e;

eof:
elog(LOG, "failed to parse entry %d", ecode);
elog(DEBUG1, "failed to parse entry %d", ecode);
free_entry(e);
while (ch != EOF && ch != '\n')
ch = get_char(file);
Expand Down
29 changes: 23 additions & 6 deletions src/job_metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -1485,7 +1485,17 @@ ParseSchedule(char *scheduleText)
uint32 secondsInterval = 0;

/*
* Parse as interval on seconds or fall back to trying cron schedule.
* First try to parse as a cron schedule.
*/
entry *schedule = parse_cron_entry(scheduleText);
if (schedule != NULL)
{
/* valid cron schedule */
return schedule;
}

/*
* Parse as interval on seconds.
*/
if (TryParseInterval(scheduleText, &secondsInterval))
{
Expand All @@ -1494,7 +1504,8 @@ ParseSchedule(char *scheduleText)
return schedule;
}

return parse_cron_entry(scheduleText);
elog(LOG, "failed to parse schedule: %s", scheduleText);
return NULL;
}


Expand All @@ -1505,19 +1516,25 @@ ParseSchedule(char *scheduleText)
static bool
TryParseInterval(char *scheduleText, uint32 *secondsInterval)
{
char lastChar = '\0';
char plural = '\0';
char extra = '\0';
char *lowercaseSchedule = asc_tolower(scheduleText, strlen(scheduleText));

int numParts = sscanf(lowercaseSchedule, " %u second%c %c", secondsInterval,
&plural, &extra);
int numParts = sscanf(lowercaseSchedule, " %u secon%c%c %c", secondsInterval,
&lastChar, &plural, &extra);
if (lastChar != 'd')
{
/* value did not have a "second" suffix */
return false;
}

if (numParts == 1)
if (numParts == 2)
{
/* <number> second (allow "2 second") */
return 0 < *secondsInterval && *secondsInterval < 60;
}
else if (numParts == 2 && plural == 's')
else if (numParts == 3 && plural == 's')
{
/* <number> seconds (allow "1 seconds") */
return 0 < *secondsInterval && *secondsInterval < 60;
Expand Down

0 comments on commit 37b5687

Please sign in to comment.