Skip to content

Commit

Permalink
Fix: Possible to set/run invalid scheduler config
Browse files Browse the repository at this point in the history
  • Loading branch information
r3-gabriel committed Oct 1, 2024
1 parent d0ef5b9 commit acebe40
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
15 changes: 8 additions & 7 deletions scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ func getNextRunFromSchedule(s taskSchedule) int64 {
}

// more complex intervals, add dates and set to target day/time
// as no timezone is defined, tm will be in local time, which will affect all date operations
tm := time.Unix(s.runLastUnix, 0)

switch s.intervalType {
Expand All @@ -531,23 +532,23 @@ func getNextRunFromSchedule(s taskSchedule) int64 {
targetDay := tm.Day()
targetMonth := tm.Month()

// overwrite invalid inputs
s.atDay = schema.GetValidAtDay(s.intervalType, s.atDay)

switch s.intervalType {
case "weeks":
// 6 is highest allowed value (0 = sunday, 6 = saturday)
if s.atDay <= 6 {
// add difference between target weekday and current weekday to target day
targetDay += s.atDay - int(tm.Weekday())
}
// add difference between target weekday and last ran weekday to target day
targetDay += s.atDay - int(tm.Weekday())
case "months":
// set specified day
targetDay = s.atDay
case "years":
// set to month january, adding days as specified (70 days will end up in March)
targetMonth = 1
targetDay = s.atDay
targetMonth = 1
}

// apply target month/day and time
// apply target month/day and time at local time
tm = time.Date(tm.Year(), targetMonth, targetDay, s.atHour, s.atMinute,
s.atSecond, 0, tm.Location())

Expand Down
3 changes: 3 additions & 0 deletions schema/pgFunction/pgFunction.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ func Set_tx(tx pgx.Tx, moduleId uuid.UUID, id uuid.UUID, name string,
return err
}

// overwrite invalid inputs
s.AtDay = schema.GetValidAtDay(s.IntervalType, s.AtDay)

if known {
if _, err := tx.Exec(db.Ctx, `
UPDATE app.pg_function_schedule
Expand Down
21 changes: 21 additions & 0 deletions schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,27 @@ func IsContentText(content string) bool {
return content == "varchar" || content == "text"
}

// scheduler checks
func GetValidAtDay(intervalType string, atDay int) int {
switch intervalType {
case "months":
// day < 1 would go to previous month, which is undesirable on a monthly interval
if atDay < 1 || atDay > 31 {
atDay = 1
}
case "weeks":
// 0 = Sunday, 6 = Saturday
if atDay < 0 || atDay > 6 {
atDay = 1
}
case "years":
if atDay > 365 {
atDay = 1
}
}
return atDay
}

// fully validates module dependencies
func ValidateDependency_tx(tx pgx.Tx, moduleId uuid.UUID) error {
var cnt int
Expand Down

0 comments on commit acebe40

Please sign in to comment.