Skip to content
Snippets Groups Projects
Commit 5e431ff7 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets
Browse files

Prevent build race condition for same project

parent 8e6f94c2
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -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
Loading
Loading
Loading
Loading
@@ -124,6 +124,10 @@ class Project < ActiveRecord::Base
def schedule_id
"project-#{id}"
end
def no_running_builds?
builds.running.empty?
end
end
 
 
Loading
Loading
@@ -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)
#
Loading
Loading
@@ -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)
Loading
Loading
@@ -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
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment