events.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package backup
  2. import (
  3. "log"
  4. "time"
  5. "strings"
  6. "box-gm/utils"
  7. )
  8. func getNextTime(now time.Time, startTime string) time.Time {
  9. arr := strings.Split(startTime, ":")
  10. hour := utils.StringToInt(arr[0])
  11. min := utils.StringToInt(arr[1])
  12. sec := utils.StringToInt(arr[2])
  13. next := time.Date(now.Year(), now.Month(), now.Day(), hour, min, sec, 0, now.Location())
  14. if now.Before(next) {
  15. return next
  16. }
  17. next = now.Add(time.Hour * 24)
  18. next = time.Date(next.Year(), next.Month(), next.Day(), hour, min, sec, 0, next.Location())
  19. return next
  20. }
  21. // 备份程序初始化
  22. func Init_backup_event_proc() {
  23. for {
  24. now := time.Now()
  25. //返回一个指定时间的下次执行的时间戳
  26. timeBackup := utils.GetKeyConf("backup", "time_event")
  27. if timeBackup == "" {
  28. break
  29. }
  30. next := getNextTime(now, timeBackup)
  31. t := time.NewTimer(next.Sub(now))
  32. log.Printf("下一次日志备份开始时间为[%s]", next.String())
  33. <-t.C
  34. log.Println("备份日志")
  35. go run_event_backup()
  36. }
  37. }
  38. // 开始备份
  39. func run_event_backup() {
  40. }