Merge pull request #7213 from ywk253100/190324_status

Convert job status to replication task status
This commit is contained in:
Wenkai Yin 2019-03-24 18:32:36 +08:00 committed by GitHub
commit 084ae573ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 144 additions and 3 deletions

View File

@ -24,6 +24,7 @@ import (
"github.com/goharbor/harbor/src/common/utils/log"
"github.com/goharbor/harbor/src/core/api"
"github.com/goharbor/harbor/src/replication/ng"
"github.com/goharbor/harbor/src/replication/ng/hook"
)
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.
type Handler struct {
api.BaseController
id int64
status string
id int64
status string
rawStatus string
}
// Prepare ...
@ -60,6 +62,7 @@ func (h *Handler) Prepare() {
h.Abort("200")
return
}
h.rawStatus = data.Status
status, ok := statusMap[data.Status]
if !ok {
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
func (h *Handler) HandleReplicationTask() {
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)
h.HandleInternalServerError(err.Error())
return

View 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)
}

View 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)
}
}