forked from riverqueue/river
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_unique_job_test.go
133 lines (106 loc) · 3.52 KB
/
example_unique_job_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package river_test
import (
"context"
"fmt"
"log/slog"
"time"
"github.com/jackc/pgx/v5/pgxpool"
"weavelab.xyz/river"
"weavelab.xyz/river/internal/riverinternaltest"
"weavelab.xyz/river/internal/util/slogutil"
"weavelab.xyz/river/riverdriver/riverpgxv5"
)
// Account represents a minimal account including recent expenditures and a
// remaining total.
type Account struct {
RecentExpenditures int
AccountTotal int
}
// Map of account ID -> account.
var allAccounts = map[int]Account{ //nolint:gochecknoglobals
1: {RecentExpenditures: 100, AccountTotal: 1_000},
2: {RecentExpenditures: 999, AccountTotal: 1_000},
}
type ReconcileAccountArgs struct {
AccountID int `json:"account_id"`
}
func (ReconcileAccountArgs) Kind() string { return "reconcile_account" }
// InsertOpts returns custom insert options that every job of this type will
// inherit, including unique options.
func (ReconcileAccountArgs) InsertOpts() river.InsertOpts {
return river.InsertOpts{
UniqueOpts: river.UniqueOpts{
ByArgs: true,
ByPeriod: 24 * time.Hour,
},
}
}
type ReconcileAccountWorker struct {
river.WorkerDefaults[ReconcileAccountArgs]
}
func (w *ReconcileAccountWorker) Work(ctx context.Context, job *river.Job[ReconcileAccountArgs]) error {
account := allAccounts[job.Args.AccountID]
account.AccountTotal -= account.RecentExpenditures
account.RecentExpenditures = 0
fmt.Printf("Reconciled account %d; new total: %d\n", job.Args.AccountID, account.AccountTotal)
return nil
}
// Example_uniqueJob demonstrates the use of a job with custom
// job-specific insertion options.
func Example_uniqueJob() {
ctx := context.Background()
dbPool, err := pgxpool.NewWithConfig(ctx, riverinternaltest.DatabaseConfig("river_testdb_example"))
if err != nil {
panic(err)
}
defer dbPool.Close()
// Required for the purpose of this test, but not necessary in real usage.
if err := riverinternaltest.TruncateRiverTables(ctx, dbPool); err != nil {
panic(err)
}
workers := river.NewWorkers()
river.AddWorker(workers, &ReconcileAccountWorker{})
riverClient, err := river.NewClient(riverpgxv5.New(dbPool), &river.Config{
Logger: slog.New(&slogutil.SlogMessageOnlyHandler{Level: slog.LevelWarn}),
Queues: map[string]river.QueueConfig{
river.QueueDefault: {MaxWorkers: 100},
},
Workers: workers,
})
if err != nil {
panic(err)
}
// Out of example scope, but used to wait until a job is worked.
subscribeChan, subscribeCancel := riverClient.Subscribe(river.EventKindJobCompleted)
defer subscribeCancel()
if err := riverClient.Start(ctx); err != nil {
panic(err)
}
// First job insertion for account 1.
_, err = riverClient.Insert(ctx, ReconcileAccountArgs{AccountID: 1}, nil)
if err != nil {
panic(err)
}
// Job is inserted a second time, but it doesn't matter because its unique
// args cause the insertion to be skipped because it's meant to only run
// once per account per 24 hour period.
_, err = riverClient.Insert(ctx, ReconcileAccountArgs{AccountID: 1}, nil)
if err != nil {
panic(err)
}
// Cheat a little by waiting for the first job to come back so we can
// guarantee that this example's output comes out in order.
waitForNJobs(subscribeChan, 1)
// Because the job is unique ByArgs, another job for account 2 is allowed.
_, err = riverClient.Insert(ctx, ReconcileAccountArgs{AccountID: 2}, nil)
if err != nil {
panic(err)
}
waitForNJobs(subscribeChan, 1)
if err := riverClient.Stop(ctx); err != nil {
panic(err)
}
// Output:
// Reconciled account 1; new total: 900
// Reconciled account 2; new total: 1
}