/
event_bus.go
79 lines (67 loc) · 2.44 KB
/
event_bus.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
// Copyright 2020 PingCAP, 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package ctxt
import (
"fmt"
ev "github.com/asaskevich/EventBus"
"go.uber.org/zap"
)
// EventBus is an event bus for task events.
type EventBus struct {
eventBus ev.Bus
}
// EventKind is the task event kind.
type EventKind string
const (
// EventTaskBegin is emitted when a task is going to be executed.
EventTaskBegin EventKind = "task_begin"
// EventTaskFinish is emitted when a task finishes executing.
EventTaskFinish EventKind = "task_finish"
// EventTaskProgress is emitted when a task has made some progress.
EventTaskProgress EventKind = "task_progress"
)
// NewEventBus creates a new EventBus.
func NewEventBus() EventBus {
return EventBus{
eventBus: ev.New(),
}
}
// PublishTaskBegin publishes a TaskBegin event. This should be called only by Parallel or Serial.
func (ev *EventBus) PublishTaskBegin(task fmt.Stringer) {
zap.L().Debug("TaskBegin", zap.String("task", task.String()))
ev.eventBus.Publish(string(EventTaskBegin), task)
}
// PublishTaskFinish publishes a TaskFinish event. This should be called only by Parallel or Serial.
func (ev *EventBus) PublishTaskFinish(task fmt.Stringer, err error) {
zap.L().Debug("TaskFinish", zap.String("task", task.String()), zap.Error(err))
ev.eventBus.Publish(string(EventTaskFinish), task, err)
}
// PublishTaskProgress publishes a TaskProgress event.
func (ev *EventBus) PublishTaskProgress(task fmt.Stringer, progress string) {
zap.L().Debug("TaskProgress", zap.String("task", task.String()), zap.String("progress", progress))
ev.eventBus.Publish(string(EventTaskProgress), task, progress)
}
// Subscribe subscribes events.
func (ev *EventBus) Subscribe(eventName EventKind, handler interface{}) {
err := ev.eventBus.Subscribe(string(eventName), handler)
if err != nil {
panic(err)
}
}
// Unsubscribe unsubscribes events.
func (ev *EventBus) Unsubscribe(eventName EventKind, handler interface{}) {
err := ev.eventBus.Unsubscribe(string(eventName), handler)
if err != nil {
panic(err)
}
}