2023-08-10 09:06:00 +00:00
|
|
|
// Copyright 2023 Woodpecker Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-03-05 07:56:08 +00:00
|
|
|
package logging
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
2023-06-06 07:52:08 +00:00
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
2017-03-05 07:56:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
2022-11-09 07:12:17 +00:00
|
|
|
// statistics for the multiplexer. Streams, subscribers, etc
|
2017-03-05 07:56:08 +00:00
|
|
|
// 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
|
|
|
|
|
2023-06-06 07:52:08 +00:00
|
|
|
stepID int64
|
|
|
|
list []*model.LogEntry
|
|
|
|
subs map[*subscriber]struct{}
|
|
|
|
done chan struct{}
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type log struct {
|
|
|
|
sync.Mutex
|
|
|
|
|
2023-06-06 07:52:08 +00:00
|
|
|
streams map[int64]*stream
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new logger.
|
|
|
|
func New() Log {
|
|
|
|
return &log{
|
2023-06-06 07:52:08 +00:00
|
|
|
streams: map[int64]*stream{},
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-06 07:52:08 +00:00
|
|
|
func (l *log) Open(_ context.Context, stepID int64) error {
|
2017-03-05 07:56:08 +00:00
|
|
|
l.Lock()
|
2023-06-06 07:52:08 +00:00
|
|
|
_, ok := l.streams[stepID]
|
2017-03-05 07:56:08 +00:00
|
|
|
if !ok {
|
2023-06-06 07:52:08 +00:00
|
|
|
l.streams[stepID] = &stream{
|
|
|
|
stepID: stepID,
|
|
|
|
subs: make(map[*subscriber]struct{}),
|
|
|
|
done: make(chan struct{}),
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
l.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-01 16:55:00 +00:00
|
|
|
func (l *log) Write(ctx context.Context, stepID int64, logEntry *model.LogEntry) error {
|
2017-03-05 07:56:08 +00:00
|
|
|
l.Lock()
|
2023-06-06 07:52:08 +00:00
|
|
|
s, ok := l.streams[stepID]
|
2017-03-05 07:56:08 +00:00
|
|
|
l.Unlock()
|
2023-11-04 05:51:26 +00:00
|
|
|
|
|
|
|
// auto open the stream if it does not exist
|
2017-03-05 07:56:08 +00:00
|
|
|
if !ok {
|
2023-11-04 05:51:26 +00:00
|
|
|
err := l.Open(ctx, stepID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-05 08:31:06 +00:00
|
|
|
s = l.streams[stepID]
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
2023-11-04 05:51:26 +00:00
|
|
|
|
2017-03-05 07:56:08 +00:00
|
|
|
s.Lock()
|
2023-06-06 07:52:08 +00:00
|
|
|
s.list = append(s.list, logEntry)
|
2017-03-05 07:56:08 +00:00
|
|
|
for sub := range s.subs {
|
2023-06-06 07:52:08 +00:00
|
|
|
go sub.handler(logEntry)
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
s.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-06 07:52:08 +00:00
|
|
|
func (l *log) Tail(c context.Context, stepID int64, handler Handler) error {
|
2017-03-05 07:56:08 +00:00
|
|
|
l.Lock()
|
2023-06-06 07:52:08 +00:00
|
|
|
s, ok := l.streams[stepID]
|
2017-03-05 07:56:08 +00:00
|
|
|
l.Unlock()
|
|
|
|
if !ok {
|
|
|
|
return ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
sub := &subscriber{
|
|
|
|
handler: handler,
|
|
|
|
}
|
|
|
|
s.Lock()
|
2021-10-08 16:35:56 +00:00
|
|
|
if len(s.list) != 0 {
|
|
|
|
sub.handler(s.list...)
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
s.subs[sub] = struct{}{}
|
|
|
|
s.Unlock()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-c.Done():
|
|
|
|
case <-s.done:
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Lock()
|
|
|
|
delete(s.subs, sub)
|
|
|
|
s.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-06 07:52:08 +00:00
|
|
|
func (l *log) Close(_ context.Context, stepID int64) error {
|
2017-03-05 07:56:08 +00:00
|
|
|
l.Lock()
|
2023-06-06 07:52:08 +00:00
|
|
|
s, ok := l.streams[stepID]
|
2017-03-05 07:56:08 +00:00
|
|
|
l.Unlock()
|
|
|
|
if !ok {
|
|
|
|
return ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Lock()
|
|
|
|
close(s.done)
|
|
|
|
s.Unlock()
|
|
|
|
|
|
|
|
l.Lock()
|
2023-06-06 07:52:08 +00:00
|
|
|
delete(l.streams, stepID)
|
2017-03-05 07:56:08 +00:00
|
|
|
l.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|