Example_batchInsert demonstrates how many jobs can be inserted for work as part of a single operation.
()
| 30 | // Example_batchInsert demonstrates how many jobs can be inserted for work as |
| 31 | // part of a single operation. |
| 32 | func Example_batchInsert() { |
| 33 | ctx := context.Background() |
| 34 | |
| 35 | dbPool, err := pgxpool.New(ctx, riversharedtest.TestDatabaseURL()) |
| 36 | if err != nil { |
| 37 | panic(err) |
| 38 | } |
| 39 | defer dbPool.Close() |
| 40 | |
| 41 | workers := river.NewWorkers() |
| 42 | river.AddWorker(workers, &BatchInsertWorker{}) |
| 43 | |
| 44 | riverClient, err := river.NewClient(riverpgxv5.New(dbPool), initTestConfig(ctx, dbPool, &river.Config{ |
| 45 | Queues: map[string]river.QueueConfig{ |
| 46 | river.QueueDefault: {MaxWorkers: 100}, |
| 47 | }, |
| 48 | Workers: workers, |
| 49 | })) |
| 50 | if err != nil { |
| 51 | panic(err) |
| 52 | } |
| 53 | |
| 54 | // Out of example scope, but used to wait until a job is worked. |
| 55 | subscribeChan, subscribeCancel := riverClient.Subscribe(river.EventKindJobCompleted) |
| 56 | defer subscribeCancel() |
| 57 | |
| 58 | results, err := riverClient.InsertMany(ctx, []river.InsertManyParams{ |
| 59 | {Args: BatchInsertArgs{}}, |
| 60 | {Args: BatchInsertArgs{}}, |
| 61 | {Args: BatchInsertArgs{}}, |
| 62 | {Args: BatchInsertArgs{}, InsertOpts: &river.InsertOpts{Priority: 3}}, |
| 63 | {Args: BatchInsertArgs{}, InsertOpts: &river.InsertOpts{Priority: 4}}, |
| 64 | }) |
| 65 | if err != nil { |
| 66 | panic(err) |
| 67 | } |
| 68 | fmt.Printf("Inserted %d jobs\n", len(results)) |
| 69 | |
| 70 | // Start the client after inserting and printing so that the "Inserted" |
| 71 | // message is guaranteed to appear before any "Worked" messages. |
| 72 | if err := riverClient.Start(ctx); err != nil { |
| 73 | panic(err) |
| 74 | } |
| 75 | |
| 76 | // Wait for jobs to complete. Only needed for purposes of the example test. |
| 77 | riversharedtest.WaitOrTimeoutN(testutil.PanicTB(), subscribeChan, 5) |
| 78 | |
| 79 | if err := riverClient.Stop(ctx); err != nil { |
| 80 | panic(err) |
| 81 | } |
| 82 | |
| 83 | // Output: |
| 84 | // Inserted 5 jobs |
| 85 | // Worked a job |
| 86 | // Worked a job |
| 87 | // Worked a job |
| 88 | // Worked a job |
| 89 | // Worked a job |
nothing calls this directly
no test coverage detected
searching dependent graphs…