fix(preheat):improve preheat job logs

Signed-off-by: Steven Zou <szou@vmware.com>
This commit is contained in:
Steven Zou 2020-07-27 22:43:18 +08:00
parent f73cd6fc0e
commit 1adaf58ab1
2 changed files with 40 additions and 15 deletions

View File

@ -70,6 +70,12 @@ func (j *Job) Run(ctx job.Context, params job.Parameters) error {
// Get logger
myLogger := ctx.GetLogger()
// preheatJobRunningError is an internal error format
preheatJobRunningError := func(err error) error {
myLogger.Error(err)
return errors.Wrap(err, "preheat job running error")
}
// Parse parameters, ignore errors as they have been validated already
p, _ := parseParamProvider(params)
pi, _ := parseParamImage(params)
@ -88,14 +94,12 @@ func (j *Job) Run(ctx job.Context, params job.Parameters) error {
fac, ok := pr.GetProvider(p.Vendor)
if !ok {
err := errors.Errorf("No driver registered for provider %s", p.Vendor)
myLogger.Error(err)
return preheatJobRunningError(err)
}
// Construct driver
d, err := fac(p)
if err != nil {
myLogger.Error(err)
return preheatJobRunningError(err)
}
@ -105,13 +109,11 @@ func (j *Job) Run(ctx job.Context, params job.Parameters) error {
// First, check the health of the provider
h, err := d.GetHealth()
if err != nil {
myLogger.Error(err)
return preheatJobRunningError(err)
}
if h.Status != pr.DriverStatusHealthy {
err = errors.Errorf("unhealthy target preheat provider: %s", p.Vendor)
myLogger.Error(err)
return preheatJobRunningError(err)
}
@ -120,7 +122,6 @@ func (j *Job) Run(ctx job.Context, params job.Parameters) error {
// Then send the preheat requests to the target provider.
st, err := d.Preheat(pi)
if err != nil {
myLogger.Error(err)
return preheatJobRunningError(err)
}
@ -134,7 +135,6 @@ func (j *Job) Run(ctx job.Context, params job.Parameters) error {
return nil
case provider.PreheatingStatusFail:
err = errors.New("preheating is failed")
myLogger.Error(err)
return preheatJobRunningError(err)
case provider.PreheatingStatusPending,
provider.PreheatingStatusRunning:
@ -142,11 +142,10 @@ func (j *Job) Run(ctx job.Context, params job.Parameters) error {
default:
// in case
err = errors.Errorf("unknown status '%s' returned by the preheat provider %s-%s:%s", st.Status, p.Vendor, p.Name, p.Endpoint)
myLogger.Error(err)
return preheatJobRunningError(err)
}
myLogger.Info("Start to loop check the preheating status until it's ready or timeout(30m)")
myLogger.Info("Start to loop check the preheating status until it's success or timeout(30m)")
// If process is not completed, loop check the status until it's ready.
tk := time.NewTicker(checkInterval)
defer tk.Stop()
@ -159,13 +158,13 @@ func (j *Job) Run(ctx job.Context, params job.Parameters) error {
case <-tk.C:
s, err := d.CheckProgress(st.TaskID)
if err != nil {
myLogger.Error(err)
return preheatJobRunningError(err)
}
myLogger.Infof("Check preheat progress: %s", s)
// Finished
if s.Status == provider.PreheatingStatusSuccess {
myLogger.Info("Preheating is completed")
return nil
}
case <-tm.C:
@ -174,11 +173,6 @@ func (j *Job) Run(ctx job.Context, params job.Parameters) error {
}
}
// preheatJobRunningError is an internal error format
func preheatJobRunningError(err error) error {
return errors.Wrap(err, "preheat job running error")
}
// parseParamProvider parses the provider param.
func parseParamProvider(params job.Parameters) (*provider.Instance, error) {
data, err := parseStrValue(params, PreheatParamProvider)

View File

@ -1,5 +1,13 @@
package provider
import (
"fmt"
"strings"
"time"
"github.com/goharbor/harbor/src/pkg/p2p/preheat/models/provider"
)
const (
// DriverStatusHealthy represents the healthy status
DriverStatusHealthy = "Healthy"
@ -58,3 +66,26 @@ type PreheatingStatus struct {
StartTime string `json:"start_time"`
FinishTime string `json:"finish_time"`
}
// String format of PreheatingStatus
func (ps *PreheatingStatus) String() string {
t := fmt.Sprintf("Task [%s] is %s", ps.TaskID, strings.ToLower(ps.Status))
switch ps.Status {
case provider.PreheatingStatusFail:
t = fmt.Sprintf("%s with error: %s", t, ps.Error)
case provider.PreheatingStatusSuccess:
if len(ps.StartTime) > 0 && len(ps.FinishTime) > 0 {
if st, err := time.Parse(time.RFC3339, ps.StartTime); err == nil {
if ft, err := time.Parse(time.RFC3339, ps.FinishTime); err == nil {
d := ft.Sub(st)
t = fmt.Sprintf("%s with duration: %s", t, d)
}
}
}
default:
t = fmt.Sprintf("%s, start time=%s", t, ps.StartTime)
}
return t
}