diff --git a/cmd/server/flags.go b/cmd/server/flags.go index f4a94bc5a..3e8bbd8c2 100644 --- a/cmd/server/flags.go +++ b/cmd/server/flags.go @@ -27,14 +27,34 @@ import ( var flags = append([]cli.Flag{ &cli.BoolFlag{ - Sources: cli.EnvVars("WOODPECKER_LOG_XORM"), - Name: "log-xorm", - Usage: "enable xorm logging", + Sources: cli.EnvVars("WOODPECKER_DATABASE_LOG", "WOODPECKER_LOG_XORM"), + Name: "db-log", + Aliases: []string{"log-xorm"}, // TODO: remove in v4.0.0 + Usage: "enable logging in database engine (currently xorm)", }, &cli.BoolFlag{ - Sources: cli.EnvVars("WOODPECKER_LOG_XORM_SQL"), - Name: "log-xorm-sql", - Usage: "enable xorm sql command logging", + Sources: cli.EnvVars("WOODPECKER_DATABASE_LOG_SQL", "WOODPECKER_LOG_XORM_SQL"), + Name: "db-log-sql", + 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{ Sources: cli.EnvVars("WOODPECKER_HOST"), @@ -205,7 +225,8 @@ var flags = append([]cli.Flag{ }, &cli.StringFlag{ Sources: cli.EnvVars("WOODPECKER_DATABASE_DRIVER"), - Name: "driver", + Name: "db-driver", + Aliases: []string{"driver"}, // TODO: remove in v4.0.0 Usage: "database driver", Value: "sqlite3", }, @@ -213,9 +234,10 @@ var flags = append([]cli.Flag{ Sources: cli.NewValueSourceChain( cli.File(os.Getenv("WOODPECKER_DATABASE_DATASOURCE_FILE")), cli.EnvVar("WOODPECKER_DATABASE_DATASOURCE")), - Name: "datasource", - Usage: "database driver configuration string", - Value: datasourceDefaultValue(), + Name: "db-datasource", + Aliases: []string{"datasource"}, // TODO: remove in v4.0.0 + Usage: "database driver configuration string", + Value: datasourceDefaultValue(), }, &cli.StringFlag{ Sources: cli.NewValueSourceChain( diff --git a/cmd/server/setup.go b/cmd/server/setup.go index c408e4023..83a92f01f 100644 --- a/cmd/server/setup.go +++ b/cmd/server/setup.go @@ -51,11 +51,14 @@ const ( ) func setupStore(ctx context.Context, c *cli.Command) (store.Store, error) { - datasource := c.String("datasource") - driver := c.String("driver") + datasource := c.String("db-datasource") + driver := c.String("db-driver") xorm := store.XORM{ - Log: c.Bool("log-xorm"), - ShowSQL: c.Bool("log-xorm-sql"), + Log: c.Bool("db-log"), + 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" { diff --git a/docs/docs/30-administration/10-server-config.md b/docs/docs/30-administration/10-server-config.md index 082e3d3b8..17da7abd2 100644 --- a/docs/docs/30-administration/10-server-config.md +++ b/docs/docs/30-administration/10-server-config.md @@ -159,17 +159,35 @@ Configures the logging level. Possible values are `trace`, `debug`, `info`, `war Output destination for logs. 'stdout' and 'stderr' can be used as special keywords. -### `WOODPECKER_LOG_XORM` +### `WOODPECKER_DATABASE_LOG` > Default: `false` -Enable XORM logs. +Enable logging in database engine (currently xorm). -### `WOODPECKER_LOG_XORM_SQL` +### `WOODPECKER_DATABASE_LOG_SQL` > 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` diff --git a/docs/src/pages/migrations.md b/docs/src/pages/migrations.md index 2eec56a80..9b7639366 100644 --- a/docs/src/pages/migrations.md +++ b/docs/src/pages/migrations.md @@ -36,6 +36,7 @@ This will be the next version of Woodpecker. ## 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` - Move docker resource limit settings from server into agent configuration - Rename server environment variable `WOODPECKER_ESCALATE` to `WOODPECKER_PLUGINS_PRIVILEGED` diff --git a/server/store/common.go b/server/store/common.go index 4a051d838..025cf82cc 100644 --- a/server/store/common.go +++ b/server/store/common.go @@ -14,9 +14,14 @@ package store +import "time" + type XORM struct { - Log bool - ShowSQL bool + Log bool + ShowSQL bool + MaxIdleConns int + MaxOpenConns int + ConnMaxLifetime time.Duration } // Opts are options for a new database connection. diff --git a/server/store/datastore/engine.go b/server/store/datastore/engine.go index 8499681ac..316347591 100644 --- a/server/store/datastore/engine.go +++ b/server/store/datastore/engine.go @@ -45,6 +45,9 @@ func NewEngine(opts *store.Opts) (store.Store, error) { logger := newXORMLogger(level) engine.SetLogger(logger) engine.ShowSQL(opts.XORM.ShowSQL) + engine.SetMaxOpenConns(opts.XORM.MaxOpenConns) + engine.SetMaxIdleConns(opts.XORM.MaxIdleConns) + engine.SetConnMaxLifetime(opts.XORM.ConnMaxLifetime) return &storage{ engine: engine,