Add options to controll the database connections (open,idle,timeout) (#4212)

This commit is contained in:
6543 2024-11-05 15:03:22 +01:00 committed by GitHub
parent 4270eca597
commit 6e38a950cd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 72 additions and 20 deletions

View file

@ -27,14 +27,34 @@ import (
var flags = append([]cli.Flag{ var flags = append([]cli.Flag{
&cli.BoolFlag{ &cli.BoolFlag{
Sources: cli.EnvVars("WOODPECKER_LOG_XORM"), Sources: cli.EnvVars("WOODPECKER_DATABASE_LOG", "WOODPECKER_LOG_XORM"),
Name: "log-xorm", Name: "db-log",
Usage: "enable xorm logging", Aliases: []string{"log-xorm"}, // TODO: remove in v4.0.0
Usage: "enable logging in database engine (currently xorm)",
}, },
&cli.BoolFlag{ &cli.BoolFlag{
Sources: cli.EnvVars("WOODPECKER_LOG_XORM_SQL"), Sources: cli.EnvVars("WOODPECKER_DATABASE_LOG_SQL", "WOODPECKER_LOG_XORM_SQL"),
Name: "log-xorm-sql", Name: "db-log-sql",
Usage: "enable xorm sql command logging", Aliases: []string{"log-xorm-sql"}, // TODO: remove in v4.0.0
Usage: "enable logging of sql commands",
},
&cli.IntFlag{
Sources: cli.EnvVars("WOODPECKER_DATABASE_MAX_CONNECTIONS"),
Name: "db-max-open-connections",
Usage: "max connections xorm is allowed create",
Value: 100,
},
&cli.IntFlag{
Sources: cli.EnvVars("WOODPECKER_DATABASE_IDLE_CONNECTIONS"),
Name: "db-max-idle-connections",
Usage: "amount of connections xorm will hold open",
Value: 2,
},
&cli.DurationFlag{
Sources: cli.EnvVars("WOODPECKER_DATABASE_CONNECTION_TIMEOUT"),
Name: "db-max-connection-timeout",
Usage: "time an active connection is allowed to stay open",
Value: 3 * time.Second,
}, },
&cli.StringFlag{ &cli.StringFlag{
Sources: cli.EnvVars("WOODPECKER_HOST"), Sources: cli.EnvVars("WOODPECKER_HOST"),
@ -205,7 +225,8 @@ var flags = append([]cli.Flag{
}, },
&cli.StringFlag{ &cli.StringFlag{
Sources: cli.EnvVars("WOODPECKER_DATABASE_DRIVER"), Sources: cli.EnvVars("WOODPECKER_DATABASE_DRIVER"),
Name: "driver", Name: "db-driver",
Aliases: []string{"driver"}, // TODO: remove in v4.0.0
Usage: "database driver", Usage: "database driver",
Value: "sqlite3", Value: "sqlite3",
}, },
@ -213,7 +234,8 @@ var flags = append([]cli.Flag{
Sources: cli.NewValueSourceChain( Sources: cli.NewValueSourceChain(
cli.File(os.Getenv("WOODPECKER_DATABASE_DATASOURCE_FILE")), cli.File(os.Getenv("WOODPECKER_DATABASE_DATASOURCE_FILE")),
cli.EnvVar("WOODPECKER_DATABASE_DATASOURCE")), cli.EnvVar("WOODPECKER_DATABASE_DATASOURCE")),
Name: "datasource", Name: "db-datasource",
Aliases: []string{"datasource"}, // TODO: remove in v4.0.0
Usage: "database driver configuration string", Usage: "database driver configuration string",
Value: datasourceDefaultValue(), Value: datasourceDefaultValue(),
}, },

View file

@ -51,11 +51,14 @@ const (
) )
func setupStore(ctx context.Context, c *cli.Command) (store.Store, error) { func setupStore(ctx context.Context, c *cli.Command) (store.Store, error) {
datasource := c.String("datasource") datasource := c.String("db-datasource")
driver := c.String("driver") driver := c.String("db-driver")
xorm := store.XORM{ xorm := store.XORM{
Log: c.Bool("log-xorm"), Log: c.Bool("db-log"),
ShowSQL: c.Bool("log-xorm-sql"), ShowSQL: c.Bool("db-log-sql"),
MaxOpenConns: int(c.Int("db-max-open-connections")),
MaxIdleConns: int(c.Int("db-max-idle-connections")),
ConnMaxLifetime: c.Duration("db-max-connection-timeout"),
} }
if driver == "sqlite3" { if driver == "sqlite3" {

View file

@ -159,17 +159,35 @@ Configures the logging level. Possible values are `trace`, `debug`, `info`, `war
Output destination for logs. Output destination for logs.
'stdout' and 'stderr' can be used as special keywords. 'stdout' and 'stderr' can be used as special keywords.
### `WOODPECKER_LOG_XORM` ### `WOODPECKER_DATABASE_LOG`
> Default: `false` > Default: `false`
Enable XORM logs. Enable logging in database engine (currently xorm).
### `WOODPECKER_LOG_XORM_SQL` ### `WOODPECKER_DATABASE_LOG_SQL`
> Default: `false` > Default: `false`
Enable XORM SQL command logs. Enable logging of sql commands.
### `WOODPECKER_DATABASE_MAX_CONNECTIONS`
> Default: `100`
Max database connections xorm is allowed create.
### `WOODPECKER_DATABASE_IDLE_CONNECTIONS`
> Default: `2`
Amount of database connections xorm will hold open.
### `WOODPECKER_DATABASE_CONNECTION_TIMEOUT`
> Default: `3 Seconds`
Time an active database connection is allowed to stay open.
### `WOODPECKER_DEBUG_PRETTY` ### `WOODPECKER_DEBUG_PRETTY`

View file

@ -36,6 +36,7 @@ This will be the next version of Woodpecker.
## Admin migrations ## Admin migrations
- Deprecate `WOODPECKER_LOG_XORM` and `WOODPECKER_LOG_XORM_SQL` use `"WOODPECKER_DATABASE_LOG` and `"WOODPECKER_DATABASE_LOG_SQL`
- Deprecate `WOODPECKER_FILTER_LABELS` use `WOODPECKER_AGENT_LABELS` - Deprecate `WOODPECKER_FILTER_LABELS` use `WOODPECKER_AGENT_LABELS`
- Move docker resource limit settings from server into agent configuration - Move docker resource limit settings from server into agent configuration
- Rename server environment variable `WOODPECKER_ESCALATE` to `WOODPECKER_PLUGINS_PRIVILEGED` - Rename server environment variable `WOODPECKER_ESCALATE` to `WOODPECKER_PLUGINS_PRIVILEGED`

View file

@ -14,9 +14,14 @@
package store package store
import "time"
type XORM struct { type XORM struct {
Log bool Log bool
ShowSQL bool ShowSQL bool
MaxIdleConns int
MaxOpenConns int
ConnMaxLifetime time.Duration
} }
// Opts are options for a new database connection. // Opts are options for a new database connection.

View file

@ -45,6 +45,9 @@ func NewEngine(opts *store.Opts) (store.Store, error) {
logger := newXORMLogger(level) logger := newXORMLogger(level)
engine.SetLogger(logger) engine.SetLogger(logger)
engine.ShowSQL(opts.XORM.ShowSQL) engine.ShowSQL(opts.XORM.ShowSQL)
engine.SetMaxOpenConns(opts.XORM.MaxOpenConns)
engine.SetMaxIdleConns(opts.XORM.MaxIdleConns)
engine.SetConnMaxLifetime(opts.XORM.ConnMaxLifetime)
return &storage{ return &storage{
engine: engine, engine: engine,