var wg sync.WaitGroup
var once sync.Once
wg.Add(1)
XXLJobExecutor.RegTask("task.test.demo", func(cxt context.Context, param *xxl.RunReq) string {
defer func() {
once.Do(func() {
wg.Done() //只能done一次
})
}()
t.Log("OK")
return "OK"
})
t.Log("step")
go func() {
require.NoError(t, XXLJobExecutor.Run())
t.Log("can not print this log because the stop is wrong")
}()
wg.Wait()
time.Sleep(time.Second)
t.Log("stopping")
XXLJobExecutor.Stop()
t.Log("stopped")
time.Sleep(time.Second)
log is:
2024/04/25 11:54:15 INFO 执行器注册成功:{"code":200,"msg":null,"content":null}
2024/04/25 11:54:32 INFO 任务参数:&{2 task.test.demo SERIAL_EXECUTION 0 24 1714020872593 BEAN 1714016646000 0 1}
2024/04/25 11:54:32 INFO 任务[2]开始执行:task.test.demo
xxl_job_test.go:34: OK
2024/04/25 11:54:32 INFO 任务回调成功:{"code":200,"msg":null,"content":null}
xxl_job_test.go:48: stopping
2024/04/25 11:54:33 INFO 执行器摘除成功:{"code":200,"msg":null,"content":null}
xxl_job_test.go:50: stopped
2024/04/25 11:54:35 INFO 执行器注册成功:{"code":200,"msg":null,"content":null}
2024/04/25 11:54:46 INFO 任务参数:&{2 task.test.demo SERIAL_EXECUTION 0 25 1714020886165 BEAN 1714016646000 0 1}
2024/04/25 11:54:46 INFO 任务[2]开始执行:task.test.demo
xxl_job_test.go:34: OK
2024/04/25 11:54:46 INFO 任务回调成功:{"code":200,"msg":null,"content":null}
2024/04/25 11:54:54 INFO 任务参数:&{2 task.test.demo SERIAL_EXECUTION 0 26 1714020894733 BEAN 1714016646000 0 1}
2024/04/25 11:54:54 INFO 任务[2]开始执行:task.test.demo
xxl_job_test.go:34: OK
2024/04/25 11:54:54 INFO 任务回调成功:{"code":200,"msg":null,"content":null}
如下面的代码所示,我想让它执行一次以后就结束,因为这本身就是在一个简单的测试函数里。
结果执行了以后呢,确实是能够看到日志 stopped,预期是在stopped以后就啥也不做,退出,但实际结果不符合这样的预期。
但是代码里的task还是会持续进行。
log is: