mirror of
https://github.com/goharbor/harbor.git
synced 2024-09-16 16:04:27 +02:00
Merge pull request #8444 from bitsf/tag_retention_exec_status
get execution status on the fly
This commit is contained in:
commit
c99f12fe33
@ -109,16 +109,9 @@ create table retention_execution
|
|||||||
(
|
(
|
||||||
id serial PRIMARY KEY NOT NULL,
|
id serial PRIMARY KEY NOT NULL,
|
||||||
policy_id integer,
|
policy_id integer,
|
||||||
status varchar(20),
|
|
||||||
dry_run boolean,
|
dry_run boolean,
|
||||||
trigger varchar(20),
|
trigger varchar(20),
|
||||||
total integer,
|
start_time timestamp
|
||||||
succeed integer,
|
|
||||||
failed integer,
|
|
||||||
in_progress integer,
|
|
||||||
stopped integer,
|
|
||||||
start_time timestamp,
|
|
||||||
end_time timestamp
|
|
||||||
);
|
);
|
||||||
|
|
||||||
create table retention_task
|
create table retention_task
|
||||||
|
@ -190,7 +190,7 @@ func (r *RetentionAPI) CreateRetention() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := r.pm.GetMetadataManager().Add(p.Scope.Reference,
|
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.SendInternalServerError(err)
|
||||||
}
|
}
|
||||||
r.Redirect(http.StatusCreated, strconv.FormatInt(id, 10))
|
r.Redirect(http.StatusCreated, strconv.FormatInt(id, 10))
|
||||||
|
@ -192,26 +192,13 @@ func (r *DefaultAPIController) TriggerRetentionExec(policyID int64, trigger stri
|
|||||||
exec := &Execution{
|
exec := &Execution{
|
||||||
PolicyID: policyID,
|
PolicyID: policyID,
|
||||||
StartTime: time.Now(),
|
StartTime: time.Now(),
|
||||||
Status: ExecutionStatusInProgress,
|
|
||||||
Trigger: trigger,
|
Trigger: trigger,
|
||||||
DryRun: dryRun,
|
DryRun: dryRun,
|
||||||
}
|
}
|
||||||
id, err := r.manager.CreateExecution(exec)
|
id, err := r.manager.CreateExecution(exec)
|
||||||
num, err := r.launcher.Launch(p, id, dryRun)
|
if _, err = r.launcher.Launch(p, id, dryRun); err != nil {
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
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
|
return id, err
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,14 @@ import (
|
|||||||
"github.com/astaxie/beego/orm"
|
"github.com/astaxie/beego/orm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// const definitions
|
||||||
|
const (
|
||||||
|
ExecutionStatusInProgress string = "InProgress"
|
||||||
|
ExecutionStatusSucceed string = "Succeed"
|
||||||
|
ExecutionStatusFailed string = "Failed"
|
||||||
|
ExecutionStatusStopped string = "Stopped"
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
orm.RegisterModel(
|
orm.RegisterModel(
|
||||||
new(RetentionPolicy),
|
new(RetentionPolicy),
|
||||||
@ -31,17 +39,18 @@ type RetentionPolicy struct {
|
|||||||
type RetentionExecution struct {
|
type RetentionExecution struct {
|
||||||
ID int64 `orm:"pk;auto;column(id)" json:"id"`
|
ID int64 `orm:"pk;auto;column(id)" json:"id"`
|
||||||
PolicyID int64 `orm:"column(policy_id)"`
|
PolicyID int64 `orm:"column(policy_id)"`
|
||||||
Status string
|
|
||||||
DryRun bool
|
DryRun bool
|
||||||
// manual, scheduled
|
// manual, scheduled
|
||||||
Trigger string
|
Trigger string
|
||||||
Total int
|
|
||||||
Succeed int
|
|
||||||
Failed int
|
|
||||||
InProgress int
|
|
||||||
Stopped int
|
|
||||||
StartTime time.Time
|
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 ...
|
// RetentionTask ...
|
||||||
|
@ -3,7 +3,6 @@ package dao
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/goharbor/harbor/src/common/dao"
|
"github.com/goharbor/harbor/src/common/dao"
|
||||||
"github.com/goharbor/harbor/src/pkg/retention/dao/models"
|
"github.com/goharbor/harbor/src/pkg/retention/dao/models"
|
||||||
"github.com/goharbor/harbor/src/pkg/retention/q"
|
"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 {
|
if err := o.Read(e); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if err := fillStatus(e); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return e, nil
|
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
|
// ListExecutions List Executions
|
||||||
func ListExecutions(policyID int64, query *q.Query) ([]*models.RetentionExecution, error) {
|
func ListExecutions(policyID int64, query *q.Query) ([]*models.RetentionExecution, error) {
|
||||||
o := dao.GetOrmer()
|
o := dao.GetOrmer()
|
||||||
@ -102,6 +135,11 @@ func ListExecutions(policyID int64, query *q.Query) ([]*models.RetentionExecutio
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
for _, e := range execs {
|
||||||
|
if err := fillStatus(e); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
return execs, nil
|
return execs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,8 +42,6 @@ type Manager interface {
|
|||||||
GetPolicy(ID int64) (*policy.Metadata, error)
|
GetPolicy(ID int64) (*policy.Metadata, error)
|
||||||
// Create a new retention execution
|
// Create a new retention execution
|
||||||
CreateExecution(execution *Execution) (int64, error)
|
CreateExecution(execution *Execution) (int64, error)
|
||||||
// Update the specified execution
|
|
||||||
UpdateExecution(execution *Execution) error
|
|
||||||
// Get the specified execution
|
// Get the specified execution
|
||||||
GetExecution(eid int64) (*Execution, error)
|
GetExecution(eid int64) (*Execution, error)
|
||||||
// List execution histories
|
// List execution histories
|
||||||
@ -123,20 +121,10 @@ func (d *DefaultManager) CreateExecution(execution *Execution) (int64, error) {
|
|||||||
exec.PolicyID = execution.PolicyID
|
exec.PolicyID = execution.PolicyID
|
||||||
exec.StartTime = time.Now()
|
exec.StartTime = time.Now()
|
||||||
exec.DryRun = execution.DryRun
|
exec.DryRun = execution.DryRun
|
||||||
exec.Status = execution.Status
|
|
||||||
exec.Trigger = execution.Trigger
|
exec.Trigger = execution.Trigger
|
||||||
return dao.CreateExecution(exec)
|
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
|
// ListExecutions List Executions
|
||||||
func (d *DefaultManager) ListExecutions(policyID int64, query *q.Query) ([]*Execution, error) {
|
func (d *DefaultManager) ListExecutions(policyID int64, query *q.Query) ([]*Execution, error) {
|
||||||
execs, err := dao.ListExecutions(policyID, query)
|
execs, err := dao.ListExecutions(policyID, query)
|
||||||
|
@ -145,7 +145,6 @@ func TestExecution(t *testing.T) {
|
|||||||
e1 := &Execution{
|
e1 := &Execution{
|
||||||
PolicyID: policyID,
|
PolicyID: policyID,
|
||||||
StartTime: time.Now(),
|
StartTime: time.Now(),
|
||||||
Status: ExecutionStatusInProgress,
|
|
||||||
Trigger: ExecutionTriggerManual,
|
Trigger: ExecutionTriggerManual,
|
||||||
DryRun: false,
|
DryRun: false,
|
||||||
}
|
}
|
||||||
@ -158,15 +157,6 @@ func TestExecution(t *testing.T) {
|
|||||||
assert.NotNil(t, e1)
|
assert.NotNil(t, e1)
|
||||||
assert.EqualValues(t, id, e1.ID)
|
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)
|
es, err := m.ListExecutions(policyID, nil)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.EqualValues(t, 1, len(es))
|
assert.EqualValues(t, 1, len(es))
|
||||||
@ -187,7 +177,7 @@ func TestTask(t *testing.T) {
|
|||||||
// get
|
// get
|
||||||
tk, err := m.GetTask(id)
|
tk, err := m.GetTask(id)
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
assert.Equal(t, id, tk.ExecutionID)
|
assert.EqualValues(t, 1, tk.ExecutionID)
|
||||||
|
|
||||||
// update
|
// update
|
||||||
task.ID = id
|
task.ID = id
|
||||||
|
Loading…
Reference in New Issue
Block a user