Example_jobArgsHooks demonstrates the use of hooks to modify River behavior.
()
| 76 | |
| 77 | // Example_jobArgsHooks demonstrates the use of hooks to modify River behavior. |
| 78 | func Example_jobArgsHooks() { |
| 79 | ctx := context.Background() |
| 80 | |
| 81 | dbPool, err := pgxpool.New(ctx, riversharedtest.TestDatabaseURL()) |
| 82 | if err != nil { |
| 83 | panic(err) |
| 84 | } |
| 85 | defer dbPool.Close() |
| 86 | |
| 87 | workers := river.NewWorkers() |
| 88 | river.AddWorker(workers, &JobWithHooksWorker{}) |
| 89 | |
| 90 | riverClient, err := river.NewClient(riverpgxv5.New(dbPool), initTestConfig(ctx, dbPool, &river.Config{ |
| 91 | Queues: map[string]river.QueueConfig{ |
| 92 | river.QueueDefault: {MaxWorkers: 100}, |
| 93 | }, |
| 94 | Workers: workers, |
| 95 | })) |
| 96 | if err != nil { |
| 97 | panic(err) |
| 98 | } |
| 99 | |
| 100 | // Out of example scope, but used to wait until a job is worked. |
| 101 | subscribeChan, subscribeCancel := riverClient.Subscribe(river.EventKindJobCompleted) |
| 102 | defer subscribeCancel() |
| 103 | |
| 104 | if err := riverClient.Start(ctx); err != nil { |
| 105 | panic(err) |
| 106 | } |
| 107 | |
| 108 | _, err = riverClient.Insert(ctx, JobWithHooksArgs{}, nil) |
| 109 | if err != nil { |
| 110 | panic(err) |
| 111 | } |
| 112 | |
| 113 | // Wait for jobs to complete. Only needed for purposes of the example test. |
| 114 | riversharedtest.WaitOrTimeoutN(testutil.PanicTB(), subscribeChan, 1) |
| 115 | |
| 116 | if err := riverClient.Stop(ctx); err != nil { |
| 117 | panic(err) |
| 118 | } |
| 119 | |
| 120 | // Output: |
| 121 | // JobWithHooksInsertAndWorkBeginHook.InsertBegin ran |
| 122 | // JobWithHooksInsertBeginHook.InsertBegin ran |
| 123 | // JobWithHooksInsertAndWorkBeginHook.WorkBegin ran |
| 124 | // JobWithHooksWorkBeginHook.WorkBegin ran |
| 125 | // JobWithHooksWorker.Work ran |
| 126 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…