-
Notifications
You must be signed in to change notification settings - Fork 17
/
tasks.go
67 lines (60 loc) · 2.2 KB
/
tasks.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
// Copyright (C) 2014 Space Monkey, Inc.
//
// 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 monitor
import (
"sync"
"time"
"github.com/spacemonkeygo/monotime"
)
// TaskMonitor is a type for keeping track of tasks. A TaskMonitor will keep
// track of the current number of tasks, the highwater number (the maximum
// amount of concurrent tasks), the total started, the total completed, the
// total that returned without error, the average/min/max/most recent amount
// of time the task took to succeed/fail/both, the number of different kinds
// of errors the task had, and how many times the task had a panic.
//
// N.B.: Error types are best tracked when you're using Space Monkey's
// hierarchical error package: http://github.com/spacemonkeygo/errors
type TaskMonitor struct {
mtx sync.Mutex
current uint64
highwater uint64
total_started uint64
total_completed uint64
success uint64
success_timing *IntValueMonitor
error_timing *IntValueMonitor
total_timing *IntValueMonitor
errors map[string]uint64
panics uint64
running map[*TaskCtx]bool
}
// NewTaskMonitor returns a new TaskMonitor. You probably want to create
// a TaskMonitor using MonitorGroup.Task instead.
func NewTaskMonitor() *TaskMonitor {
return &TaskMonitor{
success_timing: NewIntValueMonitor(),
error_timing: NewIntValueMonitor(),
total_timing: NewIntValueMonitor(),
errors: make(map[string]uint64),
running: make(map[*TaskCtx]bool)}
}
// TaskCtx keeps track of a task as it is running.
type TaskCtx struct {
start time.Duration
monitor *TaskMonitor
}
func (t TaskCtx) ElapsedTime() time.Duration {
return monotime.Monotonic() - t.start
}