Skip to content

Commit 1090ff0

Browse files
committed
more refactor Start() function for better use outside package
1 parent 459eb09 commit 1090ff0

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

actions.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,9 @@ func stop(process *os.Process) {
7878
// for using in daemon default actions.
7979
func start() {
8080
fmt.Printf("Starting %s...", AppName)
81-
switch started, err := Start(1); {
82-
case err != nil:
81+
if err := Start(1); err != nil {
8382
failed(err)
84-
case !started:
85-
failed(fmt.Errorf("%s stopped and not runing", AppName))
86-
default:
83+
} else {
8784
fmt.Println("OK")
8885
}
8986
}

daemoni.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -172,35 +172,37 @@ func Status() (isRunning bool, pr *os.Process, e error) {
172172
// If daemonized process keeps running after timeout seconds passed
173173
// then process seems to be successfully started.
174174
//
175-
// Note, that this function can return success=false and e=nil.
176-
// That's a case when application successfully started,
177-
// but stops before timeout seconds passed.
178-
//
179175
// This function can also be used when writing your own daemon actions.
180-
func Start(timeout uint8) (success bool, e error) {
181-
path, e := filepath.Abs(AppPath)
182-
if e != nil {
176+
func Start(timeout uint8) (e error) {
177+
const errLoc = "daemonigo.Start()"
178+
path, err := filepath.Abs(AppPath)
179+
if err != nil {
180+
e = fmt.Errorf("%s: failed to resolve absolute path of %s, reason -> %s", errLoc, AppName, err.Error())
183181
return
184182
}
185183
cmd := exec.Command(path)
186184
cmd.Env = append(
187185
os.Environ(),
188186
fmt.Sprintf("%s=%s", EnvVarName, EnvVarValue),
189187
)
190-
if e = cmd.Start(); e != nil {
188+
if err = cmd.Start(); err != nil {
189+
e = fmt.Errorf("%s: failed to start %s, reason -> %s", errLoc, AppName, err.Error())
191190
return
192191
}
193192
select {
194193
case <-func() chan bool {
195194
ch := make(chan bool)
196195
go func() {
197-
e = cmd.Wait()
196+
if err := cmd.Wait(); err!= nil {
197+
e = fmt.Errorf("%s: %s running failed, reason -> %s", errLoc, AppName, err.Error())
198+
} else {
199+
e = fmt.Errorf("%s: %s stopped and not running", errLoc, AppName)
200+
}
198201
ch <- true
199202
}()
200203
return ch
201204
}():
202205
case <-time.After(time.Duration(timeout) * time.Second):
203-
success = true
204206
}
205207
return
206208
}

0 commit comments

Comments
 (0)