-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaemon_darwin.go
45 lines (37 loc) · 983 Bytes
/
daemon_darwin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package cyberdaemon
import (
"log"
"os"
"os/signal"
"syscall"
)
type darwinDaemonizer struct {
logConfig LogConfig
}
func (o *darwinDaemonizer) RunUntilExit(application Application) error {
// The 'PS1' environment variable will be empty / not set when
// this is run non-interactively. Only do native log things
// when running non-interactively.
if o.logConfig.UseNativeLogger && len(os.Getenv("PS1")) == 0 {
log.SetOutput(os.Stderr)
if o.logConfig.NativeLogFlags > 0 {
originalLogFlags := log.Flags()
log.SetFlags(o.logConfig.NativeLogFlags)
defer log.SetFlags(originalLogFlags)
}
}
err := application.Start()
if err != nil {
return err
}
interruptsAndTerms := make(chan os.Signal)
signal.Notify(interruptsAndTerms, os.Interrupt, syscall.SIGTERM)
<-interruptsAndTerms
signal.Stop(interruptsAndTerms)
return application.Stop()
}
func NewDaemonizer(logConfig LogConfig) Daemonizer {
return &darwinDaemonizer{
logConfig: logConfig,
}
}