package main
import (
"encoding/json"
"fmt"
"github.com/go-redis/redis/v8"
"context"
"time"
)
type Job struct {
ID string `json:"id"`
Type string `json:"type"`
Data map[string]interface{} `json:"data"`
}
func main() {
// Connect to queue
queue := redis.NewClient(&redis.Options{
Addr: "localhost:6380",
})
ctx := context.Background()
// Producer
job := Job{
ID: "job_123",
Type: "email",
Data: map[string]interface{}{
"to": "user@example.com",
"subject": "Hello",
},
}
jobJSON, _ := json.Marshal(job)
queue.LPush(ctx, "jobs:email", jobJSON)
// Consumer
for {
result := queue.BRPop(ctx, 5*time.Second, "jobs:email")
if len(result.Val()) > 0 {
var job Job
json.Unmarshal([]byte(result.Val()[1]), &job)
fmt.Printf("Processing job: %+v\n", job)
// Process the job here
processJob(job)
}
}
}