From 233692c1274aecf839b6fdbc4381630c1245227c Mon Sep 17 00:00:00 2001 From: Steven Zou Date: Sun, 8 Apr 2018 11:29:43 +0800 Subject: [PATCH] fix issue: job context may be nil pointer when trying to be closed in defer func --- src/jobservice/pool/redis_job_wrapper.go | 30 +++++++++++++----------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/jobservice/pool/redis_job_wrapper.go b/src/jobservice/pool/redis_job_wrapper.go index 6beb92465..ed11a8c9b 100644 --- a/src/jobservice/pool/redis_job_wrapper.go +++ b/src/jobservice/pool/redis_job_wrapper.go @@ -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