-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservicelistener.go
97 lines (82 loc) · 2.56 KB
/
servicelistener.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
package jobqueue
import (
"context"
"github.com/domonda/go-errs"
"github.com/domonda/go-types/uu"
)
type ServiceListener interface {
OnJobStopped(ctx context.Context, jobID uu.ID, jobType, jobOrigin string)
OnJobBundleStopped(ctx context.Context, jobBundleID uu.ID, jobBundleType, jobBundleOrigin string)
}
func NewDefaultServiceListener(service Service) ServiceListener {
return defaultServiceListener{Service: service}
}
type defaultServiceListener struct {
Service
}
func (l defaultServiceListener) OnJobStopped(ctx context.Context, jobID uu.ID, jobType, jobOrigin string) {
job, err := l.Service.GetJob(ctx, jobID)
if err != nil {
if errs.IsErrNotFound(err) {
log.Warn("OnJobStopped called for an already deleted job").
UUID("jobID", jobID).
Str("jobType", jobType).
Str("jobOrigin", jobOrigin).
Log()
} else {
log.ErrorCtx(ctx, "OnJobStopped GetJob error").
Err(err).
UUID("jobID", jobID).
Str("jobType", jobType).
Str("jobOrigin", jobOrigin).
Log()
}
return
}
jobStoppedListenersMtx.RLock()
listeners := jobStoppedListeners
jobStoppedListenersMtx.RUnlock()
for _, listener := range listeners {
listener.OnJobStopped(job)
}
}
func (l defaultServiceListener) OnJobBundleStopped(ctx context.Context, jobBundleID uu.ID, jobBundleType, jobBundleOrigin string) {
jobBundle, err := l.Service.GetJobBundle(ctx, jobBundleID)
if err != nil {
log.ErrorCtx(ctx, "OnJobBundleStopped GetJobBundle error, ignoring and continuing...").
Err(err).
UUID("jobBundleID", jobBundleID).
Str("jobBundleType", jobBundleType).
Str("jobBundleOrigin", jobBundleOrigin).
Log()
return
}
// jobBundle.Jobs, err = service.GetJobBundleJobs(jobBundleID)
// if err != nil {
// log.ErrorCtx(ctx, ).Err(err).UUID("jobBundleID", jobBundleID).Msg("GetJobBundle")
// return
// }
jobBundleStoppedListenersMtx.RLock()
allTypesListeners := jobBundleStoppedListeners
jobBundleStoppedListenersMtx.RUnlock()
for _, listener := range allTypesListeners {
listener.OnJobBundleStopped(jobBundle)
}
jobBundleOfTypeStoppedListenersMtx.RLock()
typeListener := jobBundleOfTypeStoppedListeners[jobBundleType]
jobBundleOfTypeStoppedListenersMtx.RUnlock()
if typeListener != nil {
typeListener.OnJobBundleStopped(jobBundle)
}
if !jobBundle.HasError() {
err = l.Service.DeleteJobBundle(ctx, jobBundleID)
if err != nil {
log.ErrorCtx(ctx, "OnJobBundleStopped DeleteJobBundle error").
Err(err).
UUID("jobBundleID", jobBundleID).
Str("jobBundleType", jobBundleType).
Str("jobBundleOrigin", jobBundleOrigin).
Log()
}
}
}