Class: CiLite::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/cilite/runner.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Runner

Returns a new instance of Runner.



10
11
12
13
14
15
# File 'lib/cilite/runner.rb', line 10

def initialize(options)
  options = (KVS['config'] || {}).merge(options)
  @branch = options[:branch]
  @command = options[:command]
  @interval = options[:interval]
end

Instance Attribute Details

#branchObject (readonly)

Returns the value of attribute branch.



7
8
9
# File 'lib/cilite/runner.rb', line 7

def branch
  @branch
end

#commandObject (readonly)

Returns the value of attribute command.



7
8
9
# File 'lib/cilite/runner.rb', line 7

def command
  @command
end

#intervalObject (readonly)

Returns the value of attribute interval.



7
8
9
# File 'lib/cilite/runner.rb', line 7

def interval
  @interval
end

#startedObject

Returns the value of attribute started.



8
9
10
# File 'lib/cilite/runner.rb', line 8

def started
  @started
end

Class Method Details

.start(options) ⇒ Object



3
4
5
# File 'lib/cilite/runner.rb', line 3

def self.start(options)
  self.new(options).start
end

Instance Method Details

#git_updateObject



40
41
42
43
44
45
46
47
48
# File 'lib/cilite/runner.rb', line 40

def git_update
  if system("git fetch origin && git reset --hard origin/#{branch}")
    hash = `git rev-parse origin/#{branch}`.chomp
    raise "failed to get HEAD commit of origin/#{branch}" unless $? == 0
    hash
  else
    raise "failed to update origin/#{branch}"
  end
end

#output_result(hash, build) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/cilite/runner.rb', line 64

def output_result(hash, build)
  puts build.output
  if build.success?
    puts "<green>[Success!] #{hash}</green>".termcolor
  else
    puts "<red>[Failure!] #{hash}</red>".termcolor
  end
end

#startObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cilite/runner.rb', line 17

def start
  return if started
  Thread.start do
    loop do
      begin
        test_if_updated
      rescue Exception => e
        puts "<red>#{TermColor.escape(e.to_s)}</red>".termcolor
      ensure
        sleep interval
      end
    end
  end
  self.started = true
end

#test(hash) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/cilite/runner.rb', line 50

def test(hash)
  puts "start: #{hash}", command

  build = Build.new(command)
  build.start
  Log[hash] = build.to_hash.merge(
                :hash => hash,
                :created_at => Time.now,
                :branch => branch
              )

  output_result(hash, build)
end

#test_if_updatedObject



33
34
35
36
37
38
# File 'lib/cilite/runner.rb', line 33

def test_if_updated
  hash = git_update
  unless KVS[hash]
    test(hash)
  end
end