Skip to content

Commit

Permalink
replace magic H with ~
Browse files Browse the repository at this point in the history
  • Loading branch information
fopina committed Feb 11, 2024
1 parent f38f135 commit 7552f19
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 4 additions & 4 deletions dkron/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,17 +306,17 @@ func (j *Job) GetTimeLocation() *time.Location {
}

// scheduleHash replaces H in the cron spec by a value derived from job Name
// such as "0 0 H * * *"
// such as "0 0 ~ * * *"
func (j *Job) scheduleHash() string {
spec := j.Schedule
if strings.Contains(spec, "H") && strings.Count(strings.TrimSpace(spec), " ") == 5 {
if strings.Contains(spec, "~") && strings.Count(strings.TrimSpace(spec), " ") == 5 {
hash := 0
for _, c := range j.Name {
hash += int(c)
}
parts := strings.Split(spec, " ")
for index, part := range parts {
if strings.Contains(part, "H") {
if strings.Contains(part, "~") {
// mods taken in accordance with https://dkron.io/docs/usage/cron-spec/#cron-expression-format
partHash := hash
switch index {
Expand All @@ -331,7 +331,7 @@ func (j *Job) scheduleHash() string {
default:
partHash %= 60
}
parts[index] = strings.ReplaceAll(part, "H", strconv.Itoa(partHash))
parts[index] = strings.ReplaceAll(part, "~", strconv.Itoa(partHash))
}
}
return strings.Join(parts, " ")
Expand Down
8 changes: 8 additions & 0 deletions dkron/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ func Test_isRunnable(t *testing.T) {
}
}

func Test_scheduleHash(t *testing.T) {
job := &Job{
Name: "test_job",
Schedule: "0 0 ~ * * *",
}
assert.Equal(t, "0 0 18 * * *", job.scheduleHash())
}

type gRPCClientMock struct {
}

Expand Down

0 comments on commit 7552f19

Please sign in to comment.