From 635b73a811033498bc8c622c0be0be2569068aab Mon Sep 17 00:00:00 2001 From: Michael Nutt Date: Wed, 12 Mar 2014 00:30:00 -0400 Subject: [PATCH 1/3] 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 +} From 138beeeb4579435f45c0c52826befe4456b5f31a Mon Sep 17 00:00:00 2001 From: Michael Nutt Date: Mon, 24 Mar 2014 00:21:33 -0400 Subject: [PATCH 2/3] also fail pending builds on startup --- cmd/droned/drone.go | 4 ++-- pkg/database/builds.go | 14 +++++++++++++- pkg/database/commits.go | 14 +++++++++++++- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/cmd/droned/drone.go b/cmd/droned/drone.go index e8d0e2ab6..1087fbed4 100644 --- a/cmd/droned/drone.go +++ b/cmd/droned/drone.go @@ -122,12 +122,12 @@ func setupDatabase() { // the drone process was shut down mid-build and thus the // builds will never complete. func discardOldBuilds() { - err := database.FailStartedBuilds() + err := database.FailUnfinishedBuilds() if err != nil { log.Fatal(err) } - err = database.FailStartedCommits() + err = database.FailUnfinishedCommits() if err != nil { log.Fatal(err) } diff --git a/pkg/database/builds.go b/pkg/database/builds.go index c589b625c..a77f7ec2e 100644 --- a/pkg/database/builds.go +++ b/pkg/database/builds.go @@ -39,6 +39,13 @@ SET status = 'Failure' WHERE status = 'Started' ` +// SQL Queries to fail all builds that are pending +const buildFailPendingStmt = ` +UPDATE builds +SET status = 'Failure' +WHERE status = 'Pending' +` + // SQL Queries to delete a Commit. const buildDeleteStmt = ` DELETE FROM builds WHERE id = ? @@ -77,7 +84,12 @@ func ListBuilds(id int64) ([]*Build, error) { return builds, err } -func FailStartedBuilds() error { +func FailUnfinishedBuilds() error { _, err := db.Exec(buildFailStartedStmt) + if err != nil { + return err + } + + _, err = db.Exec(buildFailPendingStmt) return err } diff --git a/pkg/database/commits.go b/pkg/database/commits.go index b58f3f8d9..26c7f0fea 100644 --- a/pkg/database/commits.go +++ b/pkg/database/commits.go @@ -113,6 +113,13 @@ SET status = 'Failure' WHERE status = 'Started' ` +// SQL Queries to fail all commits that are currently pending +const commitFailPendingStmt = ` +UPDATE commits +SET status = 'Failure' +WHERE status = 'Started' +` + // Returns the Commit with the given ID. func GetCommit(id int64) (*Commit, error) { commit := Commit{} @@ -180,7 +187,12 @@ func ListBranches(repo int64) ([]*Commit, error) { return commits, err } -func FailStartedCommits() error { +func FailUnfinishedCommits() error { _, err := db.Exec(commitFailStartedStmt) + if err != nil { + return err + } + + _, err = db.Exec(commitFailPendingStmt) return err } From ce0a1721362a48aa39636416d5121baa92f1c8bd Mon Sep 17 00:00:00 2001 From: Michael Nutt Date: Tue, 1 Apr 2014 15:29:14 -0400 Subject: [PATCH 3/3] combine sql statements to set both pending and started to failure on startup --- pkg/database/builds.go | 22 ++++++---------------- pkg/database/commits.go | 18 +++--------------- 2 files changed, 9 insertions(+), 31 deletions(-) diff --git a/pkg/database/builds.go b/pkg/database/builds.go index a77f7ec2e..fefe1e7bb 100644 --- a/pkg/database/builds.go +++ b/pkg/database/builds.go @@ -32,18 +32,11 @@ WHERE slug = ? AND commit_id = ? LIMIT 1 ` -// SQL Queries to fail all builds that are running -const buildFailStartedStmt = ` +// SQL Queries to fail all builds that are running or pending +const buildFailUnfinishedStmt = ` UPDATE builds SET status = 'Failure' -WHERE status = 'Started' -` - -// SQL Queries to fail all builds that are pending -const buildFailPendingStmt = ` -UPDATE builds -SET status = 'Failure' -WHERE status = 'Pending' +WHERE status IN ('Started', 'Pending') ` // SQL Queries to delete a Commit. @@ -84,12 +77,9 @@ func ListBuilds(id int64) ([]*Build, error) { return builds, err } +// FailUnfinishedBuilds sets status=Failure to all builds +// in the Pending and Started states func FailUnfinishedBuilds() error { - _, err := db.Exec(buildFailStartedStmt) - if err != nil { - return err - } - - _, err = db.Exec(buildFailPendingStmt) + _, err := db.Exec(buildFailUnfinishedStmt) return err } diff --git a/pkg/database/commits.go b/pkg/database/commits.go index 26c7f0fea..86a02b1cd 100644 --- a/pkg/database/commits.go +++ b/pkg/database/commits.go @@ -107,17 +107,10 @@ LIMIT 1 ` // SQL Queries to fail all commits that are currently building -const commitFailStartedStmt = ` +const commitFailUnfinishedStmt = ` UPDATE commits SET status = 'Failure' -WHERE status = 'Started' -` - -// SQL Queries to fail all commits that are currently pending -const commitFailPendingStmt = ` -UPDATE commits -SET status = 'Failure' -WHERE status = 'Started' +WHERE status IN ('Started', 'Pending') ` // Returns the Commit with the given ID. @@ -188,11 +181,6 @@ func ListBranches(repo int64) ([]*Commit, error) { } func FailUnfinishedCommits() error { - _, err := db.Exec(commitFailStartedStmt) - if err != nil { - return err - } - - _, err = db.Exec(commitFailPendingStmt) + _, err := db.Exec(commitFailUnfinishedStmt) return err }