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

.app_process_from_grep(stdout, name = nil) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/grid_runner/applications.rb', line 76

def self.app_process_from_grep(stdout, name = nil)
  stdout.split(/\n/).find do |l| 
    l.match(name) && 
    !l.match("grep") && 
    !l.match("grid_runner")
  end
end

.find(args) ⇒ Object



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

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

.from_procfile(line) ⇒ Object



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

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].strip if ps_out[:pid]

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

.ps_out(name) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/grid_runner/applications.rb', line 84

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 = app_process_from_grep(stdout, name)
      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



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

def display(color_index = rand(0..COLORS.length))
  puts
  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



45
46
47
48
49
50
51
52
# File 'lib/grid_runner/applications.rb', line 45

def kill!
  if status == :running
    # imgops wasnt working with HUP but did with 3 ('sigquit')
    Process.kill(3, pid.to_i)
    Process.kill("HUP", pid.to_i)
    puts Rainbow("killing #{name}").red
  end
end

#logObject



54
55
56
# File 'lib/grid_runner/applications.rb', line 54

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

#runObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/grid_runner/applications.rb', line 58

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

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