Skip to content

Commit 3ca6190

Browse files
Swopxvzf
authored andcommitted
feat: badger storage, e2e, improved testability (#4)
* feat: badger storage, e2e, improved testability * fix: use dedicated context when saving state
1 parent 08f387c commit 3ca6190

19 files changed

+2076
-142
lines changed

.pre-commit-config.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ repos:
1212
entry: make test
1313
types: [go]
1414
pass_filenames: false
15+
- id: go-e2e
16+
name: go e2e
17+
language: system
18+
entry: make e2e
19+
types: [ go ]
20+
pass_filenames: false
1521
- id: golangci-lint
1622
name: golangci-lint
1723
entry: make lint

Makefile

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ LEASE_SERVICE_PORT ?= 9000
44
GOOS := $(shell go env GOOS)
55
GOARCH := $(shell go env GOARCH)
66

7+
E2E_GINKGO_VERSION := v2.8.2
8+
79
default: clean lint test build
810

911
.PHONY: help
@@ -13,12 +15,20 @@ help: ## Print this help with list of available commands/targets and their purpo
1315
lint: ## run linters on the code base
1416
golangci-lint run
1517

18+
clear-state: ## cleanup storage DB
19+
rm -rf /tmp/state
20+
1621
.PHONY: clean
1722
clean: ## cleanup test cover output
1823
rm -rf cover.out
1924

2025
test: ## run unit tests
21-
go test -race -cover ./...
26+
go test -race -cover $(shell go list ./... | grep -v /e2e)
27+
28+
.PHONY: e2e
29+
e2e: ## run e2e tests
30+
cd e2e && \
31+
go run github.com/onsi/ginkgo/v2/ginkgo@$(E2E_GINKGO_VERSION)
2232

2333
.PHONY: build
2434
build: ## build the application (server & cli)

cmd/server/main.go

+45-24
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,75 @@ package main
33
import (
44
"flag"
55
"os"
6-
"strconv"
6+
"os/signal"
7+
"syscall"
78

8-
"github.com/ankorstore/gh-action-mq-lease-service/internal/config"
9-
"github.com/ankorstore/gh-action-mq-lease-service/internal/lease"
109
"github.com/ankorstore/gh-action-mq-lease-service/internal/server"
1110
"github.com/ankorstore/gh-action-mq-lease-service/internal/version"
1211
"github.com/ankorstore/gh-action-mq-lease-service/pkg/util/logger"
13-
"github.com/gofiber/fiber/v2"
1412
)
1513

1614
var (
17-
serverPort uint
18-
configPath string
15+
serverPort uint
16+
configPath string
17+
logDebug bool
18+
logJSON bool
19+
persistentStateDir string
1920
)
2021

2122
func init() {
23+
// General flags
2224
flag.UintVar(&serverPort, "port", 9000, "server listening port")
2325
flag.StringVar(&configPath, "config", "./config.yaml", "Configuration path")
2426

25-
// Register logging flags
26-
logger.InitFlags()
27+
// Logging flags
28+
flag.BoolVar(&logDebug, "log-debug", false, "Enable debug logging")
29+
flag.BoolVar(&logJSON, "log-json", true, "Enable console logging format")
30+
31+
// Persistent state flags
32+
flag.StringVar(&persistentStateDir, "persistents-state-dir", "/tmp/state", "Setup the directory for persistent state storage")
2733
}
2834

2935
func main() {
3036
flag.Parse()
3137

3238
// Logger
33-
log := logger.New(version.Version{})
39+
log := logger.New(logger.NewOpts{
40+
AppInfo: version.Version{},
41+
Debug: logDebug,
42+
JSON: logJSON,
43+
})
3444

35-
// Config
36-
cfg, err := config.LoadServerConfig(configPath)
37-
if err != nil {
38-
log.Error().Msg("Failed loading configuration")
39-
os.Exit(1)
45+
// Main server
46+
s := server.New(server.NewOpts{
47+
Port: int(serverPort),
48+
Logger: &log,
49+
ConfigPath: configPath,
50+
PersistentStateDir: persistentStateDir,
51+
})
52+
if err := s.Init(); err != nil {
53+
log.Panic().Err(err).Msg("Failed initializing the server")
4054
}
4155

42-
// Lease provider orchestrator (handling all repos merge queue leases)
43-
orchestrator := lease.NewProviderOrchestrator(cfg.Repositories)
56+
// Signal handling (SIGTERM) to be able to gracefully shut down the server (both fiber + other resources,
57+
// like the storage for ex).
58+
c := make(chan os.Signal, 1)
59+
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
4460

45-
// Fiber app
46-
app := fiber.New(fiber.Config{DisableStartupMessage: true})
47-
app.Use(logger.FiberMiddleware(log))
48-
server.RegisterRoutes(app, orchestrator)
61+
globShutdown := make(chan struct{}, 1)
4962

50-
log.Info().Msg("Bootstrap completed, starting...")
63+
go func() {
64+
<-c
65+
log.Warn().Msg("SIGTERM received")
66+
if err := s.Shutdown(); err != nil {
67+
log.Error().Err(err).Msg("Could not shutdown server gracefully")
68+
}
69+
globShutdown <- struct{}{}
70+
}()
5171

52-
if err := app.Listen(":" + strconv.Itoa(int(serverPort))); err != nil {
53-
log.Err(err).Msg("Fiber server failed")
54-
defer os.Exit(1)
72+
if err := s.Start(); err != nil {
73+
log.Panic().Err(err).Msg("Server error")
5574
}
75+
76+
<-globShutdown
5677
}

0 commit comments

Comments
 (0)