-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.go
More file actions
210 lines (174 loc) · 5.42 KB
/
runner.go
File metadata and controls
210 lines (174 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
Copyright 2016 - Jaume Arús
Author Jaume Arús - jaumearus@gmail.com
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
package runner
import (
"errors"
"fmt"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// Getting a runner instance
// Parameters :
// tasks_checking_interval: Frequency in milliseconds of tasks checking done by the runner.
func GetRunner(tasks_checking_interval int64) (Runner_I, error) {
var lcfg zap.Config = zap.NewProductionConfig()
lcfg.EncoderConfig.EncodeTime = SyslogTimeEncoder
logger, _ := lcfg.Build()
return &runner{tasks_checking_interval: tasks_checking_interval, tasks: make(map[int64]*RunningTask), isRunnnig: false, logger: logger}, nil
}
func SyslogTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format(time.RFC3339))
}
type RunningTask struct {
Task Task_I
Args []interface{}
FirstWakingUp time.Time
RunningFrom []time.Time
}
type Runner_I interface {
Start() error
Shutdown() error
WakeUpTask(Task_I, ...interface{}) error
IsTaskRunning(int64) (bool, error)
GetRunningTasksIDs() ([]int64, error)
FinishTaskById(int64) error
}
type runner struct {
logger *zap.Logger
tasks_checking_interval int64
tasks map[int64]*RunningTask
isRunnnig bool
}
// Waking up a new task, that implements the Task_I interface
// Parameters:
// task: The task to be waked up
// args: A list of one or more arguments which will be passed to the task implementation as parameters
func (r *runner) WakeUpTask(task Task_I, args ...interface{}) error {
// Check if already exists a task with the same ID
if _, ok := r.tasks[task.GetID()]; ok {
return errors.New(fmt.Sprintf("Already exists a task with ID: %d", task.GetID()))
}
// Register the task
task.SetLogger(r.logger)
r.tasks[task.GetID()] = &RunningTask{Task: task, Args: args}
return nil
}
func (r *runner) IsTaskRunning(id int64) (bool, error) {
t, ok := r.tasks[id]
if !ok {
return false, errors.New(fmt.Sprintf("There is not any task with ID %d !!!", id))
}
return t.Task.IsRunning(), nil
}
func (r *runner) GetRunningTasksIDs() ([]int64, error) {
ids := make([]int64, len(r.tasks))
c := 0
for id, _ := range r.tasks {
ids[c] = id
c += 1
}
return ids, nil
}
// Starting the runner. If this method is not called, the tasks wont be fire up
func (r *runner) Start() error {
go func(r *runner) {
t := time.NewTicker(time.Duration(r.tasks_checking_interval) * time.Millisecond)
for _ = range t.C {
if !r.isRunnnig {
r.run()
}
}
}(r)
return nil
}
func (r *runner) Shutdown() error {
var finisher TaskManager_I
for _, task := range r.tasks {
t := task
finisher = &taskManager{t.Task}
finisher.Finish()
}
return nil
}
func (r *runner) FinishTaskById(id int64) error {
t, ok := r.tasks[id]
if !ok {
return errors.New(fmt.Sprintf("There is not any task with ID %d !!!", id))
}
finisher := &taskManager{t.Task}
finisher.Finish()
return nil
}
func (r *runner) run() {
defer func() {
r.isRunnnig = false
}()
t := time.NewTicker(time.Duration(r.tasks_checking_interval) * time.Millisecond)
for _ = range t.C {
r.checkTasks()
}
}
// Checking the status of the tasks.
// This method is invoked with the frequency filled in the tasks_checking_interval of the method GetRunner()
func (r *runner) checkTasks() error {
var t time.Time
var runningTime int64
var taskDuration int64
for _, rtask := range r.tasks {
t = time.Now()
if rtask.RunningFrom == nil {
// The task has not been woke up yet. Waking it up...
r.startTaskRunning(rtask, t)
} else {
// Check task duration (task duration <= 0 stands for infinite task)
runningTime = int64(t.Sub(rtask.FirstWakingUp).Seconds() * 1000)
taskDuration = int64(rtask.Task.GetDuration().Seconds() * 1000)
if rtask.Task.IsFinished() || (taskDuration > 0 && (runningTime >= taskDuration)) {
r.ensureTaskFinish(rtask)
} else {
// Check if the task is running
if !rtask.Task.IsRunning() {
r.ensureTaskIsRunning(rtask, t)
} else {
// Keeping the task running ..
//r.logger.Debug("keep the task running",zap.Int64("running_time",runningTime),zap.Int64("task_duration", taskDuration))
}
}
}
}
return nil
}
func (r *runner) startTaskRunning(rtask *RunningTask, t time.Time) {
rtask.FirstWakingUp = t
rtask.RunningFrom = []time.Time{t}
rtask.Task.Run(rtask.Args)
}
func (r *runner) ensureTaskFinish(rtask *RunningTask) {
if rtask.Task.IsRunning() {
// This finishes the task and fires up the response sending
rtask.Task.Finalize()
}
delete(r.tasks, rtask.Task.GetID())
return
}
func (r *runner) ensureTaskIsRunning(rtask *RunningTask, t time.Time) {
rtask.RunningFrom = append(rtask.RunningFrom, t)
rtask.Task.Run(rtask.Args)
}