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.
()
| 32 | // schedule using a third party cron package to parse more elaborate crontab |
| 33 | // syntax. |
| 34 | func 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 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…