-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrunner_daemon.go
57 lines (48 loc) · 1020 Bytes
/
runner_daemon.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"context"
"fmt"
"github.com/acicn/minit/pkg/mlog"
"time"
)
const KindDaemon = "daemon"
type DaemonRunner struct {
Unit
logger *mlog.Logger
}
func (r *DaemonRunner) Run(ctx context.Context) {
r.logger.Printf("控制器启动")
defer r.logger.Printf("控制器退出")
forLoop:
for {
// 检查 ctx 是否已经结束
if ctx.Err() != nil {
break forLoop
}
var err error
if err = execute(r.ExecuteOptions, r.logger); err != nil {
r.logger.Errorf("启动失败: %s", err.Error())
}
// 检查 ctx 是否已经结束
if ctx.Err() != nil {
break forLoop
}
// 重试
r.logger.Printf("5s 后重启")
timer := time.NewTimer(time.Second * 5)
select {
case <-timer.C:
case <-ctx.Done():
break forLoop
}
}
}
func NewDaemonRunner(unit Unit, logger *mlog.Logger) (Runner, error) {
if len(unit.Command) == 0 {
return nil, fmt.Errorf("没有指定命令,检查 command 字段")
}
return &DaemonRunner{
Unit: unit,
logger: logger,
}, nil
}