-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.go
More file actions
181 lines (152 loc) · 4.26 KB
/
task.go
File metadata and controls
181 lines (152 loc) · 4.26 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
/*
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 (
"runtime/debug"
"sync"
"time"
"go.uber.org/zap"
)
// Task implementation type.
// The first parameter is a chan to catch the END signal. When this channel closes, the implementation task must to finish
// The second parameter is the runner
// The third parameters is a slice of input parameters for the task implementation function
type TASK_IMPLEMENTATION func(chan struct{}, TaskManager_I, []interface{}) ([]interface{}, error)
const INFINITE_TASK_DURATION = 0
//
// Task
//
type Task_I interface {
SetLogger(log *zap.Logger)
GetID() int64
GetDuration() time.Duration
GetResponseChan() chan *TaskResponse
Run(args []interface{}) error
IsRunning() bool
IsFinished() bool
Finalize() error
Flush()
}
type Task struct {
logger *zap.Logger
Id int64
Duration time.Duration
Implementation TASK_IMPLEMENTATION
isRunning bool
isFinished bool
Eot chan struct{}
Response *TaskResponse
ResponseChan chan *TaskResponse
once *sync.Once
}
type TaskResponse struct {
Result interface{}
Err error
}
func (t *Task) SetLogger(logger *zap.Logger) {
t.logger = logger
}
func (t *Task) GetID() int64 {
return t.Id
}
func (t *Task) GetDuration() time.Duration {
return t.Duration
}
func (t *Task) Run(args []interface{}) error {
if !t.isRunning {
go func(t1 *Task) {
// Panic catching
defer func() {
t.isRunning = false
if r := recover(); r != nil {
t.logger.Error("Task finished with panic", zap.Int64("task_id", t.Id), zap.Time("when", time.Now()), zap.Any("stack", debug.Stack()))
}
}()
t.isRunning = true
t.Response.Result, t.Response.Err = t.Implementation(t.Eot, &taskManager{t}, args)
t.Flush()
}(t)
}
return nil
}
func (t *Task) IsRunning() bool {
return t.isRunning
}
func (t *Task) IsFinished() bool {
return t.isFinished
}
func (t *Task) Finalize() error {
t.isRunning = false
close(t.Eot)
t.isFinished = true
return nil
}
func (t *Task) GetResponseChan() chan *TaskResponse {
return t.ResponseChan
}
func (t *Task) Flush() {
t.ResponseChan <- t.Response
}
// TaskImplmentation_I interface
func (t *Task) Finish() {
f := func() {
t.Flush()
t.Finalize()
}
t.once.Do(f)
}
// taking a Task to be waked up by a runner instance
// Parameters:
// id: Task's identification. It must be unique per task
// duration: Maximum duration of the task. The task's implementation function is responsible for catching
// the closing of the EOT channel and process these event as a finalization request. See the test
// cases for more details about it.
// **special case**: 0 duration stands for infinite task
// implementation: Implementation function of the task. It my be complaint with the TASK_IMPLEMENTATION type
func GetTask(id int64, duration time.Duration, implementation TASK_IMPLEMENTATION) (Task_I, error) {
return &Task{Id: id,
Duration: duration,
Implementation: implementation,
isRunning: false,
isFinished: false,
Eot: make(chan struct{}),
ResponseChan: make(chan *TaskResponse, 2),
Response: &TaskResponse{},
once: &sync.Once{},
}, nil
}
//
// Task finisher
//
type TaskManager_I interface {
Finish()
Flush()
}
type taskManager struct {
task Task_I
}
// This finish the task
func (tf *taskManager) Finish() {
tf.task.Flush()
tf.task.Finalize()
}
// This flushes the task, by does not ends it
func (tf *taskManager) Flush() {
tf.task.Flush()
}