feat: add status starting

This commit is contained in:
Denis Gukov 2023-09-12 20:58:44 +02:00
parent 0522ad169f
commit aef7cbca04
9 changed files with 55 additions and 36 deletions

View File

@ -130,7 +130,15 @@ func StopTask(w http.ResponseWriter, r *http.Request) {
return
}
err := helpers.TaskPool(r).StopTask(targetTask)
var stopObj struct {
Force bool `json:"force"`
}
if !helpers.Bind(w, r, &stopObj) {
return
}
err := helpers.TaskPool(r).StopTask(targetTask, stopObj.Force)
if err != nil {
helpers.WriteError(w, err)
return

View File

@ -51,7 +51,7 @@ func GetRunner(w http.ResponseWriter, r *http.Request) {
continue
}
if tsk.Task.Status == db.TaskRunningStatus {
if tsk.Task.Status == db.TaskStartingStatus {
data.NewJobs = append(data.NewJobs, runners.JobData{
Username: tsk.Username,

View File

@ -8,6 +8,7 @@ type TaskStatus string
const (
TaskWaitingStatus TaskStatus = "waiting"
TaskStartingStatus TaskStatus = "starting"
TaskRunningStatus TaskStatus = "running"
TaskStoppingStatus TaskStatus = "stopping"
TaskStoppedStatus TaskStatus = "stopped"

View File

@ -229,6 +229,9 @@ func (t *LocalJob) destroyKeys() {
}
func (t *LocalJob) Run(username string, incomingVersion *string) (err error) {
t.SetStatus(db.TaskRunningStatus)
err = t.prepareRun()
if err != nil {
return err

View File

@ -205,7 +205,7 @@ func CreateTaskPool(store db.Store) TaskPool {
}
}
func (p *TaskPool) StopTask(targetTask db.Task) error {
func (p *TaskPool) StopTask(targetTask db.Task, forceStop bool) error {
tsk := p.GetTask(targetTask.ID)
if tsk == nil { // task not active, but exists in database
tsk = &TaskRunner{
@ -221,7 +221,11 @@ func (p *TaskPool) StopTask(targetTask db.Task) error {
} else {
status := tsk.Task.Status
tsk.SetStatus(db.TaskStoppingStatus)
if forceStop {
tsk.SetStatus(db.TaskStoppedStatus)
} else {
tsk.SetStatus(db.TaskStoppingStatus)
}
if status == db.TaskRunningStatus {
tsk.kill()
@ -328,7 +332,7 @@ func (p *TaskPool) AddTask(taskObj db.Task, userID *int, projectID int) (newTask
err = taskRunner.populateDetails()
if err != nil {
taskRunner.Log("Error: " + err.Error())
taskRunner.fail()
taskRunner.SetStatus(db.TaskFailStatus)
return
}

View File

@ -61,7 +61,7 @@ func (t *TaskRunner) SetStatus(status db.TaskStatus) {
return
}
switch t.Task.Status {
switch t.Task.Status { // check old status
case db.TaskRunningStatus:
if status == db.TaskWaitingStatus {
//panic("running TaskRunner cannot be " + status)
@ -81,18 +81,13 @@ func (t *TaskRunner) SetStatus(status db.TaskStatus) {
return
}
//if t.Task.Status == db.TaskStoppingStatus {
// switch status {
// case db.TaskFailStatus:
// status = db.TaskStoppedStatus
// case db.TaskStoppedStatus:
// default:
// panic("stopping TaskRunner cannot be " + status)
// }
//}
t.Task.Status = status
if status == db.TaskRunningStatus {
now := time.Now()
t.Task.Start = &now
}
t.saveStatus()
if status == db.TaskFailStatus {
@ -132,10 +127,6 @@ func (t *TaskRunner) kill() {
t.job.Kill()
}
func (t *TaskRunner) fail() {
t.SetStatus(db.TaskFailStatus)
}
func (t *TaskRunner) createTaskEvent() {
objType := db.EventTask
desc := "Task ID " + strconv.Itoa(t.Task.ID) + " (" + t.Template.Name + ")" + " finished - " + strings.ToUpper(string(t.Task.Status))
@ -170,15 +161,13 @@ func (t *TaskRunner) run() {
t.createTaskEvent()
}()
// TODO: more details
// Mark task as stopped if user stopped task during preparation (before task run).
if t.Task.Status == db.TaskStoppingStatus {
t.SetStatus(db.TaskStoppedStatus)
return
}
now := time.Now()
t.Task.Start = &now
t.SetStatus(db.TaskRunningStatus)
t.SetStatus(db.TaskStartingStatus)
objType := db.EventTask
desc := "Task ID " + strconv.Itoa(t.Task.ID) + " (" + t.Template.Name + ")" + " is running"
@ -199,12 +188,6 @@ func (t *TaskRunner) run() {
t.Log("Started: " + strconv.Itoa(t.Task.ID))
t.Log("Run TaskRunner with template: " + t.Template.Name + "\n")
// Mark task as stopped if user stops task during preparation (before task run).
if t.Task.Status == db.TaskStoppingStatus {
t.SetStatus(db.TaskStoppedStatus)
return
}
var username string
var incomingVersion *string
@ -225,7 +208,7 @@ func (t *TaskRunner) run() {
if err != nil {
t.Log("Running playbook failed: " + err.Error())
t.fail()
t.SetStatus(db.TaskFailStatus)
return
}
@ -262,7 +245,7 @@ func (t *TaskRunner) prepareError(err error, errMsg string) error {
}
if err != nil {
t.fail()
t.SetStatus(db.TaskFailStatus)
panic(err)
}

View File

@ -67,11 +67,12 @@
<v-btn
color="error"
style="position: absolute; bottom: 10px; right: 10px;"
v-if="item.status === 'running' || item.status === 'waiting'"
@click="stopTask()"
v-if="canStop"
@click="stopTask(item.status === 'stopping')"
>
{{ $t('stop') }}
{{ item.status === 'stopping' ? $t('forceStop') : $t('stop') }}
</v-btn>
</div>
</template>
@ -139,6 +140,7 @@ export default {
user: {},
};
},
watch: {
async itemId() {
this.reset();
@ -150,17 +152,27 @@ export default {
await this.loadData();
},
},
computed: {
canStop() {
return ['running', 'stopping', 'waiting', 'starting'].includes(this.item.status);
},
},
async created() {
socket.addListener((data) => this.onWebsocketDataReceived(data));
await this.loadData();
},
methods: {
async stopTask() {
async stopTask(force) {
await axios({
method: 'post',
url: `/api/project/${this.projectId}/tasks/${this.itemId}/stop`,
responseType: 'json',
data: {
force,
},
});
},

View File

@ -10,6 +10,7 @@ import IndeterminateProgressCircular from '@/components/IndeterminateProgressCir
const TaskStatus = Object.freeze({
WAITING: 'waiting',
STARTING: 'starting',
RUNNING: 'running',
SUCCESS: 'success',
ERROR: 'error',
@ -28,6 +29,8 @@ export default {
switch (status) {
case TaskStatus.WAITING:
return 'mdi-alarm';
case TaskStatus.STARTING:
return 'mdi-play-circle';
case TaskStatus.RUNNING:
return '';
case TaskStatus.SUCCESS:
@ -47,6 +50,8 @@ export default {
switch (status) {
case TaskStatus.WAITING:
return 'Waiting';
case TaskStatus.STARTING:
return 'Starting...';
case TaskStatus.RUNNING:
return 'Running';
case TaskStatus.SUCCESS:
@ -66,6 +71,8 @@ export default {
switch (status) {
case TaskStatus.WAITING:
return '';
case TaskStatus.STARTING:
return 'warning';
case TaskStatus.RUNNING:
return 'primary';
case TaskStatus.SUCCESS:

View File

@ -133,6 +133,7 @@ export default {
author: 'Author',
duration: 'Duration',
stop: 'Stop',
forceStop: 'Force Stop',
deleteTeamMember: 'Delete team member',
team2: 'Team',
newTeamMember: 'New Team Member',