优化逻辑
This commit is contained in:
@@ -8,8 +8,8 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Time 时间控制器
|
||||
func Time(rootRules []cronrange.Rule, cron string, play func(c context.Context) error) func(c context.Context) error {
|
||||
// Cron 时间控制器
|
||||
func Cron(rootRules []cronrange.Rule, cron string, play func(c context.Context) error) func(c context.Context) error {
|
||||
// 设定默认时间规则
|
||||
if cron == "" {
|
||||
cron = "* * * *"
|
||||
21
internal/routes/standby_ctrl/device.go
Normal file
21
internal/routes/standby_ctrl/device.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package standby_ctrl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"game-driver/internal/common"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func Device(d *common.Device, lock bool, play func(c context.Context) error) func(c context.Context) error {
|
||||
return func(c context.Context) error {
|
||||
if lock {
|
||||
zap.S().Infoln("待机任务锁定设备")
|
||||
defer zap.S().Infoln("待机任务解锁设备")
|
||||
|
||||
d.Lock()
|
||||
defer d.Unlock()
|
||||
}
|
||||
|
||||
return play(c)
|
||||
}
|
||||
}
|
||||
23
internal/routes/standby_ctrl/duration.go
Normal file
23
internal/routes/standby_ctrl/duration.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package standby_ctrl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.uber.org/zap"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Duration 持续时长控制器
|
||||
func Duration(duration int64, play func(c context.Context) error) func(c context.Context) error {
|
||||
return func(c context.Context) error {
|
||||
zap.S().Infoln("待机持续时长控制器: ", duration)
|
||||
defer zap.S().Infoln("待机持续时长控制器结束: ", duration)
|
||||
|
||||
if duration > 0 {
|
||||
ctx, cancel := context.WithTimeout(c, time.Duration(duration)*time.Second)
|
||||
defer cancel()
|
||||
c = ctx
|
||||
}
|
||||
|
||||
return play(c)
|
||||
}
|
||||
}
|
||||
34
internal/routes/standby_ctrl/interval.go
Normal file
34
internal/routes/standby_ctrl/interval.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package standby_ctrl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.uber.org/zap"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Interval(interval int64, play func(c context.Context) error) func(c context.Context) error {
|
||||
return func(c context.Context) error {
|
||||
zap.S().Infoln("待机间隔控制器: ", interval)
|
||||
defer zap.S().Infoln("待机间隔控制器结束: ", interval)
|
||||
|
||||
for {
|
||||
err := play(c)
|
||||
if err != nil {
|
||||
zap.S().Errorln("执行后续操作异常: ", err)
|
||||
}
|
||||
if interval > 0 {
|
||||
select {
|
||||
case <-c.Done():
|
||||
return nil
|
||||
case <-time.After(time.Duration(interval) * time.Second):
|
||||
}
|
||||
} else {
|
||||
select {
|
||||
case <-c.Done():
|
||||
return nil
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user