Merge pull request #8444 from bitsf/tag_retention_exec_status

get execution status on the fly
This commit is contained in:
Wenkai Yin(尹文开) 2019-07-29 17:08:37 +08:00 committed by GitHub
commit c99f12fe33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 59 additions and 54 deletions

View File

@ -109,16 +109,9 @@ create table retention_execution
(
id serial PRIMARY KEY NOT NULL,
policy_id integer,
status varchar(20),
dry_run boolean,
trigger varchar(20),
total integer,
succeed integer,
failed integer,
in_progress integer,
stopped integer,
start_time timestamp,
end_time timestamp
start_time timestamp
);
create table retention_task

View File

@ -190,7 +190,7 @@ func (r *RetentionAPI) CreateRetention() {
return
}
if err := r.pm.GetMetadataManager().Add(p.Scope.Reference,
map[string]string{"retention_id": strconv.FormatInt(p.Scope.Reference, 10)}); err != nil {
map[string]string{"retention_id": strconv.FormatInt(id, 10)}); err != nil {
r.SendInternalServerError(err)
}
r.Redirect(http.StatusCreated, strconv.FormatInt(id, 10))

View File

@ -192,26 +192,13 @@ func (r *DefaultAPIController) TriggerRetentionExec(policyID int64, trigger stri
exec := &Execution{
PolicyID: policyID,
StartTime: time.Now(),
Status: ExecutionStatusInProgress,
Trigger: trigger,
DryRun: dryRun,
}
id, err := r.manager.CreateExecution(exec)
num, err := r.launcher.Launch(p, id, dryRun)
if err != nil {
if _, err = r.launcher.Launch(p, id, dryRun); err != nil {
return 0, err
}
if num == 0 {
exec := &Execution{
ID: id,
EndTime: time.Now(),
Status: ExecutionStatusSucceed,
}
err = r.manager.UpdateExecution(exec)
if err != nil {
return 0, err
}
}
return id, err
}

View File

@ -6,6 +6,14 @@ import (
"github.com/astaxie/beego/orm"
)
// const definitions
const (
ExecutionStatusInProgress string = "InProgress"
ExecutionStatusSucceed string = "Succeed"
ExecutionStatusFailed string = "Failed"
ExecutionStatusStopped string = "Stopped"
)
func init() {
orm.RegisterModel(
new(RetentionPolicy),
@ -31,17 +39,18 @@ type RetentionPolicy struct {
type RetentionExecution struct {
ID int64 `orm:"pk;auto;column(id)" json:"id"`
PolicyID int64 `orm:"column(policy_id)"`
Status string
DryRun bool
// manual, scheduled
Trigger string
Total int
Succeed int
Failed int
InProgress int
Stopped int
StartTime time.Time
EndTime time.Time
EndTime time.Time `orm:"-"`
Status string `orm:"-"`
Total int `orm:"-"`
Succeed int `orm:"-"`
Failed int `orm:"-"`
InProgress int `orm:"-"`
Stopped int `orm:"-"`
Pending int `orm:"-"`
}
// RetentionTask ...

View File

@ -3,7 +3,6 @@ package dao
import (
"errors"
"fmt"
"github.com/goharbor/harbor/src/common/dao"
"github.com/goharbor/harbor/src/pkg/retention/dao/models"
"github.com/goharbor/harbor/src/pkg/retention/q"
@ -85,9 +84,43 @@ func GetExecution(id int64) (*models.RetentionExecution, error) {
if err := o.Read(e); err != nil {
return nil, err
}
if err := fillStatus(e); err != nil {
return nil, err
}
return e, nil
}
// fillStatus the priority is InProgress Stopped Failed Succeed
func fillStatus(exec *models.RetentionExecution) error {
o := dao.GetOrmer()
if _, err := o.Raw("select status, count(*) num from retention_task where execution_id = ? group by status", exec.ID).
RowsToStruct(exec, "status", "num"); err != nil {
return err
}
exec.Total = exec.Pending + exec.InProgress + exec.Succeed + exec.Failed + exec.Stopped
if exec.Total == 0 {
exec.Status = models.ExecutionStatusSucceed
exec.EndTime = exec.StartTime
return nil
}
if exec.Pending+exec.InProgress > 0 {
exec.Status = models.ExecutionStatusInProgress
} else if exec.Stopped > 0 {
exec.Status = models.ExecutionStatusStopped
} else if exec.Failed > 0 {
exec.Status = models.ExecutionStatusFailed
} else {
exec.Status = models.ExecutionStatusSucceed
}
if exec.Status != models.ExecutionStatusInProgress {
if err := o.Raw("select max(end_time) from retention_task where execution_id = ?", exec.ID).
QueryRow(&exec.EndTime); err != nil {
return err
}
}
return nil
}
// ListExecutions List Executions
func ListExecutions(policyID int64, query *q.Query) ([]*models.RetentionExecution, error) {
o := dao.GetOrmer()
@ -102,6 +135,11 @@ func ListExecutions(policyID int64, query *q.Query) ([]*models.RetentionExecutio
if err != nil {
return nil, err
}
for _, e := range execs {
if err := fillStatus(e); err != nil {
return nil, err
}
}
return execs, nil
}

View File

@ -42,8 +42,6 @@ type Manager interface {
GetPolicy(ID int64) (*policy.Metadata, error)
// Create a new retention execution
CreateExecution(execution *Execution) (int64, error)
// Update the specified execution
UpdateExecution(execution *Execution) error
// Get the specified execution
GetExecution(eid int64) (*Execution, error)
// List execution histories
@ -123,20 +121,10 @@ func (d *DefaultManager) CreateExecution(execution *Execution) (int64, error) {
exec.PolicyID = execution.PolicyID
exec.StartTime = time.Now()
exec.DryRun = execution.DryRun
exec.Status = execution.Status
exec.Trigger = execution.Trigger
return dao.CreateExecution(exec)
}
// UpdateExecution Update Execution
func (d *DefaultManager) UpdateExecution(execution *Execution) error {
exec := &models.RetentionExecution{}
exec.ID = execution.ID
exec.EndTime = execution.EndTime
exec.Status = execution.Status
return dao.UpdateExecution(exec, "end_time", "status")
}
// ListExecutions List Executions
func (d *DefaultManager) ListExecutions(policyID int64, query *q.Query) ([]*Execution, error) {
execs, err := dao.ListExecutions(policyID, query)

View File

@ -145,7 +145,6 @@ func TestExecution(t *testing.T) {
e1 := &Execution{
PolicyID: policyID,
StartTime: time.Now(),
Status: ExecutionStatusInProgress,
Trigger: ExecutionTriggerManual,
DryRun: false,
}
@ -158,15 +157,6 @@ func TestExecution(t *testing.T) {
assert.NotNil(t, e1)
assert.EqualValues(t, id, e1.ID)
e1.Status = ExecutionStatusFailed
err = m.UpdateExecution(e1)
assert.Nil(t, err)
e1, err = m.GetExecution(id)
assert.Nil(t, err)
assert.NotNil(t, e1)
assert.EqualValues(t, ExecutionStatusFailed, e1.Status)
es, err := m.ListExecutions(policyID, nil)
assert.Nil(t, err)
assert.EqualValues(t, 1, len(es))
@ -187,7 +177,7 @@ func TestTask(t *testing.T) {
// get
tk, err := m.GetTask(id)
require.Nil(t, err)
assert.Equal(t, id, tk.ExecutionID)
assert.EqualValues(t, 1, tk.ExecutionID)
// update
task.ID = id