Close the open IO stream used by the job logger after job eixting

This commit is contained in:
Steven Zou 2018-03-24 16:45:26 +08:00
parent 75e29782f6
commit 4cc9e0bf47
4 changed files with 32 additions and 0 deletions

View File

@ -12,6 +12,7 @@ import (
//It used in the job to output logs to the logfile.
type JobLogger struct {
backendLogger *log.Logger
streamRef *os.File
}
//New logger
@ -26,9 +27,20 @@ func New(logPath string, level string) logger.Interface {
return &JobLogger{
backendLogger: backendLogger,
streamRef: f,
}
}
//Close the opened io stream
//Implements logger.Closer interface
func (jl *JobLogger) Close() error {
if jl.streamRef != nil {
jl.streamRef.Close()
}
return nil
}
//Debug ...
func (jl *JobLogger) Debug(v ...interface{}) {
jl.backendLogger.Debug(v...)

View File

@ -8,6 +8,8 @@ import (
"strings"
"time"
"github.com/vmware/harbor/src/common/dao"
"github.com/vmware/harbor/src/common/models"
"github.com/vmware/harbor/src/jobservice_v2/opm"
"github.com/vmware/harbor/src/jobservice_v2/errs"
@ -59,6 +61,9 @@ func (rj *ReplicationJob) Run(ctx env.JobContext, params map[string]interface{})
if v, ok := ctx.Get("email_from"); ok {
fmt.Printf("Get prop form context: email_from=%s\n", v)
}
if u, err := dao.GetUser(models.User{}); err == nil {
fmt.Printf("u=%#+v\n", u)
}
/*if 1 != 0 {
return errors.New("I suicide")

View File

@ -0,0 +1,9 @@
// Copyright 2018 The Harbor Authors. All rights reserved.
package logger
//Closer defines method to close the open io stream used by logger.
type Closer interface {
//Close the opened io stream
Close() error
}

View File

@ -9,6 +9,7 @@ import (
"github.com/vmware/harbor/src/jobservice_v2/env"
"github.com/vmware/harbor/src/jobservice_v2/errs"
"github.com/vmware/harbor/src/jobservice_v2/job"
"github.com/vmware/harbor/src/jobservice_v2/logger"
"github.com/vmware/harbor/src/jobservice_v2/opm"
)
@ -48,6 +49,11 @@ func (rj *RedisJob) Run(j *work.Job) error {
runningJob = Wrap(rj.job)
defer func() {
//Close open io stream first
if closer, ok := execContext.GetLogger().(logger.Closer); ok {
closer.Close()
}
if err == nil {
return //nothing need to do
}