Skip to content

Commit

Permalink
fix: add first delay (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeessy2 authored Jul 8, 2024
1 parent c3c7423 commit 7520de4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
4 changes: 3 additions & 1 deletion client/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand Down
11 changes: 5 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func main() {
uninstallService()
default:
if util.IsRunInDocker() {
run()
run(100 * time.Millisecond)
} else {
s := getService()
status, _ := s.Status()
Expand All @@ -67,7 +67,7 @@ func main() {
default:
log.Println("可使用 ./backup-x -s install 安装服务运行")
}
run()
run(100 * time.Millisecond)
}
}
}
Expand All @@ -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))
Expand All @@ -97,7 +97,7 @@ func run() {

// 运行
go client.DeleteOldBackup()
go client.RunLoop()
go client.RunLoop(firstDelay)

err := http.ListenAndServe(*listen, nil)

Expand All @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion web/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"strconv"
"strings"
"time"
)

// Save 保存
Expand Down Expand Up @@ -116,7 +117,7 @@ func Save(writer http.ResponseWriter, request *http.Request) {
}
// 重新进行循环
client.StopRunLoop()
go client.RunLoop()
go client.RunLoop(100 * time.Millisecond)
}

// 回写错误信息
Expand Down

0 comments on commit 7520de4

Please sign in to comment.