-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Announcements and Monitoring of DST #269
Changes from 1 commit
435b1a8
a19ed66
b2ed5f8
f355611
ead66fc
153dac9
4b69efd
6717d19
e386f65
22837d8
e03f15f
25e331a
e597bea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,8 @@ | ||
package announcements | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
) | ||
|
||
type EventHandler func(event Event, monitor *Monitor) | ||
|
||
type Monitor struct { | ||
errors []error | ||
eventHandlers map[string]EventHandler // Map to store event handlers by event type | ||
mutex sync.Mutex // Mutex for thread safety | ||
} | ||
|
||
func NewMonitor() *Monitor { | ||
return &Monitor{ | ||
eventHandlers: make(map[string]EventHandler), | ||
} | ||
} | ||
|
||
func (m *Monitor) Apply(event Event) { | ||
m.mutex.Lock() | ||
defer m.mutex.Unlock() | ||
|
||
handler, ok := m.eventHandlers[event.Type] | ||
if !ok { | ||
m.errors = append(m.errors, fmt.Errorf("no handler found for event type: %s", event.Type)) | ||
return | ||
} | ||
|
||
handler(event, m) | ||
} | ||
|
||
func (m *Monitor) Status() []error { | ||
return m.errors | ||
} | ||
|
||
func (m *Monitor) RegisterEventHandler(eventType string, handler EventHandler) { | ||
m.mutex.Lock() | ||
defer m.mutex.Unlock() | ||
|
||
m.eventHandlers[eventType] = handler | ||
} | ||
|
||
// ---------------------------------------------------------------------------------- | ||
|
||
// Task Cache | ||
type TaskCache struct { | ||
tasks map[string]Event | ||
mutex sync.Mutex | ||
} | ||
|
||
var taskCache = TaskCache{ | ||
tasks: make(map[string]Event), | ||
} | ||
// monitor.go | ||
|
||
// TaskClaimedHandler is an event handler for the "TaskClaimed" event type. | ||
// It checks if the corresponding "TaskCompleted" event is received within a certain timeframe. | ||
// If not, it raises an alert. | ||
func TaskClaimedHandler(event Event, monitor *Monitor) { | ||
taskID, err := event.Get("taskId") | ||
// fmt.Println("-------------------------Monitor: Task claimed:", taskID) | ||
if err != nil { | ||
monitor.errors = append(monitor.errors, fmt.Errorf("error getting task ID: %v", err)) | ||
return | ||
} | ||
|
||
// Check if the task is already in the cache | ||
// If not, add it to the cache | ||
if _, ok := taskCache.tasks[taskID.(string)]; !ok { | ||
taskCache.mutex.Lock() | ||
taskCache.tasks[taskID.(string)] = event | ||
taskCache.mutex.Unlock() | ||
} | ||
} | ||
package announcements | ||
|
||
func TaskCompletedHandler(event Event, monitor *Monitor) { | ||
// Task completion event received, can perform any necessary actions | ||
// For example, log the event or update some state | ||
// fmt.Println("-------------------------Monitor: Task completed:", event) | ||
if taskID, err := event.Get("taskId"); err == nil { | ||
taskCache.mutex.Lock() | ||
delete(taskCache.tasks, taskID.(string)) | ||
taskCache.mutex.Unlock() | ||
} else { | ||
fmt.Println("Monitor: Error getting task ID:", err) | ||
monitor.errors = append(monitor.errors, fmt.Errorf("error getting task ID: %v", err)) | ||
} | ||
type Monitors interface { | ||
Apply(event Event) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would rename the interface to |
||
Status() []error | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package monitors | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/resonatehq/resonate/internal/announcements" | ||
) | ||
|
||
type taskMonitor struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TaskMonitor must not part of the core package for announcements and monitors, but part of the test/dst -- it's application specific |
||
eventTypeCount map[string]int // Map to count occurrences of each event type | ||
mutex sync.Mutex // Mutex for thread safety | ||
} | ||
|
||
func NewTaskMonitor() *taskMonitor { | ||
return &taskMonitor{ | ||
eventTypeCount: make(map[string]int), | ||
} | ||
} | ||
|
||
func (tm *taskMonitor) Apply(event announcements.Event) { | ||
tm.mutex.Lock() | ||
defer tm.mutex.Unlock() | ||
|
||
// Increment event type count | ||
tm.eventTypeCount[event.Type]++ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the right idea: When an event happens, modify a counter. When we check the monitors at the end of the run, here we want taskCreated == taskCompleted. If that check is too aggressive, at least we want to check that taskCreated >= taskCompleted
|
||
|
||
// If the event type is TaskCreated or TaskCompleted, print the event | ||
if event.Type == "TaskCreated" || event.Type == "TaskCompleted" { | ||
fmt.Println("Received event:", event.Type, event.Data) | ||
} | ||
} | ||
|
||
// Status returns any errors or status information associated with the taskMonitor. | ||
func (tm *taskMonitor) Status() []error { | ||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Usually a better practice to have the mutex at the start of the struct.
As the struct need not calculate the offset every time to find the mutex variable inside the struct.