-progress cli param for bars
This commit is contained in:
@@ -3,8 +3,9 @@ package jobm
|
||||
import (
|
||||
"runtime"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"gitbase.de/apairon/mark2web/pkg/logger"
|
||||
"gitbase.de/apairon/mark2web/pkg/progress"
|
||||
)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
@@ -17,37 +18,40 @@ type Job struct {
|
||||
Category string
|
||||
}
|
||||
|
||||
var jobChan = make(chan Job)
|
||||
var jobChan = make(chan []Job)
|
||||
|
||||
func worker(jobChan <-chan Job) {
|
||||
func worker(jobChan <-chan []Job) {
|
||||
defer wg.Done()
|
||||
|
||||
for job := range jobChan {
|
||||
job.Function()
|
||||
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)
|
||||
//logger.Log.Infof("number of CPU core: %d", numCPU)
|
||||
// one core for main thread
|
||||
for i := 0; i < (numCPU - 1); i++ {
|
||||
for i := 0; i < numCPU; i++ {
|
||||
wg.Add(1)
|
||||
go worker(jobChan)
|
||||
}
|
||||
}
|
||||
|
||||
// Enqueue enqueues a job to the job queue
|
||||
func Enqueue(job Job) {
|
||||
if numCPU <= 1 {
|
||||
// no threading
|
||||
job.Function()
|
||||
return
|
||||
func Enqueue(jobs ...Job) {
|
||||
for _, job := range jobs {
|
||||
progress.IncrTotal(job.Category)
|
||||
}
|
||||
jobChan <- job
|
||||
jobChan <- jobs
|
||||
}
|
||||
|
||||
// Wait will wait for all jobs to finish
|
||||
func Wait() {
|
||||
time.Sleep(time.Millisecond * 300)
|
||||
close(jobChan)
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user