From 1a143f8fb433966aa8526014021e8f54a697fc1d Mon Sep 17 00:00:00 2001 From: Brad Date: Wed, 11 Jun 2014 12:39:29 -0700 Subject: [PATCH] cancel all pending or started builds when the app starts --- server/main.go | 3 +++ server/resource/commit/manager.go | 20 +++++++++++++++++++- server/resource/commit/model.go | 1 + 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/server/main.go b/server/main.go index 65c2202aa..91a2cbe4c 100644 --- a/server/main.go +++ b/server/main.go @@ -93,6 +93,9 @@ func main() { perms := perm.NewManager(db) commits := commit.NewManager(db) + // cancel all previously running builds + go commits.CancelAll() + // setup the session managers sess := session.NewSession(users) diff --git a/server/resource/commit/manager.go b/server/resource/commit/manager.go index 21c0e9d2d..96bf8766d 100644 --- a/server/resource/commit/manager.go +++ b/server/resource/commit/manager.go @@ -43,6 +43,10 @@ type CommitManager interface { // Delete removes the commit from the datastore. Delete(commit *Commit) error + + // CancelAll will update the status of all Started or Pending + // builds to a status of Killed (cancelled). + CancelAll() error } // commitManager manages a list of commits in a SQL database. @@ -140,7 +144,16 @@ UPDATE output SET output_raw = ? WHERE commit_id = ?; // SQL statement to delete a Commit by ID. const deleteStmt = ` -DELETE FROM commits WHERE commit_id = ? +DELETE FROM commits WHERE commit_id = ?; +` + +// SQL statement to cancel all running Commits. +const cancelStmt = ` +UPDATE commits SET +commit_status = ?, +commit_started = ?, +commit_finished = ? +WHERE commit_status IN ('Started', 'Pending'); ` func (db *commitManager) Find(id int64) (*Commit, error) { @@ -215,3 +228,8 @@ func (db *commitManager) Delete(commit *Commit) error { _, err := db.Exec(deleteStmt, commit.ID) return err } + +func (db *commitManager) CancelAll() error { + _, err := db.Exec(cancelStmt, StatusKilled, time.Now().Unix(), time.Now().Unix()) + return err +} diff --git a/server/resource/commit/model.go b/server/resource/commit/model.go index fbaa2f0d0..3af1bdd38 100644 --- a/server/resource/commit/model.go +++ b/server/resource/commit/model.go @@ -12,6 +12,7 @@ const ( StatusSuccess = "Success" StatusFailure = "Failure" StatusError = "Error" + StatusKilled = "Killed" ) type Commit struct {