-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent.go
More file actions
129 lines (117 loc) · 2.92 KB
/
Copy pathevent.go
File metadata and controls
129 lines (117 loc) · 2.92 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
package taskengine
import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
)
type EventType int
const (
EventNil EventType = iota
EventStart
EventSuccess
EventError
EventCanceled
)
// String representation of an EventType.
func (t EventType) String() string {
if t < EventNil || t > EventCanceled {
return "invalid"
}
strings := []string{
"nil",
"start",
"success",
"error",
"canceled",
}
return strings[t]
}
// MarshalJSON returns the json representation of the EventType
func (t EventType) MarshalJSON() ([]byte, error) {
return json.Marshal(t.String())
}
// Event type contains the informations of the task execution.
// Events objects are emitted by the engine.ExecuteEvents method.
// For each (worker, task) pair, it is emitted a Start event
// followeb by a final event that can be a Success, Canceled or Error event.
// The event.Type() method returns the type of event.
type Event struct {
Result Result // nil for Start event
WorkerID WorkerID
WorkerInst int
Task Task
TaskStat TaskStat
TimeStart time.Time
TimeEnd time.Time // same as TimeStart for Start event
}
// String returns a representation of an event.
func (e *Event) String() string {
return fmt.Sprintf("%s[%d] %s%v %s",
e.WorkerID, e.WorkerInst,
e.Task.TaskID(), e.TaskStat,
e.Type())
}
// Type method returns the type of Event.
func (e *Event) Type() EventType {
// TODO: maybe EventCanceled must consider context.DeadlineExceeded error also.
if e == nil {
return EventNil
}
if e.Result == nil {
return EventStart
}
if e.Result.Error() == nil {
return EventSuccess
}
if errors.Is(e.Result.Error(), context.Canceled) {
return EventCanceled
}
return EventError
}
// IsFirstSuccessOrLastResult returns true if it is the first success result
// or it is the last result and no success was previously found.
func IsFirstSuccessOrLastResult(e *Event) bool {
if !IsResult(e) {
return false
}
stat := &e.TaskStat
if e.Result.Error() == nil {
// first success
return stat.Success == 1
} else {
// last results and no success was found
return stat.Todo == 0 && stat.Doing == 0 && stat.Success == 0
}
}
// IsResultUntilFirstSuccess returns true for all the results
// until the first success (included).
func IsResultUntilFirstSuccess(e *Event) bool {
if !IsResult(e) {
return false
}
stat := &e.TaskStat
if e.Result.Error() == nil {
// first success
return stat.Success == 1
} else {
// it is not a success and no success was found
return stat.Success == 0
}
}
// IsSuccessOrError returns true if it is a result and
// it is a success or an error result.
// Return false in case of canceled result.
func IsSuccessOrError(e *Event) bool {
if !IsResult(e) {
return false
}
err := e.Result.Error()
return !errors.Is(err, context.Canceled)
}
// IsResult return true if the event has a not nil result
// i.e. not a start event.
func IsResult(e *Event) bool {
return (e != nil) && (e.Result != nil)
}