-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathclient_examples_test.go
97 lines (77 loc) · 2.4 KB
/
client_examples_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
package asyncjobs
import (
"context"
"fmt"
"log"
"time"
)
func newEmail(to, subject, body string) map[string]any {
return map[string]any{
"to": to,
"subject": subject,
"body": body,
}
}
func panicIfErr(err error) {
if err != nil {
panic(err)
}
}
func ExampleClient_producer() {
queue := &Queue{
Name: "P100",
MaxRunTime: 60 * time.Minute,
MaxConcurrent: 20,
MaxTries: 100,
}
email := newEmail("[email protected]", "Test Subject", "Test Body")
// Creates a new task that has a deadline for processing 1 hour from now
task, err := NewTask("email:send", email, TaskDeadline(time.Now().Add(time.Hour)))
panicIfErr(err)
// Uses the NATS CLI context WQ for connection details, will create the queue if
// it does not already exist
client, err := NewClient(NatsContext("WQ"), WorkQueue(queue))
panicIfErr(err)
// Adds the task to the queue called P100
err = client.EnqueueTask(context.Background(), task)
panicIfErr(err)
}
func ExampleNewTask_with_deadline() {
email := newEmail("[email protected]", "Test Subject", "Test Body")
// Creates a new task that has a deadline for processing 1 hour from now
task, err := NewTask("email:send", email, TaskDeadline(time.Now().Add(time.Hour)))
if err != nil {
panic(fmt.Sprintf("Could not create task: %v", err))
}
fmt.Printf("Task ID: %s\n", task.ID)
}
func ExampleClient_consumer() {
queue := &Queue{
Name: "P100",
MaxRunTime: 60 * time.Minute,
MaxConcurrent: 20,
MaxTries: 100,
}
// Uses the NATS CLI context WQ for connection details, will create the queue if
// it does not already exist
client, err := NewClient(NatsContext("WQ"), WorkQueue(queue), RetryBackoffPolicy(RetryLinearOneHour))
panicIfErr(err)
router := NewTaskRouter()
err = router.HandleFunc("email:send", func(_ context.Context, _ Logger, t *Task) (any, error) {
log.Printf("Processing task: %s", t.ID)
// handle task.Payload which is a JSON encoded email
// task record will be updated with this payload result
return "success", nil
})
panicIfErr(err)
// Starts handling registered tasks, blocks until canceled
err = client.Run(context.Background(), router)
panicIfErr(err)
}
func ExampleClient_LoadTaskByID() {
client, err := NewClient(NatsContext("WQ"))
panicIfErr(err)
task, err := client.LoadTaskByID("24ErgVol4ZjpoQ8FAima9R2jEHB")
panicIfErr(err)
fmt.Printf("Loaded task %s in state %s", task.ID, task.State)
}