mc_autotask.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "regexp"
  6. "time"
  7. )
  8. func CrashRestart() {
  9. go func() {
  10. fmt.Println("Started restart handler")
  11. start := 0
  12. for {
  13. <-time.After(10 * time.Second) //run every 10 second
  14. end := MCServer.LenLog()
  15. if !(start > MCServer.LenLog()-1) {
  16. serverlog := MCServer.ReadRangeLog(start, end)
  17. for _, s := range serverlog {
  18. 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."
  19. cond1, _ := regexp.MatchString(pattern1, s.Log)
  20. pattern2 := "\\[[0-2][0-9]:[0-6][0-9]:[0-6][0-9]\\] \\[Server Watchdog\\/ERROR\\]: This crash report has been saved to:"
  21. cond2, _ := regexp.MatchString(pattern2, s.Log)
  22. if cond1 || cond2 {
  23. log.Println("Seems server crashed. Attempting to restart...")
  24. MCServer.KillServer()
  25. <-time.After(10 * time.Second) // wait for a while for rest
  26. MCServer.StartService(serverConfig.Folder)
  27. }
  28. }
  29. start = end
  30. }
  31. }
  32. }()
  33. }