Merge pull request #12599 from steven-zou/fix/p2p_job_log

fix(preheat):improve preheat job logs
This commit is contained in:
Steven Zou 2020-07-28 13:10:37 +08:00 committed by GitHub
commit 1d361a89a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 16 deletions

View File

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

View File

@ -1,5 +1,13 @@
package provider package provider
import (
"fmt"
"strings"
"time"
"github.com/goharbor/harbor/src/pkg/p2p/preheat/models/provider"
)
const ( const (
// DriverStatusHealthy represents the healthy status // DriverStatusHealthy represents the healthy status
DriverStatusHealthy = "Healthy" DriverStatusHealthy = "Healthy"
@ -58,3 +66,26 @@ type PreheatingStatus struct {
StartTime string `json:"start_time"` StartTime string `json:"start_time"`
FinishTime string `json:"finish_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
}