feat: use runners to startup the services
This commit is contained in:
committed by
Jörn Friedrich Dreyer
parent
61796a2b0b
commit
9e1b80a1be
@@ -3,13 +3,13 @@ package command
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os/signal"
|
||||
"reflect"
|
||||
|
||||
ehsvc "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/eventhistory/v0"
|
||||
"github.com/opencloud-eu/reva/v2/pkg/store"
|
||||
microstore "go-micro.dev/v4/store"
|
||||
|
||||
"github.com/oklog/run"
|
||||
"github.com/urfave/cli/v2"
|
||||
|
||||
"github.com/opencloud-eu/reva/v2/pkg/events"
|
||||
@@ -19,6 +19,7 @@ import (
|
||||
"github.com/opencloud-eu/opencloud/pkg/config/configlog"
|
||||
"github.com/opencloud-eu/opencloud/pkg/generators"
|
||||
"github.com/opencloud-eu/opencloud/pkg/registry"
|
||||
"github.com/opencloud-eu/opencloud/pkg/runner"
|
||||
"github.com/opencloud-eu/opencloud/pkg/service/grpc"
|
||||
"github.com/opencloud-eu/opencloud/pkg/tracing"
|
||||
settingssvc "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/settings/v0"
|
||||
@@ -57,11 +58,14 @@ func Server(cfg *config.Config) *cli.Command {
|
||||
return err
|
||||
}
|
||||
|
||||
gr := run.Group{}
|
||||
|
||||
ctx, cancel := context.WithCancel(c.Context)
|
||||
defer cancel()
|
||||
var cancel context.CancelFunc
|
||||
ctx := cfg.Context
|
||||
if ctx == nil {
|
||||
ctx, cancel = signal.NotifyContext(context.Background(), runner.StopSignals...)
|
||||
defer cancel()
|
||||
}
|
||||
|
||||
gr := runner.NewGroup()
|
||||
{
|
||||
debugServer, err := debug.Server(
|
||||
debug.Logger(logger),
|
||||
@@ -73,10 +77,7 @@ func Server(cfg *config.Config) *cli.Command {
|
||||
return err
|
||||
}
|
||||
|
||||
gr.Add(debugServer.ListenAndServe, func(_ error) {
|
||||
_ = debugServer.Shutdown(ctx)
|
||||
cancel()
|
||||
})
|
||||
gr.Add(runner.NewGolangHttpServerRunner("notifications_debug", debugServer))
|
||||
}
|
||||
|
||||
// evs defines a list of events to subscribe to
|
||||
@@ -139,11 +140,21 @@ func Server(cfg *config.Config) *cli.Command {
|
||||
cfg.Notifications.EmailTemplatePath, cfg.Notifications.DefaultLanguage, cfg.WebUIURL,
|
||||
cfg.Notifications.TranslationPath, cfg.Notifications.SMTP.Sender, notificationStore, historyClient, registeredEvents)
|
||||
|
||||
gr.Add(svc.Run, func(error) {
|
||||
cancel()
|
||||
})
|
||||
gr.Add(runner.New("notifications_svc", func() error {
|
||||
return svc.Run()
|
||||
}, func() {
|
||||
svc.Close()
|
||||
}))
|
||||
|
||||
return gr.Run()
|
||||
grResults := gr.Run(ctx)
|
||||
|
||||
// return the first non-nil error found in the results
|
||||
for _, grResult := range grResults {
|
||||
if grResult.RunnerError != nil {
|
||||
return grResult.RunnerError
|
||||
}
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,11 +5,10 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path"
|
||||
"strings"
|
||||
"syscall"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
ehsvc "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/eventhistory/v0"
|
||||
"go-micro.dev/v4/store"
|
||||
@@ -44,6 +43,7 @@ func init() {
|
||||
// Service should be named `Runner`
|
||||
type Service interface {
|
||||
Run() error
|
||||
Close()
|
||||
}
|
||||
|
||||
// NewEventsNotifier provides a new eventsNotifier
|
||||
@@ -62,7 +62,6 @@ func NewEventsNotifier(
|
||||
logger: logger,
|
||||
channel: channel,
|
||||
events: events,
|
||||
signals: make(chan os.Signal, 1),
|
||||
gatewaySelector: gatewaySelector,
|
||||
valueService: valueService,
|
||||
serviceAccountID: serviceAccountID,
|
||||
@@ -76,6 +75,8 @@ func NewEventsNotifier(
|
||||
splitter: newIntervalSplitter(logger, valueService),
|
||||
userEventStore: newUserEventStore(logger, store, historyClient),
|
||||
registeredEvents: registeredEvents,
|
||||
stopCh: make(chan struct{}, 1),
|
||||
stopped: new(atomic.Bool),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,7 +84,6 @@ type eventsNotifier struct {
|
||||
logger log.Logger
|
||||
channel channels.Channel
|
||||
events <-chan events.Event
|
||||
signals chan os.Signal
|
||||
gatewaySelector pool.Selectable[gateway.GatewayAPIClient]
|
||||
valueService settingssvc.ValueService
|
||||
emailTemplatePath string
|
||||
@@ -97,16 +97,27 @@ type eventsNotifier struct {
|
||||
splitter *intervalSplitter
|
||||
userEventStore *userEventStore
|
||||
registeredEvents map[string]events.Unmarshaller
|
||||
stopCh chan struct{}
|
||||
stopped *atomic.Bool
|
||||
}
|
||||
|
||||
func (s eventsNotifier) Run() error {
|
||||
signal.Notify(s.signals, syscall.SIGINT, syscall.SIGTERM)
|
||||
var wg sync.WaitGroup
|
||||
|
||||
s.logger.Debug().
|
||||
Msg("eventsNotifier started")
|
||||
EventLoop:
|
||||
for {
|
||||
select {
|
||||
case evt := <-s.events:
|
||||
case evt, ok := <-s.events:
|
||||
if !ok {
|
||||
break EventLoop
|
||||
}
|
||||
// TODO: needs to be replaced with a worker pool
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
|
||||
switch e := evt.Event.(type) {
|
||||
case events.SpaceShared:
|
||||
s.handleSpaceShared(e, evt.ID)
|
||||
@@ -124,12 +135,25 @@ func (s eventsNotifier) Run() error {
|
||||
s.sendGroupedEmailsJob(e, evt.ID)
|
||||
}
|
||||
}()
|
||||
case <-s.signals:
|
||||
|
||||
if s.stopped.Load() {
|
||||
break EventLoop
|
||||
}
|
||||
case <-s.stopCh:
|
||||
s.logger.Debug().
|
||||
Msg("eventsNotifier stopped")
|
||||
return nil
|
||||
break EventLoop
|
||||
}
|
||||
}
|
||||
// wait until all the goroutines processing events have finished
|
||||
wg.Wait()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s eventsNotifier) Close() {
|
||||
if s.stopped.CompareAndSwap(false, true) {
|
||||
close(s.stopCh)
|
||||
}
|
||||
}
|
||||
|
||||
func (s eventsNotifier) render(ctx context.Context, template email.MessageTemplate,
|
||||
|
||||
Reference in New Issue
Block a user