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
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

Instance Method Details

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



36
37
38
39
40
41
42
# 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}"
end

#kill!Object



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

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



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

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

#runObject



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

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

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