/
collect_deploy_time.go
95 lines (84 loc) · 2.89 KB
/
collect_deploy_time.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package events
import (
"time"
"github.com/sirupsen/logrus"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/modules/orchestrator/dbclient"
)
type DeployTimeCollector struct {
manager *EventManager
db *dbclient.DBClient
}
func NewDeployTimeCollector(manager *EventManager, db *dbclient.DBClient) *EventListener {
var l EventListener = &DeployTimeCollector{manager: manager, db: db}
return &l
}
func (c *DeployTimeCollector) OnEvent(event *RuntimeEvent) {
c.collectDeployTimes(event)
}
func (c *DeployTimeCollector) collectDeployTimes(event *RuntimeEvent) {
// only care DeployStatusChanged events
if event.EventName != RuntimeDeployStatusChanged {
return
}
deployment, err := c.db.GetDeployment(event.Deployment.ID)
if err != nil {
logrus.Warnf("failed to get deployment: %v for time collector, err: %v",
event.Deployment.ID, err.Error())
return
}
if deployment == nil {
// not found
return
}
isChanged := true
now := time.Now()
// TODO: need refactor, should introduce deployment_phases table to tracking times
// TODO: Script is a virtual Phase now, it indicate the `pre-service` Phase
switch deployment.Status {
case apistructs.DeploymentStatusDeploying:
// Status is Deploying, entering the Phase, so set the StartAt
switch deployment.Phase {
case apistructs.DeploymentPhaseAddon:
deployment.Extra.AddonPhaseStartAt = &now
case apistructs.DeploymentPhaseScript:
// the end of addon, service start at same time
deployment.Extra.AddonPhaseEndAt = &now
deployment.Extra.ServicePhaseStartAt = &now
case apistructs.DeploymentPhaseCompleted:
deployment.Extra.ServicePhaseEndAt = &now
default:
isChanged = false
}
case apistructs.DeploymentStatusFailed, apistructs.DeploymentStatusCanceled:
// Status is Failed or Canceled, Phase broken (end) at current time
switch deployment.Phase {
case apistructs.DeploymentPhaseAddon:
deployment.Extra.AddonPhaseEndAt = &now
case apistructs.DeploymentPhaseScript, apistructs.DeploymentPhaseService:
deployment.Extra.ServicePhaseEndAt = &now
default:
isChanged = false
}
default:
isChanged = false
}
if isChanged {
if err := c.db.UpdateDeployment(deployment); err != nil {
logrus.Errorf("[alert] failed to update Phase Times into deployment: %d, %v",
deployment.ID, err)
}
}
}