mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 07:20:55 +00:00
fix race problem on toolbox/task
This commit is contained in:
parent
c0ecf32d17
commit
e980f92c63
@ -20,6 +20,7 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -32,6 +33,7 @@ type bounds struct {
|
|||||||
// The bounds for each field.
|
// The bounds for each field.
|
||||||
var (
|
var (
|
||||||
AdminTaskList map[string]Tasker
|
AdminTaskList map[string]Tasker
|
||||||
|
taskLock sync.Mutex
|
||||||
stop chan bool
|
stop chan bool
|
||||||
changed chan bool
|
changed chan bool
|
||||||
isstart bool
|
isstart bool
|
||||||
@ -389,6 +391,8 @@ func dayMatches(s *Schedule, t time.Time) bool {
|
|||||||
|
|
||||||
// StartTask start all tasks
|
// StartTask start all tasks
|
||||||
func StartTask() {
|
func StartTask() {
|
||||||
|
taskLock.Lock()
|
||||||
|
defer taskLock.Unlock()
|
||||||
if isstart {
|
if isstart {
|
||||||
//If already started, no need to start another goroutine.
|
//If already started, no need to start another goroutine.
|
||||||
return
|
return
|
||||||
@ -440,6 +444,8 @@ func run() {
|
|||||||
|
|
||||||
// StopTask stop all tasks
|
// StopTask stop all tasks
|
||||||
func StopTask() {
|
func StopTask() {
|
||||||
|
taskLock.Lock()
|
||||||
|
defer taskLock.Unlock()
|
||||||
if isstart {
|
if isstart {
|
||||||
isstart = false
|
isstart = false
|
||||||
stop <- true
|
stop <- true
|
||||||
@ -449,6 +455,8 @@ func StopTask() {
|
|||||||
|
|
||||||
// AddTask add task with name
|
// AddTask add task with name
|
||||||
func AddTask(taskname string, t Tasker) {
|
func AddTask(taskname string, t Tasker) {
|
||||||
|
taskLock.Lock()
|
||||||
|
defer taskLock.Unlock()
|
||||||
t.SetNext(time.Now().Local())
|
t.SetNext(time.Now().Local())
|
||||||
AdminTaskList[taskname] = t
|
AdminTaskList[taskname] = t
|
||||||
if isstart {
|
if isstart {
|
||||||
@ -458,6 +466,8 @@ func AddTask(taskname string, t Tasker) {
|
|||||||
|
|
||||||
// DeleteTask delete task with name
|
// DeleteTask delete task with name
|
||||||
func DeleteTask(taskname string) {
|
func DeleteTask(taskname string) {
|
||||||
|
taskLock.Lock()
|
||||||
|
defer taskLock.Unlock()
|
||||||
delete(AdminTaskList, taskname)
|
delete(AdminTaskList, taskname)
|
||||||
if isstart {
|
if isstart {
|
||||||
changed <- true
|
changed <- true
|
||||||
|
Loading…
Reference in New Issue
Block a user