Uh oh!
There was an error while loading. Please reload this page.
forked from riverqueue/river
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_error_handler_test.go
More file actions
Latest commit
121 lines (97 loc) · 3.72 KB
/
Copy pathexample_error_handler_test.go
File metadata and controls
121 lines (97 loc) · 3.72 KB
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
package river_test
import (
"context"
"errors"
"fmt"
"log/slog"
"os"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/riverqueue/river"
"github.com/riverqueue/river/riverdbtest"
"github.com/riverqueue/river/riverdriver/riverpgxv5"
"github.com/riverqueue/river/rivershared/riversharedtest"
"github.com/riverqueue/river/rivershared/util/slogutil"
"github.com/riverqueue/river/rivershared/util/testutil"
"github.com/riverqueue/river/rivertype"
)
typeCustomErrorHandlerstruct{}
func (*CustomErrorHandler) HandleError(ctx context.Context, job*rivertype.JobRow, errerror) *river.ErrorHandlerResult {
fmt.Printf("Job errored with: %s\n", err)
returnnil
}
func (*CustomErrorHandler) HandlePanic(ctx context.Context, job*rivertype.JobRow, panicValany, tracestring) *river.ErrorHandlerResult {
fmt.Printf("Job panicked with: %v\n", panicVal)
// Either function can also set the job to be immediately cancelled.
return&river.ErrorHandlerResult{SetCancelled: true}
}
typeErroringArgsstruct {
ShouldErrorbool
ShouldPanicbool
}
func (ErroringArgs) Kind() string { return"erroring" }
// Here to make sure our jobs are never accidentally retried which would add
// additional output and fail the example.
func (ErroringArgs) InsertOpts() river.InsertOpts {
return river.InsertOpts{MaxAttempts: 1}
}
typeErroringWorkerstruct {
river.WorkerDefaults[ErroringArgs]
}
func (w*ErroringWorker) Work(ctx context.Context, job*river.Job[ErroringArgs]) error {
switch {
casejob.Args.ShouldError:
returnerrors.New("this job errored")
casejob.Args.ShouldPanic:
panic("this job panicked")
}
returnnil
}
// Example_errorHandler demonstrates how to use the ErrorHandler interface for
// custom application telemetry.
funcExample_errorHandler() {
ctx:=context.Background()
dbPool, err:=pgxpool.New(ctx, riversharedtest.TestDatabaseURL())
iferr!=nil {
panic(err)
}
deferdbPool.Close()
workers:=river.NewWorkers()
river.AddWorker(workers, &ErroringWorker{})
riverClient, err:=river.NewClient(riverpgxv5.New(dbPool), &river.Config{
ErrorHandler: &CustomErrorHandler{},
Logger: slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.Level(9), ReplaceAttr: slogutil.NoLevelTime})), // Suppress logging so example output is cleaner (9 > slog.LevelError).
Queues: map[string]river.QueueConfig{
river.QueueDefault: {MaxWorkers: 10},
},
Schema: riverdbtest.TestSchema(ctx, testutil.PanicTB(), riverpgxv5.New(dbPool), nil), // only necessary for the example test
TestOnly: true, // suitable only for use in tests; remove for live environments
Workers: workers,
})
iferr!=nil {
panic(err)
}
// Not strictly needed, but used to help this test wait until job is worked.
subscribeChan, subscribeCancel:=riverClient.Subscribe(river.EventKindJobCancelled, river.EventKindJobFailed)
defersubscribeCancel()
iferr:=riverClient.Start(ctx); err!=nil {
panic(err)
}
if_, err=riverClient.Insert(ctx, ErroringArgs{ShouldError: true}, nil); err!=nil {
panic(err)
}
// Wait for the first job before inserting another to guarantee test output
// is ordered correctly.
// Wait for jobs to complete. Only needed for purposes of the example test.
riversharedtest.WaitOrTimeoutN(testutil.PanicTB(), subscribeChan, 1)
if_, err=riverClient.Insert(ctx, ErroringArgs{ShouldPanic: true}, nil); err!=nil {
panic(err)
}
// Wait for jobs to complete. Only needed for purposes of the example test.
riversharedtest.WaitOrTimeoutN(testutil.PanicTB(), subscribeChan, 1)
iferr:=riverClient.Stop(ctx); err!=nil {
panic(err)
}
// Output:
// Job errored with: this job errored
// Job panicked with: this job panicked
}