forked from concourse/concourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coordinator.go
73 lines (57 loc) · 1.5 KB
/
coordinator.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
package component
import (
"context"
"code.cloudfoundry.org/lager/lagerctx"
"github.com/pf-qiu/concourse/v6/atc/db/lock"
)
// Coordinator ensures that the given component is not executed concurrently.
type Coordinator struct {
Locker lock.LockFactory
Component Component
Runnable Runnable
}
func (coordinator *Coordinator) RunPeriodically(ctx context.Context) {
coordinator.run(ctx, false)
}
func (coordinator *Coordinator) RunImmediately(ctx context.Context) {
coordinator.run(ctx, true)
}
func (coordinator *Coordinator) run(ctx context.Context, immediate bool) {
logger := lagerctx.FromContext(ctx)
lockID := lock.NewTaskLockID(coordinator.Component.Name())
lock, acquired, err := coordinator.Locker.Acquire(logger, lockID)
if err != nil {
logger.Error("failed-to-acquire-lock", err)
return
}
if !acquired {
logger.Debug("lock-unavailable")
return
}
defer lock.Release()
exists, err := coordinator.Component.Reload()
if err != nil {
logger.Error("failed-to-reload-component", err)
return
}
if !exists {
logger.Info("component-disappeared")
return
}
if coordinator.Component.Paused() {
logger.Debug("component-paused")
return
}
if !immediate && !coordinator.Component.IntervalElapsed() {
logger.Debug("interval-not-elapsed")
return
}
if err := coordinator.Runnable.Run(ctx); err != nil {
logger.Error("component-failed", err)
return
}
if err := coordinator.Component.UpdateLastRan(); err != nil {
logger.Error("failed-to-update-last-ran", err)
return
}
}