2021-11-13 19:18:06 +00:00
|
|
|
// Copyright 2021 Woodpecker Authors
|
2018-02-19 22:24:10 +00:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00: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 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00: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.
|
|
|
|
|
2021-11-22 11:55:13 +00:00
|
|
|
package queue
|
2017-04-14 11:32:36 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2021-11-23 14:36:52 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-10-12 07:25:13 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
2017-04-14 11:32:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// WithTaskStore returns a queue that is backed by the TaskStore. This
|
|
|
|
// ensures the task Queue can be restored when the system starts.
|
2021-11-22 11:55:13 +00:00
|
|
|
func WithTaskStore(q Queue, s model.TaskStore) Queue {
|
2017-04-14 11:32:36 +00:00
|
|
|
tasks, _ := s.TaskList()
|
2023-03-20 03:50:56 +00:00
|
|
|
if err := q.PushAtOnce(context.Background(), tasks); err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
log.Error().Err(err).Msg("PushAtOnce failed")
|
|
|
|
}
|
2017-04-14 11:32:36 +00:00
|
|
|
return &persistentQueue{q, s}
|
|
|
|
}
|
|
|
|
|
|
|
|
type persistentQueue struct {
|
2021-11-22 11:55:13 +00:00
|
|
|
Queue
|
|
|
|
store model.TaskStore
|
2017-04-14 11:32:36 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 15:38:19 +00:00
|
|
|
// Push pushes a task to the tail of this queue.
|
2023-03-20 03:50:56 +00:00
|
|
|
func (q *persistentQueue) Push(c context.Context, task *model.Task) error {
|
|
|
|
if err := q.store.TaskInsert(task); err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-04-14 11:32:36 +00:00
|
|
|
err := q.Queue.Push(c, task)
|
2017-05-04 11:09:51 +00:00
|
|
|
if err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
if err2 := q.store.TaskDelete(task.ID); err2 != nil {
|
|
|
|
err = errors.Wrapf(err, "delete task '%s' failed: %v", task.ID, err2)
|
|
|
|
}
|
2017-04-14 11:32:36 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-22 11:55:13 +00:00
|
|
|
// PushAtOnce pushes multiple tasks to the tail of this queue.
|
2023-03-20 03:50:56 +00:00
|
|
|
func (q *persistentQueue) PushAtOnce(c context.Context, tasks []*model.Task) error {
|
2021-11-23 14:36:52 +00:00
|
|
|
// TODO: invent store.NewSession who return context including a session and make TaskInsert & TaskDelete use it
|
2019-06-13 15:38:19 +00:00
|
|
|
for _, task := range tasks {
|
2023-03-20 03:50:56 +00:00
|
|
|
if err := q.store.TaskInsert(task); err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-06-13 15:38:19 +00:00
|
|
|
}
|
|
|
|
err := q.Queue.PushAtOnce(c, tasks)
|
|
|
|
if err != nil {
|
|
|
|
for _, task := range tasks {
|
2021-11-23 14:36:52 +00:00
|
|
|
if err := q.store.TaskDelete(task.ID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-13 15:38:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-14 11:32:36 +00:00
|
|
|
// Poll retrieves and removes a task head of this queue.
|
2023-03-21 13:10:43 +00:00
|
|
|
func (q *persistentQueue) Poll(c context.Context, agentID int64, f FilterFn) (*model.Task, error) {
|
|
|
|
task, err := q.Queue.Poll(c, agentID, f)
|
2017-05-04 01:14:48 +00:00
|
|
|
if task != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Debug().Msgf("pull queue item: %s: remove from backup", task.ID)
|
2017-05-04 01:14:48 +00:00
|
|
|
if derr := q.store.TaskDelete(task.ID); derr != nil {
|
2024-01-10 19:57:12 +00:00
|
|
|
log.Error().Err(derr).Msgf("pull queue item: %s: failed to remove from backup", task.ID)
|
2017-05-04 01:14:48 +00:00
|
|
|
} else {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Debug().Msgf("pull queue item: %s: successfully removed from backup", task.ID)
|
2017-05-04 01:14:48 +00:00
|
|
|
}
|
2017-04-14 11:32:36 +00: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 {
|
2021-11-23 14:36:52 +00:00
|
|
|
return q.store.TaskDelete(id)
|
2017-04-14 11:32:36 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2019-09-16 13:18:15 +00:00
|
|
|
|
2021-11-22 11:55:13 +00:00
|
|
|
// EvictAtOnce removes a pending task from the queue.
|
2019-09-16 13:18:15 +00:00
|
|
|
func (q *persistentQueue) EvictAtOnce(c context.Context, ids []string) error {
|
2021-11-23 14:36:52 +00:00
|
|
|
if err := q.Queue.EvictAtOnce(c, ids); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, id := range ids {
|
|
|
|
if err := q.store.TaskDelete(id); err != nil {
|
|
|
|
return err
|
2019-09-16 13:18:15 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-23 14:36:52 +00:00
|
|
|
return nil
|
2019-09-16 13:18:15 +00:00
|
|
|
}
|