diff --git a/src/jobservice/job/workerpool.go b/src/jobservice/job/workerpool.go index 082abeef8..910c55f4d 100644 --- a/src/jobservice/job/workerpool.go +++ b/src/jobservice/job/workerpool.go @@ -16,6 +16,7 @@ package job import ( "fmt" + "sync" "github.com/vmware/harbor/src/common/models" "github.com/vmware/harbor/src/common/utils/log" @@ -33,6 +34,9 @@ type workerPool struct { // WorkerPools is a map contains workerpools for different types of jobs. var WorkerPools map[Type]*workerPool +// For WorkerPools initialization. +var once sync.Once + //TODO: remove the hard code? const maxScanWorker = 3 @@ -118,16 +122,15 @@ func NewWorker(id int, t Type, wp *workerPool) *Worker { // InitWorkerPools create worker pools for different types of jobs. func InitWorkerPools() error { - if len(WorkerPools) > 0 { - return fmt.Errorf("The WorkerPool map has been initialised") - } maxRepWorker, err := config.MaxJobWorkers() if err != nil { return err } - WorkerPools = make(map[Type]*workerPool) - WorkerPools[ReplicationType] = createWorkerPool(maxRepWorker, ReplicationType) - WorkerPools[ScanType] = createWorkerPool(maxScanWorker, ScanType) + once.Do(func() { + WorkerPools = make(map[Type]*workerPool) + WorkerPools[ReplicationType] = createWorkerPool(maxRepWorker, ReplicationType) + WorkerPools[ScanType] = createWorkerPool(maxScanWorker, ScanType) + }) return nil } diff --git a/src/jobservice/main.go b/src/jobservice/main.go index e62a7a0e6..e6e9c1546 100644 --- a/src/jobservice/main.go +++ b/src/jobservice/main.go @@ -42,7 +42,9 @@ func main() { } initRouters() - job.InitWorkerPools() + if err := job.InitWorkerPools(); err != nil { + log.Fatalf("Failed to initialize worker pools, error: %v", err) + } go job.Dispatch() resumeJobs() beego.Run() @@ -71,6 +73,8 @@ func init() { configPath := os.Getenv("CONFIG_PATH") if len(configPath) != 0 { log.Infof("Config path: %s", configPath) - beego.LoadAppConfig("ini", configPath) + if err := beego.LoadAppConfig("ini", configPath); err != nil { + log.Fatalf("Failed to load config file: %s, error: %v", configPath, err) + } } }