Skip to content

Commit

Permalink
Ⓜ️ Add final tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
jsnjack committed Feb 25, 2020
1 parent 5046ced commit 46a6eca
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
14 changes: 13 additions & 1 deletion src/backend/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

// ItemStatus handles information about the item status (currently is used for
// both Builds and Tasks)
// both Builds and Tasks) and type of OnStatus changed tasks
type ItemStatus string

// StatusRunning ...
Expand All @@ -39,6 +39,9 @@ const StatusPending = "pending"
// StatusAborted ...
const StatusAborted = "aborted"

// FinalTask is the task that is executed no matter what is the result of the build
const FinalTask = "finally"

// Build ...
type Build struct {
ID int
Expand Down Expand Up @@ -96,11 +99,13 @@ func (b *Build) runOnStatusTasks(status ItemStatus) {
if task.Kind == string(status) {
task.Status = StatusRunning
task.startedAt = time.Now()
b.BroadcastUpdate()

status := b.runTask(task)

task.Status = status
task.duration = time.Since(task.startedAt)
b.BroadcastUpdate()
}
}
}
Expand Down Expand Up @@ -196,6 +201,8 @@ func (b *Build) runTask(task *Task) ItemStatus {

// Abort message was recieved via channel
if b.aborted {
// Toggle status back for OnStatus tasks
b.aborted = false
return StatusAborted
}

Expand Down Expand Up @@ -384,6 +391,7 @@ func (b *Build) GetTasksStatus() []*TaskStatus {
func (b *Build) SetBuildStatus(status ItemStatus) {
b.Logger.Printf("Status: %s\n", status)
b.Status = status
b.BroadcastUpdate()
defer b.BroadcastUpdate()
// Wait for pending task to finish before running anything else
b.pendingTasksWG.Wait()
Expand Down Expand Up @@ -417,21 +425,25 @@ func (b *Build) SetBuildStatus(status ItemStatus) {
break
case StatusAborted:
b.runOnStatusTasks(status)
b.runOnStatusTasks(FinalTask)
b.Duration = time.Since(b.StartedAt)
b.Cleanup()
break
case StatusFailed:
b.runOnStatusTasks(status)
b.runOnStatusTasks(FinalTask)
b.Duration = time.Since(b.StartedAt)
b.Cleanup()
break
case StatusFinished:
b.CollectArtifacts()
b.runOnStatusTasks(status)
b.runOnStatusTasks(FinalTask)
b.Duration = time.Since(b.StartedAt)
b.Cleanup()
break
}

}

// CreateBuild creates Build instance and all necessary files and folders in wakespace
Expand Down
8 changes: 8 additions & 0 deletions src/backend/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ type OnTasks struct {
OnFailed []*Task `yaml:"on_failed"`
OnAborted []*Task `yaml:"on_aborted"`
OnFinished []*Task `yaml:"on_finished"`
Finally []*Task `yaml:"finally"`
}

// CreateJobFromFile reads job from a file
Expand Down Expand Up @@ -150,6 +151,13 @@ func CreateJobFromFile(path string) (*Job, error) {
job.Tasks = append(job.Tasks, ot.OnFinished...)
}

if ot.Finally != nil {
for _, t := range ot.Finally {
t.Kind = "finally"
}
job.Tasks = append(job.Tasks, ot.Finally...)
}

// Assign tasks ids and status
for i, t := range job.Tasks {
t.ID = i
Expand Down
8 changes: 8 additions & 0 deletions src/frontend/src/assets/configDescription.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ tasks:

# List of patterns according to https://golang.org/pkg/path/filepath/#Match
# related to the workspace directory
# Note:
# - artifacts are collected only for builds with status `finished`
# - `on_finished` and `finally` tasks are executed after artifacts are collected
artifacts:
- "*.tar.gz"

Expand All @@ -39,6 +42,11 @@ on_pending:
- name: Log a call
command: logger "Looking for a suitable cow"

# List of tasks that are always executed
finally:
- name: List all files
command: ls -alh

# Available environmetal variables:
# WAKE_BUILD_ID - current build id, e.g. 169
# WAKE_BUILD_WORKSPACE - path to the build's workspace, e.g. ~/workspace/
Expand Down
7 changes: 5 additions & 2 deletions src/frontend/src/components/TaskItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ export default {
return `task #${this.task.id}`;
},
isVisible: function() {
// Show only "main" tasks or tasks that were started. For example,
// Show only "main" and "finally" tasks or tasks that were started. For example,
// there is no need to show "finished" tasks if build failed because
// they won't be executed anyway
if (this.task.kind === "main") {
if (this.task.kind === "main" || this.task.kind === "finally") {
return true;
}
return !(
Expand Down Expand Up @@ -226,4 +226,7 @@ section {
.border-finished {
border-left-color: $success-color;
}
.border-finally {
border-left-color: $dark-color;
}
</style>

0 comments on commit 46a6eca

Please sign in to comment.