Timer synchronization and package linting.

This commit is contained in:
Stanislav Nikitin 2018-11-10 23:14:52 +05:00
parent 6944b51d13
commit a50136d2c5

View File

@ -46,16 +46,16 @@ func (t *Timer) AddTask(task *TimerTask) error {
_, ok := t.tasks[task.Name] _, ok := t.tasks[task.Name]
if ok { if ok {
error_text := "Task '" + task.Name + "' already exist! Ignoring..." errorText := "Task '" + task.Name + "' already exist! Ignoring..."
fmt.Println(error_text) fmt.Println(errorText)
return errors.New(error_text) return errors.New(errorText)
} }
task.InProgress = false task.InProgress = false
curtime := time.Now() curTime := time.Now()
nextlaunch := curtime.Add(time.Duration(task.Timeout) * time.Second) nextLaunch := curTime.Add(time.Duration(task.Timeout) * time.Second)
task.NextLaunch = nextlaunch task.NextLaunch = nextLaunch
t.tasksMutex.Lock() t.tasksMutex.Lock()
t.tasks[task.Name] = task t.tasks[task.Name] = task
@ -67,20 +67,21 @@ func (t *Timer) AddTask(task *TimerTask) error {
func (t *Timer) executeTasks() { func (t *Timer) executeTasks() {
t.tasksMutex.Lock() t.tasksMutex.Lock()
for task_name, task := range t.tasks { defer t.tasksMutex.Unlock()
for taskName, task := range t.tasks {
// Check if task should be run. // Check if task should be run.
curtime := time.Now() curtime := time.Now()
diff := curtime.Sub(task.NextLaunch) diff := curtime.Sub(task.NextLaunch)
//fmt.Println(diff) //fmt.Println(diff)
if diff > 0 { if diff > 0 {
fmt.Println("Checking task '" + task_name + "'...") fmt.Println("Checking task '" + taskName + "'...")
// Check if task is already running. // Check if task is already running.
if task.InProgress { if task.InProgress {
fmt.Println("Already executing, skipping...") fmt.Println("Already executing, skipping...")
continue continue
} }
fmt.Println("Launching task '" + task_name + "'...") fmt.Println("Launching task '" + taskName + "'...")
task.InProgress = true task.InProgress = true
Eventer.LaunchEvent(task.Callee, map[string]string{}) Eventer.LaunchEvent(task.Callee, map[string]string{})
@ -89,12 +90,11 @@ func (t *Timer) executeTasks() {
task.NextLaunch = nextlaunch task.NextLaunch = nextlaunch
} }
} }
t.tasksMutex.Unlock()
} }
func (t *Timer) GetTaskStatus(task_name string) bool { func (t *Timer) GetTaskStatus(taskName string) bool {
t.tasksMutex.Lock() t.tasksMutex.Lock()
task, ok := t.tasks[task_name] task, ok := t.tasks[taskName]
t.tasksMutex.Unlock() t.tasksMutex.Unlock()
if !ok { if !ok {
return false return false
@ -111,7 +111,7 @@ func (t *Timer) Initialize() {
ticker := time.NewTicker(time.Second * 1) ticker := time.NewTicker(time.Second * 1)
go func() { go func() {
for _ = range ticker.C { for range ticker.C {
go t.executeTasks() go t.executeTasks()
} }
}() }()
@ -121,26 +121,26 @@ func (t *Timer) initializeStorage() {
t.tasks = make(map[string]*TimerTask) t.tasks = make(map[string]*TimerTask)
} }
func (t *Timer) RemoveTask(task_name string) { func (t *Timer) RemoveTask(taskName string) {
t.tasksMutex.Lock() t.tasksMutex.Lock()
_, ok := t.tasks[task_name] _, ok := t.tasks[taskName]
t.tasksMutex.Unlock() t.tasksMutex.Unlock()
if !ok { if !ok {
return return
} }
t.tasksMutex.Lock() t.tasksMutex.Lock()
delete(t.tasks, task_name) delete(t.tasks, taskName)
t.tasksMutex.Unlock() t.tasksMutex.Unlock()
} }
func (t *Timer) SetTaskNotInProgress(data map[string]string) { func (t *Timer) SetTaskNotInProgress(data map[string]string) {
t.tasksMutex.Lock() t.tasksMutex.Lock()
_, ok := t.tasks[data["task_name"]] defer t.tasksMutex.Unlock()
_, ok := t.tasks[data["taskName"]]
if !ok { if !ok {
return return
} }
t.tasks[data["task_name"]].InProgress = false t.tasks[data["taskName"]].InProgress = false
t.tasksMutex.Unlock()
} }