Example_sqlite demonstrates use of River's SQLite driver.
()
| 38 | |
| 39 | // Example_sqlite demonstrates use of River's SQLite driver. |
| 40 | func Example_sqlite() { //nolint:dupl |
| 41 | ctx := context.Background() |
| 42 | |
| 43 | dbPool, err := sql.Open("sqlite", ":memory:") |
| 44 | if err != nil { |
| 45 | panic(err) |
| 46 | } |
| 47 | dbPool.SetMaxOpenConns(1) |
| 48 | defer dbPool.Close() |
| 49 | |
| 50 | driver := riversqlite.New(dbPool) |
| 51 | |
| 52 | if err := migrateDB(ctx, driver); err != nil { |
| 53 | panic(err) |
| 54 | } |
| 55 | |
| 56 | workers := river.NewWorkers() |
| 57 | river.AddWorker(workers, &SortWorker{}) |
| 58 | |
| 59 | riverClient, err := river.NewClient(driver, initTestConfig(ctx, nil, &river.Config{ |
| 60 | Queues: map[string]river.QueueConfig{ |
| 61 | river.QueueDefault: {MaxWorkers: 100}, |
| 62 | }, |
| 63 | Workers: workers, |
| 64 | })) |
| 65 | if err != nil { |
| 66 | panic(err) |
| 67 | } |
| 68 | |
| 69 | // Out of example scope, but used to wait until a job is worked. |
| 70 | subscribeChan, subscribeCancel := riverClient.Subscribe(river.EventKindJobCompleted) |
| 71 | defer subscribeCancel() |
| 72 | |
| 73 | if err := riverClient.Start(ctx); err != nil { |
| 74 | panic(err) |
| 75 | } |
| 76 | |
| 77 | _, err = riverClient.Insert(ctx, SortArgs{ |
| 78 | Strings: []string{ |
| 79 | "whale", "tiger", "bear", |
| 80 | }, |
| 81 | }, nil) |
| 82 | if err != nil { |
| 83 | panic(err) |
| 84 | } |
| 85 | |
| 86 | // Wait for jobs to complete. Only needed for purposes of the example test. |
| 87 | riversharedtest.WaitOrTimeoutN(testutil.PanicTB(), subscribeChan, 1) |
| 88 | |
| 89 | if err := riverClient.Stop(ctx); err != nil { |
| 90 | panic(err) |
| 91 | } |
| 92 | |
| 93 | // Output: |
| 94 | // Sorted strings: [bear tiger whale] |
| 95 | } |
| 96 | |
| 97 | func migrateDB(ctx context.Context, driver riverdriver.Driver[*sql.Tx]) error { |
nothing calls this directly
no test coverage detected
searching dependent graphs…