Refactor the job execution context interface

This commit is contained in:
Steven Zou 2018-03-14 19:17:38 +08:00
parent 5f5fb5b6ec
commit b61dc39278
4 changed files with 31 additions and 3 deletions

View File

@ -9,9 +9,12 @@ import "context"
type JobContext interface { type JobContext interface {
//Build the context //Build the context
// //
//dep JobData : Dependencies for building the context, just in case that the build
//function need some external info
//
//Returns: //Returns:
// error if meet any problems // error if meet any problems
Build() error Build(dep JobData) error
//Get property from the context //Get property from the context
// //
@ -27,3 +30,10 @@ type JobContext interface {
// context.Context // context.Context
SystemContext() context.Context SystemContext() context.Context
} }
//JobData defines job context dependencies.
type JobData struct {
ID string
Name string
Args map[string]interface{}
}

View File

@ -4,6 +4,7 @@ import (
"context" "context"
hlog "github.com/vmware/harbor/src/common/utils/log" hlog "github.com/vmware/harbor/src/common/utils/log"
"github.com/vmware/harbor/src/jobservice_v2/env"
) )
//Context ... //Context ...
@ -22,8 +23,14 @@ func NewContext(sysCtx context.Context) *Context {
} }
} }
//InitDao ...
func (c *Context) InitDao() error {
return nil
}
//Build implements the same method in env.JobContext interface //Build implements the same method in env.JobContext interface
func (c *Context) Build() error { //This func will build the job execution context before running
func (c *Context) Build(dep env.JobData) error {
return nil return nil
} }

View File

@ -22,6 +22,16 @@ func NewRedisJob(j interface{}, ctx *env.Context) *RedisJob {
//Run the job //Run the job
func (rj *RedisJob) Run(j *work.Job) error { func (rj *RedisJob) Run(j *work.Job) error {
//Build job execution context
jData := env.JobData{
ID: j.ID,
Name: j.Name,
Args: j.Args,
}
if err := rj.context.JobContext.Build(jData); err != nil {
return err
}
//Inject data //Inject data
runningJob := rj.Wrap() runningJob := rj.Wrap()
runningJob.SetContext(rj.context.JobContext) runningJob.SetContext(rj.context.JobContext)
@ -37,6 +47,7 @@ func (rj *RedisJob) Run(j *work.Job) error {
//TODO: //TODO:
//If error is stopped error, update status to 'Stopped' and return nil //If error is stopped error, update status to 'Stopped' and return nil
//If error is cancelled error, update status to 'Cancelled' and return err //If error is cancelled error, update status to 'Cancelled' and return err
//Need to consider how to rm the retry option
return err return err
} }

View File

@ -46,7 +46,7 @@ func (bs *Bootstrap) LoadAndRun(configFile string, detectEnv bool) {
//Build specified job context //Build specified job context
jobCtx := impl.NewContext(ctx) jobCtx := impl.NewContext(ctx)
if err := jobCtx.Build(); err != nil { if err := jobCtx.InitDao(); err != nil {
log.Errorf("Failed to build job conetxt with error: %s\n", err) log.Errorf("Failed to build job conetxt with error: %s\n", err)
return return
} }