Example_periodicJob demonstrates the use of a periodic job.
()
| 30 | |
| 31 | // Example_periodicJob demonstrates the use of a periodic job. |
| 32 | func Example_periodicJob() { |
| 33 | ctx := context.Background() |
| 34 | |
| 35 | dbPool, err := pgxpool.New(ctx, riversharedtest.TestDatabaseURL()) |
| 36 | if err != nil { |
| 37 | panic(err) |
| 38 | } |
| 39 | defer dbPool.Close() |
| 40 | |
| 41 | workers := river.NewWorkers() |
| 42 | river.AddWorker(workers, &PeriodicJobWorker{}) |
| 43 | |
| 44 | riverClient, err := river.NewClient(riverpgxv5.New(dbPool), initTestConfig(ctx, dbPool, &river.Config{ |
| 45 | PeriodicJobs: []*river.PeriodicJob{ |
| 46 | river.NewPeriodicJob( |
| 47 | river.PeriodicInterval(15*time.Minute), |
| 48 | func() (river.JobArgs, *river.InsertOpts) { |
| 49 | return PeriodicJobArgs{}, nil |
| 50 | }, |
| 51 | &river.PeriodicJobOpts{RunOnStart: true}, |
| 52 | ), |
| 53 | }, |
| 54 | Queues: map[string]river.QueueConfig{ |
| 55 | river.QueueDefault: {MaxWorkers: 100}, |
| 56 | }, |
| 57 | Workers: workers, |
| 58 | })) |
| 59 | if err != nil { |
| 60 | panic(err) |
| 61 | } |
| 62 | |
| 63 | // Out of example scope, but used to wait until a job is worked. |
| 64 | subscribeChan, subscribeCancel := riverClient.Subscribe(river.EventKindJobCompleted) |
| 65 | defer subscribeCancel() |
| 66 | |
| 67 | // There's no need to explicitly insert a periodic job. One will be inserted |
| 68 | // (and worked soon after) as the client starts up. |
| 69 | if err := riverClient.Start(ctx); err != nil { |
| 70 | panic(err) |
| 71 | } |
| 72 | |
| 73 | // Wait for jobs to complete. Only needed for purposes of the example test. |
| 74 | riversharedtest.WaitOrTimeoutN(testutil.PanicTB(), subscribeChan, 1) |
| 75 | |
| 76 | // Periodic jobs can also be configured dynamically after a client has |
| 77 | // already started. Added jobs are scheduled for run immediately. |
| 78 | riverClient.PeriodicJobs().Clear() |
| 79 | riverClient.PeriodicJobs().Add( |
| 80 | river.NewPeriodicJob( |
| 81 | river.PeriodicInterval(15*time.Minute), |
| 82 | func() (river.JobArgs, *river.InsertOpts) { |
| 83 | return PeriodicJobArgs{}, nil |
| 84 | }, |
| 85 | nil, |
| 86 | ), |
| 87 | ) |
| 88 | |
| 89 | if err := riverClient.Stop(ctx); err != nil { |
nothing calls this directly
no test coverage detected
searching dependent graphs…