summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/models/build.rb3
-rw-r--r--app/models/project.rb23
-rw-r--r--lib/runner.rb13
3 files changed, 17 insertions, 22 deletions
diff --git a/app/models/build.rb b/app/models/build.rb
index 7f3f6c1..a81abed 100644
--- a/app/models/build.rb
+++ b/app/models/build.rb
@@ -10,6 +10,9 @@ class Build < ActiveRecord::Base
scope :latest_sha, where("id IN(SELECT MAX(id) FROM #{self.table_name} group by sha)")
+ scope :running, where(status: "running")
+ scope :pending, where(status: "pending")
+
state_machine :status, initial: :pending do
event :run do
transition pending: :running
diff --git a/app/models/project.rb b/app/models/project.rb
index 9d81e28..9bcec1a 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -124,6 +124,10 @@ class Project < ActiveRecord::Base
def schedule_id
"project-#{id}"
end
+
+ def no_running_builds?
+ builds.running.empty?
+ end
end
@@ -141,22 +145,3 @@ end
# token :string(255)
# default_ref :string(255)
#
-
-# == Schema Information
-#
-# Table name: projects
-#
-# id :integer(4) not null, primary key
-# name :string(255) not null
-# path :string(255) not null
-# timeout :integer(4) default(1800), not null
-# scripts :text default(""), not null
-# created_at :datetime not null
-# updated_at :datetime not null
-# token :string(255)
-# default_ref :string(255)
-# gitlab_url :string(255)
-# always_build :boolean(1) default(FALSE), not null
-# polling_interval :integer(4)
-#
-
diff --git a/lib/runner.rb b/lib/runner.rb
index 112d40a..7b6ab4d 100644
--- a/lib/runner.rb
+++ b/lib/runner.rb
@@ -4,10 +4,8 @@ require 'timeout'
class Runner
include Sidekiq::Worker
- TIMEOUT = 1800
attr_accessor :project, :build, :output
-
sidekiq_options queue: :runner
def perform(build_id)
@@ -15,7 +13,16 @@ class Runner
@project = @build.project
@output = ''
- run
+
+ if @project.no_running_builds?
+ run
+ else
+ run_later
+ end
+ end
+
+ def run_later
+ Runner.perform_in(2.minutes, @build.id)
end
def initialize