This repository has been archived by the owner on Jul 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
captain.go
177 lines (152 loc) · 4.4 KB
/
captain.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
176
177
// Package captain is used to run your job, and monitor during runtime
package captain
import (
"errors"
"sync"
"time"
)
// Config represents a configuration of a job.
// You can either create your own, or use CreateJob function which will initialize basic configuration.
type Config struct {
ResultProcessor ResultProcessor
RuntimeProcessor RuntimeProcessor
RuntimeProcessingFrequency time.Duration
LockProvider LockProvider
Worker Worker
SummaryBuffer int
}
// CommChan is a basic struct containing channels used for communication between your worker,
// runtime and result processor
type CommChan struct {
Logs chan string
Result chan string
}
// ResultProcessor is called after execution of worker.
type ResultProcessor func(results []string)
// RuntimeProcessor gets called every `RuntimeProcessingFrequency` duration.
type RuntimeProcessor func(tick time.Time, message string, startTime time.Time)
// Worker is called and is expected to do the real work.
type Worker func(channels CommChan)
// LockProvider is used if we need to make sure that two jobs aren't running at the same time.
type LockProvider interface {
Acquire() error
Release() error
}
// CreateJob creates a basic empty configuration with some defaults.
func CreateJob() Config {
return Config{
LockProvider: nil,
RuntimeProcessor: nil,
ResultProcessor: nil,
RuntimeProcessingFrequency: 200 * time.Millisecond,
SummaryBuffer: 1,
}
}
// WithLockProvider is used to set LockProvider.
func (config *Config) WithLockProvider(lockProvider LockProvider) {
config.LockProvider = lockProvider
}
// WithResultProcessor is used to set ResultProcessor.
func (config *Config) WithResultProcessor(processor ResultProcessor) {
config.ResultProcessor = processor
}
// WithRuntimeProcessingFrequency is used to set how frequently RuntimeProcessor is called.
func (config *Config) WithRuntimeProcessingFrequency(frequency time.Duration) {
config.RuntimeProcessingFrequency = frequency
}
// WithRuntimeProcessor is used to set the RuntimeProcessor.
func (config *Config) WithRuntimeProcessor(processor RuntimeProcessor) {
config.RuntimeProcessor = processor
}
// SetWorker is used to set Worker.
func (config *Config) SetWorker(worker Worker) {
config.Worker = worker
}
// Run starts the job
func (config *Config) Run() {
err := config.ensureLock()
if err != nil {
panic(err)
}
err = config.runWorker()
if err != nil {
panic(err)
}
}
func (config *Config) getCommunicationChannel() CommChan {
return CommChan{
Logs: make(chan string),
Result: make(chan string, config.SummaryBuffer),
}
}
func (config *Config) runWorker() error {
if config.Worker == nil {
return errors.New("worker not set")
}
channels := config.getCommunicationChannel()
wg := sync.WaitGroup{}
wg.Add(1)
go config.reportRuntimeProcessors(channels, time.Now())
go config.invokeWorker(channels, &wg)
wg.Wait()
return nil
}
func (config *Config) invokeWorker(commChan CommChan, group *sync.WaitGroup) {
defer group.Done()
defer commChan.close()
config.Worker(commChan)
summary := getSummary(commChan.Result)
config.invokeResultProcessor(summary)
}
func (config *Config) invokeResultProcessor(summary []string) {
if config.ResultProcessor == nil {
return
}
config.ResultProcessor(summary)
}
func (config *Config) reportRuntimeProcessors(commChan CommChan, startTime time.Time) {
ticker := time.NewTicker(config.RuntimeProcessingFrequency)
defer ticker.Stop()
for t := range ticker.C {
message := getCommunicationMessages(commChan)
if config.RuntimeProcessor == nil {
return
}
config.invokeRuntimeProcessor(t, message, startTime)
}
}
func (config *Config) invokeRuntimeProcessor(t time.Time, message string, startTime time.Time) {
config.RuntimeProcessor(t, message, startTime)
}
func (config *Config) ensureLock() error {
if config.LockProvider == nil {
return nil
}
return config.LockProvider.Acquire()
}
func getCommunicationMessages(ch CommChan) string {
return getString(ch.Logs)
}
func getSummary(ch chan string) []string {
var summary []string
for {
msg := getString(ch)
if msg == "" {
break
}
summary = append(summary, msg)
}
return summary
}
func getString(ch chan string) string {
select {
case msg := <-ch:
return msg
default:
return ""
}
}
func (channels *CommChan) close() {
close(channels.Logs)
close(channels.Result)
}