Files
ocis/services/postprocessing/pkg/command/server.go
T

108 lines
3.1 KiB
Go
Raw Normal View History

2022-12-08 11:00:42 +01:00
package command
import (
2023-05-02 16:59:13 +02:00
"context"
2022-12-08 11:00:42 +01:00
"fmt"
"os"
2024-05-02 14:10:32 +02:00
"os/signal"
2022-12-08 11:00:42 +01:00
2025-03-04 16:02:36 +01:00
"github.com/owncloud/reva/v2/pkg/events/stream"
"github.com/owncloud/reva/v2/pkg/store"
"github.com/urfave/cli/v2"
microstore "go-micro.dev/v4/store"
"github.com/owncloud/ocis/v2/ocis-pkg/generators"
2024-05-02 14:10:32 +02:00
"github.com/owncloud/ocis/v2/ocis-pkg/runner"
"github.com/owncloud/ocis/v2/ocis-pkg/tracing"
2022-12-08 11:00:42 +01:00
"github.com/owncloud/ocis/v2/services/postprocessing/pkg/config"
"github.com/owncloud/ocis/v2/services/postprocessing/pkg/config/parser"
"github.com/owncloud/ocis/v2/services/postprocessing/pkg/logging"
2024-10-10 11:57:59 +02:00
"github.com/owncloud/ocis/v2/services/postprocessing/pkg/server/debug"
2022-12-08 11:00:42 +01:00
"github.com/owncloud/ocis/v2/services/postprocessing/pkg/service"
)
// Server is the entrypoint for the server command.
func Server(cfg *config.Config) *cli.Command {
return &cli.Command{
Name: "server",
Usage: fmt.Sprintf("start %s service without runtime (unsupervised mode)", cfg.Service.Name),
Category: "server",
Before: func(c *cli.Context) error {
err := parser.ParseConfig(cfg)
if err != nil {
fmt.Printf("%v", err)
os.Exit(1)
}
return err
},
Action: func(c *cli.Context) error {
2024-05-02 14:10:32 +02:00
logger := logging.Configure(cfg.Service.Name, cfg.Log)
var cancel context.CancelFunc
if cfg.Context == nil {
cfg.Context, cancel = signal.NotifyContext(context.Background(), runner.StopSignals...)
2024-05-02 14:10:32 +02:00
defer cancel()
}
ctx := cfg.Context
2023-05-02 16:59:13 +02:00
traceProvider, err := tracing.GetServiceTraceProvider(cfg.Tracing, cfg.Service.Name)
if err != nil {
return err
}
2024-05-02 14:10:32 +02:00
gr := runner.NewGroup()
2023-05-02 16:59:13 +02:00
{
2025-03-19 10:29:01 +01:00
connName := generators.GenerateConnectionName(cfg.Service.Name, generators.NTypeBus)
bus, err := stream.NatsFromConfig(connName, false, stream.NatsConfig(cfg.Postprocessing.Events))
2023-05-02 16:59:13 +02:00
if err != nil {
return err
2022-12-08 11:00:42 +01:00
}
2023-05-11 13:05:49 +02:00
st := store.Create(
store.Store(cfg.Store.Store),
store.TTL(cfg.Store.TTL),
microstore.Nodes(cfg.Store.Nodes...),
microstore.Database(cfg.Store.Database),
microstore.Table(cfg.Store.Table),
2023-12-18 14:58:20 +01:00
store.Authentication(cfg.Store.AuthUsername, cfg.Store.AuthPassword),
store.TLS(cfg.Store.EnableTLS, cfg.Store.TLSInsecure, cfg.Store.TLSRootCACertificate),
2023-05-11 13:05:49 +02:00
)
svc, err := service.NewPostprocessingService(ctx, bus, logger, st, traceProvider, cfg.Postprocessing)
2023-05-02 16:59:13 +02:00
if err != nil {
return err
}
2025-05-27 13:02:34 +02:00
gr.Add(runner.New(cfg.Service.Name+".svc", func() error {
2024-05-02 14:10:32 +02:00
return svc.Run()
}, func() {
svc.Close()
}))
2022-12-08 11:00:42 +01:00
}
2023-05-02 16:59:13 +02:00
{
2024-10-10 11:57:59 +02:00
debugServer, err := debug.Server(
2023-05-02 16:59:13 +02:00
debug.Logger(logger),
2024-10-10 11:57:59 +02:00
debug.Context(ctx),
debug.Config(cfg),
2023-05-02 16:59:13 +02:00
)
2024-10-10 11:57:59 +02:00
if err != nil {
logger.Info().Err(err).Str("transport", "debug").Msg("Failed to initialize server")
return err
}
2023-05-02 16:59:13 +02:00
2024-05-02 14:10:32 +02:00
gr.Add(runner.NewGolangHttpServerRunner("postprocessing_debug", debugServer))
}
2025-06-10 18:21:49 +02:00
logger.Warn().Msgf("starting service %s", cfg.Service.Name)
2024-05-02 14:10:32 +02:00
grResults := gr.Run(ctx)
2025-06-10 18:21:49 +02:00
if err := runner.ProcessResults(grResults); err != nil {
logger.Error().Err(err).Msgf("service %s stopped with error", cfg.Service.Name)
return err
2022-12-08 11:00:42 +01:00
}
2025-06-10 18:21:49 +02:00
logger.Warn().Msgf("service %s stopped without error", cfg.Service.Name)
2024-05-02 14:10:32 +02:00
return nil
2022-12-08 11:00:42 +01:00
},
}
}