This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
local_eventsink.go
72 lines (59 loc) · 1.84 KB
/
local_eventsink.go
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
package events
import (
"bufio"
"context"
"fmt"
"os"
"sync"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/lyft/flyteidl/gen/pb-go/flyteidl/event"
)
type localSink struct {
mu sync.Mutex
writer writer
}
func (s *localSink) Sink(ctx context.Context, message proto.Message) error {
s.mu.Lock()
defer s.writer.Flush()
defer s.mu.Unlock()
var eventOutput string
switch e := message.(type) {
case *event.WorkflowExecutionEvent:
eventOutput = fmt.Sprintf("[--WF EVENT--] %s, Phase: %s, OccuredAt: %s\n",
e.ExecutionId, e.Phase, ptypes.TimestampString(e.OccurredAt))
case *event.NodeExecutionEvent:
eventOutput = fmt.Sprintf("[--NODE EVENT--] %s, Phase: %s, OccuredAt: %s\n",
e.Id, e.Phase, ptypes.TimestampString(e.OccurredAt))
case *event.TaskExecutionEvent:
eventOutput = fmt.Sprintf("[--TASK EVENT--] %s,%s, Phase: %s, OccuredAt: %s\n",
e.TaskId, e.ParentNodeExecutionId, e.Phase, ptypes.TimestampString(e.OccurredAt))
}
return s.writer.Write(ctx, eventOutput)
}
func (s *localSink) Close() error {
return nil
}
// EventSink will sink events to a writer that puts the events somewhere depending on how it was configured
type writer interface {
Write(ctx context.Context, content string) error
Flush() error
}
func NewLogSink() (EventSink, error) {
return &localSink{writer: &LogWriter{}}, nil
}
func NewStdoutSink() (EventSink, error) {
return &localSink{writer: &StdWriter{}}, nil
}
// TODO this will cause multiple handles to the same file if we open multiple syncs. Maybe we should remove this
func NewFileSink(path string) (EventSink, error) {
f, err := os.OpenFile(path, os.O_APPEND|os.O_RDWR|os.O_CREATE, os.FileMode(0666))
if err != nil {
return nil, err
}
w := bufio.NewWriter(f)
if err != nil {
return nil, err
}
return &localSink{writer: &FileWriter{ioWriter: w}}, nil
}