fix issue: job context may be nil pointer when trying to be closed in defer func

This commit is contained in:
Steven Zou 2018-04-08 11:29:43 +08:00
parent 1cb40368f5
commit 233692c127

View File

@ -40,21 +40,7 @@ func (rj *RedisJob) Run(j *work.Job) error {
execContext env.JobContext
)
execContext, err = rj.buildContext(j)
if err != nil {
buildContextFailed = true
goto FAILED //no need to retry
}
//Wrap job
runningJob = Wrap(rj.job)
defer func() {
//Close open io stream first
if closer, ok := execContext.GetLogger().(logger.Closer); ok {
closer.Close()
}
if err == nil {
logger.Infof("Job '%s:%s' exit with success", j.Name, j.ID)
return //nothing need to do
@ -85,6 +71,22 @@ func (rj *RedisJob) Run(j *work.Job) error {
}
}()
//Wrap job
runningJob = Wrap(rj.job)
execContext, err = rj.buildContext(j)
if err != nil {
buildContextFailed = true
goto FAILED //no need to retry
}
defer func() {
//Close open io stream first
if closer, ok := execContext.GetLogger().(logger.Closer); ok {
closer.Close()
}
}()
//Start to run
rj.jobRunning(j.ID)
//Inject data