Skip to content

Commit 1b83be7

Browse files
committed
fix(tasks): stopping task
1 parent 4b8096b commit 1b83be7

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

services/tasks/LocalJob.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,35 @@ import (
1717
)
1818

1919
type LocalJob struct {
20-
// Received constant fields
2120
Task db.Task
2221
Template db.Template
2322
Inventory db.Inventory
2423
Repository db.Repository
2524
Environment db.Environment
26-
Secret string
27-
Logger task_logger.Logger
25+
Secret string // Secret contains secrets received from Survey variables
26+
Logger task_logger.Logger // Logger allows to send logs and status to the server
2827

2928
App db_lib.LocalApp
3029

31-
// Internal field
30+
killed bool // killed means that API request to stop the job has been received
3231
Process *os.Process
3332

3433
sshKeyInstallation db.AccessKeyInstallation
3534
becomeKeyInstallation db.AccessKeyInstallation
3635
vaultFileInstallations map[string]db.AccessKeyInstallation
3736
}
3837

38+
func (t *LocalJob) IsKilled() bool {
39+
return t.killed
40+
}
41+
3942
func (t *LocalJob) Kill() {
43+
t.killed = true
44+
4045
if t.Process == nil {
4146
return
4247
}
48+
4349
err := t.Process.Kill()
4450
if err != nil {
4551
t.Log(err.Error())
@@ -561,6 +567,11 @@ func (t *LocalJob) Run(username string, incomingVersion *string, alias string) (
561567
}
562568
}
563569

570+
if t.killed {
571+
t.SetStatus(task_logger.TaskStoppedStatus)
572+
return nil
573+
}
574+
564575
return t.App.Run(db_lib.LocalAppRunningArgs{
565576
CliArgs: args,
566577
EnvironmentVars: environmentVariables,

services/tasks/RemoteJob.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type RemoteJob struct {
1616
RunnerTag *string
1717
Task db.Task
1818
taskPool *TaskPool
19+
killed bool
1920
}
2021

2122
type runnerWebhookPayload struct {
@@ -162,5 +163,10 @@ func (t *RemoteJob) Run(username string, incomingVersion *string, alias string)
162163
}
163164

164165
func (t *RemoteJob) Kill() {
166+
t.killed = true
165167
// Do nothing because you can't kill remote process
166168
}
169+
170+
func (t *RemoteJob) IsKilled() bool {
171+
return t.killed
172+
}

services/tasks/TaskRunner.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
type Job interface {
2020
Run(username string, incomingVersion *string, alias string) error
2121
Kill()
22+
IsKilled() bool
2223
}
2324

2425
type TaskRunner struct {
@@ -163,8 +164,12 @@ func (t *TaskRunner) run() {
163164
err = t.job.Run(username, incomingVersion, t.Alias)
164165

165166
if err != nil {
166-
t.Log("Running app failed: " + err.Error())
167-
t.SetStatus(task_logger.TaskFailStatus)
167+
if t.job.IsKilled() {
168+
t.SetStatus(task_logger.TaskStoppedStatus)
169+
} else {
170+
t.Log("Running app failed: " + err.Error())
171+
t.SetStatus(task_logger.TaskFailStatus)
172+
}
168173
return
169174
}
170175

0 commit comments

Comments
 (0)