mark2web/pkg/jobm/jobmanager.go

63 lines
1.1 KiB
Go
Raw Normal View History

2019-03-25 10:16:33 +01:00
package jobm
import (
"runtime"
"sync"
2019-03-25 14:01:28 +01:00
"time"
2019-03-25 10:16:33 +01:00
2019-03-25 14:01:28 +01:00
"gitbase.de/apairon/mark2web/pkg/progress"
2019-03-25 10:16:33 +01:00
)
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
}
2019-03-25 14:01:28 +01:00
var jobChan = make(chan []Job)
2019-03-25 10:16:33 +01:00
2019-03-25 14:01:28 +01:00
func worker(jobChan <-chan []Job) {
2019-03-25 10:16:33 +01:00
defer wg.Done()
2019-03-25 14:01:28 +01:00
for jobs := range jobChan {
for _, job := range jobs {
progress.DescribeCurrent(job.Category, job.Description)
job.Function()
progress.IncrDone(job.Category)
}
2019-03-25 10:16:33 +01:00
}
}
func init() {
2019-03-25 14:01:28 +01:00
//logger.Log.Infof("number of CPU core: %d", numCPU)
2019-03-25 10:16:33 +01:00
// one core for main thread
2019-03-25 14:01:28 +01:00
for i := 0; i < numCPU; i++ {
2019-03-25 10:16:33 +01:00
wg.Add(1)
go worker(jobChan)
}
}
// Enqueue enqueues a job to the job queue
2019-03-25 14:01:28 +01:00
func Enqueue(jobs ...Job) {
for _, job := range jobs {
progress.IncrTotal(job.Category)
2019-03-25 10:16:33 +01:00
}
2019-03-25 14:01:28 +01:00
jobChan <- jobs
2019-03-25 10:16:33 +01:00
}
// Wait will wait for all jobs to finish
func Wait() {
close(jobChan)
2019-03-25 15:07:02 +01:00
time.Sleep(time.Millisecond * 500)
2019-03-25 10:16:33 +01:00
wg.Wait()
}
// SetNumCPU is for testing package without threading
func SetNumCPU(i int) {
numCPU = i
}