1234567891011121314151617181920212223242526272829303132333435363738 |
- package main
- import (
- "fmt"
- "log"
- "regexp"
- "time"
- )
- func CrashRestart() {
- go func() {
- fmt.Println("Started restart handler")
- start := 0
- for {
- <-time.After(10 * time.Second) //run every 10 second
- end := MCServer.LenLog()
- if !(start > MCServer.LenLog()-1) {
- serverlog := MCServer.ReadRangeLog(start, end)
- for _, s := range serverlog {
- //pattern1 := "\\[[0-2][0-9]:[0-6][0-9]:[0-6][0-9]\\] \\[Server Watchdog\\/FATAL\\]: Considering it to be crashed, server will forcibly shutdown."
- //cond1, _ := regexp.MatchString(pattern1, s.Log)
- pattern2 := "\\[[0-2][0-9]:[0-6][0-9]:[0-6][0-9]\\] \\[Server Watchdog\\/ERROR\\]: This crash report has been saved to:"
- cond2, _ := regexp.MatchString(pattern2, s.Log)
- //if cond1 || cond2 {
- if cond2 {
- log.Println("Seems server crashed. Attempting to restart...")
- MCServer.KillServer()
- <-time.After(10 * time.Second) // wait for a while for rest
- MCServer.StartService(serverConfig.Folder)
- }
- }
- start = end
- }
- }
- }()
- }
|