75 lines
1.4 KiB
Go
Executable File
75 lines
1.4 KiB
Go
Executable File
package client
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"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://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",
|
|
Subject: "Test",
|
|
Body: "Hello World",
|
|
}
|
|
|
|
// Enqueue a task
|
|
err := client.Publish("send.email", newEmailPayload, &EnqueueOptions{
|
|
Priority: 10,
|
|
DelaySec: 0,
|
|
MaxAttempts: 5,
|
|
})
|
|
if err != nil {
|
|
log.Fatal("enqueue:", err)
|
|
}
|
|
fmt.Println("Enqueued task")
|
|
|
|
// Block until SIGINT or SIGTERM
|
|
sigChan := make(chan os.Signal, 1)
|
|
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
<-sigChan
|
|
|
|
fmt.Println("\nShutting down...")
|
|
|
|
// Unsubscribe so polling stops
|
|
client.Unsubscribe("send.email", subID)
|
|
client.Unsubscribe("send.email", subID2)
|
|
|
|
fmt.Println("Clean exit.")
|
|
}
|