提交 e980f92c 编写于 作者: weixiaoyiri's avatar weixiaoyiri

fix race problem on toolbox/task

上级 c0ecf32d
......@@ -20,6 +20,7 @@ import (
"sort"
"strconv"
"strings"
"sync"
"time"
)
......@@ -32,6 +33,7 @@ type bounds struct {
// The bounds for each field.
var (
AdminTaskList map[string]Tasker
taskLock sync.Mutex
stop chan bool
changed chan bool
isstart bool
......@@ -389,6 +391,8 @@ func dayMatches(s *Schedule, t time.Time) bool {
// StartTask start all tasks
func StartTask() {
taskLock.Lock()
defer taskLock.Unlock()
if isstart {
//If already started, no need to start another goroutine.
return
......@@ -440,6 +444,8 @@ func run() {
// StopTask stop all tasks
func StopTask() {
taskLock.Lock()
defer taskLock.Unlock()
if isstart {
isstart = false
stop <- true
......@@ -449,6 +455,8 @@ func StopTask() {
// AddTask add task with name
func AddTask(taskname string, t Tasker) {
taskLock.Lock()
defer taskLock.Unlock()
t.SetNext(time.Now().Local())
AdminTaskList[taskname] = t
if isstart {
......@@ -458,6 +466,8 @@ func AddTask(taskname string, t Tasker) {
// DeleteTask delete task with name
func DeleteTask(taskname string) {
taskLock.Lock()
defer taskLock.Unlock()
delete(AdminTaskList, taskname)
if isstart {
changed <- true
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册