71 lines
1.4 KiB
Go
71 lines
1.4 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
type EmailPayload struct {
|
|
To string `json:"to"`
|
|
Subject string `json:"subject"`
|
|
Body string `json:"body"`
|
|
}
|
|
|
|
func TestClient(t *testing.T) {
|
|
|
|
workerId, _ := os.Hostname()
|
|
|
|
client := New("http://localhost:10101")
|
|
ctx := context.Background()
|
|
|
|
newEmailPayload := EmailPayload{
|
|
To: "example@example.com",
|
|
Subject: "Test",
|
|
Body: "Hello World",
|
|
}
|
|
|
|
// Enqueue a task
|
|
task, err := client.Enqueue(ctx, &EnqueueRequest{
|
|
ApplicationId: "email",
|
|
Payload: newEmailPayload,
|
|
Priority: 10,
|
|
DelaySec: 0,
|
|
MaxAttempts: 5,
|
|
})
|
|
if err != nil {
|
|
log.Fatal("enqueue:", err)
|
|
}
|
|
fmt.Println("Enqueued task:", task.ID)
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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)
|
|
|
|
// 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")
|
|
}
|