优化逻辑

This commit is contained in:
2025-03-13 19:18:10 +08:00
parent 7a07f39f1b
commit 4189705922
14 changed files with 185 additions and 37 deletions

View File

@@ -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 = "* * * *"

View 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)
}
}

View 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)
}
}

View 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:
}
}
}
}
}