2015-04-07 08:20:55 +00:00
|
|
|
package bolt
|
|
|
|
|
|
|
|
import (
|
2015-04-13 06:08:55 +00:00
|
|
|
"bytes"
|
|
|
|
"io"
|
2015-04-07 08:20:55 +00:00
|
|
|
"strconv"
|
|
|
|
|
2015-04-10 19:52:33 +00:00
|
|
|
"github.com/boltdb/bolt"
|
2015-04-13 06:08:55 +00:00
|
|
|
"github.com/drone/drone/common"
|
2015-04-07 08:20:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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{}
|
2015-04-13 06:08:55 +00:00
|
|
|
err := db.View(func(t *bolt.Tx) error {
|
2015-04-10 19:52:33 +00:00
|
|
|
return get(t, bucketBuildTasks, key, task_)
|
|
|
|
})
|
2015-04-07 08:20:55 +00:00
|
|
|
return task_, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTaskLogs gets the task logs at index N for
|
|
|
|
// the named repository and build number.
|
2015-04-13 06:08:55 +00:00
|
|
|
func (db *DB) GetTaskLogs(repo string, build int, task int) (io.Reader, error) {
|
2015-04-07 08:20:55 +00:00
|
|
|
key := []byte(repo + "/" + strconv.Itoa(build) + "/" + strconv.Itoa(task))
|
2015-04-10 19:52:33 +00:00
|
|
|
|
|
|
|
var log []byte
|
2015-04-13 06:08:55 +00:00
|
|
|
err := db.View(func(t *bolt.Tx) error {
|
2015-04-10 19:52:33 +00:00
|
|
|
var err error
|
|
|
|
log, err = raw(t, bucketBuildLogs, key)
|
|
|
|
return err
|
|
|
|
})
|
2015-04-13 06:08:55 +00:00
|
|
|
buf := bytes.NewBuffer(log)
|
|
|
|
return buf, err
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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))
|
2015-04-13 06:08:55 +00:00
|
|
|
return db.Update(func(t *bolt.Tx) error {
|
2015-04-10 19:52:33 +00:00
|
|
|
return update(t, bucketBuildTasks, key, task)
|
|
|
|
})
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
}
|