@@ -10,16 +10,13 @@ import (
10
10
"github.com/cschleiden/go-workflows/backend"
11
11
"github.com/cschleiden/go-workflows/internal/activity"
12
12
"github.com/cschleiden/go-workflows/internal/history"
13
+ "github.com/cschleiden/go-workflows/internal/metrickeys"
13
14
"github.com/cschleiden/go-workflows/internal/task"
14
15
"github.com/cschleiden/go-workflows/internal/workflow"
16
+ "github.com/cschleiden/go-workflows/metrics"
15
17
)
16
18
17
- type ActivityWorker interface {
18
- Start (context.Context ) error
19
- WaitForCompletion () error
20
- }
21
-
22
- type activityWorker struct {
19
+ type ActivityWorker struct {
23
20
backend backend.Backend
24
21
25
22
options * Options
@@ -32,8 +29,8 @@ type activityWorker struct {
32
29
clock clock.Clock
33
30
}
34
31
35
- func NewActivityWorker (backend backend.Backend , registry * workflow.Registry , clock clock.Clock , options * Options ) ActivityWorker {
36
- return & activityWorker {
32
+ func NewActivityWorker (backend backend.Backend , registry * workflow.Registry , clock clock.Clock , options * Options ) * ActivityWorker {
33
+ return & ActivityWorker {
37
34
backend : backend ,
38
35
39
36
options : options ,
@@ -47,7 +44,7 @@ func NewActivityWorker(backend backend.Backend, registry *workflow.Registry, clo
47
44
}
48
45
}
49
46
50
- func (aw * activityWorker ) Start (ctx context.Context ) error {
47
+ func (aw * ActivityWorker ) Start (ctx context.Context ) error {
51
48
for i := 0 ; i <= aw .options .ActivityPollers ; i ++ {
52
49
go aw .runPoll (ctx )
53
50
}
@@ -57,15 +54,15 @@ func (aw *activityWorker) Start(ctx context.Context) error {
57
54
return nil
58
55
}
59
56
60
- func (aw * activityWorker ) WaitForCompletion () error {
57
+ func (aw * ActivityWorker ) WaitForCompletion () error {
61
58
close (aw .activityTaskQueue )
62
59
63
60
aw .wg .Wait ()
64
61
65
62
return nil
66
63
}
67
64
68
- func (aw * activityWorker ) runPoll (ctx context.Context ) {
65
+ func (aw * ActivityWorker ) runPoll (ctx context.Context ) {
69
66
for {
70
67
select {
71
68
case <- ctx .Done ():
@@ -84,7 +81,7 @@ func (aw *activityWorker) runPoll(ctx context.Context) {
84
81
}
85
82
}
86
83
87
- func (aw * activityWorker ) runDispatcher (ctx context.Context ) {
84
+ func (aw * ActivityWorker ) runDispatcher (ctx context.Context ) {
88
85
var sem chan struct {}
89
86
if aw .options .MaxParallelActivityTasks > 0 {
90
87
sem = make (chan struct {}, aw .options .MaxParallelActivityTasks )
@@ -112,7 +109,15 @@ func (aw *activityWorker) runDispatcher(ctx context.Context) {
112
109
}
113
110
}
114
111
115
- func (aw * activityWorker ) handleTask (ctx context.Context , task * task.Activity ) {
112
+ func (aw * ActivityWorker ) handleTask (ctx context.Context , task * task.Activity ) {
113
+ a := task .Event .Attributes .(* history.ActivityScheduledAttributes )
114
+ ametrics := aw .backend .Metrics ().WithTags (metrics.Tags {metrickeys .ActivityName : a .Name })
115
+
116
+ // Record how long this task was in the queue
117
+ scheduledAt := task .Event .Timestamp
118
+ timeInQueue := time .Since (scheduledAt )
119
+ ametrics .Distribution (metrickeys .ActivityTaskDelay , metrics.Tags {}, float64 (timeInQueue / time .Millisecond ))
120
+
116
121
// Start heartbeat while activity is running
117
122
heartbeatCtx , cancelHeartbeat := context .WithCancel (ctx )
118
123
go func (ctx context.Context ) {
@@ -131,6 +136,9 @@ func (aw *activityWorker) handleTask(ctx context.Context, task *task.Activity) {
131
136
}
132
137
}(heartbeatCtx )
133
138
139
+ timer := metrics .Timer (ametrics , metrickeys .ActivityTaskProcessed , metrics.Tags {})
140
+ defer timer .Stop ()
141
+
134
142
result , err := aw .activityTaskExecutor .ExecuteActivity (ctx , task )
135
143
136
144
cancelHeartbeat ()
@@ -161,7 +169,7 @@ func (aw *activityWorker) handleTask(ctx context.Context, task *task.Activity) {
161
169
}
162
170
}
163
171
164
- func (aw * activityWorker ) poll (ctx context.Context , timeout time.Duration ) (* task.Activity , error ) {
172
+ func (aw * ActivityWorker ) poll (ctx context.Context , timeout time.Duration ) (* task.Activity , error ) {
165
173
if timeout == 0 {
166
174
timeout = 30 * time .Second
167
175
}
0 commit comments