2022-01-29 19:00:21 +01:00
|
|
|
package tasks
|
|
|
|
|
|
|
|
import (
|
2024-06-12 21:53:00 +02:00
|
|
|
"errors"
|
2024-03-09 15:01:20 +01:00
|
|
|
"fmt"
|
2022-01-29 19:00:21 +01:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2024-04-12 12:32:54 +02:00
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
|
|
|
"github.com/ansible-semaphore/semaphore/db_lib"
|
|
|
|
"github.com/ansible-semaphore/semaphore/pkg/task_logger"
|
|
|
|
|
2022-01-29 19:00:21 +01:00
|
|
|
"github.com/ansible-semaphore/semaphore/util"
|
2024-03-09 14:54:27 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-01-29 19:00:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type logRecord struct {
|
|
|
|
task *TaskRunner
|
|
|
|
output string
|
|
|
|
time time.Time
|
|
|
|
}
|
|
|
|
|
2022-02-12 13:15:15 +01:00
|
|
|
type resourceLock struct {
|
|
|
|
lock bool
|
|
|
|
holder *TaskRunner
|
|
|
|
}
|
|
|
|
|
2022-01-29 19:00:21 +01:00
|
|
|
type TaskPool struct {
|
2022-02-03 08:05:13 +01:00
|
|
|
// queue contains list of tasks in status TaskWaitingStatus.
|
|
|
|
queue []*TaskRunner
|
2022-02-12 13:15:15 +01:00
|
|
|
|
2022-02-03 08:05:13 +01:00
|
|
|
// register channel used to put tasks to queue.
|
|
|
|
register chan *TaskRunner
|
|
|
|
|
2024-06-17 20:37:45 +02:00
|
|
|
// activeProj ???
|
2022-02-12 13:15:15 +01:00
|
|
|
activeProj map[int]map[int]*TaskRunner
|
2022-02-03 08:05:13 +01:00
|
|
|
|
2024-06-17 20:37:45 +02:00
|
|
|
// runningTasks contains tasks with status TaskRunningStatus. Map key is a task ID.
|
2022-01-29 19:00:21 +01:00
|
|
|
runningTasks map[int]*TaskRunner
|
2022-02-03 08:05:13 +01:00
|
|
|
|
|
|
|
// logger channel used to putting log records to database.
|
|
|
|
logger chan logRecord
|
|
|
|
|
|
|
|
store db.Store
|
2022-01-29 19:00:21 +01:00
|
|
|
|
2022-02-12 13:15:15 +01:00
|
|
|
resourceLocker chan *resourceLock
|
2023-08-29 00:51:04 +02:00
|
|
|
}
|
2023-08-27 18:02:51 +02:00
|
|
|
|
2024-06-12 21:53:00 +02:00
|
|
|
var ErrInvalidSubscription = errors.New("has no active subscription")
|
|
|
|
|
2023-09-16 23:47:06 +02:00
|
|
|
func (p *TaskPool) GetNumberOfRunningTasksOfRunner(runnerID int) (res int) {
|
|
|
|
for _, task := range p.runningTasks {
|
|
|
|
if task.RunnerID == runnerID {
|
|
|
|
res++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-29 00:51:04 +02:00
|
|
|
func (p *TaskPool) GetRunningTasks() (res []*TaskRunner) {
|
|
|
|
for _, task := range p.runningTasks {
|
|
|
|
res = append(res, task)
|
|
|
|
}
|
|
|
|
return
|
2022-01-29 19:00:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *TaskPool) GetTask(id int) (task *TaskRunner) {
|
|
|
|
|
|
|
|
for _, t := range p.queue {
|
2023-08-29 00:51:04 +02:00
|
|
|
if t.Task.ID == id {
|
2022-01-29 19:00:21 +01:00
|
|
|
task = t
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if task == nil {
|
|
|
|
for _, t := range p.runningTasks {
|
2023-08-29 00:51:04 +02:00
|
|
|
if t.Task.ID == id {
|
2022-01-29 19:00:21 +01:00
|
|
|
task = t
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-09 18:31:38 +01:00
|
|
|
// nolint: gocyclo
|
2022-01-29 19:00:21 +01:00
|
|
|
func (p *TaskPool) Run() {
|
|
|
|
ticker := time.NewTicker(5 * time.Second)
|
|
|
|
|
|
|
|
defer func() {
|
2022-02-12 13:15:15 +01:00
|
|
|
close(p.resourceLocker)
|
2022-01-29 19:00:21 +01:00
|
|
|
ticker.Stop()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Lock or unlock resources when running a TaskRunner
|
|
|
|
go func(locker <-chan *resourceLock) {
|
|
|
|
for l := range locker {
|
|
|
|
t := l.holder
|
|
|
|
|
|
|
|
if l.lock {
|
|
|
|
if p.blocks(t) {
|
|
|
|
panic("Trying to lock an already locked resource!")
|
|
|
|
}
|
|
|
|
|
2023-08-29 00:51:04 +02:00
|
|
|
projTasks, ok := p.activeProj[t.Task.ProjectID]
|
2022-02-12 13:15:15 +01:00
|
|
|
if !ok {
|
|
|
|
projTasks = make(map[int]*TaskRunner)
|
2023-08-29 00:51:04 +02:00
|
|
|
p.activeProj[t.Task.ProjectID] = projTasks
|
2022-01-29 19:00:21 +01:00
|
|
|
}
|
2023-08-29 00:51:04 +02:00
|
|
|
projTasks[t.Task.ID] = t
|
|
|
|
p.runningTasks[t.Task.ID] = t
|
2022-01-29 19:00:21 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-08-29 00:51:04 +02:00
|
|
|
if p.activeProj[t.Task.ProjectID] != nil && p.activeProj[t.Task.ProjectID][t.Task.ID] != nil {
|
|
|
|
delete(p.activeProj[t.Task.ProjectID], t.Task.ID)
|
|
|
|
if len(p.activeProj[t.Task.ProjectID]) == 0 {
|
|
|
|
delete(p.activeProj, t.Task.ProjectID)
|
2022-02-12 13:15:15 +01:00
|
|
|
}
|
2022-01-29 19:00:21 +01:00
|
|
|
}
|
|
|
|
|
2023-08-29 00:51:04 +02:00
|
|
|
delete(p.runningTasks, t.Task.ID)
|
2022-01-29 19:00:21 +01:00
|
|
|
}
|
2022-02-12 13:15:15 +01:00
|
|
|
}(p.resourceLocker)
|
2022-01-29 19:00:21 +01:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2022-02-12 13:15:15 +01:00
|
|
|
case record := <-p.logger: // new log message which should be put to database
|
2022-11-18 23:23:30 +01:00
|
|
|
db.StoreSession(p.store, "logger", func() {
|
|
|
|
_, err := p.store.CreateTaskOutput(db.TaskOutput{
|
2023-08-29 00:51:04 +02:00
|
|
|
TaskID: record.task.Task.ID,
|
2022-11-18 23:23:30 +01:00
|
|
|
Output: record.output,
|
|
|
|
Time: record.time,
|
|
|
|
})
|
2022-11-09 18:31:38 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
2022-01-29 19:00:21 +01:00
|
|
|
})
|
|
|
|
|
2022-02-12 13:15:15 +01:00
|
|
|
case task := <-p.register: // new task created by API or schedule
|
2022-11-18 23:23:30 +01:00
|
|
|
|
|
|
|
db.StoreSession(p.store, "new task", func() {
|
|
|
|
p.queue = append(p.queue, task)
|
|
|
|
log.Debug(task)
|
2023-08-29 00:51:04 +02:00
|
|
|
msg := "Task " + strconv.Itoa(task.Task.ID) + " added to queue"
|
2022-11-18 23:23:30 +01:00
|
|
|
task.Log(msg)
|
|
|
|
log.Info(msg)
|
2023-08-30 11:13:48 +02:00
|
|
|
task.saveStatus()
|
2022-11-18 23:23:30 +01:00
|
|
|
})
|
2022-01-29 19:00:21 +01:00
|
|
|
|
2022-02-12 13:15:15 +01:00
|
|
|
case <-ticker.C: // timer 5 seconds
|
2022-01-29 19:00:21 +01:00
|
|
|
if len(p.queue) == 0 {
|
2022-11-18 23:23:30 +01:00
|
|
|
break
|
2022-01-29 19:00:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//get TaskRunner from top of queue
|
|
|
|
t := p.queue[0]
|
2024-04-12 12:32:54 +02:00
|
|
|
if t.Task.Status == task_logger.TaskFailStatus {
|
2022-01-29 19:00:21 +01:00
|
|
|
//delete failed TaskRunner from queue
|
|
|
|
p.queue = p.queue[1:]
|
2023-08-29 00:51:04 +02:00
|
|
|
log.Info("Task " + strconv.Itoa(t.Task.ID) + " removed from queue")
|
2022-11-18 23:23:30 +01:00
|
|
|
break
|
2022-01-29 19:00:21 +01:00
|
|
|
}
|
2022-11-18 23:23:30 +01:00
|
|
|
|
2022-01-29 19:00:21 +01:00
|
|
|
if p.blocks(t) {
|
|
|
|
//move blocked TaskRunner to end of queue
|
|
|
|
p.queue = append(p.queue[1:], t)
|
2022-11-18 23:23:30 +01:00
|
|
|
break
|
2022-01-29 19:00:21 +01:00
|
|
|
}
|
2022-11-18 23:23:30 +01:00
|
|
|
|
2023-08-29 00:51:04 +02:00
|
|
|
log.Info("Set resource locker with TaskRunner " + strconv.Itoa(t.Task.ID))
|
2022-02-12 13:15:15 +01:00
|
|
|
p.resourceLocker <- &resourceLock{lock: true, holder: t}
|
2022-11-18 23:23:30 +01:00
|
|
|
|
2022-01-29 19:00:21 +01:00
|
|
|
go t.run()
|
2023-08-29 00:51:04 +02:00
|
|
|
|
2022-01-29 19:00:21 +01:00
|
|
|
p.queue = p.queue[1:]
|
2023-08-29 00:51:04 +02:00
|
|
|
log.Info("Task " + strconv.Itoa(t.Task.ID) + " removed from queue")
|
2022-01-29 19:00:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *TaskPool) blocks(t *TaskRunner) bool {
|
2022-02-12 13:15:15 +01:00
|
|
|
|
2023-09-17 22:06:28 +02:00
|
|
|
if util.Config.MaxParallelTasks > 0 && len(p.runningTasks) >= util.Config.MaxParallelTasks {
|
2022-01-29 19:00:21 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-08-29 00:51:04 +02:00
|
|
|
if p.activeProj[t.Task.ProjectID] == nil || len(p.activeProj[t.Task.ProjectID]) == 0 {
|
2022-02-12 13:15:15 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-08-29 00:51:04 +02:00
|
|
|
for _, r := range p.activeProj[t.Task.ProjectID] {
|
2024-06-17 20:37:45 +02:00
|
|
|
if r.Task.Status.IsFinished() {
|
|
|
|
continue
|
|
|
|
}
|
2023-08-29 00:51:04 +02:00
|
|
|
if r.Template.ID == t.Task.TemplateID {
|
2022-02-12 13:15:15 +01:00
|
|
|
return true
|
2022-01-29 19:00:21 +01:00
|
|
|
}
|
2022-02-12 13:15:15 +01:00
|
|
|
}
|
|
|
|
|
2023-08-29 00:51:04 +02:00
|
|
|
proj, err := p.store.GetProject(t.Task.ProjectID)
|
2022-01-29 19:00:21 +01:00
|
|
|
|
2022-02-12 13:15:15 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
2022-01-29 19:00:21 +01:00
|
|
|
return false
|
|
|
|
}
|
2022-02-12 13:15:15 +01:00
|
|
|
|
2023-08-29 00:51:04 +02:00
|
|
|
return proj.MaxParallelTasks > 0 && len(p.activeProj[t.Task.ProjectID]) >= proj.MaxParallelTasks
|
2022-01-29 19:00:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func CreateTaskPool(store db.Store) TaskPool {
|
|
|
|
return TaskPool{
|
2022-02-12 13:15:15 +01:00
|
|
|
queue: make([]*TaskRunner, 0), // queue of waiting tasks
|
|
|
|
register: make(chan *TaskRunner), // add TaskRunner to queue
|
|
|
|
activeProj: make(map[int]map[int]*TaskRunner),
|
|
|
|
runningTasks: make(map[int]*TaskRunner), // working tasks
|
|
|
|
logger: make(chan logRecord, 10000), // store log records to database
|
|
|
|
store: store,
|
|
|
|
resourceLocker: make(chan *resourceLock),
|
2022-01-29 19:00:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-09 14:54:27 +01:00
|
|
|
func (p *TaskPool) ConfirmTask(targetTask db.Task) error {
|
2024-03-09 15:01:20 +01:00
|
|
|
tsk := p.GetTask(targetTask.ID)
|
|
|
|
|
|
|
|
if tsk == nil { // task not active, but exists in database
|
|
|
|
return fmt.Errorf("task is not active")
|
|
|
|
}
|
|
|
|
|
2024-04-12 12:32:54 +02:00
|
|
|
tsk.SetStatus(task_logger.TaskConfirmed)
|
2024-03-09 15:01:20 +01:00
|
|
|
|
2024-03-09 14:54:27 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-12 20:58:44 +02:00
|
|
|
func (p *TaskPool) StopTask(targetTask db.Task, forceStop bool) error {
|
2022-01-30 18:43:15 +01:00
|
|
|
tsk := p.GetTask(targetTask.ID)
|
|
|
|
if tsk == nil { // task not active, but exists in database
|
|
|
|
tsk = &TaskRunner{
|
2023-08-29 00:51:04 +02:00
|
|
|
Task: targetTask,
|
2022-01-29 19:00:21 +01:00
|
|
|
pool: p,
|
|
|
|
}
|
2022-01-30 18:43:15 +01:00
|
|
|
err := tsk.populateDetails()
|
2022-01-29 19:00:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-12 12:32:54 +02:00
|
|
|
tsk.SetStatus(task_logger.TaskStoppedStatus)
|
2022-01-30 18:43:15 +01:00
|
|
|
tsk.createTaskEvent()
|
2022-01-29 19:00:21 +01:00
|
|
|
} else {
|
2023-08-29 00:51:04 +02:00
|
|
|
status := tsk.Task.Status
|
|
|
|
|
2023-09-12 20:58:44 +02:00
|
|
|
if forceStop {
|
2024-04-12 12:32:54 +02:00
|
|
|
tsk.SetStatus(task_logger.TaskStoppedStatus)
|
2023-09-12 20:58:44 +02:00
|
|
|
} else {
|
2024-04-12 12:32:54 +02:00
|
|
|
tsk.SetStatus(task_logger.TaskStoppingStatus)
|
2023-09-12 20:58:44 +02:00
|
|
|
}
|
2023-08-29 00:51:04 +02:00
|
|
|
|
2024-04-12 12:32:54 +02:00
|
|
|
if status == task_logger.TaskRunningStatus {
|
2023-08-29 00:51:04 +02:00
|
|
|
tsk.kill()
|
2022-01-29 19:00:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNextBuildVersion(startVersion string, currentVersion string) string {
|
|
|
|
re := regexp.MustCompile(`^(.*[^\d])?(\d+)([^\d].*)?$`)
|
|
|
|
m := re.FindStringSubmatch(startVersion)
|
|
|
|
|
|
|
|
if m == nil {
|
|
|
|
return startVersion
|
|
|
|
}
|
|
|
|
|
|
|
|
var prefix, suffix, body string
|
|
|
|
|
|
|
|
switch len(m) - 1 {
|
|
|
|
case 3:
|
|
|
|
prefix = m[1]
|
|
|
|
body = m[2]
|
|
|
|
suffix = m[3]
|
|
|
|
case 2:
|
|
|
|
if _, err := strconv.Atoi(m[1]); err == nil {
|
|
|
|
body = m[1]
|
|
|
|
suffix = m[2]
|
|
|
|
} else {
|
|
|
|
prefix = m[1]
|
|
|
|
body = m[2]
|
|
|
|
}
|
|
|
|
case 1:
|
|
|
|
body = m[1]
|
|
|
|
default:
|
|
|
|
return startVersion
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(currentVersion, prefix) ||
|
|
|
|
!strings.HasSuffix(currentVersion, suffix) {
|
|
|
|
return startVersion
|
|
|
|
}
|
|
|
|
|
|
|
|
curr, err := strconv.Atoi(currentVersion[len(prefix) : len(currentVersion)-len(suffix)])
|
|
|
|
if err != nil {
|
|
|
|
return startVersion
|
|
|
|
}
|
|
|
|
|
|
|
|
start, err := strconv.Atoi(body)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var newVer int
|
|
|
|
if start > curr {
|
|
|
|
newVer = start
|
|
|
|
} else {
|
|
|
|
newVer = curr + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return prefix + strconv.Itoa(newVer) + suffix
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *TaskPool) AddTask(taskObj db.Task, userID *int, projectID int) (newTask db.Task, err error) {
|
|
|
|
taskObj.Created = time.Now()
|
2024-04-12 12:32:54 +02:00
|
|
|
taskObj.Status = task_logger.TaskWaitingStatus
|
2022-01-29 19:00:21 +01:00
|
|
|
taskObj.UserID = userID
|
|
|
|
taskObj.ProjectID = projectID
|
2024-03-19 09:49:33 +01:00
|
|
|
extraSecretVars := taskObj.Secret
|
|
|
|
taskObj.Secret = "{}"
|
2022-01-29 19:00:21 +01:00
|
|
|
|
|
|
|
tpl, err := p.store.GetTemplate(projectID, taskObj.TemplateID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = taskObj.ValidateNewTask(tpl)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if tpl.Type == db.TemplateBuild { // get next version for TaskRunner if it is a Build
|
|
|
|
var builds []db.TaskWithTpl
|
2024-06-29 18:23:41 +02:00
|
|
|
builds, err = p.store.GetTemplateTasks(tpl.ProjectID, []int{tpl.ID}, db.RetrieveQueryParams{Count: 1})
|
2022-01-29 19:00:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(builds) == 0 || builds[0].Version == nil {
|
|
|
|
taskObj.Version = tpl.StartVersion
|
|
|
|
} else {
|
|
|
|
v := getNextBuildVersion(*tpl.StartVersion, *builds[0].Version)
|
|
|
|
taskObj.Version = &v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-30 09:48:36 +02:00
|
|
|
newTask, err = p.store.CreateTask(taskObj, util.Config.MaxTasksPerTemplate)
|
2022-01-29 19:00:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-12 13:15:15 +01:00
|
|
|
taskRunner := TaskRunner{
|
2023-08-29 00:51:04 +02:00
|
|
|
Task: newTask,
|
2022-01-30 12:22:18 +01:00
|
|
|
pool: p,
|
2022-01-29 19:00:21 +01:00
|
|
|
}
|
|
|
|
|
2022-02-12 13:15:15 +01:00
|
|
|
err = taskRunner.populateDetails()
|
|
|
|
if err != nil {
|
|
|
|
taskRunner.Log("Error: " + err.Error())
|
2024-04-12 12:32:54 +02:00
|
|
|
taskRunner.SetStatus(task_logger.TaskFailStatus)
|
2022-02-12 13:15:15 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-10 23:18:25 +02:00
|
|
|
var job Job
|
|
|
|
|
|
|
|
if util.Config.UseRemoteRunner {
|
|
|
|
job = &RemoteJob{
|
2024-01-30 10:26:11 +01:00
|
|
|
Task: taskRunner.Task,
|
2023-09-10 23:18:25 +02:00
|
|
|
taskPool: p,
|
|
|
|
}
|
|
|
|
} else {
|
2024-03-12 01:44:04 +01:00
|
|
|
app := db_lib.CreateApp(taskRunner.Template, taskRunner.Repository, &taskRunner)
|
2023-09-10 23:18:25 +02:00
|
|
|
job = &LocalJob{
|
|
|
|
Task: taskRunner.Task,
|
|
|
|
Template: taskRunner.Template,
|
|
|
|
Inventory: taskRunner.Inventory,
|
|
|
|
Repository: taskRunner.Repository,
|
|
|
|
Environment: taskRunner.Environment,
|
2024-03-19 09:49:33 +01:00
|
|
|
Secret: extraSecretVars,
|
2024-03-12 01:44:04 +01:00
|
|
|
Logger: app.SetLogger(&taskRunner),
|
|
|
|
App: app,
|
2023-09-10 23:18:25 +02:00
|
|
|
}
|
2023-08-29 00:51:04 +02:00
|
|
|
}
|
2023-08-27 18:02:51 +02:00
|
|
|
|
2023-09-10 23:18:25 +02:00
|
|
|
taskRunner.job = job
|
2023-08-27 18:02:51 +02:00
|
|
|
|
2022-02-12 13:15:15 +01:00
|
|
|
p.register <- &taskRunner
|
|
|
|
|
2022-01-29 19:00:21 +01:00
|
|
|
objType := db.EventTask
|
|
|
|
desc := "Task ID " + strconv.Itoa(newTask.ID) + " queued for running"
|
|
|
|
_, err = p.store.CreateEvent(db.Event{
|
|
|
|
UserID: userID,
|
|
|
|
ProjectID: &projectID,
|
|
|
|
ObjectType: &objType,
|
|
|
|
ObjectID: &newTask.ID,
|
|
|
|
Description: &desc,
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|