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

Function Example_customInsertOpts

example_custom_insert_opts_test.go:55–113  ·  view source on GitHub ↗

Example_customInsertOpts demonstrates the use of a job with custom job-specific insertion options.

()

Source from the content-addressed store, hash-verified

53// Example_customInsertOpts demonstrates the use of a job with custom
54// job-specific insertion options.
55func Example_customInsertOpts() {
56 ctx := context.Background()
57
58 dbPool, err := pgxpool.New(ctx, riversharedtest.TestDatabaseURL())
59 if err != nil {
60 panic(err)
61 }
62 defer dbPool.Close()
63
64 workers := river.NewWorkers()
65 river.AddWorker(workers, &AlwaysHighPriorityWorker{})
66 river.AddWorker(workers, &SometimesHighPriorityWorker{})
67
68 riverClient, err := river.NewClient(riverpgxv5.New(dbPool), initTestConfig(ctx, dbPool, &river.Config{
69 Queues: map[string]river.QueueConfig{
70 river.QueueDefault: {MaxWorkers: 100},
71 "high_priority": {MaxWorkers: 100},
72 },
73 Workers: workers,
74 }))
75 if err != nil {
76 panic(err)
77 }
78
79 // Out of example scope, but used to wait until a job is worked.
80 subscribeChan, subscribeCancel := riverClient.Subscribe(river.EventKindJobCompleted)
81 defer subscribeCancel()
82
83 if err := riverClient.Start(ctx); err != nil {
84 panic(err)
85 }
86
87 // This job always runs in the high-priority queue because its job-specific
88 // options on the struct above dictate that it will.
89 _, err = riverClient.Insert(ctx, AlwaysHighPriorityArgs{}, nil)
90 if err != nil {
91 panic(err)
92 }
93
94 // This job will run in the high-priority queue because of the options given
95 // at insertion time.
96 _, err = riverClient.Insert(ctx, SometimesHighPriorityArgs{}, &river.InsertOpts{
97 Queue: "high_priority",
98 })
99 if err != nil {
100 panic(err)
101 }
102
103 // Wait for jobs to complete. Only needed for purposes of the example test.
104 riversharedtest.WaitOrTimeoutN(testutil.PanicTB(), subscribeChan, 2)
105
106 if err := riverClient.Stop(ctx); err != nil {
107 panic(err)
108 }
109
110 // Output:
111 // Ran in queue: high_priority
112 // Ran in queue: high_priority

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…