Skip to content

Commit 8913a7e

Browse files
authored
Added start reason to pipelines. (#235)
* Added start reason to pipelines. * Fixed failing test * Fixed linter * Removed redundant sentence
1 parent a548246 commit 8913a7e

File tree

11 files changed

+50
-29
lines changed

11 files changed

+50
-29
lines changed

frontend/package-lock.json

+19-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
"babel-eslint": "^10.0.1",
7777
"eslint": "^5.16.0",
7878
"eslint-plugin-vue": "^5.0.0",
79-
"node-sass": "^4.9.0",
79+
"node-sass": "^4.13.1",
8080
"sass-loader": "^7.1.0",
8181
"vue-runtime-helpers": "^1.0.1",
8282
"vue-template-compiler": "^2.6.10"

frontend/src/views/pipeline/detail.vue

+5
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
<span v-else>{{ props.row.status }}</span>
8787
</span>
8888
<span v-if="props.column.field === 'duration'">{{ calculateDuration(props.row.startdate, props.row.finishdate) }}</span>
89+
<span v-if="props.column.field === 'reason'">{{ props.row.started_reason }}</span>
8990
<span v-if="props.column.field === 'action'">
9091
<a v-on:click="stopPipelineModal(pipelineID, props.row.id)"><i class="fa fa-ban"
9192
style="color: whitesmoke;"></i></a>
@@ -165,6 +166,10 @@ export default {
165166
label: 'Duration',
166167
field: 'duration'
167168
},
169+
{
170+
label: 'Reason',
171+
field: 'reason'
172+
},
168173
{
169174
label: 'Action',
170175
field: 'action'

gaia.go

+10
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,15 @@ const (
139139

140140
// ExecutablePermission is the permission used for gaia created executables.
141141
ExecutablePermission = 0700
142+
143+
// StartReasonRemote label for pipelines which were triggered through a remote token.
144+
StartReasonRemote = "remote"
145+
146+
// StartReasonManual label for pipelines which were triggered through the admin site.
147+
StartReasonManual = "manual"
148+
149+
// StartReasonScheduled label for pipelines which were triggered automated process, i.e. cron job.
150+
StartReasonScheduled = "scheduled"
142151
)
143152

144153
// User is the user object
@@ -254,6 +263,7 @@ type PipelineRun struct {
254263
ID int `json:"id"`
255264
PipelineID int `json:"pipelineid"`
256265
StartDate time.Time `json:"startdate,omitempty"`
266+
StartReason string `json:"started_reason"`
257267
FinishDate time.Time `json:"finishdate,omitempty"`
258268
ScheduleDate time.Time `json:"scheduledate,omitempty"`
259269
Status PipelineRunStatus `json:"status,omitempty"`

handlers/pipeline.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func PipelineUpdate(c echo.Context) error {
196196
// Iterate over all cron schedules.
197197
for _, schedule := range p.PeriodicSchedules {
198198
err := foundPipeline.CronInst.AddFunc(schedule, func() {
199-
_, err := schedulerService.SchedulePipeline(&foundPipeline, []*gaia.Argument{})
199+
_, err := schedulerService.SchedulePipeline(&foundPipeline, gaia.StartReasonScheduled, []*gaia.Argument{})
200200
if err != nil {
201201
gaia.Cfg.Logger.Error("cannot schedule pipeline from periodic schedule", "error", err, "pipeline", foundPipeline)
202202
return
@@ -342,7 +342,7 @@ func PipelineTrigger(c echo.Context) error {
342342
schedulerService, _ := services.SchedulerService()
343343
var args []*gaia.Argument
344344
_ = c.Bind(&args)
345-
pipelineRun, err := schedulerService.SchedulePipeline(&foundPipeline, args)
345+
pipelineRun, err := schedulerService.SchedulePipeline(&foundPipeline, gaia.StartReasonRemote, args)
346346
if err != nil {
347347
return c.String(http.StatusBadRequest, err.Error())
348348
} else if pipelineRun != nil {
@@ -456,7 +456,7 @@ func PipelineStart(c echo.Context) error {
456456
foundPipeline.Docker = docker
457457

458458
if foundPipeline.Name != "" {
459-
pipelineRun, err := schedulerService.SchedulePipeline(&foundPipeline, args)
459+
pipelineRun, err := schedulerService.SchedulePipeline(&foundPipeline, gaia.StartReasonManual, args)
460460
if err != nil {
461461
return c.String(http.StatusBadRequest, err.Error())
462462
} else if pipelineRun != nil {

handlers/pipeline_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type mockScheduleService struct {
2828
err error
2929
}
3030

31-
func (ms *mockScheduleService) SchedulePipeline(p *gaia.Pipeline, args []*gaia.Argument) (*gaia.PipelineRun, error) {
31+
func (ms *mockScheduleService) SchedulePipeline(p *gaia.Pipeline, startReason string, args []*gaia.Argument) (*gaia.PipelineRun, error) {
3232
return ms.pipelineRun, ms.err
3333
}
3434

@@ -442,7 +442,7 @@ func TestPipelineStart(t *testing.T) {
442442
t.Fatalf("expected response code %v got %v", http.StatusCreated, rec.Code)
443443
}
444444

445-
expectedBody := `{"uniqueid":"","id":999,"pipelineid":0,"startdate":"0001-01-01T00:00:00Z","finishdate":"0001-01-01T00:00:00Z","scheduledate":"0001-01-01T00:00:00Z"}
445+
expectedBody := `{"uniqueid":"","id":999,"pipelineid":0,"startdate":"0001-01-01T00:00:00Z","started_reason":"","finishdate":"0001-01-01T00:00:00Z","scheduledate":"0001-01-01T00:00:00Z"}
446446
`
447447
body, _ := ioutil.ReadAll(rec.Body)
448448
if string(body) != expectedBody {

workers/agent/agent_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type mockScheduler struct {
4242

4343
func (ms *mockScheduler) SetPipelineJobs(p *gaia.Pipeline) error { return ms.err }
4444
func (ms *mockScheduler) GetFreeWorkers() int32 { return int32(0) }
45-
func (ms *mockScheduler) SchedulePipeline(p *gaia.Pipeline, args []*gaia.Argument) (*gaia.PipelineRun, error) {
45+
func (ms *mockScheduler) SchedulePipeline(p *gaia.Pipeline, startedBy string, args []*gaia.Argument) (*gaia.PipelineRun, error) {
4646
return nil, ms.err
4747
}
4848

workers/pipeline/create_pipeline_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type mockScheduler struct {
3232
}
3333

3434
func (ms *mockScheduler) Init() {}
35-
func (ms *mockScheduler) SchedulePipeline(p *gaia.Pipeline, args []*gaia.Argument) (*gaia.PipelineRun, error) {
35+
func (ms *mockScheduler) SchedulePipeline(p *gaia.Pipeline, startedBy string, args []*gaia.Argument) (*gaia.PipelineRun, error) {
3636
return nil, nil
3737
}
3838
func (ms *mockScheduler) SetPipelineJobs(p *gaia.Pipeline) error { return ms.Error }

workers/pipeline/ticker.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ func checkActivePipelines() {
217217
// Iterate over all cron schedules.
218218
for _, schedule := range pipeline.PeriodicSchedules {
219219
err := pipeline.CronInst.AddFunc(schedule, func() {
220-
_, err := schedulerService.SchedulePipeline(pipeline, []*gaia.Argument{})
220+
_, err := schedulerService.SchedulePipeline(pipeline, gaia.StartReasonScheduled, []*gaia.Argument{})
221221
if err != nil {
222222
gaia.Cfg.Logger.Error("cannot schedule pipeline from periodic schedule", "error", err, "pipeline", pipeline)
223223
return

workers/scheduler/scheduler.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ var (
6262
// GaiaScheduler is a job scheduler for gaia pipeline runs.
6363
type GaiaScheduler interface {
6464
Init()
65-
SchedulePipeline(p *gaia.Pipeline, args []*gaia.Argument) (*gaia.PipelineRun, error)
65+
SchedulePipeline(p *gaia.Pipeline, startedBy string, args []*gaia.Argument) (*gaia.PipelineRun, error)
6666
SetPipelineJobs(p *gaia.Pipeline) error
6767
StopPipelineRun(p *gaia.Pipeline, runID int) error
6868
GetFreeWorkers() int32
@@ -368,7 +368,7 @@ var schedulerLock = sync.RWMutex{}
368368

369369
// SchedulePipeline schedules a pipeline. We create a new schedule object
370370
// and save it in our store. The scheduler will later pick this up and will continue the work.
371-
func (s *Scheduler) SchedulePipeline(p *gaia.Pipeline, args []*gaia.Argument) (*gaia.PipelineRun, error) {
371+
func (s *Scheduler) SchedulePipeline(p *gaia.Pipeline, startedReason string, args []*gaia.Argument) (*gaia.PipelineRun, error) {
372372

373373
// Introduce a semaphore locking here because this function can be called
374374
// in parallel if multiple users happen to trigger a pipeline run at the same time.
@@ -443,6 +443,7 @@ func (s *Scheduler) SchedulePipeline(p *gaia.Pipeline, args []*gaia.Argument) (*
443443
PipelineType: p.Type,
444444
PipelineTags: p.Tags,
445445
Docker: p.Docker,
446+
StartReason: startedReason,
446447
}
447448

448449
// Put run into store

workers/scheduler/scheduler_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ func TestSchedulePipeline(t *testing.T) {
407407
t.Fatal(err)
408408
}
409409
s.Init()
410-
_, err = s.SchedulePipeline(&p, prepareArgs())
410+
_, err = s.SchedulePipeline(&p, gaia.StartReasonManual, prepareArgs())
411411
if err != nil {
412412
t.Fatal(err)
413413
}
@@ -453,11 +453,11 @@ func TestSchedulePipelineParallel(t *testing.T) {
453453
var wg sync.WaitGroup
454454
wg.Add(2)
455455
go func() {
456-
run1, _ = s.SchedulePipeline(&p1, prepareArgs())
456+
run1, _ = s.SchedulePipeline(&p1, gaia.StartReasonManual, prepareArgs())
457457
wg.Done()
458458
}()
459459
go func() {
460-
run2, _ = s.SchedulePipeline(&p2, prepareArgs())
460+
run2, _ = s.SchedulePipeline(&p2, gaia.StartReasonManual, prepareArgs())
461461
wg.Done()
462462
}()
463463
wg.Wait()
@@ -488,7 +488,7 @@ func TestSchedule(t *testing.T) {
488488
if err != nil {
489489
t.Fatal(err)
490490
}
491-
_, err = s.SchedulePipeline(&p, prepareArgs())
491+
_, err = s.SchedulePipeline(&p, gaia.StartReasonManual, prepareArgs())
492492
if err != nil {
493493
t.Fatal(err)
494494
}

0 commit comments

Comments
 (0)