woodpecker/datastore/bolt/task.go
2015-04-07 01:20:55 -07:00

82 lines
2.3 KiB
Go

package bolt
import (
"strconv"
"github.com/drone/drone/common"
)
// GetTask gets the task at index N for the named
// repository and build number.
func (db *DB) GetTask(repo string, build int, task int) (*common.Task, error) {
key := []byte(repo + "/" + strconv.Itoa(build) + "/" + strconv.Itoa(task))
task_ := &common.Task{}
err := get(db, bucketBuildTasks, key, task_)
return task_, err
}
// GetTaskLogs gets the task logs at index N for
// the named repository and build number.
func (db *DB) GetTaskLogs(repo string, build int, task int) ([]byte, error) {
key := []byte(repo + "/" + strconv.Itoa(build) + "/" + strconv.Itoa(task))
log, err := raw(db, bucketBuildLogs, key)
return log, err
}
// GetTaskList gets all tasks for the named repository
// and build number.
func (db *DB) GetTaskList(repo string, build int) ([]*common.Task, error) {
// fetch the build so that we can get the
// number of child tasks.
build_, err := db.GetBuild(repo, build)
if err != nil {
return nil, err
}
t, err := db.Begin(false)
if err != nil {
return nil, err
}
defer t.Rollback()
// based on the number of child tasks, incrment
// and loop to get each task from the bucket.
tasks := []*common.Task{}
for i := 1; i <= build_.Number; i++ {
key := []byte(repo + "/" + strconv.Itoa(build) + "/" + strconv.Itoa(i))
raw := t.Bucket(bucketBuildTasks).Get(key)
if raw == nil {
return nil, ErrKeyNotFound
}
task := &common.Task{}
err := decode(raw, task)
if err != nil {
return nil, err
}
tasks = append(tasks, task)
}
return tasks, nil
}
// UpsertTask inserts or updates a task for the named
// repository and build number.
func (db *DB) UpsertTask(repo string, build int, task *common.Task) error {
key := []byte(repo + "/" + strconv.Itoa(build) + "/" + strconv.Itoa(task.Number))
return update(db, bucketBuildTasks, key, task)
}
// UpsertTaskLogs inserts or updates a task logs for the
// named repository and build number.
func (db *DB) UpsertTaskLogs(repo string, build int, task int, log []byte) error {
key := []byte(repo + "/" + strconv.Itoa(build) + "/" + strconv.Itoa(task))
t, err := db.Begin(true)
if err != nil {
return err
}
err = t.Bucket(bucketBuildLogs).Put(key, log)
if err != nil {
t.Rollback()
return err
}
return t.Commit()
}