2014-10-31 23:18:38 +00:00
|
|
|
package worker
|
|
|
|
|
2018-01-09 09:16:33 +00:00
|
|
|
import (
|
2023-10-03 18:02:57 +00:00
|
|
|
"log/slog"
|
2018-01-09 09:16:33 +00:00
|
|
|
)
|
2014-10-31 23:18:38 +00:00
|
|
|
|
2020-02-01 08:48:43 +00:00
|
|
|
// WorkerQueue is the global queue of Workers
|
2022-05-26 07:05:49 +00:00
|
|
|
var WorkerQueue chan chan Work
|
2018-12-24 13:40:00 +00:00
|
|
|
|
|
|
|
// WorkQueue is the global queue of work to dispatch
|
2022-05-26 07:05:49 +00:00
|
|
|
var WorkQueue = make(chan Work, 100)
|
2014-10-31 23:18:38 +00:00
|
|
|
|
2018-01-02 16:11:59 +00:00
|
|
|
// StartDispatcher is charged to start n workers.
|
2014-10-31 23:18:38 +00:00
|
|
|
func StartDispatcher(nworkers int) {
|
|
|
|
// First, initialize the channel we are going to but the workers' work channels into.
|
2022-05-26 07:05:49 +00:00
|
|
|
WorkerQueue = make(chan chan Work, nworkers)
|
2014-10-31 23:18:38 +00:00
|
|
|
|
|
|
|
// Now, create all of our workers.
|
|
|
|
for i := 0; i < nworkers; i++ {
|
2023-10-03 18:02:57 +00:00
|
|
|
slog.Debug("starting worker...", "worker", i+1)
|
2014-10-31 23:18:38 +00:00
|
|
|
worker := NewWorker(i+1, WorkerQueue)
|
|
|
|
worker.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
2023-10-04 19:20:54 +00:00
|
|
|
work := <-WorkQueue
|
|
|
|
go func() {
|
|
|
|
worker := <-WorkerQueue
|
|
|
|
slog.Debug("dispatching hook request", "hook", work.Name(), "id", work.ID())
|
|
|
|
worker <- work
|
|
|
|
}()
|
2014-10-31 23:18:38 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|