From 635b73a811033498bc8c622c0be0be2569068aab Mon Sep 17 00:00:00 2001 From: Michael Nutt Date: Wed, 12 Mar 2014 00:30:00 -0400 Subject: [PATCH] mark all previously started builds as failed on startup --- cmd/droned/drone.go | 17 +++++++++++++++++ pkg/database/builds.go | 12 ++++++++++++ pkg/database/commits.go | 14 +++++++++++++- 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/cmd/droned/drone.go b/cmd/droned/drone.go index b31f688c6..e8d0e2ab6 100644 --- a/cmd/droned/drone.go +++ b/cmd/droned/drone.go @@ -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. diff --git a/pkg/database/builds.go b/pkg/database/builds.go index 8aacf32dd..c589b625c 100644 --- a/pkg/database/builds.go +++ b/pkg/database/builds.go @@ -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 +} diff --git a/pkg/database/commits.go b/pkg/database/commits.go index 74e0d86b4..b58f3f8d9 100644 --- a/pkg/database/commits.go +++ b/pkg/database/commits.go @@ -101,11 +101,18 @@ WHERE id IN ( SELECT MAX(id) FROM commits WHERE repo_id = ? - AND branch = ? + AND branch = ? GROUP BY branch) 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 +}