-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
84 lines (73 loc) · 2.37 KB
/
main.go
File metadata and controls
84 lines (73 loc) · 2.37 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"
"database/sql"
"fmt"
"log"
"time"
pubsub "github.com/coregx/pubsub"
"github.com/coregx/pubsub/adapters/relica"
_ "github.com/go-sql-driver/mysql"
)
// Example Logger adapter
type SimpleLogger struct{}
func (l *SimpleLogger) Debugf(format string, args ...interface{}) {
log.Printf("[DEBUG] "+format, args...)
}
func (l *SimpleLogger) Infof(format string, args ...interface{}) {
log.Printf("[INFO] "+format, args...)
}
func (l *SimpleLogger) Warnf(format string, args ...interface{}) {
log.Printf("[WARN] "+format, args...)
}
func (l *SimpleLogger) Errorf(format string, args ...interface{}) {
log.Printf("[ERROR] "+format, args...)
}
func (l *SimpleLogger) Info(message string) { log.Printf("[INFO] %s", message) }
func main() {
// Connect to database
db, err := sql.Open("mysql", "user:password@tcp(localhost:3306)/pubsub_db?parseTime=true")
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Test connection
if err := db.Ping(); err != nil {
log.Fatalf("Failed to connect to database: %v", err)
}
// Create logger
logger := &SimpleLogger{}
// Create repositories using Relica adapters (production-ready!)
// driverName should be "mysql", "postgres", or "sqlite3"
repos := relica.NewRepositories(db, "mysql")
// Create worker using Options Pattern (2025 best practice)
worker, err := pubsub.NewQueueWorker(
pubsub.WithRepositories(
repos.Queue,
repos.Message,
repos.Subscription,
repos.DLQ,
),
pubsub.WithDelivery(
nil, // transmitterProvider (TODO: implement delivery provider)
nil, // deliveryGateway (TODO: implement HTTP/webhook gateway)
),
pubsub.WithLogger(logger),
pubsub.WithBatchSize(100), // optional: customize batch size (default: 100)
// Optional: customize notification service
pubsub.WithNotifications(pubsub.NewLoggingNotificationService(logger)),
)
if err != nil {
log.Fatalf("Failed to create worker: %v", err)
}
// Run worker
ctx := context.Background()
fmt.Println("Starting PubSub worker with Relica adapters...")
fmt.Println("✅ Using production-ready Relica repository implementations")
fmt.Println("✅ Zero production dependencies (except database driver)")
fmt.Println("✅ Type-safe query building")
fmt.Println("✅ Supports MySQL, PostgreSQL, and SQLite")
fmt.Println()
// Run worker loop (processes pending/retryable items every 30 seconds)
worker.Run(ctx, 30*time.Second)
}