Class: App

Inherits:
Object
  • Object
show all
Defined in:
lib/grid_runner/applications.rb

Constant Summary collapse

PROCFILE =
File.open(Dir.pwd + "/Procfile")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, command, status, pid) ⇒ App

Returns a new instance of App.



8
9
10
11
12
13
# File 'lib/grid_runner/applications.rb', line 8

def initialize(name, command, status, pid)
  @name    = name
  @command = command
  @status  = status
  @pid     = pid
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



6
7
8
# File 'lib/grid_runner/applications.rb', line 6

def command
  @command
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/grid_runner/applications.rb', line 6

def name
  @name
end

#pidObject (readonly)

Returns the value of attribute pid.



6
7
8
# File 'lib/grid_runner/applications.rb', line 6

def pid
  @pid
end

#statusObject (readonly)

Returns the value of attribute status.



6
7
8
# File 'lib/grid_runner/applications.rb', line 6

def status
  @status
end

Class Method Details

.allObject



15
16
17
18
19
# File 'lib/grid_runner/applications.rb', line 15

def self.all
  PROCFILE.map do |line|
    App.from_procfile(line)
  end
end

.find(args) ⇒ Object



21
22
23
# File 'lib/grid_runner/applications.rb', line 21

def self.find(args)
  App.all.select { |app| args.include?( app.name ) }
end

.from_procfile(line) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/grid_runner/applications.rb', line 25

def self.from_procfile(line)
  name = line.split(":")[0]
  cmd = line.split(":")[1]
  ps_out = ps_out(name)
  status = ps_out[:status]
  pid = ps_out[:pid]

  App.new(name, cmd, status, pid)
end

.ps_out(name) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/grid_runner/applications.rb', line 72

def self.ps_out(name)
  stdout, stderr, cmd_status = Open3.capture3("ps aux | grep #{name}")
  if cmd_status.success?
    # find processes that are either sbt or ES, and filter out the grep command itself
    if p = stdout.split(/\n/).find {|l| (l.match("sbt") || l.match("elasticsearch")) && !l.match("grep") }
      proc_status = :running
      pid = p.match(/\d{1,8}\s/)[0]
    else
      proc_status = :not_running
    end
  end
  {status: proc_status, pid: pid}
end

Instance Method Details

#display(color_index = rand(0..COLORS.length)) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/grid_runner/applications.rb', line 35

def display(color_index = rand(0..COLORS.length))
  puts Rainbow("#{name} ").send(COLORS[color_index % COLORS.length]).underline 
  puts "status: #{status}"
  puts "pid: #{pid}" if status == :running
  puts "log: #{log.path}"
  puts
end

#kill!Object



43
44
45
46
47
48
# File 'lib/grid_runner/applications.rb', line 43

def kill!
  if status == :running
    Process.kill("HUP", pid.to_i)
    puts Rainbow("killing #{name}").red
  end
end

#logObject



50
51
52
# File 'lib/grid_runner/applications.rb', line 50

def log
  File.open((Dir.pwd + "/logs/" + "#{name}.log"), "w+")
end

#runObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/grid_runner/applications.rb', line 54

def run
  puts Rainbow("running #{app.name}").green

  Process.spawn(
    ENV,
    command, 
    out: [log.path, "w"], 
    err: [log.path, "w"],
    :in => "/dev/null"
    )
end