@@ -31,6 +31,8 @@ var consoleLogger *IConsoleLogger
31
31
32
32
// Options ...
33
33
type Options struct {
34
+ AppName string
35
+ BaseFolder string
34
36
FileOpts * FileOptions `json:"file,omitempty"`
35
37
ServerWatchdog * swcgo.ClientOptions `json:"serverWatchdog,omitempty"`
36
38
ConsoleLogger * IConsoleLogger
@@ -63,13 +65,13 @@ func init() {
63
65
//------------------------------------------------------------------------------
64
66
65
67
// Initialize...
66
- func Initialize (_appName string , options Options , baseFolder string ) error {
68
+ func Initialize (options Options ) error {
67
69
var err error
68
70
69
- if len (_appName ) == 0 {
71
+ if len (options . AppName ) == 0 {
70
72
return errors .New ("invalid application name" )
71
73
}
72
- appName = _appName
74
+ appName = options . AppName
73
75
74
76
if options .FileOpts != nil {
75
77
fileOptions = & FileOptions {
@@ -84,7 +86,7 @@ func Initialize(_appName string, options Options, baseFolder string) error {
84
86
}
85
87
86
88
if ! filepath .IsAbs (fileOptions .Folder ) {
87
- fileOptions .Folder = filepath .Join (baseFolder , fileOptions .Folder )
89
+ fileOptions .Folder = filepath .Join (options . BaseFolder , fileOptions .Folder )
88
90
}
89
91
fileOptions .Folder = filepath .Clean (fileOptions .Folder )
90
92
if ! strings .HasSuffix (fileOptions .Folder , string (filepath .Separator )) {
@@ -103,34 +105,20 @@ func Initialize(_appName string, options Options, baseFolder string) error {
103
105
return err
104
106
}
105
107
106
- go func (swc * swcgo.ServerWatcherClient ) {
107
- lastRegistrationSucceeded := true
108
- loop := true
109
- var err error
108
+ go func () {
109
+ lastRegistrationSucceeded := registerAppInServerWatchdog (true )
110
110
111
+ loop := true
111
112
for loop {
112
113
select {
113
114
case <- shutdownProcessRegister :
114
115
loop = false
115
116
116
117
case <- time .After (1 * time .Minute ):
117
- err = swc .ProcessWatch (os .Getpid (), appName , "error" , "" )
118
- if err == nil {
119
- lastRegistrationSucceeded = true
120
- } else {
121
- if lastRegistrationSucceeded {
122
- lastRegistrationSucceeded = false
123
-
124
- now := getCurrentTime ()
125
- err := writeLog ("[WARN]" , "Unable to register process in ServerWatchdog" , color .Warn , now )
126
- if err != nil {
127
- printError (now , "Unable to save notification in file" , err )
128
- }
129
- }
130
- }
118
+ lastRegistrationSucceeded = registerAppInServerWatchdog (lastRegistrationSucceeded )
131
119
}
132
120
}
133
- }(swc )
121
+ }()
134
122
}
135
123
136
124
cleanOldFiles ()
@@ -277,6 +265,21 @@ func writeLog(title string, msg string, theme *color.Theme, now time.Time) error
277
265
return err
278
266
}
279
267
268
+ func registerAppInServerWatchdog (logError bool ) bool {
269
+ err := swc .ProcessWatch (os .Getpid (), appName , "error" , "" )
270
+ if err == nil {
271
+ return true
272
+ }
273
+ if logError {
274
+ now := getCurrentTime ()
275
+ err := writeLog ("[WARN]" , "Unable to register process in ServerWatchdog" , color .Warn , now )
276
+ if err != nil {
277
+ printError (now , "Unable to save notification in file" , err )
278
+ }
279
+ }
280
+ return false
281
+ }
282
+
280
283
func cleanOldFiles () {
281
284
if fileOptions == nil || fileOptions .DaysToKeep == 0 {
282
285
return
0 commit comments