class TinyCI::Scheduler
Manages the execution of test jobs. Responsible for deciding which commits need to be built and tested. Also manages the pidfile. This is the main entrypoint for TinyCI
.
@attr_reader [String] working_dir
The working directory to execute against
Attributes
Public Class Methods
Constructor, allows injection of configuration and custom {Runner} class. Config
params are passed to {Runner} instances.
@param working_dir
[String] The working directory to execute against @param logger [Logger] Logger object @param commit [String] specific git object to run against @param runner_class [TinyCI::Runner] Injection of {Runner} dependency
# File lib/tinyci/scheduler.rb, line 28 def initialize( working_dir: nil, logger: nil, commit: nil, runner_class: Runner ) @working_dir = working_dir || repo_root @logger = logger @runner_class = runner_class @commit = commit end
Public Instance Methods
Runs the TinyCI
system against the relevant commits. Also sets up the pidfile.
@return [Boolean] `true` if all commits built and tested successfully, `false` otherwise
# File lib/tinyci/scheduler.rb, line 44 def run! pid = PidFile.new(pidfile: 'tinyci.pid', piddir: @working_dir) result = if @commit run_commit get_commit @commit else run_all_commits end pid.release result end
Private Instance Methods
# File lib/tinyci/scheduler.rb, line 88 def format_commit_data(data) parts = data.split(' ') { sha: parts[0], time: parts[1], result: parts[2] } end
# File lib/tinyci/scheduler.rb, line 70 def get_commit(sha) data = execute(git_cmd('show', '--quiet', '--notes=tinyci*', '--format=%H %ct', sha)) format_commit_data(data) end
Git objects to be executed against, all those without a tinyci tag
@return [Array<String>] the sha1 hashes in reverse order of creation time
# File lib/tinyci/scheduler.rb, line 63 def retrieve_commits log = execute(git_cmd('log', '--notes=tinyci*', '--format=%H %ct %N§§§', '--reverse')) lines = log.split('§§§') lines.map { |l| format_commit_data(l) }.select { |c| c[:result].nil? } end
Repeatedly gets the list of eligable commits and runs TinyCI
against them until there are no more remaining
# File lib/tinyci/scheduler.rb, line 98 def run_all_commits commits = retrieve_commits until commits.empty? commits.each { |c| run_commit(c) } commits = retrieve_commits end end
Instantiates {Runner} for a given git object, runs it, and stores the result
# File lib/tinyci/scheduler.rb, line 77 def run_commit(commit) result = @runner_class.new( working_dir: @working_dir, commit: commit[:sha], time: commit[:time], logger: @logger ).run! set_result(commit, result) end
Stores the result in a git note
# File lib/tinyci/scheduler.rb, line 109 def set_result(commit, result) result_message = result ? 'success' : 'failure' execute git_cmd('notes', '--ref', 'tinyci-result', 'add', '-m', result_message, commit[:sha]) end