-
Notifications
You must be signed in to change notification settings - Fork 13
/
job_manager.go
175 lines (153 loc) · 4.18 KB
/
job_manager.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// SPDX-License-Identifier: ISC
// Copyright (c) 2014-2019 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"sync"
"time"
"github.com/bitmark-inc/logger"
)
type JobManager interface {
Start()
}
const (
JobManagerPrefix = "job-manager"
)
type JobManagerChannel struct {
rescheduleChannel <-chan struct{}
startEventChannel chan struct{}
stopEventChannel chan struct{}
}
type JobManagerData struct {
calendar JobCalendar
proofer Proofer
channels JobManagerChannel
log *logger.L
initialized bool
wg sync.WaitGroup
}
func newJobManager(calendar JobCalendar, proofer Proofer, rescheduleChannel <-chan struct{}, logger *logger.L) JobManager {
return &JobManagerData{
calendar: calendar,
proofer: proofer,
log: logger,
channels: JobManagerChannel{
rescheduleChannel: rescheduleChannel,
startEventChannel: make(chan struct{}, 1),
stopEventChannel: make(chan struct{}, 1),
},
initialized: false,
}
}
func (j *JobManagerData) waitForRefresh() {
j.log.Info("waiting to reschedule events")
//lint:ignore S1000 XXX: We should use for range here
for {
select {
case <-j.channels.rescheduleChannel:
j.log.Debug("receive reschedule event")
if j.initialized {
j.resetAllEvent()
} else {
j.initialized = true
now := time.Now()
j.calendar.RescheduleStartEventsPrior(now)
j.calendar.RescheduleStopEventsPrior(now)
}
j.reschedule()
}
}
// j.log.Info("stop...")
}
func (j *JobManagerData) waitNextHasingStartEvent(duration time.Duration) {
j.log.Infof("create goroutine for start events, next event duration: %.1f minutes",
duration.Minutes())
d := duration
defer j.wg.Done()
loop:
for {
select {
case <-time.After(d): // timeout
j.log.Debugf("start hashing")
j.proofer.StartHashing()
now := time.Now()
intf := j.calendar.PickNextStartEvent(now)
j.calendar.RescheduleStartEventsPrior(now)
if nil == intf {
intf = j.calendar.PickNextStartEvent(now)
}
nextEvent := intf.(time.Time)
d = j.timeDurationFromSrc2Dest(now, nextEvent)
j.log.Infof("next start event at %s, duration: %.1f minutes",
nextEvent.String(), d.Minutes())
case <-j.channels.startEventChannel:
j.log.Debug("reset event, terminate start goroutine...")
break loop
}
}
j.log.Infof("start event terminated...")
}
func (j *JobManagerData) waitNextHasingStopEvent(duration time.Duration) {
j.log.Infof("create goroutine for stop events, next event duration: %.1f minutes",
duration.Minutes())
d := duration
defer j.wg.Done()
loop:
for {
select {
case <-time.After(d): // timeout
j.log.Debug("stop hashing")
j.proofer.StopHashing()
now := time.Now()
intf := j.calendar.PickNextStopEvent(now)
j.calendar.RescheduleStopEventsPrior(now)
if nil == intf {
intf = j.calendar.PickNextStopEvent(now)
}
nextEvent := intf.(time.Time)
d = j.timeDurationFromSrc2Dest(now, nextEvent)
j.log.Infof("next stop event: %s, duration: %.1f minutes",
nextEvent.String(), d.Minutes())
case <-j.channels.stopEventChannel:
j.log.Debug("reset event, terminate stop goroutine...")
break loop
}
}
j.log.Infof("stop event terminated...")
}
func (j *JobManagerData) resetAllEvent() {
j.log.Infof("reset all events...")
j.wg.Add(2)
j.channels.startEventChannel <- struct{}{}
j.channels.stopEventChannel <- struct{}{}
j.wg.Wait()
}
func (j *JobManagerData) Start() {
go j.waitForRefresh()
}
func (j *JobManagerData) reschedule() {
j.log.Debug("reschedule...")
j.rescheduleStartEvent()
j.rescheduleStopEvent()
}
func (j *JobManagerData) rescheduleStartEvent() {
now := time.Now()
intf := j.calendar.PickInitialiseStartEvent(now)
nextEvent := intf.(time.Time)
duration := nextEvent.Sub(now)
go j.waitNextHasingStartEvent(duration)
}
func (j *JobManagerData) rescheduleStopEvent() {
if j.calendar.RunForever() {
return
}
now := time.Now()
intf := j.calendar.PickInitialiseStopEvent(now)
nextEvent := intf.(time.Time)
duration := nextEvent.Sub(now)
go j.waitNextHasingStopEvent(duration)
}
func (j *JobManagerData) timeDurationFromSrc2Dest(src time.Time, dest time.Time) time.Duration {
return dest.Sub(src)
}