mark all previously started builds as failed on startup

This commit is contained in:
Michael Nutt 2014-03-12 00:30:00 -04:00
parent fb49a2c6e9
commit 635b73a811
3 changed files with 42 additions and 1 deletions

View file

@ -72,6 +72,7 @@ func main() {
// setup database and handlers
setupDatabase()
discardOldBuilds()
setupStatic()
setupHandlers()
@ -116,6 +117,22 @@ func setupDatabase() {
migration.All().Migrate()
}
// discardOldBuilds sets builds that are in the 'Started'
// state to 'Failure' on startup. The assumption is that
// the drone process was shut down mid-build and thus the
// builds will never complete.
func discardOldBuilds() {
err := database.FailStartedBuilds()
if err != nil {
log.Fatal(err)
}
err = database.FailStartedCommits()
if err != nil {
log.Fatal(err)
}
}
// setup routes for static assets. These assets may
// be directly embedded inside the application using
// the `rice embed` command, else they are served from disk.

View file

@ -32,6 +32,13 @@ WHERE slug = ? AND commit_id = ?
LIMIT 1
`
// SQL Queries to fail all builds that are running
const buildFailStartedStmt = `
UPDATE builds
SET status = 'Failure'
WHERE status = 'Started'
`
// SQL Queries to delete a Commit.
const buildDeleteStmt = `
DELETE FROM builds WHERE id = ?
@ -69,3 +76,8 @@ func ListBuilds(id int64) ([]*Build, error) {
err := meddler.QueryAll(db, &builds, buildStmt, id)
return builds, err
}
func FailStartedBuilds() error {
_, err := db.Exec(buildFailStartedStmt)
return err
}

View file

@ -106,6 +106,13 @@ WHERE id IN (
LIMIT 1
`
// SQL Queries to fail all commits that are currently building
const commitFailStartedStmt = `
UPDATE commits
SET status = 'Failure'
WHERE status = 'Started'
`
// Returns the Commit with the given ID.
func GetCommit(id int64) (*Commit, error) {
commit := Commit{}
@ -172,3 +179,8 @@ func ListBranches(repo int64) ([]*Commit, error) {
err := meddler.QueryAll(db, &commits, commitBranchesStmt, repo)
return commits, err
}
func FailStartedCommits() error {
_, err := db.Exec(commitFailStartedStmt)
return err
}