woodpecker/server/logging/log.go
Anbraten 556607b525
Rework log streaming and related functions (#1802)
closes #1801
closes #1815 
closes #1144
closes  #983
closes  #557
closes #1827
regression of #1791

# TODO
- [x] adjust log model
- [x] add migration for logs
- [x] send log line via grpc using step-id
- [x] save log-line to db
- [x] stream log-lines to UI
- [x] use less structs for log-data
- [x] make web UI work
  - [x] display logs loaded from db
  - [x] display streaming logs
- [ ] ~~make migration work~~ -> dedicated pull (#1828)

# TESTED
- [x] new logs are stored in database
- [x] log retrieval via cli (of new logs) works
- [x] log streaming works (tested via curl & webui)
- [x] log retrieval via web (of new logs) works

---------

Co-authored-by: 6543 <6543@obermui.de>
2023-06-06 09:52:08 +02:00

125 lines
2.1 KiB
Go

package logging
import (
"context"
"sync"
"github.com/woodpecker-ci/woodpecker/server/model"
)
// TODO (bradrydzewski) writing to subscribers is currently a blocking
// operation and does not protect against slow clients from locking
// the stream. This should be resolved.
// TODO (bradrydzewski) implement a mux.Info to fetch information and
// statistics for the multiplexer. Streams, subscribers, etc
// mux.Info()
// TODO (bradrydzewski) refactor code to place publisher and subscriber
// operations in separate files with more encapsulated logic.
// sub.push()
// sub.join()
// sub.start()... event loop
type subscriber struct {
handler Handler
}
type stream struct {
sync.Mutex
stepID int64
list []*model.LogEntry
subs map[*subscriber]struct{}
done chan struct{}
}
type log struct {
sync.Mutex
streams map[int64]*stream
}
// New returns a new logger.
func New() Log {
return &log{
streams: map[int64]*stream{},
}
}
func (l *log) Open(_ context.Context, stepID int64) error {
l.Lock()
_, ok := l.streams[stepID]
if !ok {
l.streams[stepID] = &stream{
stepID: stepID,
subs: make(map[*subscriber]struct{}),
done: make(chan struct{}),
}
}
l.Unlock()
return nil
}
func (l *log) Write(_ context.Context, stepID int64, logEntry *model.LogEntry) error {
l.Lock()
s, ok := l.streams[stepID]
l.Unlock()
if !ok {
return ErrNotFound
}
s.Lock()
s.list = append(s.list, logEntry)
for sub := range s.subs {
go sub.handler(logEntry)
}
s.Unlock()
return nil
}
func (l *log) Tail(c context.Context, stepID int64, handler Handler) error {
l.Lock()
s, ok := l.streams[stepID]
l.Unlock()
if !ok {
return ErrNotFound
}
sub := &subscriber{
handler: handler,
}
s.Lock()
if len(s.list) != 0 {
sub.handler(s.list...)
}
s.subs[sub] = struct{}{}
s.Unlock()
select {
case <-c.Done():
case <-s.done:
}
s.Lock()
delete(s.subs, sub)
s.Unlock()
return nil
}
func (l *log) Close(_ context.Context, stepID int64) error {
l.Lock()
s, ok := l.streams[stepID]
l.Unlock()
if !ok {
return ErrNotFound
}
s.Lock()
close(s.done)
s.Unlock()
l.Lock()
delete(l.streams, stepID)
l.Unlock()
return nil
}