Skip to content

Commit 725fc47

Browse files
authored
Merge pull request #4 from randlabs/develop
Options rearrangement and fixed process registration at startup
2 parents 45b4ab1 + 81a88e0 commit 725fc47

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

logger.go

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ var consoleLogger *IConsoleLogger
3131

3232
// Options ...
3333
type Options struct {
34+
AppName string
35+
BaseFolder string
3436
FileOpts *FileOptions `json:"file,omitempty"`
3537
ServerWatchdog *swcgo.ClientOptions `json:"serverWatchdog,omitempty"`
3638
ConsoleLogger *IConsoleLogger
@@ -63,13 +65,13 @@ func init() {
6365
//------------------------------------------------------------------------------
6466

6567
// Initialize...
66-
func Initialize(_appName string, options Options, baseFolder string) error {
68+
func Initialize(options Options) error {
6769
var err error
6870

69-
if len(_appName) == 0 {
71+
if len(options.AppName) == 0 {
7072
return errors.New("invalid application name")
7173
}
72-
appName = _appName
74+
appName = options.AppName
7375

7476
if options.FileOpts != nil {
7577
fileOptions = &FileOptions{
@@ -84,7 +86,7 @@ func Initialize(_appName string, options Options, baseFolder string) error {
8486
}
8587

8688
if !filepath.IsAbs(fileOptions.Folder) {
87-
fileOptions.Folder = filepath.Join(baseFolder, fileOptions.Folder)
89+
fileOptions.Folder = filepath.Join(options.BaseFolder, fileOptions.Folder)
8890
}
8991
fileOptions.Folder = filepath.Clean(fileOptions.Folder)
9092
if !strings.HasSuffix(fileOptions.Folder, string(filepath.Separator)) {
@@ -103,34 +105,20 @@ func Initialize(_appName string, options Options, baseFolder string) error {
103105
return err
104106
}
105107

106-
go func(swc *swcgo.ServerWatcherClient) {
107-
lastRegistrationSucceeded := true
108-
loop := true
109-
var err error
108+
go func() {
109+
lastRegistrationSucceeded := registerAppInServerWatchdog(true)
110110

111+
loop := true
111112
for loop {
112113
select {
113114
case <-shutdownProcessRegister:
114115
loop = false
115116

116117
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)
131119
}
132120
}
133-
}(swc)
121+
}()
134122
}
135123

136124
cleanOldFiles()
@@ -277,6 +265,21 @@ func writeLog(title string, msg string, theme *color.Theme, now time.Time) error
277265
return err
278266
}
279267

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+
280283
func cleanOldFiles() {
281284
if fileOptions == nil || fileOptions.DaysToKeep == 0 {
282285
return

0 commit comments

Comments
 (0)