Replace log in commmon package to logger in logger package for loose couple

This commit is contained in:
Steven Zou 2018-03-26 15:30:16 +08:00
parent ba85378ac3
commit 5e2543833b
13 changed files with 256 additions and 65 deletions

View File

@ -9,9 +9,9 @@ import (
"net/http" "net/http"
"time" "time"
"github.com/vmware/harbor/src/common/utils/log"
"github.com/vmware/harbor/src/jobservice_v2/config" "github.com/vmware/harbor/src/jobservice_v2/config"
"github.com/vmware/harbor/src/jobservice_v2/env" "github.com/vmware/harbor/src/jobservice_v2/env"
"github.com/vmware/harbor/src/jobservice_v2/logger"
) )
//Server serves the http requests. //Server serves the http requests.
@ -91,7 +91,7 @@ func (s *Server) Start() {
var err error var err error
defer func() { defer func() {
s.context.WG.Done() s.context.WG.Done()
log.Infof("API server is gracefully shutdown") logger.Infof("API server is gracefully shutdown")
}() }()
if s.config.Protocol == config.JobServiceProtocolHTTPS { if s.config.Protocol == config.JobServiceProtocolHTTPS {
@ -110,13 +110,13 @@ func (s *Server) Start() {
func (s *Server) Stop() { func (s *Server) Stop() {
go func() { go func() {
defer func() { defer func() {
log.Info("Stop API server done!") logger.Info("Stop API server done!")
}() }()
shutDownCtx, cancel := context.WithTimeout(s.context.SystemContext, 10*time.Second) shutDownCtx, cancel := context.WithTimeout(s.context.SystemContext, 10*time.Second)
defer cancel() defer cancel()
if err := s.httpServer.Shutdown(shutDownCtx); err != nil { if err := s.httpServer.Shutdown(shutDownCtx); err != nil {
log.Errorf("Shutdown API server failed with error: %s\n", err) logger.Errorf("Shutdown API server failed with error: %s\n", err)
} }
}() }()
} }

View File

@ -5,7 +5,6 @@ import (
"strings" "strings"
"github.com/vmware/harbor/src/common/utils/log" "github.com/vmware/harbor/src/common/utils/log"
"github.com/vmware/harbor/src/jobservice_v2/logger"
) )
//JobLogger is an implementation of logger.Interface. //JobLogger is an implementation of logger.Interface.
@ -17,7 +16,7 @@ type JobLogger struct {
//New logger //New logger
//nil might be returned //nil might be returned
func New(logPath string, level string) logger.Interface { func New(logPath string, level string) *JobLogger {
f, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY, 0644) f, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil { if err != nil {
return nil return nil

View File

@ -0,0 +1,74 @@
package logger
import (
"os"
"github.com/vmware/harbor/src/common/utils/log"
)
//ServiceLogger is an implementation of logger.Interface.
//It used to log info in workerpool components.
type ServiceLogger struct {
backendLogger *log.Logger
}
//NewServiceLogger to create new logger for job service
//nil might be returned
func NewServiceLogger(level string) *ServiceLogger {
logLevel := parseLevel(level)
backendLogger := log.New(os.Stdout, log.NewTextFormatter(), logLevel)
return &ServiceLogger{
backendLogger: backendLogger,
}
}
//Debug ...
func (sl *ServiceLogger) Debug(v ...interface{}) {
sl.backendLogger.Debug(v...)
}
//Debugf with format
func (sl *ServiceLogger) Debugf(format string, v ...interface{}) {
sl.backendLogger.Debugf(format, v...)
}
//Info ...
func (sl *ServiceLogger) Info(v ...interface{}) {
sl.backendLogger.Info(v...)
}
//Infof with format
func (sl *ServiceLogger) Infof(format string, v ...interface{}) {
sl.backendLogger.Infof(format, v...)
}
//Warning ...
func (sl *ServiceLogger) Warning(v ...interface{}) {
sl.backendLogger.Warning(v...)
}
//Warningf with format
func (sl *ServiceLogger) Warningf(format string, v ...interface{}) {
sl.backendLogger.Warningf(format, v...)
}
//Error ...
func (sl *ServiceLogger) Error(v ...interface{}) {
sl.backendLogger.Error(v...)
}
//Errorf with format
func (sl *ServiceLogger) Errorf(format string, v ...interface{}) {
sl.backendLogger.Errorf(format, v...)
}
//Fatal error
func (sl *ServiceLogger) Fatal(v ...interface{}) {
sl.backendLogger.Fatal(v...)
}
//Fatalf error
func (sl *ServiceLogger) Fatalf(format string, v ...interface{}) {
sl.backendLogger.Fatalf(format, v...)
}

View File

@ -0,0 +1,115 @@
// Copyright 2018 The Harbor Authors. All rights reserved.
package logger
import (
"log"
)
//sLogger is used to log for workerpool itself
var sLogger Interface
//SetLogger sets the logger implementation
func SetLogger(logger Interface) {
sLogger = logger
}
//Debug ...
func Debug(v ...interface{}) {
if sLogger != nil {
sLogger.Debug(v...)
return
}
log.Println(v...)
}
//Debugf for debuging with format
func Debugf(format string, v ...interface{}) {
if sLogger != nil {
sLogger.Debugf(format, v...)
return
}
log.Printf(format, v...)
}
//Info ...
func Info(v ...interface{}) {
if sLogger != nil {
sLogger.Info(v...)
return
}
log.Println(v...)
}
//Infof for logging info with format
func Infof(format string, v ...interface{}) {
if sLogger != nil {
sLogger.Infof(format, v...)
return
}
log.Printf(format, v...)
}
//Warning ...
func Warning(v ...interface{}) {
if sLogger != nil {
sLogger.Warning(v...)
return
}
log.Println(v...)
}
//Warningf for warning with format
func Warningf(format string, v ...interface{}) {
if sLogger != nil {
sLogger.Warningf(format, v...)
return
}
log.Printf(format, v...)
}
//Error for logging error
func Error(v ...interface{}) {
if sLogger != nil {
sLogger.Error(v...)
return
}
log.Println(v...)
}
//Errorf for logging error with format
func Errorf(format string, v ...interface{}) {
if sLogger != nil {
sLogger.Errorf(format, v...)
return
}
log.Printf(format, v...)
}
//Fatal ...
func Fatal(v ...interface{}) {
if sLogger != nil {
sLogger.Fatal(v...)
return
}
log.Fatal(v...)
}
//Fatalf for fatal error with error
func Fatalf(format string, v ...interface{}) {
if sLogger != nil {
sLogger.Fatalf(format, v...)
return
}
log.Fatalf(format, v...)
}

View File

@ -8,8 +8,6 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"time" "time"
"github.com/vmware/harbor/src/common/utils/log"
) )
const ( const (
@ -31,7 +29,7 @@ func NewSweeper(ctx context.Context, workDir string, period uint) *Sweeper {
//Start to work //Start to work
func (s *Sweeper) Start() { func (s *Sweeper) Start() {
go s.loop() go s.loop()
log.Info("Logger sweeper is started") Info("Logger sweeper is started")
} }
func (s *Sweeper) loop() { func (s *Sweeper) loop() {
@ -41,7 +39,7 @@ func (s *Sweeper) loop() {
} }
defer func() { defer func() {
log.Info("Logger sweeper is stopped") Info("Logger sweeper is stopped")
}() }()
//First run //First run
@ -66,14 +64,14 @@ func (s *Sweeper) clear() {
count = &cleared count = &cleared
) )
log.Info("Start to clear the job outdated log files") Info("Start to clear the job outdated log files")
defer func() { defer func() {
log.Infof("%d job outdated log files cleared", *count) Infof("%d job outdated log files cleared", *count)
}() }()
logFiles, err := ioutil.ReadDir(s.workDir) logFiles, err := ioutil.ReadDir(s.workDir)
if err != nil { if err != nil {
log.Errorf("Failed to get the outdated log files under '%s' with error: %s\n", s.workDir, err) Errorf("Failed to get the outdated log files under '%s' with error: %s\n", s.workDir, err)
return return
} }
if len(logFiles) == 0 { if len(logFiles) == 0 {
@ -86,7 +84,7 @@ func (s *Sweeper) clear() {
if err := os.Remove(logFilePath); err == nil { if err := os.Remove(logFilePath); err == nil {
cleared++ cleared++
} else { } else {
log.Warningf("Failed to remove log file '%s'\n", logFilePath) Warningf("Failed to remove log file '%s'\n", logFilePath)
} }
} }
} }

View File

@ -9,6 +9,8 @@ import (
"github.com/vmware/harbor/src/jobservice_v2/config" "github.com/vmware/harbor/src/jobservice_v2/config"
"github.com/vmware/harbor/src/jobservice_v2/env" "github.com/vmware/harbor/src/jobservice_v2/env"
"github.com/vmware/harbor/src/jobservice_v2/job/impl" "github.com/vmware/harbor/src/jobservice_v2/job/impl"
ilogger "github.com/vmware/harbor/src/jobservice_v2/job/impl/logger"
"github.com/vmware/harbor/src/jobservice_v2/logger"
"github.com/vmware/harbor/src/jobservice_v2/runtime" "github.com/vmware/harbor/src/jobservice_v2/runtime"
"github.com/vmware/harbor/src/jobservice_v2/utils" "github.com/vmware/harbor/src/jobservice_v2/utils"
) )
@ -25,6 +27,12 @@ func main() {
return return
} }
//Load configurations
if err := config.DefaultConfig.Load(*configPath, true); err != nil {
fmt.Printf("Failed to load configurations with error: %s\n", err)
return
}
//Set job context initializer //Set job context initializer
runtime.JobService.SetJobContextInitializer(func(ctx *env.Context) (env.JobContext, error) { runtime.JobService.SetJobContextInitializer(func(ctx *env.Context) (env.JobContext, error) {
secret := config.GetAuthSecret() secret := config.GetAuthSecret()
@ -42,6 +50,10 @@ func main() {
return jobCtx, nil return jobCtx, nil
}) })
//New logger for job service
sLogger := ilogger.NewServiceLogger(config.GetLogLevel())
logger.SetLogger(sLogger)
//Start //Start
runtime.JobService.LoadAndRun(*configPath, true) runtime.JobService.LoadAndRun()
} }

View File

@ -13,6 +13,7 @@ import (
"time" "time"
"github.com/vmware/harbor/src/jobservice_v2/errs" "github.com/vmware/harbor/src/jobservice_v2/errs"
"github.com/vmware/harbor/src/jobservice_v2/logger"
"github.com/vmware/harbor/src/jobservice_v2/period" "github.com/vmware/harbor/src/jobservice_v2/period"
@ -21,7 +22,6 @@ import (
"github.com/gocraft/work" "github.com/gocraft/work"
"github.com/garyburd/redigo/redis" "github.com/garyburd/redigo/redis"
"github.com/vmware/harbor/src/common/utils/log"
"github.com/vmware/harbor/src/jobservice_v2/job" "github.com/vmware/harbor/src/jobservice_v2/job"
"github.com/vmware/harbor/src/jobservice_v2/models" "github.com/vmware/harbor/src/jobservice_v2/models"
"github.com/vmware/harbor/src/jobservice_v2/utils" "github.com/vmware/harbor/src/jobservice_v2/utils"
@ -94,7 +94,7 @@ func (rjs *RedisJobStatsManager) Start() {
go rjs.loop() go rjs.loop()
rjs.isRunning = true rjs.isRunning = true
log.Info("Redis job stats manager is started") logger.Info("Redis job stats manager is started")
} }
//Shutdown is implementation of same method in JobStatsManager interface. //Shutdown is implementation of same method in JobStatsManager interface.
@ -152,7 +152,7 @@ func (rjs *RedisJobStatsManager) loop() {
rjs.isRunning = false rjs.isRunning = false
//Notify other sub goroutines //Notify other sub goroutines
close(controlChan) close(controlChan)
log.Info("Redis job stats manager is stopped") logger.Info("Redis job stats manager is stopped")
}() }()
for { for {
@ -176,7 +176,7 @@ func (rjs *RedisJobStatsManager) loop() {
} }
}() }()
} else { } else {
log.Warningf("Failed to process '%s' request with error: %s (%d times tried)\n", item.op, err, maxFails) logger.Warningf("Failed to process '%s' request with error: %s (%d times tried)\n", item.op, err, maxFails)
if item.op == opReportStatus { if item.op == opReportStatus {
clearHookCache = true clearHookCache = true
} }
@ -238,7 +238,7 @@ func (rjs *RedisJobStatsManager) Stop(jobID string) error {
//thirdly expire the job stats of this periodic job if exists //thirdly expire the job stats of this periodic job if exists
if err := rjs.expirePeriodicJobStats(theJob.Stats.JobID); err != nil { if err := rjs.expirePeriodicJobStats(theJob.Stats.JobID); err != nil {
//only logged //only logged
log.Errorf("Expire the stats of job %s failed with error: %s\n", theJob.Stats.JobID, err) logger.Errorf("Expire the stats of job %s failed with error: %s\n", theJob.Stats.JobID, err)
} }
default: default:
break break
@ -378,7 +378,7 @@ func (rjs *RedisJobStatsManager) submitStatusReportingItem(jobID string, status,
hookURL, err = rjs.getHook(jobID) hookURL, err = rjs.getHook(jobID)
if err != nil { if err != nil {
//logged and exit //logged and exit
log.Warningf("no status hook found for job %s\n, abandon status reporting", jobID) logger.Warningf("no status hook found for job %s\n, abandon status reporting", jobID)
return return
} }
} }
@ -418,7 +418,7 @@ func (rjs *RedisJobStatsManager) expirePeriodicJobStats(jobID string) error {
func (rjs *RedisJobStatsManager) deleteScheduledJobsOfPeriodicPolicy(policyID string, cronSpec string) error { func (rjs *RedisJobStatsManager) deleteScheduledJobsOfPeriodicPolicy(policyID string, cronSpec string) error {
schedule, err := cron.Parse(cronSpec) schedule, err := cron.Parse(cronSpec)
if err != nil { if err != nil {
log.Errorf("cron spec '%s' is not valid", cronSpec) logger.Errorf("cron spec '%s' is not valid", cronSpec)
return err return err
} }
@ -432,7 +432,7 @@ func (rjs *RedisJobStatsManager) deleteScheduledJobsOfPeriodicPolicy(policyID st
epoch := t.Unix() epoch := t.Unix()
if err = rjs.client.DeleteScheduledJob(epoch, policyID); err != nil { if err = rjs.client.DeleteScheduledJob(epoch, policyID); err != nil {
//only logged //only logged
log.Warningf("delete scheduled instance for periodic job %s failed with error: %s\n", policyID, err) logger.Warningf("delete scheduled instance for periodic job %s failed with error: %s\n", policyID, err)
} }
} }
@ -453,7 +453,7 @@ func (rjs *RedisJobStatsManager) getCrlCommand(jobID string) (string, error) {
_, err = conn.Do("DEL", key) _, err = conn.Do("DEL", key)
if err != nil { if err != nil {
//only logged //only logged
log.Errorf("del key %s failed with error: %s\n", key, err) logger.Errorf("del key %s failed with error: %s\n", key, err)
} }
return cmd, nil return cmd, nil

View File

@ -9,8 +9,8 @@ import (
"github.com/garyburd/redigo/redis" "github.com/garyburd/redigo/redis"
"github.com/gocraft/work" "github.com/gocraft/work"
"github.com/robfig/cron" "github.com/robfig/cron"
"github.com/vmware/harbor/src/common/utils/log"
"github.com/vmware/harbor/src/jobservice_v2/job" "github.com/vmware/harbor/src/jobservice_v2/job"
"github.com/vmware/harbor/src/jobservice_v2/logger"
"github.com/vmware/harbor/src/jobservice_v2/utils" "github.com/vmware/harbor/src/jobservice_v2/utils"
) )
@ -52,7 +52,7 @@ func newPeriodicEnqueuer(namespace string, pool *redis.Pool, policyStore *period
func (pe *periodicEnqueuer) start() { func (pe *periodicEnqueuer) start() {
go pe.loop() go pe.loop()
log.Info("Periodic enqueuer is started") logger.Info("Periodic enqueuer is started")
} }
func (pe *periodicEnqueuer) stop() { func (pe *periodicEnqueuer) stop() {
@ -62,7 +62,7 @@ func (pe *periodicEnqueuer) stop() {
func (pe *periodicEnqueuer) loop() { func (pe *periodicEnqueuer) loop() {
defer func() { defer func() {
log.Info("Periodic enqueuer is stopped") logger.Info("Periodic enqueuer is stopped")
}() }()
// Begin reaping periodically // Begin reaping periodically
timer := time.NewTimer(periodicEnqueuerSleep + time.Duration(rand.Intn(30))*time.Second) timer := time.NewTimer(periodicEnqueuerSleep + time.Duration(rand.Intn(30))*time.Second)
@ -71,7 +71,7 @@ func (pe *periodicEnqueuer) loop() {
if pe.shouldEnqueue() { if pe.shouldEnqueue() {
err := pe.enqueue() err := pe.enqueue()
if err != nil { if err != nil {
log.Errorf("periodic_enqueuer.loop.enqueue:%s\n", err) logger.Errorf("periodic_enqueuer.loop.enqueue:%s\n", err)
} }
} }
@ -85,7 +85,7 @@ func (pe *periodicEnqueuer) loop() {
if pe.shouldEnqueue() { if pe.shouldEnqueue() {
err := pe.enqueue() err := pe.enqueue()
if err != nil { if err != nil {
log.Errorf("periodic_enqueuer.loop.enqueue:%s\n", err) logger.Errorf("periodic_enqueuer.loop.enqueue:%s\n", err)
} }
} }
} }
@ -133,7 +133,7 @@ func (pe *periodicEnqueuer) enqueue() error {
return err return err
} }
log.Infof("Schedule job %s for policy %s at %d\n", pj.jobName, pl.PolicyID, epoch) logger.Infof("Schedule job %s for policy %s at %d\n", pj.jobName, pl.PolicyID, epoch)
} }
//Directly use redis conn to update the periodic job (policy) status //Directly use redis conn to update the periodic job (policy) status
//Do not care the result //Do not care the result
@ -153,7 +153,7 @@ func (pe *periodicEnqueuer) shouldEnqueue() bool {
if err == redis.ErrNil { if err == redis.ErrNil {
return true return true
} else if err != nil { } else if err != nil {
log.Errorf("periodic_enqueuer.should_enqueue:%s\n", err) logger.Errorf("periodic_enqueuer.should_enqueue:%s\n", err)
return true return true
} }

View File

@ -12,8 +12,8 @@ import (
"github.com/robfig/cron" "github.com/robfig/cron"
"github.com/garyburd/redigo/redis" "github.com/garyburd/redigo/redis"
"github.com/vmware/harbor/src/common/utils/log"
"github.com/vmware/harbor/src/jobservice_v2/env" "github.com/vmware/harbor/src/jobservice_v2/env"
"github.com/vmware/harbor/src/jobservice_v2/logger"
"github.com/vmware/harbor/src/jobservice_v2/models" "github.com/vmware/harbor/src/jobservice_v2/models"
"github.com/vmware/harbor/src/jobservice_v2/utils" "github.com/vmware/harbor/src/jobservice_v2/utils"
) )
@ -54,7 +54,7 @@ func NewRedisPeriodicScheduler(ctx *env.Context, namespace string, redisPool *re
//Start to serve //Start to serve
func (rps *RedisPeriodicScheduler) Start() { func (rps *RedisPeriodicScheduler) Start() {
defer func() { defer func() {
log.Info("Redis scheduler is stopped") logger.Info("Redis scheduler is stopped")
}() }()
//Load existing periodic job policies //Load existing periodic job policies
@ -67,7 +67,7 @@ func (rps *RedisPeriodicScheduler) Start() {
//start enqueuer //start enqueuer
rps.enqueuer.start() rps.enqueuer.start()
defer rps.enqueuer.stop() defer rps.enqueuer.stop()
log.Info("Redis scheduler is started") logger.Info("Redis scheduler is started")
//blocking here //blocking here
<-rps.context.SystemContext.Done() <-rps.context.SystemContext.Done()
@ -234,14 +234,14 @@ func (rps *RedisPeriodicScheduler) Load() error {
if err := policy.DeSerialize(rawPolicy); err != nil { if err := policy.DeSerialize(rawPolicy); err != nil {
//Ignore error which means the policy data is not valid //Ignore error which means the policy data is not valid
//Only logged //Only logged
log.Warningf("failed to deserialize periodic policy with error:%s; raw data: %s\n", err, rawPolicy) logger.Warningf("failed to deserialize periodic policy with error:%s; raw data: %s\n", err, rawPolicy)
continue continue
} }
score, err := strconv.ParseInt(string(rawScore), 10, 64) score, err := strconv.ParseInt(string(rawScore), 10, 64)
if err != nil { if err != nil {
//Ignore error which means the policy data is not valid //Ignore error which means the policy data is not valid
//Only logged //Only logged
log.Warningf("failed to parse the score of the periodic policy with error:%s\n", err) logger.Warningf("failed to parse the score of the periodic policy with error:%s\n", err)
continue continue
} }
@ -261,7 +261,7 @@ func (rps *RedisPeriodicScheduler) Load() error {
rps.pstore.addAll(allPeriodicPolicies) rps.pstore.addAll(allPeriodicPolicies)
} }
log.Infof("Load %d periodic job policies", len(allPeriodicPolicies)) logger.Infof("Load %d periodic job policies", len(allPeriodicPolicies))
return nil return nil
} }

View File

@ -9,7 +9,7 @@ import (
"github.com/gocraft/work" "github.com/gocraft/work"
"github.com/garyburd/redigo/redis" "github.com/garyburd/redigo/redis"
"github.com/vmware/harbor/src/common/utils/log" "github.com/vmware/harbor/src/jobservice_v2/logger"
"github.com/vmware/harbor/src/jobservice_v2/utils" "github.com/vmware/harbor/src/jobservice_v2/utils"
) )
@ -49,7 +49,7 @@ func (s *Sweeper) ClearOutdatedScheduledJobs() error {
if r == nil { if r == nil {
//Action is already locked by other workerpool //Action is already locked by other workerpool
log.Info("Ignore clear outdated scheduled jobs") logger.Info("Ignore clear outdated scheduled jobs")
return nil return nil
} }
@ -71,7 +71,7 @@ func (s *Sweeper) ClearOutdatedScheduledJobs() error {
allErrors = append(allErrors, err) allErrors = append(allErrors, err)
} }
log.Infof("Clear outdated scheduled job: %s run at %#v\n", j.ID, time.Unix(jobScore.Score, 0).String()) logger.Infof("Clear outdated scheduled job: %s run at %#v\n", j.ID, time.Unix(jobScore.Score, 0).String())
} }
//Unlock //Unlock

View File

@ -9,11 +9,11 @@ import (
"reflect" "reflect"
"time" "time"
"github.com/vmware/harbor/src/jobservice_v2/logger"
"github.com/vmware/harbor/src/jobservice_v2/opm" "github.com/vmware/harbor/src/jobservice_v2/opm"
"github.com/vmware/harbor/src/jobservice_v2/period" "github.com/vmware/harbor/src/jobservice_v2/period"
"github.com/garyburd/redigo/redis" "github.com/garyburd/redigo/redis"
"github.com/vmware/harbor/src/common/utils/log"
"github.com/vmware/harbor/src/jobservice_v2/models" "github.com/vmware/harbor/src/jobservice_v2/models"
"github.com/vmware/harbor/src/jobservice_v2/utils" "github.com/vmware/harbor/src/jobservice_v2/utils"
) )
@ -39,7 +39,7 @@ func NewMessageServer(ctx context.Context, namespace string, redisPool *redis.Po
//Start to serve //Start to serve
func (ms *MessageServer) Start() error { func (ms *MessageServer) Start() error {
defer func() { defer func() {
log.Info("Message server is stopped") logger.Info("Message server is stopped")
}() }()
//As we get one connection from the pool, don't try to close it. //As we get one connection from the pool, don't try to close it.
@ -66,11 +66,11 @@ func (ms *MessageServer) Start() error {
m := &models.Message{} m := &models.Message{}
if err := json.Unmarshal(res.Data, m); err != nil { if err := json.Unmarshal(res.Data, m); err != nil {
//logged //logged
log.Warningf("read invalid message: %s\n", res.Data) logger.Warningf("read invalid message: %s\n", res.Data)
} }
if callback, ok := ms.callbacks[m.Event]; !ok { if callback, ok := ms.callbacks[m.Event]; !ok {
//logged //logged
log.Warningf("no handler to handle event %s\n", m.Event) logger.Warningf("no handler to handle event %s\n", m.Event)
} else { } else {
//Try to recover the concrete type //Try to recover the concrete type
var converted interface{} var converted interface{}
@ -95,17 +95,17 @@ func (ms *MessageServer) Start() error {
if e != nil { if e != nil {
err := e.(error) err := e.(error)
//logged //logged
log.Errorf("failed to fire callback with error: %s\n", err) logger.Errorf("failed to fire callback with error: %s\n", err)
} }
} }
case redis.Subscription: case redis.Subscription:
switch res.Kind { switch res.Kind {
case "subscribe": case "subscribe":
log.Infof("Subscribe redis channel %s\n", res.Channel) logger.Infof("Subscribe redis channel %s\n", res.Channel)
break break
case "unsubscribe": case "unsubscribe":
//Unsubscribe all, means main goroutine is exiting //Unsubscribe all, means main goroutine is exiting
log.Infof("Unsubscribe redis channel %s\n", res.Channel) logger.Infof("Unsubscribe redis channel %s\n", res.Channel)
done <- nil done <- nil
return return
} }
@ -113,7 +113,7 @@ func (ms *MessageServer) Start() error {
} }
}() }()
log.Info("Message server is started") logger.Info("Message server is started")
ticker := time.NewTicker(time.Minute) ticker := time.NewTicker(time.Minute)
defer ticker.Stop() defer ticker.Stop()

View File

@ -9,9 +9,9 @@ import (
"github.com/garyburd/redigo/redis" "github.com/garyburd/redigo/redis"
"github.com/gocraft/work" "github.com/gocraft/work"
"github.com/vmware/harbor/src/common/utils/log"
"github.com/vmware/harbor/src/jobservice_v2/env" "github.com/vmware/harbor/src/jobservice_v2/env"
"github.com/vmware/harbor/src/jobservice_v2/job" "github.com/vmware/harbor/src/jobservice_v2/job"
"github.com/vmware/harbor/src/jobservice_v2/logger"
"github.com/vmware/harbor/src/jobservice_v2/models" "github.com/vmware/harbor/src/jobservice_v2/models"
"github.com/vmware/harbor/src/jobservice_v2/opm" "github.com/vmware/harbor/src/jobservice_v2/opm"
"github.com/vmware/harbor/src/jobservice_v2/period" "github.com/vmware/harbor/src/jobservice_v2/period"
@ -170,20 +170,20 @@ func (gcwp *GoCraftWorkPool) Start() {
go func() { go func() {
defer func() { defer func() {
gcwp.context.WG.Done() gcwp.context.WG.Done()
log.Infof("Redis worker pool is stopped") logger.Infof("Redis worker pool is stopped")
}() }()
//Clear dirty data before pool starting //Clear dirty data before pool starting
if err := gcwp.sweeper.ClearOutdatedScheduledJobs(); err != nil { if err := gcwp.sweeper.ClearOutdatedScheduledJobs(); err != nil {
//Only logged //Only logged
log.Errorf("Clear outdated data before pool starting failed with error:%s\n", err) logger.Errorf("Clear outdated data before pool starting failed with error:%s\n", err)
} }
//Append middlewares //Append middlewares
gcwp.pool.Middleware((*RedisPoolContext).logJob) gcwp.pool.Middleware((*RedisPoolContext).logJob)
gcwp.pool.Start() gcwp.pool.Start()
log.Infof("Redis worker pool is started") logger.Infof("Redis worker pool is started")
//Block on listening context and done signal //Block on listening context and done signal
select { select {
@ -467,7 +467,7 @@ func (gcwp *GoCraftWorkPool) handleRegisterStatusHook(data interface{}) error {
//log the job //log the job
func (rpc *RedisPoolContext) logJob(job *work.Job, next work.NextMiddlewareFunc) error { func (rpc *RedisPoolContext) logJob(job *work.Job, next work.NextMiddlewareFunc) error {
log.Infof("Job incoming: %s:%s", job.Name, job.ID) logger.Infof("Job incoming: %s:%s", job.Name, job.ID)
return next() return next()
} }

View File

@ -11,7 +11,6 @@ import (
"time" "time"
"github.com/vmware/harbor/src/common/job" "github.com/vmware/harbor/src/common/job"
"github.com/vmware/harbor/src/common/utils/log"
"github.com/vmware/harbor/src/jobservice_v2/api" "github.com/vmware/harbor/src/jobservice_v2/api"
"github.com/vmware/harbor/src/jobservice_v2/config" "github.com/vmware/harbor/src/jobservice_v2/config"
"github.com/vmware/harbor/src/jobservice_v2/core" "github.com/vmware/harbor/src/jobservice_v2/core"
@ -40,13 +39,7 @@ func (bs *Bootstrap) SetJobContextInitializer(initializer env.JobContextInitiali
//LoadAndRun will load configurations, initialize components and then start the related process to serve requests. //LoadAndRun will load configurations, initialize components and then start the related process to serve requests.
//Return error if meet any problems. //Return error if meet any problems.
func (bs *Bootstrap) LoadAndRun(configFile string, detectEnv bool) { func (bs *Bootstrap) LoadAndRun() {
//Load configurations
if err := config.DefaultConfig.Load(configFile, detectEnv); err != nil {
log.Errorf("Failed to load configurations with error: %s\n", err)
return
}
//Create the root context //Create the root context
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
@ -62,7 +55,7 @@ func (bs *Bootstrap) LoadAndRun(configFile string, detectEnv bool) {
if jobCtx, err := bs.jobConextInitializer(rootContext); err == nil { if jobCtx, err := bs.jobConextInitializer(rootContext); err == nil {
rootContext.JobContext = jobCtx rootContext.JobContext = jobCtx
} else { } else {
log.Fatalf("Failed to initialize job context: %s\n", err) logger.Fatalf("Failed to initialize job context: %s\n", err)
} }
} }
@ -77,7 +70,7 @@ func (bs *Bootstrap) LoadAndRun(configFile string, detectEnv bool) {
//Start the API server //Start the API server
apiServer := bs.loadAndRunAPIServer(rootContext, config.DefaultConfig, ctl) apiServer := bs.loadAndRunAPIServer(rootContext, config.DefaultConfig, ctl)
log.Infof("Server is started at %s:%d with %s", "", config.DefaultConfig.Port, config.DefaultConfig.Protocol) logger.Infof("Server is started at %s:%d with %s", "", config.DefaultConfig.Port, config.DefaultConfig.Protocol)
//Start outdated log files sweeper //Start outdated log files sweeper
logSweeper := logger.NewSweeper(ctx, config.GetLogBasePath(), config.GetLogArchivePeriod()) logSweeper := logger.NewSweeper(ctx, config.GetLogBasePath(), config.GetLogArchivePeriod())
@ -89,7 +82,7 @@ func (bs *Bootstrap) LoadAndRun(configFile string, detectEnv bool) {
select { select {
case <-sig: case <-sig:
case err := <-rootContext.ErrorChan: case err := <-rootContext.ErrorChan:
log.Errorf("Server error:%s\n", err) logger.Errorf("Server error:%s\n", err)
} }
//Call cancel to send termination signal to other interested parts. //Call cancel to send termination signal to other interested parts.
@ -117,7 +110,7 @@ func (bs *Bootstrap) LoadAndRun(configFile string, detectEnv bool) {
rootContext.WG.Wait() rootContext.WG.Wait()
close <- true close <- true
log.Infof("Server gracefully exit") logger.Infof("Server gracefully exit")
} }
//Load and run the API server. //Load and run the API server.