1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2025-01-29 18:04:15 +02:00

Task dependencies to survive restarts

This commit is contained in:
Laszlo Fogas 2019-06-25 12:07:36 +02:00
parent 84564f9ae5
commit be0d1d73fb
13 changed files with 114 additions and 13 deletions

View File

@ -23,9 +23,11 @@ import (
// Task defines scheduled pipeline Task. // Task defines scheduled pipeline Task.
type Task struct { type Task struct {
ID string `meddler:"task_id"` ID string `meddler:"task_id"`
Data []byte `meddler:"task_data"` Data []byte `meddler:"task_data"`
Labels map[string]string `meddler:"task_labels,json"` Labels map[string]string `meddler:"task_labels,json"`
Dependencies []string `meddler:"task_dependencies,json"`
RunOn []string `meddler:"task_run_on,json"`
} }
// TaskStore defines storage for scheduled Tasks. // TaskStore defines storage for scheduled Tasks.
@ -39,13 +41,18 @@ type TaskStore interface {
// ensures the task Queue can be restored when the system starts. // ensures the task Queue can be restored when the system starts.
func WithTaskStore(q queue.Queue, s TaskStore) queue.Queue { func WithTaskStore(q queue.Queue, s TaskStore) queue.Queue {
tasks, _ := s.TaskList() tasks, _ := s.TaskList()
toEnqueue := []*queue.Task{}
for _, task := range tasks { for _, task := range tasks {
q.Push(context.Background(), &queue.Task{ toEnqueue = append(toEnqueue, &queue.Task{
ID: task.ID, ID: task.ID,
Data: task.Data, Data: task.Data,
Labels: task.Labels, Labels: task.Labels,
Dependencies: task.Dependencies,
RunOn: task.RunOn,
DepStatus: make(map[string]bool),
}) })
} }
q.PushAtOnce(context.Background(), toEnqueue)
return &persistentQueue{q, s} return &persistentQueue{q, s}
} }
@ -57,9 +64,11 @@ type persistentQueue struct {
// Push pushes a task to the tail of this queue. // Push pushes a task to the tail of this queue.
func (q *persistentQueue) Push(c context.Context, task *queue.Task) error { func (q *persistentQueue) Push(c context.Context, task *queue.Task) error {
q.store.TaskInsert(&Task{ q.store.TaskInsert(&Task{
ID: task.ID, ID: task.ID,
Data: task.Data, Data: task.Data,
Labels: task.Labels, Labels: task.Labels,
Dependencies: task.Dependencies,
RunOn: task.RunOn,
}) })
err := q.Queue.Push(c, task) err := q.Queue.Push(c, task)
if err != nil { if err != nil {
@ -72,9 +81,11 @@ func (q *persistentQueue) Push(c context.Context, task *queue.Task) error {
func (q *persistentQueue) PushAtOnce(c context.Context, tasks []*queue.Task) error { func (q *persistentQueue) PushAtOnce(c context.Context, tasks []*queue.Task) error {
for _, task := range tasks { for _, task := range tasks {
q.store.TaskInsert(&Task{ q.store.TaskInsert(&Task{
ID: task.ID, ID: task.ID,
Data: task.Data, Data: task.Data,
Labels: task.Labels, Labels: task.Labels,
Dependencies: task.Dependencies,
RunOn: task.RunOn,
}) })
} }
err := q.Queue.PushAtOnce(c, tasks) err := q.Queue.PushAtOnce(c, tasks)

View File

@ -172,6 +172,14 @@ var migrations = []struct {
name: "populate-build-config", name: "populate-build-config",
stmt: populateBuildConfig, stmt: populateBuildConfig,
}, },
{
name: "alter-table-add-task-dependencies",
stmt: alterTableAddTaskDependencies,
},
{
name: "alter-table-add-task-run-on",
stmt: alterTableAddTaskRunOn,
},
} }
// Migrate performs the database migration. If the migration fails // Migrate performs the database migration. If the migration fails
@ -673,3 +681,15 @@ var populateBuildConfig = `
INSERT INTO build_config (config_id, build_id) INSERT INTO build_config (config_id, build_id)
SELECT build_config_id, build_id FROM builds SELECT build_config_id, build_id FROM builds
` `
//
// 022_add_task_columns.sql
//
var alterTableAddTaskDependencies = `
ALTER TABLE tasks ADD COLUMN task_dependencies MEDIUMBLOB
`
var alterTableAddTaskRunOn = `
ALTER TABLE tasks ADD COLUMN task_run_on MEDIUMBLOB
`

View File

@ -0,0 +1,6 @@
-- name: alter-table-add-task-dependencies
ALTER TABLE tasks ADD COLUMN task_dependencies MEDIUMBLOB
-- name: alter-table-add-task-run-on
ALTER TABLE tasks ADD COLUMN task_run_on MEDIUMBLOB

View File

@ -172,6 +172,14 @@ var migrations = []struct {
name: "populate-build-config", name: "populate-build-config",
stmt: populateBuildConfig, stmt: populateBuildConfig,
}, },
{
name: "alter-table-add-task-dependencies",
stmt: alterTableAddTaskDependencies,
},
{
name: "alter-table-add-task-run-on",
stmt: alterTableAddTaskRunOn,
},
} }
// Migrate performs the database migration. If the migration fails // Migrate performs the database migration. If the migration fails
@ -675,3 +683,15 @@ var populateBuildConfig = `
INSERT INTO build_config (config_id, build_id) INSERT INTO build_config (config_id, build_id)
SELECT build_config_id, build_id FROM builds SELECT build_config_id, build_id FROM builds
` `
//
// 022_add_task_columns.sql
//
var alterTableAddTaskDependencies = `
ALTER TABLE tasks ADD COLUMN task_dependencies BYTEA
`
var alterTableAddTaskRunOn = `
ALTER TABLE tasks ADD COLUMN task_run_on BYTEA
`

View File

@ -0,0 +1,6 @@
-- name: alter-table-add-task-dependencies
ALTER TABLE tasks ADD COLUMN task_dependencies BYTEA
-- name: alter-table-add-task-run-on
ALTER TABLE tasks ADD COLUMN task_run_on BYTEA

View File

@ -176,6 +176,14 @@ var migrations = []struct {
name: "populate-build-config", name: "populate-build-config",
stmt: populateBuildConfig, stmt: populateBuildConfig,
}, },
{
name: "alter-table-add-task-dependencies",
stmt: alterTableAddTaskDependencies,
},
{
name: "alter-table-add-task-run-on",
stmt: alterTableAddTaskRunOn,
},
} }
// Migrate performs the database migration. If the migration fails // Migrate performs the database migration. If the migration fails
@ -674,3 +682,15 @@ var populateBuildConfig = `
INSERT INTO build_config (config_id, build_id) INSERT INTO build_config (config_id, build_id)
SELECT build_config_id, build_id FROM builds SELECT build_config_id, build_id FROM builds
` `
//
// 022_add_task_columns.sql
//
var alterTableAddTaskDependencies = `
ALTER TABLE tasks ADD COLUMN task_dependencies BLOB
`
var alterTableAddTaskRunOn = `
ALTER TABLE tasks ADD COLUMN task_run_on BLOB
`

View File

@ -0,0 +1,6 @@
-- name: alter-table-add-task-dependencies
ALTER TABLE tasks ADD COLUMN task_dependencies BLOB
-- name: alter-table-add-task-run-on
ALTER TABLE tasks ADD COLUMN task_run_on BLOB

View File

@ -4,6 +4,8 @@ SELECT
task_id task_id
,task_data ,task_data
,task_labels ,task_labels
,task_dependencies
,task_run_on
FROM tasks FROM tasks
-- name: task-delete -- name: task-delete

View File

@ -554,6 +554,8 @@ SELECT
task_id task_id
,task_data ,task_data
,task_labels ,task_labels
,task_dependencies
,task_run_on
FROM tasks FROM tasks
` `

View File

@ -4,6 +4,8 @@ SELECT
task_id task_id
,task_data ,task_data
,task_labels ,task_labels
,task_dependencies
,task_run_on
FROM tasks FROM tasks
-- name: task-delete -- name: task-delete

View File

@ -559,6 +559,8 @@ SELECT
task_id task_id
,task_data ,task_data
,task_labels ,task_labels
,task_dependencies
,task_run_on
FROM tasks FROM tasks
` `

View File

@ -4,6 +4,8 @@ SELECT
task_id task_id
,task_data ,task_data
,task_labels ,task_labels
,task_dependencies
,task_run_on
FROM tasks FROM tasks
-- name: task-delete -- name: task-delete

View File

@ -554,6 +554,8 @@ SELECT
task_id task_id
,task_data ,task_data
,task_labels ,task_labels
,task_dependencies
,task_run_on
FROM tasks FROM tasks
` `