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

Function Example_cronJob

example_cron_job_test.go:34–89  ·  view source on GitHub ↗

Example_cronJob demonstrates how to create a cron job with a more complex schedule using a third party cron package to parse more elaborate crontab syntax.

()

Source from the content-addressed store, hash-verified

32// schedule using a third party cron package to parse more elaborate crontab
33// syntax.
34func Example_cronJob() {
35 ctx := context.Background()
36
37 dbPool, err := pgxpool.New(ctx, riversharedtest.TestDatabaseURL())
38 if err != nil {
39 panic(err)
40 }
41 defer dbPool.Close()
42
43 workers := river.NewWorkers()
44 river.AddWorker(workers, &CronJobWorker{})
45
46 schedule, err := cron.ParseStandard("30 * * * *") // every hour on the half hour
47 if err != nil {
48 panic(err)
49 }
50
51 riverClient, err := river.NewClient(riverpgxv5.New(dbPool), initTestConfig(ctx, dbPool, &river.Config{
52 PeriodicJobs: []*river.PeriodicJob{
53 river.NewPeriodicJob(
54 schedule,
55 func() (river.JobArgs, *river.InsertOpts) {
56 return CronJobArgs{}, nil
57 },
58 &river.PeriodicJobOpts{RunOnStart: true},
59 ),
60 },
61 Queues: map[string]river.QueueConfig{
62 river.QueueDefault: {MaxWorkers: 100},
63 },
64 Workers: workers,
65 }))
66 if err != nil {
67 panic(err)
68 }
69
70 // Out of example scope, but used to wait until a job is worked.
71 subscribeChan, subscribeCancel := riverClient.Subscribe(river.EventKindJobCompleted)
72 defer subscribeCancel()
73
74 // There's no need to explicitly insert a periodic job. One will be inserted
75 // (and worked soon after) as the client starts up.
76 if err := riverClient.Start(ctx); err != nil {
77 panic(err)
78 }
79
80 // Wait for jobs to complete. Only needed for purposes of the example test.
81 riversharedtest.WaitOrTimeoutN(testutil.PanicTB(), subscribeChan, 1)
82
83 if err := riverClient.Stop(ctx); err != nil {
84 panic(err)
85 }
86
87 // Output:
88 // This job will run once immediately then every hour on the half hour
89}

Callers

nothing calls this directly

Calls 13

TestDatabaseURLFunction · 0.92
NewWorkersFunction · 0.92
AddWorkerFunction · 0.92
NewClientFunction · 0.92
NewFunction · 0.92
NewPeriodicJobFunction · 0.92
WaitOrTimeoutNFunction · 0.92
PanicTBFunction · 0.92
SubscribeMethod · 0.80
initTestConfigFunction · 0.70
CloseMethod · 0.65
StartMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…