-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworker-pool.go
196 lines (177 loc) · 5.01 KB
/
worker-pool.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// MIT License
//
// (C) Copyright [2018, 2021] Hewlett Packard Enterprise Development LP
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
package base
import (
"log"
)
///////////////////////////////////////////////////////////////////////////////
// Job interface
///////////////////////////////////////////////////////////////////////////////
type JobType int
type JobStatus int
const (
JSTAT_DEFAULT JobStatus = 0
JSTAT_QUEUED JobStatus = 1
JSTAT_PROCESSING JobStatus = 2
JSTAT_COMPLETE JobStatus = 3
JSTAT_CANCELLED JobStatus = 4
JSTAT_ERROR JobStatus = 5
JSTAT_MAX JobStatus = 6
)
var JStatString = map[JobStatus]string{
JSTAT_DEFAULT: "JSTAT_DEFAULT",
JSTAT_QUEUED: "JSTAT_QUEUED",
JSTAT_PROCESSING: "JSTAT_PROCESSING",
JSTAT_COMPLETE: "JSTAT_COMPLETE",
JSTAT_CANCELLED: "JSTAT_CANCELLED",
JSTAT_ERROR: "JSTAT_ERROR",
JSTAT_MAX: "JSTAT_MAX",
}
type Job interface {
Log(format string, a ...interface{})
//New(t JobType)
Type() JobType
//JobSCN() JobSCN
Run()
GetStatus() (JobStatus, error)
SetStatus(JobStatus, error) (JobStatus, error)
Cancel() JobStatus
}
///////////////////////////////////////////////////////////////////////////////
// Workers
///////////////////////////////////////////////////////////////////////////////
type Worker struct {
WorkerPool chan chan Job
JobChannel chan Job
StopChannel chan bool
}
// Create a new worker
func NewWorker(workerPool chan chan Job) Worker {
jChan := make(chan Job)
stopChan := make(chan bool)
return Worker{workerPool, jChan, stopChan}
}
// Start a worker to start consuming Jobs
func (w Worker) Start() {
go func() {
for {
// Tell the dispatcher that this worker is available
w.WorkerPool <- w.JobChannel
select {
case <-w.StopChannel:
// Received a stop signal
log.Print("Worker Stopping")
return
case job := <-w.JobChannel:
// Received a job!
job.Run()
if status, _ := job.GetStatus(); status != JSTAT_ERROR {
job.SetStatus(JSTAT_COMPLETE, nil)
}
}
}
}()
}
// Send as stop signal to the worker
func (w Worker) Stop() {
go func() {
w.StopChannel <- true
}()
}
///////////////////////////////////////////////////////////////////////////////
// WorkerPool
///////////////////////////////////////////////////////////////////////////////
type WorkerPool struct {
Workers []Worker
Pool chan chan Job
JobQueue chan Job
StopChannel chan bool
}
// Create a new pool of workers
func NewWorkerPool(maxWorkers, maxJobQueue int) *WorkerPool {
workers := make([]Worker, maxWorkers)
pool := make(chan chan Job, maxWorkers)
jobQueue := make(chan Job, maxJobQueue)
stopChan := make(chan bool)
return &WorkerPool{workers, pool, jobQueue, stopChan}
}
// Starts all of the workers and the job dispatcher
func (p *WorkerPool) Run() {
// Start the workers
for i, _ := range p.Workers {
worker := NewWorker(p.Pool)
worker.Start()
p.Workers[i] = worker
}
go p.dispatch()
}
// Hands out jobs to available workers
func (p *WorkerPool) dispatch() {
for {
// Wait for a job or a stop signal
select {
case <-p.StopChannel:
break
case job := <-p.JobQueue:
// Wait for a free worker or a stop signal
select {
case <-p.StopChannel:
break
case jobChannel := <-p.Pool:
if status, _ := job.GetStatus(); status != JSTAT_CANCELLED {
job.SetStatus(JSTAT_PROCESSING, nil)
// Send the job to the worker
jobChannel <- job
}
}
}
}
log.Print("Stopping Workers")
// Send a stop signal to all of the workers
for _, worker := range p.Workers {
worker.Stop()
}
log.Print("Stopping Dispatcher")
}
// Queue a job. Returns 1 if the operation would
// block because the work queue is full.
func (p *WorkerPool) Queue(job Job) int {
if job == nil {
//Error
return -1
}
select {
case p.JobQueue <- job:
job.SetStatus(JSTAT_QUEUED, nil)
//Job queued
default:
//WOULDBLOCK
return 1
}
return 0
}
// Command the dispatcher to stop itself and all workers
func (p *WorkerPool) Stop() {
go func() {
p.StopChannel <- true
}()
}