62 lines
1.0 KiB
Go
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
|
|
}
|