Example_customInsertOpts demonstrates the use of a job with custom job-specific insertion options.
()
| 53 | // Example_customInsertOpts demonstrates the use of a job with custom |
| 54 | // job-specific insertion options. |
| 55 | func 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 |
nothing calls this directly
no test coverage detected
searching dependent graphs…