-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
86eb235
commit 8ebf041
Showing
7 changed files
with
334 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"github.com/whiteCcinn/daemon" | ||
"log" | ||
"os" | ||
"syscall" | ||
"time" | ||
) | ||
|
||
func main() { | ||
logName := "daemon.log" | ||
panicLogName := "panic-daemon.log" | ||
stdout, err := os.OpenFile(logName, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666) | ||
panicStdout, err := os.OpenFile(panicLogName, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
ctx := context.Background() | ||
dctx := daemon.Context{ | ||
ProcAttr: syscall.SysProcAttr{}, | ||
//Logger: os.Stdout, | ||
Logger: stdout, | ||
PanicLogger: panicStdout, | ||
MaxCount: 2, | ||
RestartCallback: func(ctx context.Context) { | ||
log.Println("restart...") | ||
}, | ||
} | ||
|
||
err = dctx.Run(ctx) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// belong func main() | ||
dctx.WithRecovery(func() { | ||
log.Println(os.Getpid(), "start...") | ||
time.Sleep(time.Second * 3) | ||
panic("This trigger panic") | ||
log.Println(os.Getpid(), "end") | ||
}, nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"github.com/whiteCcinn/daemon" | ||
"log" | ||
"os" | ||
"syscall" | ||
"time" | ||
) | ||
|
||
func main() { | ||
logName := "daemon.log" | ||
stdout, err := os.OpenFile(logName, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
ctx := context.Background() | ||
dctx := daemon.Context{ | ||
ProcAttr: syscall.SysProcAttr{}, | ||
//Logger: os.Stdout, | ||
Logger: stdout, | ||
MaxCount: 2, | ||
RestartCallback: func(ctx context.Context) { | ||
log.Println("restart...") | ||
}, | ||
} | ||
|
||
err = dctx.Run(ctx) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// belong func main() | ||
dctx.WithRecovery(func() { | ||
daemon.SetSigHandler(func(sig os.Signal) (err error) { | ||
log.Println("sigint") | ||
return | ||
}, syscall.SIGINT) | ||
|
||
daemon.SetSigHandler(func(sig os.Signal) (err error) { | ||
log.Println("sigterm") | ||
return nil | ||
}, syscall.SIGTERM) | ||
|
||
go func() { | ||
err := daemon.ServeSignals() | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
}() | ||
log.Println(os.Getpid(), "start...") | ||
time.Sleep(time.Second * 10) | ||
log.Println(os.Getpid(), "end...") | ||
}, nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package daemon | ||
|
||
type PipeMessageType int | ||
|
||
const ( | ||
SupervisorToProcess PipeMessageType = iota + 1 | ||
ProcessToSupervisor | ||
) | ||
|
||
type ProcessBehavior int | ||
|
||
const ( | ||
WantSafetyClose ProcessBehavior = iota + 1 | ||
) | ||
|
||
func (pmt PipeMessageType) String() (s string) { | ||
switch pmt { | ||
case SupervisorToProcess: | ||
s = "The Process sends messages to the Supervisor" | ||
case ProcessToSupervisor: | ||
s = "The Supervisor sends messages to the Process" | ||
default: | ||
s = "Unknown PipeMessageType" | ||
} | ||
return | ||
} | ||
|
||
func (pb ProcessBehavior) String() (s string) { | ||
switch pb { | ||
case WantSafetyClose: | ||
s = "Expect a safe exit" | ||
default: | ||
s = "Unknown ProcessBehavior" | ||
} | ||
return | ||
} | ||
|
||
type PipeMessage struct { | ||
Type PipeMessageType | ||
Behavior ProcessBehavior | ||
} |
Oops, something went wrong.