MCPcopy Index your code
hub / github.com/riverqueue/river / Example_periodicJob

Function Example_periodicJob

example_periodic_job_test.go:32–95  ·  view source on GitHub ↗

Example_periodicJob demonstrates the use of a periodic job.

()

Source from the content-addressed store, hash-verified

30
31// Example_periodicJob demonstrates the use of a periodic job.
32func 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 {

Callers

nothing calls this directly

Calls 15

TestDatabaseURLFunction · 0.92
NewWorkersFunction · 0.92
AddWorkerFunction · 0.92
NewClientFunction · 0.92
NewFunction · 0.92
NewPeriodicJobFunction · 0.92
PeriodicIntervalFunction · 0.92
WaitOrTimeoutNFunction · 0.92
PanicTBFunction · 0.92
SubscribeMethod · 0.80
PeriodicJobsMethod · 0.80
initTestConfigFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…