Skip to content

Commit

Permalink
Added start reason to pipelines. (#235)
Browse files Browse the repository at this point in the history
* Added start reason to pipelines.

* Fixed failing test

* Fixed linter

* Removed redundant sentence
  • Loading branch information
Skarlso authored Apr 24, 2020
1 parent a548246 commit 8913a7e
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 29 deletions.
33 changes: 19 additions & 14 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"babel-eslint": "^10.0.1",
"eslint": "^5.16.0",
"eslint-plugin-vue": "^5.0.0",
"node-sass": "^4.9.0",
"node-sass": "^4.13.1",
"sass-loader": "^7.1.0",
"vue-runtime-helpers": "^1.0.1",
"vue-template-compiler": "^2.6.10"
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/views/pipeline/detail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<span v-else>{{ props.row.status }}</span>
</span>
<span v-if="props.column.field === 'duration'">{{ calculateDuration(props.row.startdate, props.row.finishdate) }}</span>
<span v-if="props.column.field === 'reason'">{{ props.row.started_reason }}</span>
<span v-if="props.column.field === 'action'">
<a v-on:click="stopPipelineModal(pipelineID, props.row.id)"><i class="fa fa-ban"
style="color: whitesmoke;"></i></a>
Expand Down Expand Up @@ -165,6 +166,10 @@ export default {
label: 'Duration',
field: 'duration'
},
{
label: 'Reason',
field: 'reason'
},
{
label: 'Action',
field: 'action'
Expand Down
10 changes: 10 additions & 0 deletions gaia.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ const (

// ExecutablePermission is the permission used for gaia created executables.
ExecutablePermission = 0700

// StartReasonRemote label for pipelines which were triggered through a remote token.
StartReasonRemote = "remote"

// StartReasonManual label for pipelines which were triggered through the admin site.
StartReasonManual = "manual"

// StartReasonScheduled label for pipelines which were triggered automated process, i.e. cron job.
StartReasonScheduled = "scheduled"
)

// User is the user object
Expand Down Expand Up @@ -254,6 +263,7 @@ type PipelineRun struct {
ID int `json:"id"`
PipelineID int `json:"pipelineid"`
StartDate time.Time `json:"startdate,omitempty"`
StartReason string `json:"started_reason"`
FinishDate time.Time `json:"finishdate,omitempty"`
ScheduleDate time.Time `json:"scheduledate,omitempty"`
Status PipelineRunStatus `json:"status,omitempty"`
Expand Down
6 changes: 3 additions & 3 deletions handlers/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func PipelineUpdate(c echo.Context) error {
// Iterate over all cron schedules.
for _, schedule := range p.PeriodicSchedules {
err := foundPipeline.CronInst.AddFunc(schedule, func() {
_, err := schedulerService.SchedulePipeline(&foundPipeline, []*gaia.Argument{})
_, err := schedulerService.SchedulePipeline(&foundPipeline, gaia.StartReasonScheduled, []*gaia.Argument{})
if err != nil {
gaia.Cfg.Logger.Error("cannot schedule pipeline from periodic schedule", "error", err, "pipeline", foundPipeline)
return
Expand Down Expand Up @@ -342,7 +342,7 @@ func PipelineTrigger(c echo.Context) error {
schedulerService, _ := services.SchedulerService()
var args []*gaia.Argument
_ = c.Bind(&args)
pipelineRun, err := schedulerService.SchedulePipeline(&foundPipeline, args)
pipelineRun, err := schedulerService.SchedulePipeline(&foundPipeline, gaia.StartReasonRemote, args)
if err != nil {
return c.String(http.StatusBadRequest, err.Error())
} else if pipelineRun != nil {
Expand Down Expand Up @@ -456,7 +456,7 @@ func PipelineStart(c echo.Context) error {
foundPipeline.Docker = docker

if foundPipeline.Name != "" {
pipelineRun, err := schedulerService.SchedulePipeline(&foundPipeline, args)
pipelineRun, err := schedulerService.SchedulePipeline(&foundPipeline, gaia.StartReasonManual, args)
if err != nil {
return c.String(http.StatusBadRequest, err.Error())
} else if pipelineRun != nil {
Expand Down
4 changes: 2 additions & 2 deletions handlers/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type mockScheduleService struct {
err error
}

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

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

expectedBody := `{"uniqueid":"","id":999,"pipelineid":0,"startdate":"0001-01-01T00:00:00Z","finishdate":"0001-01-01T00:00:00Z","scheduledate":"0001-01-01T00:00:00Z"}
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"}
`
body, _ := ioutil.ReadAll(rec.Body)
if string(body) != expectedBody {
Expand Down
2 changes: 1 addition & 1 deletion workers/agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type mockScheduler struct {

func (ms *mockScheduler) SetPipelineJobs(p *gaia.Pipeline) error { return ms.err }
func (ms *mockScheduler) GetFreeWorkers() int32 { return int32(0) }
func (ms *mockScheduler) SchedulePipeline(p *gaia.Pipeline, args []*gaia.Argument) (*gaia.PipelineRun, error) {
func (ms *mockScheduler) SchedulePipeline(p *gaia.Pipeline, startedBy string, args []*gaia.Argument) (*gaia.PipelineRun, error) {
return nil, ms.err
}

Expand Down
2 changes: 1 addition & 1 deletion workers/pipeline/create_pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type mockScheduler struct {
}

func (ms *mockScheduler) Init() {}
func (ms *mockScheduler) SchedulePipeline(p *gaia.Pipeline, args []*gaia.Argument) (*gaia.PipelineRun, error) {
func (ms *mockScheduler) SchedulePipeline(p *gaia.Pipeline, startedBy string, args []*gaia.Argument) (*gaia.PipelineRun, error) {
return nil, nil
}
func (ms *mockScheduler) SetPipelineJobs(p *gaia.Pipeline) error { return ms.Error }
Expand Down
2 changes: 1 addition & 1 deletion workers/pipeline/ticker.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func checkActivePipelines() {
// Iterate over all cron schedules.
for _, schedule := range pipeline.PeriodicSchedules {
err := pipeline.CronInst.AddFunc(schedule, func() {
_, err := schedulerService.SchedulePipeline(pipeline, []*gaia.Argument{})
_, err := schedulerService.SchedulePipeline(pipeline, gaia.StartReasonScheduled, []*gaia.Argument{})
if err != nil {
gaia.Cfg.Logger.Error("cannot schedule pipeline from periodic schedule", "error", err, "pipeline", pipeline)
return
Expand Down
5 changes: 3 additions & 2 deletions workers/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ var (
// GaiaScheduler is a job scheduler for gaia pipeline runs.
type GaiaScheduler interface {
Init()
SchedulePipeline(p *gaia.Pipeline, args []*gaia.Argument) (*gaia.PipelineRun, error)
SchedulePipeline(p *gaia.Pipeline, startedBy string, args []*gaia.Argument) (*gaia.PipelineRun, error)
SetPipelineJobs(p *gaia.Pipeline) error
StopPipelineRun(p *gaia.Pipeline, runID int) error
GetFreeWorkers() int32
Expand Down Expand Up @@ -368,7 +368,7 @@ var schedulerLock = sync.RWMutex{}

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

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

// Put run into store
Expand Down
8 changes: 4 additions & 4 deletions workers/scheduler/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ func TestSchedulePipeline(t *testing.T) {
t.Fatal(err)
}
s.Init()
_, err = s.SchedulePipeline(&p, prepareArgs())
_, err = s.SchedulePipeline(&p, gaia.StartReasonManual, prepareArgs())
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -453,11 +453,11 @@ func TestSchedulePipelineParallel(t *testing.T) {
var wg sync.WaitGroup
wg.Add(2)
go func() {
run1, _ = s.SchedulePipeline(&p1, prepareArgs())
run1, _ = s.SchedulePipeline(&p1, gaia.StartReasonManual, prepareArgs())
wg.Done()
}()
go func() {
run2, _ = s.SchedulePipeline(&p2, prepareArgs())
run2, _ = s.SchedulePipeline(&p2, gaia.StartReasonManual, prepareArgs())
wg.Done()
}()
wg.Wait()
Expand Down Expand Up @@ -488,7 +488,7 @@ func TestSchedule(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = s.SchedulePipeline(&p, prepareArgs())
_, err = s.SchedulePipeline(&p, gaia.StartReasonManual, prepareArgs())
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 8913a7e

Please sign in to comment.