Skip to content
This repository was archived by the owner on Jun 12, 2024. It is now read-only.

Commit 19d99c8

Browse files
authored
Allow one-time jobs in the scheduler (#49)
Sometimes it's needed to create a job that executes once.
1 parent 45b38a1 commit 19d99c8

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

pkg/queue/postgres/scheduler.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,12 @@ func (q *scheduler) Schedule(ctx context.Context, builder cdb.SQLBuilder, task q
5252
task.Spec = emptyJSON
5353
}
5454

55-
err = cvalidation.CronTab(task.CronSchedule)
56-
if err != nil {
57-
return err
55+
// empty schedule means a one-time job
56+
if task.CronSchedule != "" {
57+
err = cvalidation.CronTab(task.CronSchedule)
58+
if err != nil {
59+
return err
60+
}
5861
}
5962

6063
scheduleID := uuid.NewV4().String()

pkg/queue/postgres/scheduler_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ func TestSchedule(t *testing.T) {
8282
CronSchedule: "@weekly",
8383
},
8484
},
85+
{
86+
name: "Returns no error when a schedule is empty",
87+
task: queue.TaskScheduleRequest{
88+
TaskBase: queue.TaskBase{
89+
Queue: "queue2",
90+
Type: "test",
91+
Spec: spec,
92+
},
93+
CronSchedule: "",
94+
},
95+
},
96+
8597
{
8698
name: "Returns error when a cron schedule is not valid",
8799
task: queue.TaskScheduleRequest{

pkg/queue/workers/schedule_worker_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func TestScheduleTask(t *testing.T) {
6666
taskQueue2,
6767
taskType,
6868
taskSpec2,
69-
"",
69+
"", // empty value is a one-time job
7070
now.Add(-1*time.Minute), // this must be executed after the first
7171
now.Add(-1*time.Minute),
7272
now.Add(-1*time.Minute),

0 commit comments

Comments
 (0)