-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
84 lines (74 loc) · 2.06 KB
/
Copy pathmain.go
File metadata and controls
84 lines (74 loc) · 2.06 KB
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
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"github.com/sequinstream/sequin-go"
)
func main() {
// Parse command line flags
token := flag.String("token", "", "Sequin API token")
consumerGroup := flag.String("consumer-group", "", "Consumer Group name or ID")
outputFile := flag.String("output", "", "Output file path (optional, defaults to stdout)")
maxBatchSize := flag.Int("max-batch-size", 10, "Maximum batch size for processing messages")
baseURL := flag.String("base-url", "", "Sequin API base URL (optional, defaults to https://api.sequinstream.com/api)")
flag.Parse()
// Validate required flags
if *token == "" || *consumerGroup == "" {
log.Fatal("token and consumer-group flags are required")
}
// Setup output destination
var output *os.File
var err error
if *outputFile != "" {
output, err = os.OpenFile(*outputFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatalf("Failed to open output file: %v", err)
}
defer output.Close()
} else {
output = os.Stdout
}
// Initialize Sequin client
client := sequin.NewClient(&sequin.ClientOptions{
Token: *token,
BaseURL: *baseURL,
})
// Create message processor
processor, err := sequin.NewProcessor(
client,
*consumerGroup,
func(ctx context.Context, msgs []sequin.Message) error {
for _, msg := range msgs {
fmt.Fprintf(output, "%s\n", string(msg.Record))
}
return nil
},
sequin.ProcessorOptions{
MaxBatchSize: *maxBatchSize,
},
)
if err != nil {
log.Fatalf("Failed to create processor: %v", err)
}
// Setup context with cancellation
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Handle shutdown signals
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigChan
log.Println("Shutting down...")
cancel()
}()
// Run the processor
log.Printf("Starting consumer (max batch size: %d)", *maxBatchSize)
if err := processor.Run(ctx); err != nil && err != context.Canceled {
log.Fatalf("Processor failed: %v", err)
}
}