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

Function Example_jobSnooze

example_job_snooze_test.go:38–80  ·  view source on GitHub ↗

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.

()

Source from the content-addressed store, hash-verified

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.
38func 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}

Callers

nothing calls this directly

Calls 13

TestDatabaseURLFunction · 0.92
NewWorkersFunction · 0.92
AddWorkerFunction · 0.92
NewClientFunction · 0.92
NewFunction · 0.92
WaitOrTimeoutNFunction · 0.92
PanicTBFunction · 0.92
SubscribeMethod · 0.80
InsertMethod · 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…