Skip to content

Commit

Permalink
daemon-log add time
Browse files Browse the repository at this point in the history
  • Loading branch information
whiteCcinn committed Jul 9, 2021
1 parent 42352cb commit a3ca791
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 30 deletions.
93 changes: 69 additions & 24 deletions daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,20 @@ func attachContext(dctx *Context) (isChild bool) {

// Background Converts the current process to a background process
// If `WithNoExit()` is called, it doesn't exit
func Background(ctx context.Context, dctx *Context, opts ...Option) (*exec.Cmd, error) {
func Background(ctx context.Context, dctx *Context, opts ...Option) (isParent bool, cmd *exec.Cmd, err error) {
for _, o := range opts {
o.apply(defaultOption)
}

if attachContext(dctx) {
return nil, nil
return
}

// starting child process
cmd, err := startProc(ctx, dctx)
cmd, err = startProc(ctx, dctx)
if err != nil {
dctx.log("[start exec process failed, err: %s]\n", dctx.Pid, err)
return nil, err
return
} else {
dctx.CPid = cmd.Process.Pid
if !defaultOption.exit {
Expand All @@ -148,13 +148,13 @@ func Background(ctx context.Context, dctx *Context, opts ...Option) (*exec.Cmd,
}

if defaultOption.exit {
os.Exit(0)
isParent = true
}

return cmd, nil
return
}

// startProc start an process
// startProc start am process
func startProc(ctx context.Context, dctx *Context) (*exec.Cmd, error) {
// console show process information just use Args[0]
// so we should wrapper the args for show process full command
Expand All @@ -166,8 +166,8 @@ func startProc(ctx context.Context, dctx *Context) (*exec.Cmd, error) {
}
}
cmd := &exec.Cmd{
Path: execs[0],
Args: append([]string{strings.Join(args, " ")}, dctx.Args[1:]...),
Path: execs[0],
Args: append([]string{strings.Join(args, " ")}, dctx.Args[1:]...),
Env: dctx.Env,
SysProcAttr: &dctx.ProcAttr,
}
Expand Down Expand Up @@ -200,12 +200,22 @@ func startProc(ctx context.Context, dctx *Context) (*exec.Cmd, error) {
}

// Run An supervisor daemon
func (dctx *Context) Run(ctx context.Context) error {
_, err := Background(ctx, dctx)
func (dctx *Context) Run(ctx context.Context) (isParent bool, err error) {
if running, pid := dctx.ProcessRunning(); running {
err = processAlreadyExists{Pid: pid}

return
}

isParent, _, err = Background(ctx, dctx)
if err != nil {
log.Fatal(err)
}

if isParent {
return
}

dctx.Count = 1
isReset := false
dctx.ErrNum = 0
Expand Down Expand Up @@ -251,7 +261,7 @@ func (dctx *Context) Run(ctx context.Context) error {
dctx.ExtraFiles = extraFile

begin := time.Now()
cmd, err := Background(ctx, dctx, WithNoExit())
_, cmd, err := Background(ctx, dctx, WithNoExit())
if err != nil {
dctx.log("[supervisor(%d)] [child process start failed, err: %s]\n", dctx.Pid, err)
dctx.ErrNum++
Expand Down Expand Up @@ -399,7 +409,7 @@ func (dctx *Context) Run(ctx context.Context) error {
}
}

return nil
return
}

func (dctx *Context) cleanNamedPipeEtc() *Context {
Expand Down Expand Up @@ -442,6 +452,7 @@ func (dctx *Context) cleanAll() {

// output log-message to Context.Logger
func (dctx *Context) log(format string, args ...interface{}) {
format = fmt.Sprintf("[%s] %s", time.Now().String(), format)
_, fe := fmt.Fprintf(dctx.Logger, format, args...)
if fe != nil {
log.Fatal(fe)
Expand All @@ -450,6 +461,7 @@ func (dctx *Context) log(format string, args ...interface{}) {

// output log-message to Context.PanicLogger
func (dctx *Context) logPanic(format string, args ...interface{}) {
format = fmt.Sprintf("[%s] %s", time.Now().String(), format)
_, fe := fmt.Fprintf(dctx.PanicLogger, format, args...)
if fe != nil {
log.Fatal(fe)
Expand Down Expand Up @@ -519,29 +531,62 @@ func (dctx *Context) syncCPidIntoFile() (err error) {
return
}

func (dctx *Context) ProcessRunning() (running bool, pid int) {
if id, _ := strconv.Atoi(os.Getenv(EnvName)); id > 0 {
return
}

_, running, pid = ExistByPidFile(dctx.CPidFile)

return
}

// ExistByPidFile exists by pid file read pid
func ExistByPidFile(pidFile string) (err error, exist bool) {
file, err := os.OpenFile(pidFile, os.O_RDONLY, 0644)
func ExistByPidFile(pidFile string) (err error, exist bool, pid int) {
var file *os.File

file, err = os.OpenFile(pidFile, os.O_RDONLY, 0644)
defer file.Close()
if err != nil {
return err, false
return
}
pidStr, err := ioutil.ReadAll(file)

var pidStr []byte
pidStr, err = ioutil.ReadAll(file)
if err != nil {
return err, false
return
}
pid, err := strconv.Atoi(string(pidStr))

pid, err = strconv.Atoi(string(pidStr))
if err != nil {
return err, false
return
}
p, err := os.FindProcess(pid)

var p *os.Process
p, err = os.FindProcess(pid)
if err != nil {
return err, false
return
}

err = p.Signal(syscall.Signal(0))
if err != nil {
return err, false
return
} else {
return nil, true
exist = true

return
}
}

type processAlreadyExists struct {
Pid int
}

func (p processAlreadyExists) Error() string {
return fmt.Sprintf("a running process already exists on pid=%d", p.Pid)
}

func (p processAlreadyExists) String() string {
return p.Error()
}

4 changes: 3 additions & 1 deletion example/backgroud.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ func main() {
logName := "daemon.log"
stdout, err := os.OpenFile(logName, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)

_, err = daemon.Background(context.Background(), &daemon.Context{
isParent, _, err := daemon.Background(context.Background(), &daemon.Context{
ProcAttr: syscall.SysProcAttr{},
//Logger: os.Stdout,
Logger: stdout,
})

if err != nil {
log.Fatal(err)
}

log.Println("isParent: ", isParent)
log.Println(os.Getpid(), "start...")
time.Sleep(time.Second * 10)
log.Println(os.Getpid(), "end")
Expand Down
8 changes: 5 additions & 3 deletions example/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ func main() {
ctx := context.Background()
dctx := daemon.Context{
//Chroot: "./pipe",
//PidFile: "./daemon.pid",
//CPidFile: "./main.pid",
ProcAttr: syscall.SysProcAttr{},
//Logger: os.Stdout,
Logger: stdout,
Expand All @@ -30,11 +28,15 @@ func main() {
},
}

err = dctx.Run(ctx)
parent, err := dctx.Run(ctx)
if err != nil {
log.Fatal(err)
}

if parent {
return
}

// belong func main()
log.Println(os.Getpid(), "start...")
time.Sleep(time.Second * 10)
Expand Down
6 changes: 5 additions & 1 deletion example/daemon_recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ func main() {
},
}

err = dctx.Run(ctx)
parent, err := dctx.Run(ctx)
if err != nil {
log.Fatal(err)
}

if parent {
return
}

// belong func main()
dctx.WithRecovery(func() {
log.Println(os.Getpid(), "start...")
Expand Down
6 changes: 5 additions & 1 deletion example/daemon_signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ func main() {
},
}

err = dctx.Run(ctx)
parent, err := dctx.Run(ctx)
if err != nil {
log.Fatal(err)
}

if parent {
return
}

// belong func main()
dctx.WithRecovery(func() {
wg := new(sync.WaitGroup)
Expand Down

0 comments on commit a3ca791

Please sign in to comment.