Timer for tasks, servers autoupdating and other.
Added Timer - great package which will make periodic tasks to work. Timer exposes "taskDone" event, which can be triggered when task ends it's execution. Added first periodic task - servers information updating. By default it will execute every 10 minutes. Configurable thru options. Added new options pane: Servers updating. It controls all aspects of servers updating thing. Master server address, servers autoupdating checkbox and autoupdate timeout values are exposed for now. We're now on on 0.2.0-devel :). Fixes #29.
This commit is contained in:
32
timer/exported.go
Normal file
32
timer/exported.go
Normal file
@@ -0,0 +1,32 @@
|
||||
// URTator - Urban Terror server browser and game launcher, written in
|
||||
// Go.
|
||||
//
|
||||
// Copyright (c) 2016, Stanslav N. a.k.a pztrn (or p0z1tr0n)
|
||||
// All rights reserved.
|
||||
//
|
||||
// Licensed under Terms and Conditions of GNU General Public License
|
||||
// version 3 or any higher.
|
||||
// ToDo: put full text of license here.
|
||||
package timer
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"fmt"
|
||||
|
||||
// local
|
||||
"github.com/pztrn/urtrator/configuration"
|
||||
"github.com/pztrn/urtrator/eventer"
|
||||
)
|
||||
|
||||
var (
|
||||
Cfg *configuration.Config
|
||||
Eventer *eventer.Eventer
|
||||
)
|
||||
|
||||
func New(e *eventer.Eventer, cc *configuration.Config) *Timer {
|
||||
Cfg = cc
|
||||
Eventer = e
|
||||
fmt.Println("Creating Timer object...")
|
||||
t := Timer{}
|
||||
return &t
|
||||
}
|
125
timer/timer.go
Normal file
125
timer/timer.go
Normal file
@@ -0,0 +1,125 @@
|
||||
// URTator - Urban Terror server browser and game launcher, written in
|
||||
// Go.
|
||||
//
|
||||
// Copyright (c) 2016, Stanslav N. a.k.a pztrn (or p0z1tr0n)
|
||||
// All rights reserved.
|
||||
//
|
||||
// Licensed under Terms and Conditions of GNU General Public License
|
||||
// version 3 or any higher.
|
||||
// ToDo: put full text of license here.
|
||||
package timer
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Timer struct {
|
||||
// Tasks.
|
||||
tasks map[string]*TimerTask
|
||||
// Tasks map mutex.
|
||||
tasksMutex sync.Mutex
|
||||
}
|
||||
|
||||
func (t *Timer) AddTask(task *TimerTask) error {
|
||||
fmt.Println("Adding task '" + task.Name + "'...")
|
||||
|
||||
_, ok := t.tasks[task.Name]
|
||||
if ok {
|
||||
error_text := "Task '" + task.Name + "' already exist! Ignoring..."
|
||||
fmt.Println(error_text)
|
||||
return errors.New(error_text)
|
||||
}
|
||||
|
||||
task.InProgress = false
|
||||
|
||||
curtime := time.Now()
|
||||
nextlaunch := curtime.Add(time.Duration(task.Timeout) * time.Second)
|
||||
task.NextLaunch = nextlaunch
|
||||
|
||||
t.tasksMutex.Lock()
|
||||
t.tasks[task.Name] = task
|
||||
t.tasksMutex.Unlock()
|
||||
|
||||
fmt.Println("Added task '" + task.Name + "' with " + strconv.Itoa(task.Timeout) + " seconds timeout")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Timer) executeTasks() {
|
||||
t.tasksMutex.Lock()
|
||||
for task_name, task := range t.tasks {
|
||||
// Check if task should be run.
|
||||
curtime := time.Now()
|
||||
diff := curtime.Sub(task.NextLaunch)
|
||||
//fmt.Println(diff)
|
||||
if diff > 0 {
|
||||
fmt.Println("Checking task '" + task_name + "'...")
|
||||
// Check if task is already running.
|
||||
if task.InProgress {
|
||||
fmt.Println("Already executing, skipping...")
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Println("Launching task '" + task_name + "'...")
|
||||
task.InProgress = true
|
||||
Eventer.LaunchEvent(task.Callee, map[string]string{})
|
||||
|
||||
curtime = time.Now()
|
||||
nextlaunch := curtime.Add(time.Duration(task.Timeout) * time.Second)
|
||||
task.NextLaunch = nextlaunch
|
||||
}
|
||||
}
|
||||
t.tasksMutex.Unlock()
|
||||
}
|
||||
|
||||
func (t *Timer) GetTaskStatus(task_name string) bool {
|
||||
_, ok := t.tasks[task_name]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
return t.tasks[task_name].InProgress
|
||||
}
|
||||
|
||||
func (t *Timer) Initialize() {
|
||||
fmt.Println("Initializing timer...")
|
||||
|
||||
t.initializeStorage()
|
||||
|
||||
ticker := time.NewTicker(time.Second * 1)
|
||||
go func() {
|
||||
for _ = range ticker.C {
|
||||
go t.executeTasks()
|
||||
}
|
||||
}()
|
||||
|
||||
Eventer.AddEventHandler("taskDone", t.SetTaskNotInProgress)
|
||||
}
|
||||
|
||||
func (t *Timer) initializeStorage() {
|
||||
t.tasks = make(map[string]*TimerTask)
|
||||
}
|
||||
|
||||
func (t *Timer) RemoveTask(task_name string) {
|
||||
_, ok := t.tasks[task_name]
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
t.tasksMutex.Lock()
|
||||
delete(t.tasks, task_name)
|
||||
t.tasksMutex.Unlock()
|
||||
}
|
||||
|
||||
func (t *Timer) SetTaskNotInProgress(data map[string]string) {
|
||||
_, ok := t.tasks[data["task_name"]]
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
t.tasks[data["task_name"]].InProgress = false
|
||||
}
|
34
timer/timertask.go
Normal file
34
timer/timertask.go
Normal file
@@ -0,0 +1,34 @@
|
||||
// URTator - Urban Terror server browser and game launcher, written in
|
||||
// Go.
|
||||
//
|
||||
// Copyright (c) 2016, Stanslav N. a.k.a pztrn (or p0z1tr0n)
|
||||
// All rights reserved.
|
||||
//
|
||||
// Licensed under Terms and Conditions of GNU General Public License
|
||||
// version 3 or any higher.
|
||||
// ToDo: put full text of license here.
|
||||
package timer
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"time"
|
||||
)
|
||||
|
||||
type TimerTask struct {
|
||||
// Task name.
|
||||
Name string
|
||||
// Task timeout, in seconds.
|
||||
Timeout int
|
||||
// What we should call?
|
||||
// This should be an event name.
|
||||
Callee string
|
||||
|
||||
// Internal variables, used by Timer.
|
||||
// These variables can be defined, but they will be most likely
|
||||
// overrided after first task launch.
|
||||
// Next task launch time.
|
||||
NextLaunch time.Time
|
||||
// Is task currently executed?
|
||||
// Kinda alternative to mutex.
|
||||
InProgress bool
|
||||
}
|
Reference in New Issue
Block a user