2018-02-20 00:24:10 +02:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-04-14 13:32:36 +02:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-05-18 16:46:13 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-06-22 12:34:35 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/cncd/queue"
|
2017-04-14 13:32:36 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Task defines scheduled pipeline Task.
|
|
|
|
type Task struct {
|
2019-06-25 12:07:36 +02:00
|
|
|
ID string `meddler:"task_id"`
|
|
|
|
Data []byte `meddler:"task_data"`
|
|
|
|
Labels map[string]string `meddler:"task_labels,json"`
|
|
|
|
Dependencies []string `meddler:"task_dependencies,json"`
|
|
|
|
RunOn []string `meddler:"task_run_on,json"`
|
2017-04-14 13:32:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TaskStore defines storage for scheduled Tasks.
|
|
|
|
type TaskStore interface {
|
|
|
|
TaskList() ([]*Task, error)
|
|
|
|
TaskInsert(*Task) error
|
|
|
|
TaskDelete(string) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithTaskStore returns a queue that is backed by the TaskStore. This
|
|
|
|
// ensures the task Queue can be restored when the system starts.
|
|
|
|
func WithTaskStore(q queue.Queue, s TaskStore) queue.Queue {
|
|
|
|
tasks, _ := s.TaskList()
|
2019-06-25 12:07:36 +02:00
|
|
|
toEnqueue := []*queue.Task{}
|
2017-04-14 13:32:36 +02:00
|
|
|
for _, task := range tasks {
|
2019-06-25 12:07:36 +02:00
|
|
|
toEnqueue = append(toEnqueue, &queue.Task{
|
|
|
|
ID: task.ID,
|
|
|
|
Data: task.Data,
|
|
|
|
Labels: task.Labels,
|
|
|
|
Dependencies: task.Dependencies,
|
|
|
|
RunOn: task.RunOn,
|
2019-07-22 12:43:59 +02:00
|
|
|
DepStatus: make(map[string]string),
|
2017-04-14 13:32:36 +02:00
|
|
|
})
|
|
|
|
}
|
2019-06-25 12:07:36 +02:00
|
|
|
q.PushAtOnce(context.Background(), toEnqueue)
|
2017-04-14 13:32:36 +02:00
|
|
|
return &persistentQueue{q, s}
|
|
|
|
}
|
|
|
|
|
|
|
|
type persistentQueue struct {
|
|
|
|
queue.Queue
|
|
|
|
store TaskStore
|
|
|
|
}
|
|
|
|
|
2019-06-13 17:38:19 +02:00
|
|
|
// Push pushes a task to the tail of this queue.
|
2017-04-14 13:32:36 +02:00
|
|
|
func (q *persistentQueue) Push(c context.Context, task *queue.Task) error {
|
2017-05-04 13:09:51 +02:00
|
|
|
q.store.TaskInsert(&Task{
|
2019-06-25 12:07:36 +02:00
|
|
|
ID: task.ID,
|
|
|
|
Data: task.Data,
|
|
|
|
Labels: task.Labels,
|
|
|
|
Dependencies: task.Dependencies,
|
|
|
|
RunOn: task.RunOn,
|
2017-05-04 13:09:51 +02:00
|
|
|
})
|
2017-04-14 13:32:36 +02:00
|
|
|
err := q.Queue.Push(c, task)
|
2017-05-04 13:09:51 +02:00
|
|
|
if err != nil {
|
|
|
|
q.store.TaskDelete(task.ID)
|
2017-04-14 13:32:36 +02:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-13 17:38:19 +02:00
|
|
|
// Push pushes multiple tasks to the tail of this queue.
|
|
|
|
func (q *persistentQueue) PushAtOnce(c context.Context, tasks []*queue.Task) error {
|
|
|
|
for _, task := range tasks {
|
|
|
|
q.store.TaskInsert(&Task{
|
2019-06-25 12:07:36 +02:00
|
|
|
ID: task.ID,
|
|
|
|
Data: task.Data,
|
|
|
|
Labels: task.Labels,
|
|
|
|
Dependencies: task.Dependencies,
|
|
|
|
RunOn: task.RunOn,
|
2019-06-13 17:38:19 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
err := q.Queue.PushAtOnce(c, tasks)
|
|
|
|
if err != nil {
|
|
|
|
for _, task := range tasks {
|
|
|
|
q.store.TaskDelete(task.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-14 13:32:36 +02:00
|
|
|
// Poll retrieves and removes a task head of this queue.
|
|
|
|
func (q *persistentQueue) Poll(c context.Context, f queue.Filter) (*queue.Task, error) {
|
|
|
|
task, err := q.Queue.Poll(c, f)
|
2017-05-04 03:14:48 +02:00
|
|
|
if task != nil {
|
|
|
|
logrus.Debugf("pull queue item: %s: remove from backup", task.ID)
|
|
|
|
if derr := q.store.TaskDelete(task.ID); derr != nil {
|
|
|
|
logrus.Errorf("pull queue item: %s: failed to remove from backup: %s", task.ID, derr)
|
|
|
|
} else {
|
2017-06-28 19:21:22 +02:00
|
|
|
logrus.Debugf("pull queue item: %s: successfully removed from backup", task.ID)
|
2017-05-04 03:14:48 +02:00
|
|
|
}
|
2017-04-14 13:32:36 +02:00
|
|
|
}
|
|
|
|
return task, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Evict removes a pending task from the queue.
|
|
|
|
func (q *persistentQueue) Evict(c context.Context, id string) error {
|
|
|
|
err := q.Queue.Evict(c, id)
|
|
|
|
if err == nil {
|
|
|
|
q.store.TaskDelete(id)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2019-09-16 15:18:15 +02:00
|
|
|
|
|
|
|
// Evict removes a pending task from the queue.
|
|
|
|
func (q *persistentQueue) EvictAtOnce(c context.Context, ids []string) error {
|
|
|
|
err := q.Queue.EvictAtOnce(c, ids)
|
|
|
|
if err == nil {
|
|
|
|
for _, id := range ids {
|
|
|
|
q.store.TaskDelete(id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|