feat: use publish/subscribe for tasks
This commit is contained in:
74
client/client_test.go
Normal file → Executable file
74
client/client_test.go
Normal file → Executable file
@@ -1,10 +1,12 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -18,8 +20,26 @@ func TestClient(t *testing.T) {
|
||||
|
||||
workerId, _ := os.Hostname()
|
||||
|
||||
client := New("http://localhost:10101")
|
||||
ctx := context.Background()
|
||||
client := New("http://10.10.10.2:10101", "email", workerId)
|
||||
|
||||
subID := client.Subscribe("send.email", func(task *Task) error {
|
||||
|
||||
// Parse payload into struct
|
||||
var email EmailPayload
|
||||
if err := task.ParsePayload(&email); err != nil {
|
||||
log.Fatal("parse payload:", err)
|
||||
}
|
||||
fmt.Printf("Got email task: %+v\n", email)
|
||||
|
||||
fmt.Println("Task 1 completed")
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
subID2 := client.Subscribe("send.email", func(task *Task) error {
|
||||
|
||||
return errors.New("Test Error")
|
||||
})
|
||||
|
||||
newEmailPayload := EmailPayload{
|
||||
To: "example@example.com",
|
||||
@@ -28,43 +48,27 @@ func TestClient(t *testing.T) {
|
||||
}
|
||||
|
||||
// Enqueue a task
|
||||
task, err := client.Enqueue(ctx, &EnqueueRequest{
|
||||
ApplicationId: "email",
|
||||
Payload: newEmailPayload,
|
||||
Priority: 10,
|
||||
DelaySec: 0,
|
||||
MaxAttempts: 5,
|
||||
err := client.Publish("send.email", newEmailPayload, &EnqueueOptions{
|
||||
Priority: 10,
|
||||
DelaySec: 0,
|
||||
MaxAttempts: 5,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal("enqueue:", err)
|
||||
}
|
||||
fmt.Println("Enqueued task:", task.ID)
|
||||
fmt.Println("Enqueued task")
|
||||
|
||||
// Pop a task
|
||||
task, ok, err := client.Pop(ctx, &PopRequest{
|
||||
WorkerId: workerId,
|
||||
LeaseSeconds: 60,
|
||||
Filter: &TaskFilter{ApplicationId: "email"},
|
||||
MinPriority: 0,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal("pop:", err)
|
||||
}
|
||||
if !ok {
|
||||
fmt.Println("No tasks available")
|
||||
return
|
||||
}
|
||||
// Block until SIGINT or SIGTERM
|
||||
sigChan := make(chan os.Signal, 1)
|
||||
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
// Parse payload into struct
|
||||
var email EmailPayload
|
||||
if err := task.ParsePayload(&email); err != nil {
|
||||
log.Fatal("parse payload:", err)
|
||||
}
|
||||
fmt.Printf("Got email task: %+v\n", email)
|
||||
<-sigChan
|
||||
|
||||
// Mark task as complete
|
||||
if err := client.Complete(ctx, &CompleteRequest{TaskId: task.ID, WorkerId: workerId}); err != nil {
|
||||
log.Fatal("complete:", err)
|
||||
}
|
||||
fmt.Println("Task completed")
|
||||
fmt.Println("\nShutting down...")
|
||||
|
||||
// Unsubscribe so polling stops
|
||||
client.Unsubscribe("send.email", subID)
|
||||
client.Unsubscribe("send.email", subID2)
|
||||
|
||||
fmt.Println("Clean exit.")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user