@@ -2,15 +2,12 @@ package main
2
2
3
3
import (
4
4
"context"
5
- "errors"
6
- "fmt"
7
5
"net/http"
8
6
"os/signal"
9
7
"syscall"
10
8
"time"
11
9
12
10
"github.com/GLCharge/otelzap"
13
- "github.com/ardanlabs/conf/v3"
14
11
"github.com/spf13/cobra"
15
12
"github.com/spf13/viper"
16
13
devxCfg "github.com/xBlaz3kx/DevX/configuration"
@@ -27,95 +24,83 @@ import (
27
24
"go.uber.org/zap"
28
25
)
29
26
30
- var build = "develop "
27
+ const serviceName = "runner "
31
28
32
29
var serviceInfo = observability.ServiceInfo {
33
- Name : "runner" ,
34
- Version : build ,
30
+ Name : serviceName ,
31
+ Version : "0.1.2" ,
35
32
}
36
33
34
+ var configFilePath string
35
+
37
36
type config struct {
38
- conf.Version
39
- Web struct {
40
- ReadTimeout time.Duration `conf:"default:5s"`
41
- WriteTimeout time.Duration `conf:"default:10s"`
42
- IdleTimeout time.Duration `conf:"default:120s"`
43
- ShutdownTimeout time.Duration `conf:"default:20s"`
44
- APIHost string `conf:"default:0.0.0.0:8000"`
45
- }
46
- DB database.Config
47
- ID string `conf:"default:instance1"`
48
- Interval time.Duration `conf:"default:10s"`
49
- MaxConcurrentJobs int `conf:"default:100"`
50
- MaxJobLockTime time.Duration `conf:"default:1m"`
37
+ Observability observability.Config `mapstructure:"observability" yaml:"observability" json:"observability"`
38
+ Http devxHttp.Configuration `mapstructure:"http" yaml:"http" json:"http"`
39
+ DB database.Config `mapstructure:"db" yaml:"db" json:"db"`
40
+ ID string `mapstructure:"id" yaml:"id" json:"id,omitempty"`
41
+ JobExecutionSettings runner.JobExecutionSettings `mapstructure:"jobExecutionSettings" yaml:"jobExecutionSettings" json:"jobExecutionSettings"`
51
42
}
52
43
53
44
var rootCmd = & cobra.Command {
54
45
Use : "runner" ,
55
46
Short : "Scheduler runner" ,
56
- PreRun : func (cmd * cobra.Command , args []string ) {
47
+ PersistentPreRun : func (cmd * cobra.Command , args []string ) {
48
+ devxCfg .SetDefaults (serviceName )
49
+ devxCfg .SetupEnv (serviceName )
50
+
57
51
viper .SetDefault ("storage.encryption.key" , "ishouldreallybechanged" )
58
- devxCfg .InitConfig ("" , "./config" , "." )
52
+ viper .SetDefault ("db.disableTls" , true )
53
+ viper .SetDefault ("db.maxOpenConns" , 1 )
54
+ viper .SetDefault ("db.maxIdleConns" , 10 )
55
+ viper .SetDefault ("observability.logging.level" , observability .LogLevelInfo )
56
+
57
+ viper .SetDefault ("jobExecutionSettings.maxConcurrentJobs" , 100 )
58
+ viper .SetDefault ("jobExecutionSettings.interval" , time .Second * 10 )
59
+ viper .SetDefault ("jobExecutionSettings.maxJobLockTime" , time .Minute )
60
+
61
+ devxCfg .InitConfig (configFilePath , "./config" , "." )
59
62
60
63
postgres .SetEncryptor (security .NewEncryptorFromEnv ())
61
64
},
62
65
Run : runCmd ,
63
66
}
64
67
68
+ func init () {
69
+ rootCmd .PersistentFlags ().StringVar (& configFilePath , "config" , "" , "config file (default is $HOME/.config/runner.yaml)" )
70
+ _ = viper .BindPFlag ("config" , rootCmd .PersistentFlags ().Lookup ("config" ))
71
+ }
72
+
65
73
func main () {
66
- cobra .OnInitialize (func () {
67
- logger .SetupLogging ()
68
- devxCfg .SetupEnv ("runner" )
69
- })
74
+ cobra .OnInitialize (logger .SetupLogging )
70
75
err := rootCmd .Execute ()
71
76
if err != nil {
72
77
panic (err )
73
78
}
74
79
}
75
80
76
81
func runCmd (cmd * cobra.Command , args []string ) {
77
- ctx , cancel := signal .NotifyContext (context .Background (), syscall .SIGINT , syscall .SIGTERM )
82
+ ctx , cancel := signal .NotifyContext (context .Background (), syscall .SIGINT , syscall .SIGTERM , syscall . SIGKILL )
78
83
defer cancel ()
79
84
80
- obsConfig := observability.Config {}
81
- obs , err := observability .NewObservability (ctx , serviceInfo , obsConfig )
85
+ // Read the configuration
86
+ cfg := & config {}
87
+ devxCfg .GetConfiguration (viper .GetViper (), cfg )
88
+
89
+ obs , err := observability .NewObservability (ctx , serviceInfo , cfg .Observability )
82
90
if err != nil {
83
91
otelzap .L ().Fatal ("failed to initialize observability" , zap .Error (err ))
84
92
}
85
93
86
94
log := obs .Log ()
87
95
88
- // config
89
- cfg := config {
90
- Version : conf.Version {
91
- Build : build ,
92
- Desc : "copyright information here" ,
93
- },
94
- }
95
-
96
- const prefix = "RUNNER"
97
- help , err := conf .Parse (prefix , & cfg )
98
- if err != nil {
99
- if errors .Is (err , conf .ErrHelpWanted ) {
100
- fmt .Println (help )
101
- return
102
- }
103
- return
104
- }
105
-
106
96
// App Starting
107
- log .Info ("starting service " , zap .String ("version" , build ))
97
+ log .Info ("Starting the runner " , zap .String ("version" , serviceInfo . Version ))
108
98
defer log .Info ("shutdown complete" )
109
99
110
- out , err := conf .String (& cfg )
111
- if err != nil {
112
- log .Fatal ("parsing config" , zap .Error (err ))
113
- }
114
-
115
- log .Info ("Using config" , zap .Any ("config" , out ))
100
+ log .Info ("Using config" , zap .Any ("config" , cfg ))
116
101
117
102
// Database
118
- log .Info ("startup" , zap . String ( "status" , "initializing database support" ) , zap .String ("host" , cfg .DB .Host ))
103
+ log .Info ("Connecting to the database" , zap .String ("host" , cfg .DB .Host ))
119
104
db , err := database .Open (database.Config {
120
105
User : cfg .DB .User ,
121
106
Password : cfg .DB .Password ,
@@ -128,8 +113,9 @@ func runCmd(cmd *cobra.Command, args []string) {
128
113
if err != nil {
129
114
log .Fatal ("Unable to establish DB connection" , zap .Error (err ))
130
115
}
116
+
131
117
defer func () {
132
- log .Info ("closing database connection" )
118
+ log .Info ("Closing the database connection" )
133
119
_ = db .Close ()
134
120
}()
135
121
@@ -143,34 +129,31 @@ func runCmd(cmd *cobra.Command, args []string) {
143
129
executorFactory := executor .NewFactory (& http.Client {Timeout : 30 * time .Second })
144
130
145
131
runner := runner .New (runner.Config {
146
- JobService : jobService ,
147
- Metrics : metrics .NewRunnerMetrics (obsConfig .Metrics ),
148
- Log : log ,
149
- ExecutorFactory : executorFactory ,
150
- InstanceId : cfg .ID ,
151
- Interval : cfg .Interval ,
152
- MaxConcurrentJobs : cfg .MaxConcurrentJobs ,
153
- JobLockDuration : cfg .MaxJobLockTime ,
132
+ JobService : jobService ,
133
+ Metrics : metrics .NewRunnerMetrics (cfg .Observability .Metrics ),
134
+ Log : log ,
135
+ ExecutorFactory : executorFactory ,
136
+ InstanceId : cfg .ID ,
137
+ JobExecution : cfg .JobExecutionSettings ,
154
138
})
155
139
runner .Start ()
156
140
157
- httpServer := devxHttp .NewServer (devxHttp. Configuration { Address : cfg .Web . APIHost } , obs )
141
+ httpServer := devxHttp .NewServer (cfg .Http , obs )
158
142
go func () {
159
- log .Info ("Started HTTP server" , zap .String ("host " , cfg .Web . APIHost ))
143
+ log .Info ("Started HTTP server" , zap .String ("address " , cfg .Http . Address ))
160
144
databaseCheck := database .NewHealthChecker (db )
161
145
httpServer .Run (databaseCheck )
162
146
}()
163
147
164
148
//nolint:all
165
149
select {
166
150
case _ = <- ctx .Done ():
167
- log .Info ("shutdown" , zap . String ( "status" , "shutdown started" ) )
151
+ log .Info ("Shutting down the runner" )
168
152
169
153
ctx , cancel := context .WithTimeout (context .Background (), 30 * time .Second )
170
154
defer cancel ()
171
155
172
156
// stop the runner
173
157
runner .Stop (ctx )
174
- log .Info ("shutdown" , zap .String ("status" , "shutdown complete" ))
175
158
}
176
159
}
0 commit comments