Periodic JobArgs lost on subsequent invocations #194
Replies: 4 comments
-
@stevecalvert You don't need these lines: // Insert a transaction to run periodically
_, err = riverClient.InsertTx(ctx, tx, job1Args, nil)
if err != nil {
// handle error
fmt.Printf("Error inserting transaction for jobArg:%s, error:%s\n", job1Args.JobVal, err.Error())
}
tx.Commit(ctx) It's the job of the periodic job scheduler to insert those jobs for you automatically, so you can omit any manual insertions. To fix your problem, change your periodic job configuration to: PeriodicJobs: []*river.PeriodicJob{
river.NewPeriodicJob(
river.PeriodicInterval(30*time.Second),
func() (river.JobArgs, *river.InsertOpts) {
return job1Args, nil
},
&river.PeriodicJobOpts{RunOnStart: true},
),
}, Or even better, avoid the global state altogether (it may be prone to accidental mutation), and replace that with: PeriodicJobs: []*river.PeriodicJob{
river.NewPeriodicJob(
river.PeriodicInterval(30*time.Second),
func() (river.JobArgs, *river.InsertOpts) {
return PeriodicJobArgs{JobVal: "11112222"}, nil
},
&river.PeriodicJobOpts{RunOnStart: true},
),
}, |
Beta Was this translation helpful? Give feedback.
-
Thanks, @brandur, that works well, but the reason I had the |
Beta Was this translation helpful? Give feedback.
-
I just want to make sure that it's clear here that what you're running into isn't a bug in River. You seem to be expecting that if you insert a job manually with I think the piece that you need to focus on is the lastJobVal := "11112222"
...
PeriodicJobs: []*river.PeriodicJob{
river.NewPeriodicJob(
river.PeriodicInterval(30*time.Second),
func() (river.JobArgs, *river.InsertOpts) {
return PeriodicJobArgs{JobVal: lastJobVal}, nil
},
&river.PeriodicJobOpts{RunOnStart: true},
),
}, |
Beta Was this translation helpful? Give feedback.
-
Hi @brandur, I had a think about refactoring the But I think I've found an alternative solution, which is to go back to dynamically inserting a single-shot job, perform the task in the Maybe there is a solution using the PeriodicJob, but creating them at startup as per the example was not aligning with my use case. Anyway, I'm back in business, so I think this ticket can be closed. Thanks so much for your help! |
Beta Was this translation helpful? Give feedback.
-
Hi River devs, I'm trying out the periodic job queue. I was hoping to be able to insert a periodic job and have it run at regular intervals, using the
PeriodicJobArgs
until it's no longer needed. I see the first run of the job with the correct JobArgs values, but subsequent runs no longer have access to the JobArgs. Apologies if this is an obvious omission on my part. I suppose I could get around this by using a single-shot job and re-inserting a new job before exiting theWork()
function, but that defeats the object of the periodic job!Following taken from the periodic job example, with updates to get around no access to internal imports, and adding a transaction to insert a job. Any suggestions welcomed 😄
Output is:
Beta Was this translation helpful? Give feedback.
All reactions