Skip to content
This repository has been archived by the owner on May 12, 2018. It is now read-only.

Support for multiple runners #97

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/runner
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ require 'bundler/setup'

require_relative '../lib/runner'

GitlabCi::Runner.new
GitlabCi::Runner.new(ARGV.fetch(0, '0'))
exit
5 changes: 3 additions & 2 deletions lib/build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class Build

attr_accessor :id, :commands, :ref, :tmp_file_path, :output, :before_sha, :run_at, :post_message

def initialize(data)
def initialize(runner, data)
@runner = runner
@output = ""
@post_message = ""
@commands = data[:commands].to_a
Expand Down Expand Up @@ -182,7 +183,7 @@ def repo_exists?
end

def config
@config ||= Config.new
@config ||= Config.new(@runner)
end

def project_dir
Expand Down
6 changes: 4 additions & 2 deletions lib/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ module GitlabCi
class Config
attr_reader :config

def initialize
def initialize(runner)
@runner = runner
if File.exists?(config_path)
@config = YAML.load_file(config_path)
else
Expand All @@ -26,7 +27,8 @@ def url
end

def builds_dir
@builds_path ||= File.join($root_path, 'tmp', 'builds')
rid = "runner-" + @runner.to_s
@builds_path ||= File.join($root_path, "tmp", "builds", rid)
end

def write(key, value)
Expand Down
8 changes: 6 additions & 2 deletions lib/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ module GitlabCi
class Network
include HTTParty

def initialize(runner)
@runner = runner
end

# check for available build from coordinator
# and pick a pending one
# {
Expand Down Expand Up @@ -95,7 +99,7 @@ def register_runner(token)
private

def broadcast message
print "#{Time.now.to_s} | #{message}"
print "#{Time.now.to_s} | #{@runner} | #{message}"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant use of Object#to_s in interpolation.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not a ruby expert, so could you please show me how to fix this issue and I will apply it to later ones

end

def api_url
Expand All @@ -107,7 +111,7 @@ def token
end

def config
@config ||= Config.new
@config ||= Config.new(@runner)
end

def default_options
Expand Down
15 changes: 8 additions & 7 deletions lib/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ module GitlabCi
class Runner
attr_accessor :current_build, :thread

def initialize
puts '* Gitlab CI Runner started'
def initialize(runner)
@runner = runner
puts "* Gitlab CI Runner #{@runner} started"
puts '* Waiting for builds'
loop do
if running?
Expand Down Expand Up @@ -34,7 +35,7 @@ def abort_if_timeout

def update_build
return unless @current_build.completed?
puts "#{Time.now.to_s} | Completed build #{@current_build.id}, #{@current_build.state}."
puts "#{Time.now.to_s} | Completed build #{@current_build.id}, #{@current_build.state} on runner #{@runner}."

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant use of Object#to_s in interpolation.
Line is too long. [115/80]


# Make sure we push latest build info submitted
# before we clean build
Expand Down Expand Up @@ -70,14 +71,14 @@ def get_build
end

def network
@network ||= Network.new
@network ||= Network.new(@runner)
end

def run(build_data)
@current_build = GitlabCi::Build.new(build_data)
puts "#{Time.now.to_s} | Starting new build #{@current_build.id}..."
@current_build = GitlabCi::Build.new(@runner, build_data)
puts "#{Time.now.to_s} | Starting new build #{@current_build.id} with runner #{@runner}..."

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant use of Object#to_s in interpolation.
Line is too long. [97/80]

@current_build.run
puts "#{Time.now.to_s} | Build #{@current_build.id} started."
puts "#{Time.now.to_s} | Build #{@current_build.id} on runner #{@runner} started."

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant use of Object#to_s in interpolation.
Line is too long. [88/80]

end

def collect_trace
Expand Down
2 changes: 1 addition & 1 deletion lib/setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def register_runner
def write_token(token)
puts "Runner token: #{token}"

Config.new.write('token', token)
Config.new(0).write("token", token)
end
end
end
16 changes: 10 additions & 6 deletions lib/support/init.d/gitlab_ci_runner
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ NAME="gitlab-ci-runner"
DESC="GitLab CI runner"
RUNNERS_REGISTERED=0
RUNNERS_RUNNING=0
INIT_LOG="/var/log/gitlab_ci_runner.log"
INIT_LOG='/var/log/gitlab_ci_runner.log'
#INIT_LOG='/var/log/gitlab_ci_runner_$RUNNER.log'

check_pid() {
# Number of registered runners in PID file
Expand Down Expand Up @@ -54,13 +55,16 @@ start() {
# Spawn runners
for (( i=1; i<=$RUNNERS_NUM; i++ ))
do
# eval is required for making dedicated logs for each logger. Then use $RUNNER substitution in INIT_LOG variable
RUNNER=$((i-1))
eval "LOGFILE=\"$INIT_LOG\""
# Check log file
if [ ! -f $INIT_LOG ]; then
touch $INIT_LOG
chown $APP_USER $INIT_LOG
if [ ! -f $LOGFILE ]; then
touch $LOGFILE
chown $APP_USER $LOGFILE
fi
echo "Starting runner #$i"
execute "$START_RUNNER >> $INIT_LOG 2>&1 & echo \$! >> $RUNNERS_PID"
execute "$START_RUNNER $i >> $LOGFILE 2>&1 & echo \$! >> $RUNNERS_PID"
done
echo "SUCCESS: Started $RUNNERS_NUM $DESC(s)."
fi
Expand Down Expand Up @@ -99,7 +103,7 @@ stop() {
ps -C "$PROCESS_NAME" -o "%p" h | xargs kill -USR2
[ $? -eq 0 ] && echo "OK"
else
echo "No ghost runners have been found.This is good."
echo "No ghost runners have been found. This is good."
fi
}

Expand Down
2 changes: 1 addition & 1 deletion spec/build_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

describe 'Build' do
describe :run do
let(:build) { GitlabCi::Build.new(build_data) }
let(:build) { GitlabCi::Build.new(0, build_data) }

before do
build.run
Expand Down