mark2web/pkg/jobm/jobmanager.go
Sebastian Frank 7695f42e20
All checks were successful
continuous-integration/drone/push Build is passing
started new logger output
2019-03-27 13:52:22 +01:00

62 lines
1.0 KiB
Go

package jobm
import (
"runtime"
"sync"
"gitbase.de/apairon/mark2web/pkg/progress"
)
var wg sync.WaitGroup
var numCPU = runtime.NumCPU()
// Job is a wrapper to descripe a Job function
type Job struct {
Function func()
Description string
Category string
}
var jobChan = make(chan []Job)
func worker(jobChan <-chan []Job) {
defer wg.Done()
for jobs := range jobChan {
for _, job := range jobs {
progress.DescribeCurrent(job.Category, job.Description)
job.Function()
progress.IncrDone(job.Category)
}
}
}
func init() {
//logger.Log.Infof("number of CPU core: %d", numCPU)
// one core for main thread
for i := 0; i < numCPU; i++ {
wg.Add(1)
go worker(jobChan)
}
}
// Enqueue enqueues a job to the job queue
func Enqueue(jobs ...Job) {
for _, job := range jobs {
progress.IncrTotal(job.Category)
}
jobChan <- jobs
}
// Wait will wait for all jobs to finish
func Wait() {
close(jobChan)
progress.Stop()
wg.Wait()
}
// SetNumCPU is for testing package without threading
func SetNumCPU(i int) {
numCPU = i
}