Example_jobSnooze demonstrates how to snooze a job from within Work using JobSnooze. The job will be run again after 5 minutes and the snooze attempt will decrement the job's attempt count, ensuring that one can snooze as many times as desired without being impacted by the max attempts.
()
| 36 | // will decrement the job's attempt count, ensuring that one can snooze as many |
| 37 | // times as desired without being impacted by the max attempts. |
| 38 | func Example_jobSnooze() { //nolint:dupl |
| 39 | ctx := context.Background() |
| 40 | |
| 41 | dbPool, err := pgxpool.New(ctx, riversharedtest.TestDatabaseURL()) |
| 42 | if err != nil { |
| 43 | panic(err) |
| 44 | } |
| 45 | defer dbPool.Close() |
| 46 | |
| 47 | workers := river.NewWorkers() |
| 48 | river.AddWorker(workers, &SnoozingWorker{}) |
| 49 | |
| 50 | riverClient, err := river.NewClient(riverpgxv5.New(dbPool), initTestConfig(ctx, dbPool, &river.Config{ |
| 51 | Queues: map[string]river.QueueConfig{ |
| 52 | river.QueueDefault: {MaxWorkers: 10}, |
| 53 | }, |
| 54 | Workers: workers, |
| 55 | })) |
| 56 | if err != nil { |
| 57 | panic(err) |
| 58 | } |
| 59 | |
| 60 | // The subscription bits are not needed in real usage, but are used to make |
| 61 | // sure the test waits until the job is worked. |
| 62 | subscribeChan, subscribeCancel := riverClient.Subscribe(river.EventKindJobSnoozed) |
| 63 | defer subscribeCancel() |
| 64 | |
| 65 | if err := riverClient.Start(ctx); err != nil { |
| 66 | panic(err) |
| 67 | } |
| 68 | if _, err = riverClient.Insert(ctx, SnoozingArgs{ShouldSnooze: true}, nil); err != nil { |
| 69 | panic(err) |
| 70 | } |
| 71 | // Wait for jobs to complete. Only needed for purposes of the example test. |
| 72 | riversharedtest.WaitOrTimeoutN(testutil.PanicTB(), subscribeChan, 1) |
| 73 | |
| 74 | if err := riverClient.Stop(ctx); err != nil { |
| 75 | panic(err) |
| 76 | } |
| 77 | |
| 78 | // Output: |
| 79 | // snoozing job for 5 minutes |
| 80 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…