mirror of
https://github.com/goharbor/harbor.git
synced 2025-02-17 04:11:24 +01:00
Refine the context related things
change job context to interface move job conetxt to the env package move the concrete job implementation to separate folder
This commit is contained in:
parent
414c36205c
commit
5f5fb5b6ec
3
src/jobservice_v2/env/context.go
vendored
3
src/jobservice_v2/env/context.go
vendored
@ -17,4 +17,7 @@ type Context struct {
|
||||
//Report errors to bootstrap component
|
||||
//Once error is reported by lower components, the whole system should exit
|
||||
ErrorChan chan error
|
||||
|
||||
//The job context reference
|
||||
JobContext JobContext
|
||||
}
|
||||
|
29
src/jobservice_v2/env/job_context.go
vendored
Normal file
29
src/jobservice_v2/env/job_context.go
vendored
Normal file
@ -0,0 +1,29 @@
|
||||
// Copyright 2018 The Harbor Authors. All rights reserved.
|
||||
|
||||
package env
|
||||
|
||||
import "context"
|
||||
|
||||
//JobContext is combination of BaseContext and other job specified resources.
|
||||
//JobContext will be the real execution context for one job.
|
||||
type JobContext interface {
|
||||
//Build the context
|
||||
//
|
||||
//Returns:
|
||||
// error if meet any problems
|
||||
Build() error
|
||||
|
||||
//Get property from the context
|
||||
//
|
||||
//prop string : key of the context property
|
||||
//
|
||||
//Returns:
|
||||
// The data of the specified context property
|
||||
Get(prop string) interface{}
|
||||
|
||||
//SystemContext returns the system context
|
||||
//
|
||||
//Returns:
|
||||
// context.Context
|
||||
SystemContext() context.Context
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
// Copyright 2018 The Harbor Authors. All rights reserved.
|
||||
|
||||
package job
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
hlog "github.com/vmware/harbor/src/common/utils/log"
|
||||
)
|
||||
|
||||
//Context is combination of BaseContext and other job specified resources.
|
||||
//Context will be the real execution context for one job.
|
||||
//Use pointer to point to the singleton BaseContext copy.
|
||||
type Context struct {
|
||||
//System context
|
||||
SystemContext context.Context
|
||||
|
||||
//Logger for job
|
||||
Logger *hlog.Logger
|
||||
}
|
38
src/jobservice_v2/job/impl/context.go
Normal file
38
src/jobservice_v2/job/impl/context.go
Normal file
@ -0,0 +1,38 @@
|
||||
package impl
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
hlog "github.com/vmware/harbor/src/common/utils/log"
|
||||
)
|
||||
|
||||
//Context ...
|
||||
type Context struct {
|
||||
//System context
|
||||
sysContext context.Context
|
||||
|
||||
//Logger for job
|
||||
logger *hlog.Logger
|
||||
}
|
||||
|
||||
//NewContext ...
|
||||
func NewContext(sysCtx context.Context) *Context {
|
||||
return &Context{
|
||||
sysContext: sysCtx,
|
||||
}
|
||||
}
|
||||
|
||||
//Build implements the same method in env.JobContext interface
|
||||
func (c *Context) Build() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
//Get implements the same method in env.JobContext interface
|
||||
func (c *Context) Get(prop string) interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
//SystemContext implements the same method in env.JobContext interface
|
||||
func (c *Context) SystemContext() context.Context {
|
||||
return c.sysContext
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
// Copyright 2018 The Harbor Authors. All rights reserved.
|
||||
|
||||
package job
|
||||
package impl
|
||||
|
||||
//Define the register name constants of known jobs
|
||||
|
@ -1,32 +1,35 @@
|
||||
// Copyright 2018 The Harbor Authors. All rights reserved.
|
||||
|
||||
package job
|
||||
package impl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/vmware/harbor/src/jobservice_v2/env"
|
||||
"github.com/vmware/harbor/src/jobservice_v2/job"
|
||||
)
|
||||
|
||||
//ReplicationJob is the job for replicating repositories.
|
||||
type ReplicationJob struct {
|
||||
ctx Context
|
||||
ctx env.JobContext
|
||||
params map[string]interface{}
|
||||
opCmdFunc CheckOPCmdFunc
|
||||
opCmdFunc job.CheckOPCmdFunc
|
||||
}
|
||||
|
||||
//SetContext is implementation of same method in Interface.
|
||||
func (rj *ReplicationJob) SetContext(ctx Context) {
|
||||
func (rj *ReplicationJob) SetContext(ctx env.JobContext) {
|
||||
rj.ctx = ctx
|
||||
fmt.Printf("ReplicationJob context=%#v\n", rj.ctx)
|
||||
}
|
||||
|
||||
//SetParams is implementation of same method in Interface.
|
||||
func (rj *ReplicationJob) SetParams(params map[string]interface{}) {
|
||||
func (rj *ReplicationJob) SetParams(params map[string]interface{}) error {
|
||||
rj.params = params
|
||||
fmt.Printf("ReplicationJob args: %v\n", rj.params)
|
||||
return nil
|
||||
}
|
||||
|
||||
//SetCheckOPCmdFunc is implementation of same method in Interface.
|
||||
func (rj *ReplicationJob) SetCheckOPCmdFunc(f CheckOPCmdFunc) {}
|
||||
func (rj *ReplicationJob) SetCheckOPCmdFunc(f job.CheckOPCmdFunc) {}
|
||||
|
||||
//MaxFails is implementation of same method in Interface.
|
||||
func (rj *ReplicationJob) MaxFails() uint {
|
@ -2,6 +2,8 @@
|
||||
|
||||
package job
|
||||
|
||||
import "github.com/vmware/harbor/src/jobservice_v2/env"
|
||||
|
||||
//CheckOPCmdFunc is the function to check if the related operation commands
|
||||
//like STOP or CANCEL is fired for the specified job. If yes, return the
|
||||
//command code for job to determin if take corresponding action.
|
||||
@ -11,13 +13,16 @@ type CheckOPCmdFunc func(string) (uint, bool)
|
||||
type Interface interface {
|
||||
//SetContext used to inject the job context if needed.
|
||||
//
|
||||
//ctx Context: Job execution context.
|
||||
SetContext(ctx Context)
|
||||
//ctx env.JobContext: Job execution context.
|
||||
SetContext(ctx env.JobContext)
|
||||
|
||||
//Pass parameters via this method if have.
|
||||
//
|
||||
//params map[string]interface{}: parameters with key-pair style for the job execution.
|
||||
SetParams(params map[string]interface{})
|
||||
//
|
||||
//Returns:
|
||||
// return error if the parameters are not valid or nil
|
||||
SetParams(params map[string]interface{}) error
|
||||
|
||||
//Inject the func into the job for OP command check.
|
||||
//
|
||||
|
@ -23,14 +23,12 @@ func NewRedisJob(j interface{}, ctx *env.Context) *RedisJob {
|
||||
//Run the job
|
||||
func (rj *RedisJob) Run(j *work.Job) error {
|
||||
//Inject data
|
||||
jobContext := Context{
|
||||
SystemContext: rj.context.SystemContext,
|
||||
}
|
||||
|
||||
runningJob := rj.Wrap()
|
||||
runningJob.SetContext(jobContext)
|
||||
runningJob.SetContext(rj.context.JobContext)
|
||||
if runningJob.ParamsRequired() {
|
||||
runningJob.SetParams(j.Args)
|
||||
if err := runningJob.SetParams(j.Args); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: Update job status to 'Running'
|
||||
|
@ -14,7 +14,7 @@ import (
|
||||
"github.com/vmware/harbor/src/jobservice_v2/config"
|
||||
"github.com/vmware/harbor/src/jobservice_v2/core"
|
||||
"github.com/vmware/harbor/src/jobservice_v2/env"
|
||||
"github.com/vmware/harbor/src/jobservice_v2/job"
|
||||
"github.com/vmware/harbor/src/jobservice_v2/job/impl"
|
||||
"github.com/vmware/harbor/src/jobservice_v2/pool"
|
||||
)
|
||||
|
||||
@ -44,6 +44,14 @@ func (bs *Bootstrap) LoadAndRun(configFile string, detectEnv bool) {
|
||||
ErrorChan: make(chan error, 1), //with 1 buffer
|
||||
}
|
||||
|
||||
//Build specified job context
|
||||
jobCtx := impl.NewContext(ctx)
|
||||
if err := jobCtx.Build(); err != nil {
|
||||
log.Errorf("Failed to build job conetxt with error: %s\n", err)
|
||||
return
|
||||
}
|
||||
rootContext.JobContext = jobCtx
|
||||
|
||||
//Start the pool
|
||||
var backendPool pool.Interface
|
||||
if cfg.PoolConfig.Backend == config.JobServicePoolBackendRedis {
|
||||
@ -108,7 +116,7 @@ func (bs *Bootstrap) loadAndRunRedisWorkerPool(ctx *env.Context, cfg config.Conf
|
||||
|
||||
redisWorkerPool := pool.NewGoCraftWorkPool(ctx, redisPoolCfg)
|
||||
//Register jobs here
|
||||
if err := redisWorkerPool.RegisterJob(job.KnownJobReplication, (*job.ReplicationJob)(nil)); err != nil {
|
||||
if err := redisWorkerPool.RegisterJob(impl.KnownJobReplication, (*impl.ReplicationJob)(nil)); err != nil {
|
||||
//exit
|
||||
ctx.ErrorChan <- err
|
||||
return redisWorkerPool //avoid nil pointer issue
|
||||
|
Loading…
Reference in New Issue
Block a user