Class: Wildcloud::Runner::Runner

Inherits:
Object
  • Object
show all
Includes:
Singleton, RubyTemplate, Tools
Defined in:
lib/wildcloud/runner/runner.rb,
lib/wildcloud/runner/template/ruby.rb

Instance Method Summary collapse

Methods included from RubyTemplate

#ruby_use

Methods included from Tools

#logger, #run

Constructor Details

#initializeRunner

Returns a new instance of Runner.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/wildcloud/runner/runner.rb', line 28

def initialize

  $0 = "Wildcloud::Runner"

  logger.info('Runner', 'Changing to application directory')
  Dir.chdir('/home/wildcloud')

  logger.info('Runner', 'Loading Cloudfile')
  @cloudfile = YAML.load_file('Cloudfile') if File.exists?('Cloudfile')
  @cloudfile ||= {}
  @cloudfile['templates'] ||= @cloudfile['modules']

  if @cloudfile['templates'].kind_of?(Hash)
    @cloudfile['templates'].each do |name, config|
      config ||= {}
      template = File.expand_path("../template/#{name}.rb", __FILE__)
      if File.exists?(template)
        logger.info('Runner', "Running template: #{name}")
        require template
        send("#{name}_run".to_sym, config)
      else
        logger.info('Runner', "Unavailable template: #{name}")
      end
    end
  end

  @processes = {}
  @commands = YAML.load_file('Procfile')
  @commands.each do |name, command|
    start_process(name, command)
  end

end

Instance Method Details

#ruby_run(config) ⇒ Object



59
60
61
# File 'lib/wildcloud/runner/template/ruby.rb', line 59

def ruby_run(config)
  ruby_use(config)
end

#start_process(name, command) ⇒ Object



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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/wildcloud/runner/runner.rb', line 62

def start_process(name, command)
  prc = @processes[name] = {}

  prc[:name] = name
  prc[:command] = command

  prc[:input], prc[:output] = IO.pipe

  logger.info(name, "Starting #{command}")

  prc[:pid] = fork do

    $0 = "Process: #{name}"

    $stdout.reopen(prc[:output])
    $stderr = $stdout

    prc[:input].close

    stdout, stdin, pid = PTY.spawn(command)

    while line = stdout.readline
      $stdout << line
    end

    Process.wait(pid)

  end

  logger.info(name, "Process started with PID #{prc[:pid]}.")

  prc[:output].close

  logger.info(name, "Starting control thread.")

  prc[:collector] = Thread.new do
    Process.wait(prc[:pid])
    logger.info(name, "Process waiting 10s to restart.")
    sleep(10)
    start_process(name, command)
  end

  logger.info(name, "Starting reading thread.")

  prc[:reader] = Thread.new do
    logger.info(name, "Waiting for data.")
    until prc[:input].eof?
      begin
        logger.info(name, prc[:input].gets.chomp)
      rescue Exception => e
        logger.info(name, "Exception #{e.message}")
      end
    end
    logger.info(name, "Pipe closed.")
  end

rescue Exception => e
  logger.info(name, "Exception #{e.message}")
end