Class: GV::Valley::Runner

Inherits:
Bedrock::Service
  • Object
show all
Includes:
Common::DockerHelper, Common::HostHelper
Defined in:
lib/gv/valley/runner.rb

Instance Method Summary collapse

Constructor Details

#initializeRunner

Returns a new instance of Runner.



15
16
17
18
19
# File 'lib/gv/valley/runner.rb', line 15

def initialize
  super
  pull_image_if_does_not_exists "flynn/slugrunner"
  register_all
end

Instance Method Details

#logs(name, follow = false, &block) ⇒ Object

retrives logs



96
97
98
99
100
# File 'lib/gv/valley/runner.rb', line 96

def logs name, follow = false, &block
  debug "Runner#logs name:#{name}, follow:#{follow}"     
  
  pipe "docker logs #{follow ? "-f" : nil} #{container_id(name)}", &block
end

#remove(name, type = nil, &block) ⇒ Object

force removes all matching jobs



48
49
50
51
52
53
54
# File 'lib/gv/valley/runner.rb', line 48

def remove name, type = nil, &block
  debug "Runner#remove name:#{name}, type:#{type}"     
  
  batch "#{name}.#{type}", "kill", true
  batch "#{name}.#{type}", "rm", true   
  unregister_all(/#{name}\.#{type}\./)
end

#run(name, cmd, &block) ⇒ Object

runs one-off job



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/gv/valley/runner.rb', line 25

def run name, cmd, &block
  app = App.find!(name)  
                
  cleanup
 
  params =  %(--rm -a stdout -a stderr ) #note that space at the end!
  params << %(-i -a stdin) if cmd =~ Sticks::Pipe::INTERACTIVE_COMMANDS
  cmd    =  %(docker run --name=#{app[:name]}.run.#{genuuid()} #{params} #{getenv(app)} flynn/slugrunner #{cmd})

  debug "Runner#run name:#{name}, cmd:#{cmd}"     
    
  result = pipe cmd, &block
  
  sleep 2
  
  cleanup
  result
end

#start(name, type, index, &block) ⇒ Object

starts process



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/gv/valley/runner.rb', line 60

def start name, type, index, &block
  raise "AppNotFound" unless app = App.find(name) 
  
  info "Starting #{name}.#{type}.#{index}"
  
  cleanup
    
  params = %(-d -p #{self.external_ip}::#{App::PORT})
  cmd = %(docker run --name=#{name}.#{type}.#{index} #{params} #{getenv(app)} flynn/slugrunner start #{type})
  
  debug "Runner#start name:#{name}, type:#{type} index:#{index}"     
          
  container_id = pipe(cmd)
  
  if container_id =~ /^[a-zA-Z0-9]{64}$/
    
    register "#{name}.#{type}.#{index}"
    info(container_id)
    
  elsif container_id.include? "Error: Conflict"
    error "Container name conflict: #{name}.#{type}.#{index}, docker service reloading..." 
    
    restart_docker!

    remove name, type
    start name, type, index, &block
  else
    raise container_id
  end
  
end