Browse Source

fix: add first delay (#115)

jeessy2 1 year ago
parent
commit
f936e6b57f
3 changed files with 10 additions and 8 deletions
  1. 3 1
      client/backup.go
  2. 5 6
      main.go
  3. 2 1
      web/save.go

+ 3 - 1
client/backup.go

@@ -26,12 +26,14 @@ type backupLooper struct {
 var bl = &backupLooper{Wg: sync.WaitGroup{}}
 
 // RunLoop backup db loop
-func RunLoop() {
+func RunLoop(firstDelay time.Duration) {
 	conf, err := entity.GetConfigCache()
 	if err != nil {
 		return
 	}
 
+	time.Sleep(firstDelay)
+
 	// clear
 	bl.Tickers = []*time.Ticker{}
 

+ 5 - 6
main.go

@@ -52,7 +52,7 @@ func main() {
 		uninstallService()
 	default:
 		if util.IsRunInDocker() {
-			run()
+			run(100 * time.Millisecond)
 		} else {
 			s := getService()
 			status, _ := s.Status()
@@ -67,7 +67,7 @@ func main() {
 				default:
 					log.Println("可使用 ./backup-x -s install 安装服务运行")
 				}
-				run()
+				run(100 * time.Millisecond)
 			}
 		}
 	}
@@ -81,7 +81,7 @@ func faviconFsFunc(writer http.ResponseWriter, request *http.Request) {
 	http.FileServer(http.FS(faviconEmbededFile)).ServeHTTP(writer, request)
 }
 
-func run() {
+func run(firstDelay time.Duration) {
 	// 启动静态文件服务
 	http.HandleFunc("/static/", web.BasicAuth(staticFsFunc))
 	http.HandleFunc("/favicon.ico", web.BasicAuth(faviconFsFunc))
@@ -97,7 +97,7 @@ func run() {
 
 	// 运行
 	go client.DeleteOldBackup()
-	go client.RunLoop()
+	go client.RunLoop(firstDelay)
 
 	err := http.ListenAndServe(*listen, nil)
 
@@ -116,8 +116,7 @@ func (p *program) Start(s service.Service) error {
 }
 func (p *program) run() {
 	// 服务运行,延时20秒运行,等待网络
-	time.Sleep(20 * time.Second)
-	run()
+	run(20 * time.Second)
 }
 func (p *program) Stop(s service.Service) error {
 	// Stop should not block. Return with a few seconds.

+ 2 - 1
web/save.go

@@ -8,6 +8,7 @@ import (
 	"net/http"
 	"strconv"
 	"strings"
+	"time"
 )
 
 // Save 保存
@@ -116,7 +117,7 @@ func Save(writer http.ResponseWriter, request *http.Request) {
 		}
 		// 重新进行循环
 		client.StopRunLoop()
-		go client.RunLoop()
+		go client.RunLoop(100 * time.Millisecond)
 	}
 
 	// 回写错误信息