Merge pull request #2866 from reasonerjt/master

fix #1953, fail jobservice when configuration init encounters errors.
This commit is contained in:
Daniel Jiang 2017-07-26 10:58:04 +03:00 committed by GitHub
commit 9d02fc1c70
2 changed files with 15 additions and 8 deletions

View File

@ -16,6 +16,7 @@ package job
import ( import (
"fmt" "fmt"
"sync"
"github.com/vmware/harbor/src/common/models" "github.com/vmware/harbor/src/common/models"
"github.com/vmware/harbor/src/common/utils/log" "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. // WorkerPools is a map contains workerpools for different types of jobs.
var WorkerPools map[Type]*workerPool var WorkerPools map[Type]*workerPool
// For WorkerPools initialization.
var once sync.Once
//TODO: remove the hard code? //TODO: remove the hard code?
const maxScanWorker = 3 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. // InitWorkerPools create worker pools for different types of jobs.
func InitWorkerPools() error { func InitWorkerPools() error {
if len(WorkerPools) > 0 {
return fmt.Errorf("The WorkerPool map has been initialised")
}
maxRepWorker, err := config.MaxJobWorkers() maxRepWorker, err := config.MaxJobWorkers()
if err != nil { if err != nil {
return err return err
} }
once.Do(func() {
WorkerPools = make(map[Type]*workerPool) WorkerPools = make(map[Type]*workerPool)
WorkerPools[ReplicationType] = createWorkerPool(maxRepWorker, ReplicationType) WorkerPools[ReplicationType] = createWorkerPool(maxRepWorker, ReplicationType)
WorkerPools[ScanType] = createWorkerPool(maxScanWorker, ScanType) WorkerPools[ScanType] = createWorkerPool(maxScanWorker, ScanType)
})
return nil return nil
} }

View File

@ -42,7 +42,9 @@ func main() {
} }
initRouters() initRouters()
job.InitWorkerPools() if err := job.InitWorkerPools(); err != nil {
log.Fatalf("Failed to initialize worker pools, error: %v", err)
}
go job.Dispatch() go job.Dispatch()
resumeJobs() resumeJobs()
beego.Run() beego.Run()
@ -71,6 +73,8 @@ func init() {
configPath := os.Getenv("CONFIG_PATH") configPath := os.Getenv("CONFIG_PATH")
if len(configPath) != 0 { if len(configPath) != 0 {
log.Infof("Config path: %s", configPath) 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)
}
} }
} }