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