Skip to content

Commit

Permalink
minor: improved logging
Browse files Browse the repository at this point in the history
  • Loading branch information
FMotalleb committed Jun 8, 2024
1 parent 74275aa commit 42dadc1
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"env": {},
"args": [
"-c",
"config.example.yaml"
"config.local.yaml"
]
}
]
Expand Down
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@
],
"json.schemaDownload.enable": true,
"yaml.schemas": {
"https://raw.githubusercontent.com/FMotalleb/crontab-go/main/schema.json": [
"config.example.yaml",
"config.yaml",
"config.doc.yaml"
],
"./schema.json": [
"config.example.yaml",
"config.yaml"
Expand Down
3 changes: 1 addition & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func Execute() {
}

func init() {
warnOnErr(godotenv.Load(), "Cannot initialize .env file: %s")
cobra.OnInitialize(initConfig)

rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is config.yaml)")
Expand All @@ -56,8 +57,6 @@ func panicOnErr(err error, message string) {
}

func initConfig() {
warnOnErr(godotenv.Load(), "Cannot initialize .env file: %s")

viper.SetDefault("log_timestamp_format", "2006-01-02T15:04:05Z07:00")
warnOnErr(
viper.BindEnv(
Expand Down
75 changes: 75 additions & 0 deletions config.doc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/FMotalleb/crontab-go/main/schema.json
#TODO: unix/tcp socket controller
#TODO: prometheus exporter

jobs:
# Jobs can be assigned a unique name, which will be included in log messages for easier debugging.
- name: Test Job
# Description is reserved for the future
description: Sample of a single job
# Jobs can be disabled explicitly instead of commenting/removing the job
# disabled: true
tasks:
# This line specifies the actual command to be executed.
- command: echo $(whoami)
# This setting determines the number of times the task will be retried if it fails
# A failure is defined by either a non-zero exit code for commands or a status code of 400 or higher for HTTP requests.
retries: 3
# This specifies the delay between retries.
retry-delay: 1s
# This sets a maximum time limit for the task to complete.
# If the task exceeds 15 seconds, it will be considered failed and stopped (command) or canceled (http requests)
timeout: 15s
user: root
# # This defines the working directory for the command.
working-dir: /home/user
# This section allows you to set environment variables that will be available to the command during execution.
# `SHELL` and `SHELL_ARGS` can be used to change this commands shell environment
env:
SHELL: /usr/bin/bash
SHELL_ARGS: -c
# DB_HOST: 10.0.0.5

# # A simple get request
# - get: https://example.com/get
# # headers of request `map[string]string`
# headers:
# - "Accepts": "Application/Json"

# # A simple post request example
# - post: https://example.com/post
# # headers of request `map[string]string`
# headers:
# - "Accepts": "Application/Json"
# # Body of post request (can be a json object)
# data:
# key: value
schedulers:
- on-init: true
# Schedulers can be defined using either a cron expression or an interval, but not both simultaneously.
# However, you can combine multiple cron expressions and intervals within the same scheduler.

# The cron scheduler allows you to specify schedules down to the second level of precision using cron expressions.
- cron: "@yearly"
# Intervals can be defined using human-readable formats.
# For example, '10h' represents 10 hours, '10m' represents 10 minutes, and '10m15s' represents every 10 minutes and 15 seconds.
# You can use units of hours (h), minutes (m), seconds (s), milliseconds (ms), and nanoseconds (ns) to define your intervals.
- interval: 10m10s
hooks:
# Hooks are essentially tasks like those used in jobs, but they do not support nested hooks.
# Additionally, errors or completion status of hooks are not directly managed by the system.
# Instead, they are logged for informational purposes.

# Every command within a job triggers its own set of hooks.
# For instance, a job with two tasks and three 'done' hooks will execute a total of six hooks (if done)
# during a single run: three for each task.

# The 'done' hook will be triggered for HTTP requests with status codes below 400,
# indicating successful completion.
# For commands, the 'done' hook will be triggered if the exit code is 0,
# signifying successful execution.
# otherwise the `failed` hooks will be executed
done:
- command: echo Ok
failed:
- command: echo Failed
2 changes: 1 addition & 1 deletion config.example.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# yaml-language-server: $schema=schema.json
# yaml-language-server: $schema=https://raw.githubusercontent.com/FMotalleb/crontab-go/main/schema.json
#TODO: unix/tcp socket controller
#TODO: prometheus exporter

Expand Down
14 changes: 14 additions & 0 deletions config.local.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# yaml-language-server: $schema=schema.json
#TODO: unix/tcp socket controller
#TODO: prometheus exporter

jobs:
- name: Test Job
tasks:
- command: ip a
env:
SHELL: C:\WINDOWS\System32\OpenSSH\ssh.exe
SHELL_ARGS: "-i;C:/Users/Motalleb/.xpipe/storage/data/id_rsa_old;[email protected];-p;9011"
schedulers:
- on-init: true
- interval: 10m10s
11 changes: 4 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import (
)

type (
EnvVariables = map[string]string
JobMetadata = map[string]interface{}

Config struct {
LogTimestampFormat string `mapstructure:"log_timestamp_format" json:"log_timestamp_format"`
LogFormat enums.LoggerFormatType `mapstructure:"log_format" json:"log_format"`
Expand Down Expand Up @@ -53,10 +50,10 @@ type (
UserName string `mapstructure:"user" json:"user,omitempty"`
GroupName string `mapstructure:"group" json:"group,omitempty"`

Retries uint `mapstructure:"retries" json:"retries,omitempty"`
RetryDelay time.Duration `mapstructure:"retry-delay" json:"retry_delay,omitempty"`
Timeout time.Duration `mapstructure:"timeout" json:"timeout,omitempty"`
Env EnvVariables `mapstructure:"env" json:"env,omitempty"`
Retries uint `mapstructure:"retries" json:"retries,omitempty"`
RetryDelay time.Duration `mapstructure:"retry-delay" json:"retry_delay,omitempty"`
Timeout time.Duration `mapstructure:"timeout" json:"timeout,omitempty"`
Env map[string]string `mapstructure:"env" json:"env,omitempty"`

OnDone []Task `mapstructure:"on-done" json:"on_done,omitempty"`
OnFail []Task `mapstructure:"on-fail" json:"on_fail,omitempty"`
Expand Down
3 changes: 1 addition & 2 deletions core/os_credential/unix_credential.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !windows
// +build !windows
//go:build unix

package credential

Expand Down
14 changes: 11 additions & 3 deletions core/task/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,13 @@ func (c *Command) Execute(ctx context.Context) (e error) {
proc.Stderr = &res
proc.Start()

Check failure on line 93 in core/task/command.go

View workflow job for this annotation

GitHub Actions / analyze (go)

Error return value of `proc.Start` is not checked (errcheck)
e = proc.Wait()
log.Infof("command finished with answer: `%s`", strings.TrimSpace(string(res.Bytes())))

if e != nil {
log.Warnf("command failed with answer: %s", strings.TrimSpace(string(res.Bytes())))

Check failure on line 97 in core/task/command.go

View workflow job for this annotation

GitHub Actions / analyze (go)

S1030: should use res.String() instead of string(res.Bytes()) (gosimple)
log.Warn("failed to execute the command ", e)
return c.Execute(ctx)
} else {
log.Infof("command finished with answer: %s", strings.TrimSpace(string(res.Bytes())))

Check failure on line 101 in core/task/command.go

View workflow job for this annotation

GitHub Actions / analyze (go)

S1030: should use res.String() instead of string(res.Bytes()) (gosimple)
}

runTasks(c.doneHooks)
Expand Down Expand Up @@ -132,8 +135,13 @@ func NewCommand(
exe: task.Command,
envVars: &env,
workingDirectory: wd,
log: log.WithField(
"working_directory", wd,
log: log.WithFields(
logrus.Fields{
"working_directory": wd,
"shell": shell,
"shell_args": shellArgs,
"command": task.Command,
},
),
shell: shell,
shellArgs: shellArgs,
Expand Down

0 comments on commit 42dadc1

Please sign in to comment.