Gracefully shutdown server (#3896)

This commit is contained in:
6543
2024-07-13 16:46:01 -07:00
committed by GitHub
parent 30cd800110
commit 757f5a58e2
13 changed files with 375 additions and 164 deletions

View File

@@ -26,11 +26,8 @@ import (
"time"
"github.com/gorilla/securecookie"
"github.com/prometheus/client_golang/prometheus"
prometheus_auto "github.com/prometheus/client_golang/prometheus/promauto"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
"golang.org/x/sync/errgroup"
"go.woodpecker-ci.org/woodpecker/v2/server"
"go.woodpecker-ci.org/woodpecker/v2/server/cache"
@@ -49,7 +46,12 @@ import (
"go.woodpecker-ci.org/woodpecker/v2/shared/constant"
)
func setupStore(c *cli.Context) (store.Store, error) {
const (
queueInfoRefreshInterval = 500 * time.Millisecond
storeInfoRefreshInterval = 10 * time.Second
)
func setupStore(ctx context.Context, c *cli.Context) (store.Store, error) {
datasource := c.String("datasource")
driver := c.String("driver")
xorm := store.XORM{
@@ -86,7 +88,7 @@ func setupStore(c *cli.Context) (store.Store, error) {
return nil, fmt.Errorf("could not open datastore: %w", err)
}
if err := store.Migrate(c.Bool("migrations-allow-long")); err != nil {
if err := store.Migrate(ctx, c.Bool("migrations-allow-long")); err != nil {
return nil, fmt.Errorf("could not migrate datastore: %w", err)
}
@@ -102,74 +104,14 @@ func checkSqliteFileExist(path string) error {
return err
}
func setupQueue(c *cli.Context, s store.Store) queue.Queue {
return queue.WithTaskStore(queue.New(c.Context), s)
func setupQueue(ctx context.Context, s store.Store) queue.Queue {
return queue.WithTaskStore(ctx, queue.New(ctx), s)
}
func setupMembershipService(_ *cli.Context, _store store.Store) cache.MembershipService {
func setupMembershipService(_ context.Context, _store store.Store) cache.MembershipService {
return cache.NewMembershipService(_store)
}
func setupMetrics(g *errgroup.Group, _store store.Store) {
pendingSteps := prometheus_auto.NewGauge(prometheus.GaugeOpts{
Namespace: "woodpecker",
Name: "pending_steps",
Help: "Total number of pending pipeline steps.",
})
waitingSteps := prometheus_auto.NewGauge(prometheus.GaugeOpts{
Namespace: "woodpecker",
Name: "waiting_steps",
Help: "Total number of pipeline waiting on deps.",
})
runningSteps := prometheus_auto.NewGauge(prometheus.GaugeOpts{
Namespace: "woodpecker",
Name: "running_steps",
Help: "Total number of running pipeline steps.",
})
workers := prometheus_auto.NewGauge(prometheus.GaugeOpts{
Namespace: "woodpecker",
Name: "worker_count",
Help: "Total number of workers.",
})
pipelines := prometheus_auto.NewGauge(prometheus.GaugeOpts{
Namespace: "woodpecker",
Name: "pipeline_total_count",
Help: "Total number of pipelines.",
})
users := prometheus_auto.NewGauge(prometheus.GaugeOpts{
Namespace: "woodpecker",
Name: "user_count",
Help: "Total number of users.",
})
repos := prometheus_auto.NewGauge(prometheus.GaugeOpts{
Namespace: "woodpecker",
Name: "repo_count",
Help: "Total number of repos.",
})
g.Go(func() error {
for {
stats := server.Config.Services.Queue.Info(context.TODO())
pendingSteps.Set(float64(stats.Stats.Pending))
waitingSteps.Set(float64(stats.Stats.WaitingOnDeps))
runningSteps.Set(float64(stats.Stats.Running))
workers.Set(float64(stats.Stats.Workers))
time.Sleep(500 * time.Millisecond)
}
})
g.Go(func() error {
for {
repoCount, _ := _store.GetRepoCount()
userCount, _ := _store.GetUserCount()
pipelineCount, _ := _store.GetPipelineCount()
pipelines.Set(float64(pipelineCount))
users.Set(float64(userCount))
repos.Set(float64(repoCount))
time.Sleep(10 * time.Second)
}
})
}
func setupLogStore(c *cli.Context, s store.Store) (logService.Service, error) {
switch c.String("log-store") {
case "file":
@@ -202,12 +144,12 @@ func setupJWTSecret(_store store.Store) (string, error) {
return jwtSecret, nil
}
func setupEvilGlobals(c *cli.Context, s store.Store) error {
func setupEvilGlobals(ctx context.Context, c *cli.Context, s store.Store) error {
// services
server.Config.Services.Queue = setupQueue(c, s)
server.Config.Services.Queue = setupQueue(ctx, s)
server.Config.Services.Logs = logging.New()
server.Config.Services.Pubsub = pubsub.New()
server.Config.Services.Membership = setupMembershipService(c, s)
server.Config.Services.Membership = setupMembershipService(ctx, s)
serviceManager, err := services.NewManager(c, s, setup.Forge)
if err != nil {
return fmt.Errorf("could not setup service manager: %w", err)