diff --git a/server/main.go b/server/main.go index 65c2202a..91a2cbe4 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 21c0e9d2..96bf8766 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 fbaa2f0d..3af1bdd3 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 {