关于Context的优化
This commit is contained in:
+10
-10
@@ -240,10 +240,7 @@ func (c *Cluster) addJob(ctx context.Context, taskId string, jobData JobData, ca
|
|||||||
// TODO:考虑不同实例系统时间不一样,可能计算的下次时间不一致,会有重复执行的可能
|
// TODO:考虑不同实例系统时间不一样,可能计算的下次时间不一致,会有重复执行的可能
|
||||||
func (c *Cluster) getNextTime() {
|
func (c *Cluster) getNextTime() {
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(c.ctx)
|
lock := lockx.NewGlobalLock(c.ctx, c.redis, c.lockKey)
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
lock := lockx.NewGlobalLock(ctx, c.redis, c.lockKey)
|
|
||||||
// 获取锁
|
// 获取锁
|
||||||
lockBool := lock.Lock()
|
lockBool := lock.Lock()
|
||||||
if !lockBool {
|
if !lockBool {
|
||||||
@@ -294,7 +291,7 @@ func (c *Cluster) getNextTime() {
|
|||||||
// TODO:
|
// TODO:
|
||||||
expireTime := time.Minute
|
expireTime := time.Minute
|
||||||
|
|
||||||
res, err := c.redis.Eval(ctx, script, []string{c.zsetKey}, cacheKey, expireTime.Seconds(), nextTime.UnixMilli(), val.TaskId).Result()
|
res, err := c.redis.Eval(c.ctx, script, []string{c.zsetKey}, cacheKey, expireTime.Seconds(), nextTime.UnixMilli(), val.TaskId).Result()
|
||||||
|
|
||||||
if err == nil && res.(string) == "SUCCESS" {
|
if err == nil && res.(string) == "SUCCESS" {
|
||||||
// 设置成功
|
// 设置成功
|
||||||
@@ -373,11 +370,8 @@ func (c *Cluster) watch() {
|
|||||||
// 执行任务
|
// 执行任务
|
||||||
func (c *Cluster) doTask(ctx context.Context, taskId string) {
|
func (c *Cluster) doTask(ctx context.Context, taskId string) {
|
||||||
|
|
||||||
defer func() {
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
if err := recover(); err != nil {
|
defer cancel()
|
||||||
c.logger.Errorf(ctx, "timer:定时器出错 err:%+v stack:%s", err, string(debug.Stack()))
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
val, ok := clusterWorkerList.Load(taskId)
|
val, ok := clusterWorkerList.Load(taskId)
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -395,6 +389,12 @@ func (c *Cluster) doTask(ctx context.Context, taskId string) {
|
|||||||
}
|
}
|
||||||
defer lock.Unlock()
|
defer lock.Unlock()
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if err := recover(); err != nil {
|
||||||
|
c.logger.Errorf(ctx, "timer:定时器出错 err:%+v stack:%s", err, string(debug.Stack()))
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
// 执行任务
|
// 执行任务
|
||||||
t.Callback(ctx, t.ExtendData)
|
t.Callback(ctx, t.ExtendData)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user