refactor: daemon, config, and logging for better clarity (#225)

- Import "path", "runtime", "strconv", and "strings" packages in daemon.go
- Move "Starting runner daemon" log message to a different location
- Refactor log formatter initialization and add debug level caller information
- Split Config struct into separate Log, Runner, Cache, and Container structs with comments in config.go

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>

Reviewed-on: https://gitea.com/gitea/act_runner/pulls/225
Reviewed-by: Jason Song <i@wolfogre.com>
Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
Co-committed-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2023-06-05 13:11:23 +00:00 committed by Lunny Xiao
parent 01ef57c667
commit 69c55ee003
2 changed files with 63 additions and 30 deletions

View File

@ -7,6 +7,10 @@ import (
"context" "context"
"fmt" "fmt"
"os" "os"
"path"
"runtime"
"strconv"
"strings"
"github.com/mattn/go-isatty" "github.com/mattn/go-isatty"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -23,14 +27,13 @@ import (
func runDaemon(ctx context.Context, configFile *string) func(cmd *cobra.Command, args []string) error { func runDaemon(ctx context.Context, configFile *string) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error { return func(cmd *cobra.Command, args []string) error {
log.Infoln("Starting runner daemon")
cfg, err := config.LoadDefault(*configFile) cfg, err := config.LoadDefault(*configFile)
if err != nil { if err != nil {
return fmt.Errorf("invalid configuration: %w", err) return fmt.Errorf("invalid configuration: %w", err)
} }
initLogging(cfg) initLogging(cfg)
log.Infoln("Starting runner daemon")
reg, err := config.LoadRegistration(cfg.Runner.File) reg, err := config.LoadRegistration(cfg.Runner.File)
if os.IsNotExist(err) { if os.IsNotExist(err) {
@ -79,10 +82,11 @@ func runDaemon(ctx context.Context, configFile *string) func(cmd *cobra.Command,
// initLogging setup the global logrus logger. // initLogging setup the global logrus logger.
func initLogging(cfg *config.Config) { func initLogging(cfg *config.Config) {
isTerm := isatty.IsTerminal(os.Stdout.Fd()) isTerm := isatty.IsTerminal(os.Stdout.Fd())
log.SetFormatter(&log.TextFormatter{ format := &log.TextFormatter{
DisableColors: !isTerm, DisableColors: !isTerm,
FullTimestamp: true, FullTimestamp: true,
}) }
log.SetFormatter(format)
if l := cfg.Log.Level; l != "" { if l := cfg.Log.Level; l != "" {
level, err := log.ParseLevel(l) level, err := log.ParseLevel(l)
@ -90,6 +94,22 @@ func initLogging(cfg *config.Config) {
log.WithError(err). log.WithError(err).
Errorf("invalid log level: %q", l) Errorf("invalid log level: %q", l)
} }
// debug level
if level == log.DebugLevel {
log.SetReportCaller(true)
format.CallerPrettyfier = func(f *runtime.Frame) (string, string) {
// get function name
s := strings.Split(f.Function, ".")
funcname := "[" + s[len(s)-1] + "]"
// get file name and line number
_, filename := path.Split(f.File)
filename = "[" + filename + ":" + strconv.Itoa(f.Line) + "]"
return funcname, filename
}
log.SetFormatter(format)
}
if log.GetLevel() != level { if log.GetLevel() != level {
log.Infof("log level changed to %v", level) log.Infof("log level changed to %v", level)
log.SetLevel(level) log.SetLevel(level)

View File

@ -14,33 +14,46 @@ import (
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
type Config struct { // Log represents the configuration for logging.
Log struct { type Log struct {
Level string `yaml:"level"` Level string `yaml:"level"` // Level indicates the logging level.
} `yaml:"log"` }
Runner struct {
File string `yaml:"file"` // Runner represents the configuration for the runner.
Capacity int `yaml:"capacity"` type Runner struct {
Envs map[string]string `yaml:"envs"` File string `yaml:"file"` // File specifies the file path for the runner.
EnvFile string `yaml:"env_file"` Capacity int `yaml:"capacity"` // Capacity specifies the capacity of the runner.
Timeout time.Duration `yaml:"timeout"` Envs map[string]string `yaml:"envs"` // Envs stores environment variables for the runner.
Insecure bool `yaml:"insecure"` EnvFile string `yaml:"env_file"` // EnvFile specifies the path to the file containing environment variables for the runner.
FetchTimeout time.Duration `yaml:"fetch_timeout"` Timeout time.Duration `yaml:"timeout"` // Timeout specifies the duration for runner timeout.
FetchInterval time.Duration `yaml:"fetch_interval"` Insecure bool `yaml:"insecure"` // Insecure indicates whether the runner operates in an insecure mode.
} `yaml:"runner"` FetchTimeout time.Duration `yaml:"fetch_timeout"` // FetchTimeout specifies the timeout duration for fetching resources.
Cache struct { FetchInterval time.Duration `yaml:"fetch_interval"` // FetchInterval specifies the interval duration for fetching resources.
Enabled *bool `yaml:"enabled"` // pointer to distinguish between false and not set, and it will be true if not set }
Dir string `yaml:"dir"`
Host string `yaml:"host"` // Cache represents the configuration for caching.
Port uint16 `yaml:"port"` type Cache struct {
} `yaml:"cache"` Enabled *bool `yaml:"enabled"` // Enabled indicates whether caching is enabled. It is a pointer to distinguish between false and not set. If not set, it will be true.
Container struct { Dir string `yaml:"dir"` // Dir specifies the directory path for caching.
Network string `yaml:"network"` Host string `yaml:"host"` // Host specifies the caching host.
Port uint16 `yaml:"port"` // Port specifies the caching port.
}
// Container represents the configuration for the container.
type Container struct {
Network string `yaml:"network"` // Network specifies the network for the container.
NetworkMode string `yaml:"network_mode"` // Deprecated: use Network instead. Could be removed after Gitea 1.20 NetworkMode string `yaml:"network_mode"` // Deprecated: use Network instead. Could be removed after Gitea 1.20
Privileged bool `yaml:"privileged"` Privileged bool `yaml:"privileged"` // Privileged indicates whether the container runs in privileged mode.
Options string `yaml:"options"` Options string `yaml:"options"` // Options specifies additional options for the container.
WorkdirParent string `yaml:"workdir_parent"` WorkdirParent string `yaml:"workdir_parent"` // WorkdirParent specifies the parent directory for the container's working directory.
} `yaml:"container"` }
// Config represents the overall configuration.
type Config struct {
Log Log `yaml:"log"` // Log represents the configuration for logging.
Runner Runner `yaml:"runner"` // Runner represents the configuration for the runner.
Cache Cache `yaml:"cache"` // Cache represents the configuration for caching.
Container Container `yaml:"container"` // Container represents the configuration for the container.
} }
// LoadDefault returns the default configuration. // LoadDefault returns the default configuration.