forked from intelsdi-x/snap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
152 lines (129 loc) · 3.98 KB
/
task.go
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
/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015 Intel Corporation
Licensed 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 core
import (
"time"
log "github.com/Sirupsen/logrus"
"github.com/intelsdi-x/snap/core/serror"
"github.com/intelsdi-x/snap/pkg/schedule"
"github.com/intelsdi-x/snap/scheduler/wmap"
)
type TaskState int
const (
TaskDisabled TaskState = iota - 1
TaskStopped
TaskSpinning
TaskFiring
TaskEnded
TaskStopping
)
var (
TaskStateLookup = map[TaskState]string{
TaskDisabled: "Disabled", // on error, not resumable
TaskStopped: "Stopped", // stopped but resumable
TaskSpinning: "Running", // running
TaskFiring: "Running", // running (firing can happen so briefly we don't want to try and render it as a string state)
TaskEnded: "Ended", // ended, not resumable because the schedule will not fire again
TaskStopping: "Stopping", // channel has been closed, wait for TaskStopped state
}
)
type TaskWatcherCloser interface {
Close() error
}
type TaskWatcherHandler interface {
CatchCollection([]Metric)
CatchTaskStarted()
CatchTaskStopped()
CatchTaskDisabled(string)
}
func (t TaskState) String() string {
return TaskStateLookup[t]
}
type Task interface {
ID() string
// Status() WorkflowState TODO, switch to string
State() TaskState
HitCount() uint
GetName() string
SetName(string)
SetID(string)
MissedCount() uint
FailedCount() uint
LastFailureMessage() string
LastRunTime() *time.Time
CreationTime() *time.Time
DeadlineDuration() time.Duration
SetDeadlineDuration(time.Duration)
SetTaskID(id string)
SetStopOnFailure(uint)
GetStopOnFailure() uint
Option(...TaskOption) TaskOption
WMap() *wmap.WorkflowMap
Schedule() schedule.Schedule
}
type TaskOption func(Task) TaskOption
// TaskDeadlineDuration sets the tasks deadline.
// The deadline is the amount of time that can pass before a worker begins
// processing the tasks collect job.
func TaskDeadlineDuration(v time.Duration) TaskOption {
return func(t Task) TaskOption {
previous := t.DeadlineDuration()
t.SetDeadlineDuration(v)
log.WithFields(log.Fields{
"_module": "core",
"_block": "TaskDeadlineDuration",
"task-id": t.ID(),
"task-name": t.GetName(),
"task deadline duration": t.DeadlineDuration(),
}).Debug("Setting deadlineDuration on task")
return TaskDeadlineDuration(previous)
}
}
// TaskStopOnFailure sets the tasks stopOnFailure
// The stopOnFailure is the number of consecutive task failures that will
// trigger disabling the task
func OptionStopOnFailure(v uint) TaskOption {
return func(t Task) TaskOption {
previous := t.GetStopOnFailure()
t.SetStopOnFailure(v)
log.WithFields(log.Fields{
"_module": "core",
"_block": "OptionStopOnFailure",
"task-id": t.ID(),
"task-name": t.GetName(),
"consecutive failure limit": t.GetStopOnFailure(),
}).Debug("Setting stop-on-failure limit for task")
return OptionStopOnFailure(previous)
}
}
// SetTaskName sets the name of the task.
// This is optional.
// If task name is not set, the task name is then defaulted to "Task-<task-id>"
func SetTaskName(name string) TaskOption {
return func(t Task) TaskOption {
previous := t.GetName()
t.SetName(name)
return SetTaskName(previous)
}
}
func SetTaskID(id string) TaskOption {
return func(t Task) TaskOption {
previous := t.ID()
t.SetID(id)
return SetTaskID(previous)
}
}
type TaskErrors interface {
Errors() []serror.SnapError
}