1 Star 0 Fork 0

h79 / goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
worker.go 828 Bytes
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2023-12-15 02:07 . 协程安全退出
package scheduler
import (
"gitee.com/h79/goutils/common/system"
)
type jobWorker struct {
pool *Pool
job chan *Job
running system.RunningCheck
stop chan bool
}
func (w *jobWorker) start() {
w.running.GoRunning(w.run)
}
func (w *jobWorker) run() {
w.pool.workerRunning()
defer w.pool.workerQuit()
for {
w.pool.workers <- w //把idle work放进去
select {
case job := <-w.job:
ctrl := system.NewCtrl()
//超时
res, err := ctrl.RunAfter(job.Timeout, func() (interface{}, error) {
return job.Execute()
})
job.done(&Result{Type: job.Type, Id: job.Id, Data: res, Err: err})
w.pool.workerIdle()
case <-w.stop:
w.stop <- true
return
}
}
}
func newWorker(pool *Pool) *jobWorker {
return &jobWorker{
pool: pool,
job: make(chan *Job),
stop: make(chan bool),
}
}
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.20.57

搜索帮助