update the code of other components influenced by the js code change

Signed-off-by: Steven Zou <szou@vmware.com>
This commit is contained in:
Steven Zou 2019-04-22 20:04:57 +08:00
parent 3937c8b0dc
commit ad68a3f79d
7 changed files with 33 additions and 140 deletions

View File

@ -1,104 +0,0 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package impl
import (
"context"
"fmt"
"os"
"testing"
"time"
"github.com/goharbor/harbor/src/jobservice/config"
"github.com/goharbor/harbor/src/jobservice/env"
"github.com/goharbor/harbor/src/jobservice/job"
"github.com/goharbor/harbor/src/jobservice/models"
)
func TestDefaultContext(t *testing.T) {
defaultContext := NewDefaultContext(context.Background())
jobData := env.JobData{
ID: "fake_id",
Name: "DEMO",
Args: make(map[string]interface{}),
ExtraData: make(map[string]interface{}),
}
var opCmdFund job.CheckOPCmdFunc = func() (string, bool) {
return "stop", true
}
var checkInFunc job.CheckInFunc = func(msg string) {
fmt.Println(msg)
}
var launchJobFunc job.LaunchJobFunc = func(req models.JobRequest) (models.JobStats, error) {
return models.JobStats{
Stats: &models.JobStatData{
JobID: "fake_sub_job_id",
Status: "pending",
JobName: "DEMO",
JobKind: job.KindGeneric,
EnqueueTime: time.Now().Unix(),
UpdateTime: time.Now().Unix(),
},
}, nil
}
jobData.ExtraData["opCommandFunc"] = opCmdFund
jobData.ExtraData["checkInFunc"] = checkInFunc
jobData.ExtraData["launchJobFunc"] = launchJobFunc
oldLogConfig := config.DefaultConfig.JobLoggerConfigs
defer func() {
config.DefaultConfig.JobLoggerConfigs = oldLogConfig
}()
logSettings := map[string]interface{}{}
logSettings["base_dir"] = os.TempDir()
config.DefaultConfig.JobLoggerConfigs = []*config.LoggerConfig{
{
Level: "DEBUG",
Name: "FILE",
Settings: logSettings,
},
}
newJobContext, err := defaultContext.Build(jobData)
if err != nil {
t.Fatal(err)
}
cmd, ok := newJobContext.OPCommand()
if !ok || cmd != "stop" {
t.Fatalf("expect op command 'stop' but got %s", cmd)
}
if err := newJobContext.Checkin("hello"); err != nil {
t.Fatal(err)
}
stats, err := newJobContext.LaunchJob(models.JobRequest{})
if err != nil {
t.Fatal(err)
}
if stats.Stats.JobID != "fake_sub_job_id" {
t.Fatalf("expect job id 'fake_sub_job_id' but got %s", stats.Stats.JobID)
}
ctx := newJobContext.SystemContext()
if ctx == nil {
t.Fatal("got nil system context")
}
}

View File

@ -18,6 +18,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/goharbor/harbor/src/jobservice/job"
"github.com/goharbor/harbor/src/replication/model" "github.com/goharbor/harbor/src/replication/model"
"github.com/goharbor/harbor/src/replication/transfer" "github.com/goharbor/harbor/src/replication/transfer"
@ -30,7 +31,6 @@ import (
// register the DockerHub adapter // register the DockerHub adapter
_ "github.com/goharbor/harbor/src/replication/adapter/dockerhub" _ "github.com/goharbor/harbor/src/replication/adapter/dockerhub"
// register the Native adapter // register the Native adapter
"github.com/goharbor/harbor/src/jobservice/job"
_ "github.com/goharbor/harbor/src/replication/adapter/native" _ "github.com/goharbor/harbor/src/replication/adapter/native"
// register the Huawei adapter // register the Huawei adapter
_ "github.com/goharbor/harbor/src/replication/adapter/huawei" _ "github.com/goharbor/harbor/src/replication/adapter/huawei"

View File

@ -90,7 +90,7 @@ func (f *fakedExecutionManager) GetTaskLog(int64) ([]byte, error) {
type fakedScheduler struct{} type fakedScheduler struct{}
func (f *fakedScheduler) Preprocess(src []*model.Resource, dst []*model.Resource) ([]*scheduler.ScheduleItem, error) { func (f *fakedScheduler) Preprocess(src []*model.Resource, dst []*model.Resource) ([]*scheduler.ScheduleItem, error) {
items := []*scheduler.ScheduleItem{} items := make([]*scheduler.ScheduleItem, 0)
for i, res := range src { for i, res := range src {
items = append(items, &scheduler.ScheduleItem{ items = append(items, &scheduler.ScheduleItem{
SrcResource: res, SrcResource: res,
@ -100,7 +100,7 @@ func (f *fakedScheduler) Preprocess(src []*model.Resource, dst []*model.Resource
return items, nil return items, nil
} }
func (f *fakedScheduler) Schedule(items []*scheduler.ScheduleItem) ([]*scheduler.ScheduleResult, error) { func (f *fakedScheduler) Schedule(items []*scheduler.ScheduleItem) ([]*scheduler.ScheduleResult, error) {
results := []*scheduler.ScheduleResult{} results := make([]*scheduler.ScheduleResult, 0)
for _, item := range items { for _, item := range items {
results = append(results, &scheduler.ScheduleResult{ results = append(results, &scheduler.ScheduleResult{
TaskID: item.TaskID, TaskID: item.TaskID,

View File

@ -22,18 +22,19 @@ import (
// UpdateTask update the status of the task // UpdateTask update the status of the task
func UpdateTask(ctl operation.Controller, id int64, status string) error { func UpdateTask(ctl operation.Controller, id int64, status string) error {
jobStatus := job.Status(status)
// convert the job status to task status // convert the job status to task status
s := "" s := ""
switch status { switch jobStatus {
case job.JobStatusPending: case job.PendingStatus:
s = models.TaskStatusPending s = models.TaskStatusPending
case job.JobStatusScheduled, job.JobStatusRunning: case job.ScheduledStatus, job.RunningStatus:
s = models.TaskStatusInProgress s = models.TaskStatusInProgress
case job.JobStatusStopped, job.JobStatusCancelled: case job.StoppedStatus:
s = models.TaskStatusStopped s = models.TaskStatusStopped
case job.JobStatusError: case job.ErrorStatus:
s = models.TaskStatusFailed s = models.TaskStatusFailed
case job.JobStatusSuccess: case job.SuccessStatus:
s = models.TaskStatusSucceed s = models.TaskStatusSucceed
} }
return ctl.UpdateTaskStatus(id, s) return ctl.UpdateTaskStatus(id, s)

View File

@ -61,31 +61,27 @@ func TestUpdateTask(t *testing.T) {
expectedStatus string expectedStatus string
}{ }{
{ {
inputStatus: job.JobStatusPending, inputStatus: job.PendingStatus.String(),
expectedStatus: models.TaskStatusPending, expectedStatus: models.TaskStatusPending,
}, },
{ {
inputStatus: job.JobStatusScheduled, inputStatus: job.ScheduledStatus.String(),
expectedStatus: models.TaskStatusInProgress, expectedStatus: models.TaskStatusInProgress,
}, },
{ {
inputStatus: job.JobStatusRunning, inputStatus: job.RunningStatus.String(),
expectedStatus: models.TaskStatusInProgress, expectedStatus: models.TaskStatusInProgress,
}, },
{ {
inputStatus: job.JobStatusStopped, inputStatus: job.StoppedStatus.String(),
expectedStatus: models.TaskStatusStopped, expectedStatus: models.TaskStatusStopped,
}, },
{ {
inputStatus: job.JobStatusCancelled, inputStatus: job.ErrorStatus.String(),
expectedStatus: models.TaskStatusStopped,
},
{
inputStatus: job.JobStatusError,
expectedStatus: models.TaskStatusFailed, expectedStatus: models.TaskStatusFailed,
}, },
{ {
inputStatus: job.JobStatusSuccess, inputStatus: job.SuccessStatus.String(),
expectedStatus: models.TaskStatusSucceed, expectedStatus: models.TaskStatusSucceed,
}, },
} }

View File

@ -19,20 +19,19 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/goharbor/harbor/src/common/job" cjob "github.com/goharbor/harbor/src/common/job"
common_job "github.com/goharbor/harbor/src/common/job"
"github.com/goharbor/harbor/src/common/job/models" "github.com/goharbor/harbor/src/common/job/models"
"github.com/goharbor/harbor/src/jobservice/opm" "github.com/goharbor/harbor/src/jobservice/job"
"github.com/goharbor/harbor/src/replication/config" "github.com/goharbor/harbor/src/replication/config"
"github.com/goharbor/harbor/src/replication/model" "github.com/goharbor/harbor/src/replication/model"
) )
type defaultScheduler struct { type defaultScheduler struct {
client job.Client client cjob.Client
} }
// NewScheduler returns an instance of Scheduler // NewScheduler returns an instance of Scheduler
func NewScheduler(js job.Client) Scheduler { func NewScheduler(js cjob.Client) Scheduler {
return &defaultScheduler{ return &defaultScheduler{
client: js, client: js,
} }
@ -95,14 +94,14 @@ func (d *defaultScheduler) Schedule(items []*ScheduleItem) ([]*ScheduleResult, e
results = append(results, result) results = append(results, result)
continue continue
} }
job := &models.JobData{ j := &models.JobData{
Metadata: &models.JobMetadata{ Metadata: &models.JobMetadata{
JobKind: job.JobKindGeneric, JobKind: job.KindGeneric,
}, },
StatusHook: fmt.Sprintf("%s/service/notifications/jobs/replication/task/%d", config.Config.CoreURL, item.TaskID), StatusHook: fmt.Sprintf("%s/service/notifications/jobs/replication/task/%d", config.Config.CoreURL, item.TaskID),
} }
job.Name = common_job.Replication j.Name = job.Replication
src, err := json.Marshal(item.SrcResource) src, err := json.Marshal(item.SrcResource)
if err != nil { if err != nil {
result.Error = err result.Error = err
@ -115,11 +114,11 @@ func (d *defaultScheduler) Schedule(items []*ScheduleItem) ([]*ScheduleResult, e
results = append(results, result) results = append(results, result)
continue continue
} }
job.Parameters = map[string]interface{}{ j.Parameters = map[string]interface{}{
"src_resource": string(src), "src_resource": string(src),
"dst_resource": string(dest), "dst_resource": string(dest),
} }
id, joberr := d.client.SubmitJob(job) id, joberr := d.client.SubmitJob(j)
if joberr != nil { if joberr != nil {
result.Error = joberr result.Error = joberr
results = append(results, result) results = append(results, result)
@ -133,7 +132,7 @@ func (d *defaultScheduler) Schedule(items []*ScheduleItem) ([]*ScheduleResult, e
// Stop the transfer job // Stop the transfer job
func (d *defaultScheduler) Stop(id string) error { func (d *defaultScheduler) Stop(id string) error {
err := d.client.PostAction(id, opm.CtlCommandStop) err := d.client.PostAction(id, string(job.StopCommand))
if err != nil { if err != nil {
return err return err
} }

View File

@ -19,10 +19,11 @@ import (
"net/http" "net/http"
"time" "time"
common_http "github.com/goharbor/harbor/src/common/http" commonHttp "github.com/goharbor/harbor/src/common/http"
"github.com/goharbor/harbor/src/common/job" "github.com/goharbor/harbor/src/common/job"
job_models "github.com/goharbor/harbor/src/common/job/models" jobModels "github.com/goharbor/harbor/src/common/job/models"
"github.com/goharbor/harbor/src/common/utils/log" "github.com/goharbor/harbor/src/common/utils/log"
jsJob "github.com/goharbor/harbor/src/jobservice/job"
"github.com/goharbor/harbor/src/replication/config" "github.com/goharbor/harbor/src/replication/config"
"github.com/goharbor/harbor/src/replication/dao" "github.com/goharbor/harbor/src/replication/dao"
"github.com/goharbor/harbor/src/replication/dao/models" "github.com/goharbor/harbor/src/replication/dao/models"
@ -61,13 +62,13 @@ func (s *scheduler) Schedule(policyID int64, cron string) error {
log.Debugf("the schedule job record %d added", id) log.Debugf("the schedule job record %d added", id)
statusHookURL := fmt.Sprintf("%s/service/notifications/jobs/replication/%d", config.Config.CoreURL, id) statusHookURL := fmt.Sprintf("%s/service/notifications/jobs/replication/%d", config.Config.CoreURL, id)
jobID, err := s.jobservice.SubmitJob(&job_models.JobData{ jobID, err := s.jobservice.SubmitJob(&jobModels.JobData{
Name: job.ReplicationScheduler, Name: jsJob.ReplicationScheduler,
Parameters: map[string]interface{}{ Parameters: map[string]interface{}{
"url": config.Config.CoreURL, "url": config.Config.CoreURL,
"policy_id": policyID, "policy_id": policyID,
}, },
Metadata: &job_models.JobMetadata{ Metadata: &jobModels.JobMetadata{
JobKind: job.JobKindPeriodic, JobKind: job.JobKindPeriodic,
Cron: cron, Cron: cron,
}, },
@ -103,7 +104,7 @@ func (s *scheduler) Unschedule(policyID int64) error {
if err = s.jobservice.PostAction(sj.JobID, job.JobActionStop); err != nil { if err = s.jobservice.PostAction(sj.JobID, job.JobActionStop); err != nil {
// if the job specified by jobID is not found in jobservice, just delete // if the job specified by jobID is not found in jobservice, just delete
// the record from database // the record from database
if e, ok := err.(*common_http.Error); !ok || e.Code != http.StatusNotFound { if e, ok := err.(*commonHttp.Error); !ok || e.Code != http.StatusNotFound {
return err return err
} }
log.Debugf("the stop action for schedule job %s submitted to the jobservice", sj.JobID) log.Debugf("the stop action for schedule job %s submitted to the jobservice", sj.JobID)