-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotifier.go
More file actions
100 lines (87 loc) · 2.78 KB
/
notifier.go
File metadata and controls
100 lines (87 loc) · 2.78 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package tributary
import (
"context"
"fmt"
"time"
)
// startNotificationListener starts a goroutine that listens for PostgreSQL notifications
// and signals workers when new steps are available.
func (c *Client) startNotificationListener(ctx context.Context) {
c.wg.Add(1)
go c.runNotificationListener(ctx)
}
// runNotificationListener is the main loop for listening to PostgreSQL notifications.
func (c *Client) runNotificationListener(ctx context.Context) {
defer c.wg.Done()
for {
select {
case <-ctx.Done():
return
case <-c.ctx.Done():
return
default:
// Attempt to listen for notifications
if err := c.listenForNotifications(ctx); err != nil {
c.config.Logger.Warn("Notification listener error, reconnecting in 5s", Field{Key: "error", Value: err})
time.Sleep(5 * time.Second)
}
}
}
}
// listenForNotifications establishes a connection and listens for PostgreSQL NOTIFY events.
func (c *Client) listenForNotifications(ctx context.Context) error {
// Acquire a dedicated connection for LISTEN
conn, err := c.pool.Acquire(ctx)
if err != nil {
return fmt.Errorf("failed to acquire connection: %w", err)
}
defer conn.Release()
// Subscribe to the notification channels
_, err = conn.Exec(ctx, "LISTEN "+ChannelStepAvailable)
if err != nil {
return fmt.Errorf("failed to LISTEN: %w", err)
}
c.config.Logger.Info("PostgreSQL LISTEN active",
Field{Key: "channel_available", Value: ChannelStepAvailable},
Field{Key: "channel_completed", Value: ChannelStepCompleted})
// Also listen for step completion events
_, err = conn.Exec(ctx, "LISTEN "+ChannelStepCompleted)
if err != nil {
return fmt.Errorf("failed to LISTEN: %w", err)
}
// Wait for notifications
// Use a short timeout to periodically check for context cancellation
for {
// Check for shutdown first
select {
case <-ctx.Done():
return ctx.Err()
case <-c.ctx.Done():
return c.ctx.Err()
default:
}
// Wait for notification with a timeout
notificationCtx, cancel := context.WithTimeout(ctx, 1*time.Second)
notification, err := conn.Conn().WaitForNotification(notificationCtx)
cancel() // Always cancel to free resources
if err != nil {
// Check if context was cancelled
if ctx.Err() != nil || c.ctx.Err() != nil {
return nil
}
// Timeout is expected - continue
continue
}
// Notification received - wake up workers
if notification.Channel == ChannelStepAvailable || notification.Channel == ChannelStepCompleted {
c.notifyWorkers()
}
}
}
// notifyWorkers signals local workers to check for new steps.
// This is called both from LISTEN/NOTIFY and from local insertions.
func (c *Client) notifyWorkers() {
// Use the new subscription-based notification system
// This will call all registered worker callbacks simultaneously
c.notifyAllWorkers()
}