forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loggregator_logs_repository.go
111 lines (90 loc) · 2.87 KB
/
loggregator_logs_repository.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
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
101
102
103
104
105
106
107
108
109
110
111
package logs
import (
"errors"
"time"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/cf/api/authentication"
"github.com/cloudfoundry/cli/cf/configuration/coreconfig"
consumer "github.com/cloudfoundry/loggregator_consumer"
"github.com/cloudfoundry/loggregatorlib/logmessage"
noaa_errors "github.com/cloudfoundry/noaa/errors"
)
type LoggregatorLogsRepository struct {
consumer consumer.LoggregatorConsumer
config coreconfig.Reader
tokenRefresher authentication.TokenRefresher
messageQueue *LoggregatorMessageQueue
BufferTime time.Duration
}
func NewLoggregatorLogsRepository(config coreconfig.Reader, consumer consumer.LoggregatorConsumer, refresher authentication.TokenRefresher) *LoggregatorLogsRepository {
return &LoggregatorLogsRepository{
config: config,
consumer: consumer,
tokenRefresher: refresher,
messageQueue: NewLoggregatorMessageQueue(),
BufferTime: defaultBufferTime,
}
}
func (repo *LoggregatorLogsRepository) Close() {
repo.consumer.Close()
}
func loggableMessagesFromLoggregatorMessages(messages []*logmessage.LogMessage) []Loggable {
loggableMessages := make([]Loggable, len(messages))
for i, m := range messages {
loggableMessages[i] = NewLoggregatorLogMessage(m)
}
return loggableMessages
}
func (repo *LoggregatorLogsRepository) RecentLogsFor(appGUID string) ([]Loggable, error) {
messages, err := repo.consumer.Recent(appGUID, repo.config.AccessToken())
switch err.(type) {
case nil: // do nothing
case *noaa_errors.UnauthorizedError:
repo.tokenRefresher.RefreshAuthToken()
return repo.RecentLogsFor(appGUID)
default:
return loggableMessagesFromLoggregatorMessages(messages), err
}
consumer.SortRecent(messages)
return loggableMessagesFromLoggregatorMessages(messages), nil
}
func (repo *LoggregatorLogsRepository) TailLogsFor(appGUID string, onConnect func(), logChan chan<- Loggable, errChan chan<- error) {
ticker := time.NewTicker(repo.BufferTime)
endpoint := repo.config.LoggregatorEndpoint()
if endpoint == "" {
errChan <- errors.New(T("Loggregator endpoint missing from config file"))
return
}
repo.consumer.SetOnConnectCallback(onConnect)
c, err := repo.consumer.Tail(appGUID, repo.config.AccessToken())
switch err.(type) {
case nil: // do nothing
case *noaa_errors.UnauthorizedError:
repo.tokenRefresher.RefreshAuthToken()
c, err = repo.consumer.Tail(appGUID, repo.config.AccessToken())
default:
errChan <- err
return
}
if err != nil {
errChan <- err
return
}
go func() {
for range ticker.C {
repo.flushMessages(logChan)
}
}()
go func() {
for msg := range c {
repo.messageQueue.PushMessage(msg)
}
repo.flushMessages(logChan)
close(logChan)
}()
}
func (repo *LoggregatorLogsRepository) flushMessages(c chan<- Loggable) {
repo.messageQueue.EnumerateAndClear(func(m *logmessage.LogMessage) {
c <- NewLoggregatorLogMessage(m)
})
}