diff --git a/periodic_job.go b/periodic_job.go index 90d55cbb..20648e09 100644 --- a/periodic_job.go +++ b/periodic_job.go @@ -183,6 +183,9 @@ func (b *PeriodicJobBundle) toInternal(periodicJob *PeriodicJob) *maintenance.Pe return &maintenance.PeriodicJob{ ConstructorFunc: func() (*riverdriver.JobInsertFastParams, *dbunique.UniqueOpts, error) { args, options := periodicJob.constructorFunc() + if args == nil { + return nil, nil, maintenance.ErrNoJobToInsert + } return insertParamsFromConfigArgsAndOptions(&b.periodicJobEnqueuer.Archetype, b.clientConfig, args, options) }, RunOnStart: opts.RunOnStart, diff --git a/periodic_job_test.go b/periodic_job_test.go index 299b70b3..ae469e04 100644 --- a/periodic_job_test.go +++ b/periodic_job_test.go @@ -59,6 +59,26 @@ func TestPeriodicJobBundle(t *testing.T) { require.NoError(t, err) require.Equal(t, 2, mustUnmarshalJSON[TestJobArgs](t, insertParams2.EncodedArgs).JobNum) }) + + t.Run("ReturningNilDoesntInsertNewJob", func(t *testing.T) { + t.Parallel() + + periodicJobBundle, _ := setup(t) + + periodicJob := NewPeriodicJob( + PeriodicInterval(15*time.Minute), + func() (JobArgs, *InsertOpts) { + // Returning nil from the constructor function should not insert a new job. + return nil, nil + }, + nil, + ) + + internalPeriodicJob := periodicJobBundle.toInternal(periodicJob) + + _, _, err := internalPeriodicJob.ConstructorFunc() + require.ErrorIs(t, err, maintenance.ErrNoJobToInsert) + }) } func mustUnmarshalJSON[T any](t *testing.T, data []byte) *T {