mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-05 10:10:12 +01:00
Convert job status to replication task status
This commits converts job status to task status Signed-off-by: Wenkai Yin <yinw@vmware.com>
This commit is contained in:
parent
39485efc9a
commit
679b0d3d6a
@ -24,6 +24,7 @@ import (
|
|||||||
"github.com/goharbor/harbor/src/common/utils/log"
|
"github.com/goharbor/harbor/src/common/utils/log"
|
||||||
"github.com/goharbor/harbor/src/core/api"
|
"github.com/goharbor/harbor/src/core/api"
|
||||||
"github.com/goharbor/harbor/src/replication/ng"
|
"github.com/goharbor/harbor/src/replication/ng"
|
||||||
|
"github.com/goharbor/harbor/src/replication/ng/hook"
|
||||||
)
|
)
|
||||||
|
|
||||||
var statusMap = map[string]string{
|
var statusMap = map[string]string{
|
||||||
@ -39,8 +40,9 @@ var statusMap = map[string]string{
|
|||||||
// Handler handles reqeust on /service/notifications/jobs/*, which listens to the webhook of jobservice.
|
// Handler handles reqeust on /service/notifications/jobs/*, which listens to the webhook of jobservice.
|
||||||
type Handler struct {
|
type Handler struct {
|
||||||
api.BaseController
|
api.BaseController
|
||||||
id int64
|
id int64
|
||||||
status string
|
status string
|
||||||
|
rawStatus string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare ...
|
// Prepare ...
|
||||||
@ -60,6 +62,7 @@ func (h *Handler) Prepare() {
|
|||||||
h.Abort("200")
|
h.Abort("200")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
h.rawStatus = data.Status
|
||||||
status, ok := statusMap[data.Status]
|
status, ok := statusMap[data.Status]
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Debugf("drop the job status update event: job id-%d, status-%s", id, status)
|
log.Debugf("drop the job status update event: job id-%d, status-%s", id, status)
|
||||||
@ -92,7 +95,7 @@ func (h *Handler) HandleReplication() {
|
|||||||
// HandleReplicationTask handles the webhook of replication task
|
// HandleReplicationTask handles the webhook of replication task
|
||||||
func (h *Handler) HandleReplicationTask() {
|
func (h *Handler) HandleReplicationTask() {
|
||||||
log.Debugf("received replication task status update event: task-%d, status-%s", h.id, h.status)
|
log.Debugf("received replication task status update event: task-%d, status-%s", h.id, h.status)
|
||||||
if err := ng.OperationCtl.UpdateTaskStatus(h.id, h.status); err != nil {
|
if err := hook.UpdateTask(ng.OperationCtl, h.id, h.rawStatus); err != nil {
|
||||||
log.Errorf("Failed to update replication task status, id: %d, status: %s", h.id, h.status)
|
log.Errorf("Failed to update replication task status, id: %d, status: %s", h.id, h.status)
|
||||||
h.HandleInternalServerError(err.Error())
|
h.HandleInternalServerError(err.Error())
|
||||||
return
|
return
|
||||||
|
40
src/replication/ng/hook/task.go
Normal file
40
src/replication/ng/hook/task.go
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
// 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 hook
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/goharbor/harbor/src/jobservice/job"
|
||||||
|
"github.com/goharbor/harbor/src/replication/ng/dao/models"
|
||||||
|
"github.com/goharbor/harbor/src/replication/ng/operation"
|
||||||
|
)
|
||||||
|
|
||||||
|
// UpdateTask update the status of the task
|
||||||
|
func UpdateTask(ctl operation.Controller, id int64, status string) error {
|
||||||
|
// convert the job status to task status
|
||||||
|
s := ""
|
||||||
|
switch status {
|
||||||
|
case job.JobStatusPending:
|
||||||
|
s = models.TaskStatusPending
|
||||||
|
case job.JobStatusScheduled, job.JobStatusRunning:
|
||||||
|
s = models.TaskStatusInProgress
|
||||||
|
case job.JobStatusStopped, job.JobStatusCancelled:
|
||||||
|
s = models.TaskStatusStopped
|
||||||
|
case job.JobStatusError:
|
||||||
|
s = models.TaskStatusFailed
|
||||||
|
case job.JobStatusSuccess:
|
||||||
|
s = models.TaskStatusSucceed
|
||||||
|
}
|
||||||
|
return ctl.UpdateTaskStatus(id, s)
|
||||||
|
}
|
98
src/replication/ng/hook/task_test.go
Normal file
98
src/replication/ng/hook/task_test.go
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
// 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 hook
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/goharbor/harbor/src/jobservice/job"
|
||||||
|
"github.com/goharbor/harbor/src/replication/ng/dao/models"
|
||||||
|
"github.com/goharbor/harbor/src/replication/ng/model"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
type fakedOperationController struct {
|
||||||
|
status string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakedOperationController) StartReplication(*model.Policy, *model.Resource) (int64, error) {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
func (f *fakedOperationController) StopReplication(int64) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (f *fakedOperationController) ListExecutions(...*models.ExecutionQuery) (int64, []*models.Execution, error) {
|
||||||
|
return 0, nil, nil
|
||||||
|
}
|
||||||
|
func (f *fakedOperationController) GetExecution(int64) (*models.Execution, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
func (f *fakedOperationController) ListTasks(...*models.TaskQuery) (int64, []*models.Task, error) {
|
||||||
|
return 0, nil, nil
|
||||||
|
}
|
||||||
|
func (f *fakedOperationController) GetTask(int64) (*models.Task, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
func (f *fakedOperationController) UpdateTaskStatus(id int64, status string, statusCondition ...string) error {
|
||||||
|
f.status = status
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (f *fakedOperationController) GetTaskLog(int64) ([]byte, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUpdateTask(t *testing.T) {
|
||||||
|
mgr := &fakedOperationController{}
|
||||||
|
cases := []struct {
|
||||||
|
inputStatus string
|
||||||
|
expectedStatus string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
inputStatus: job.JobStatusPending,
|
||||||
|
expectedStatus: models.TaskStatusPending,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
inputStatus: job.JobStatusScheduled,
|
||||||
|
expectedStatus: models.TaskStatusInProgress,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
inputStatus: job.JobStatusRunning,
|
||||||
|
expectedStatus: models.TaskStatusInProgress,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
inputStatus: job.JobStatusStopped,
|
||||||
|
expectedStatus: models.TaskStatusStopped,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
inputStatus: job.JobStatusCancelled,
|
||||||
|
expectedStatus: models.TaskStatusStopped,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
inputStatus: job.JobStatusError,
|
||||||
|
expectedStatus: models.TaskStatusFailed,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
inputStatus: job.JobStatusSuccess,
|
||||||
|
expectedStatus: models.TaskStatusSucceed,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range cases {
|
||||||
|
err := UpdateTask(mgr, 1, c.inputStatus)
|
||||||
|
require.Nil(t, err)
|
||||||
|
assert.Equal(t, c.expectedStatus, mgr.status)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user