alt-foreman 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/bin/alt-foreman CHANGED
@@ -1,9 +1,25 @@
1
1
  #!/usr/bin/ruby
2
2
 
3
3
  require 'alt-foreman'
4
+ require 'alt-foreman/version'
4
5
  require 'alt-foreman/process'
5
6
 
6
- Dir.chdir(File.dirname(ARGV[0]))
7
+
8
+ pf = ARGV[0] || "Procfile"
9
+ is_fail = !File.exist?(pf)
10
+ is_help = ARGV.include?('-h') or ARGV.include?("--help")
11
+
12
+ if is_help or is_fail
13
+ unless is_help
14
+ puts "No such file `#{ARGV[0]}'"
15
+ end
16
+ puts "alt-foreman #{Alt::Foreman::VERSION}"
17
+ puts "Crude alternative to foreman for local development"
18
+ puts "Usage: alt-foreman [/path/to/Procfile]"
19
+ exit(is_help ? 0 : 1)
20
+ end
21
+
22
+ Dir.chdir(File.dirname(ARGV[0] || 'Procfile'))
7
23
 
8
24
  if File.exist?('Gemfile')
9
25
  ENV["BUNDLE_GEMFILE"]= File.expand_path("Gemfile")
@@ -11,14 +27,21 @@ end
11
27
 
12
28
  processes = IO.readlines(ARGV[0]).map do |line|
13
29
  line = line.chomp.gsub(/#.*$/, '')
14
- if line =~ /:/
15
- Alt::Foreman::Process.new(*line.split(/:/,2))
30
+ if line =~ /:\s+/
31
+ Alt::Foreman::Process.new(*line.split(/:\s+/,2))
16
32
  else
17
33
  nil
18
34
  end
19
35
  end.compact
20
36
 
21
37
  threads = processes.map do |process|
22
- Thread.new { process.run }
38
+ Thread.new do
39
+ loop do
40
+ process.run
41
+ process.log("Process #{$?.pid} `#{process.cmd}' died (#{$?.status})", :red)
42
+ sleep 2
43
+ process.log("`#{process.cmd}' re-spawning", :red)
44
+ end
45
+ end
23
46
  end.each { |t| t.join }
24
47
 
@@ -1,3 +1,5 @@
1
+ require 'shellwords'
2
+
1
3
  module Alt
2
4
  module Foreman
3
5
  class Process
@@ -5,12 +7,55 @@ module Alt
5
7
  def initialize(name, cmd)
6
8
  @name, @cmd = name, cmd
7
9
  end
10
+ def log(info, col = nil)
11
+ Alt::Foreman.log(@name, info, col)
12
+ end
8
13
  def run
14
+ args = Shellwords.split(@cmd)
15
+ args.each_with_index do |arg, index|
16
+ if arg =~ /-p(\d+)/
17
+ Foreman.kill_tcp_server!($1, @name)
18
+ elsif arg == '-p' && args[index+1] =~ /\d+/
19
+ Foreman.kill_tcp_server!(args[index+1], @name)
20
+ end
21
+ end
22
+
9
23
  IO.popen(@cmd, 'rb') do |io|
10
24
  while line = io.gets
11
- Alt::Foreman.log(@name, line)
25
+ log(line)
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+
32
+ module_function
33
+ def kill_tcp_server!(need_port, name)
34
+ tries = 0
35
+ loop do
36
+ found = false
37
+
38
+ `netstat -nWlp 2>/dev/null|grep '^tcp '`.each_line do |line|
39
+ _, _, _, addr, _, what, program = line.strip.split(/\s+/)
40
+ ip, port = addr.split(%r/:/)
41
+ pid, process = program.split(%r:/:)
42
+
43
+ if (port.to_i == need_port.to_i) && (pid.to_i > 0)
44
+ found = true
45
+ sig = (tries <= 5 ? 'TERM' : 'KILL')
46
+ Alt::Foreman.log(name, "Found conflicting server process - sending #{sig} to #{pid} & sleeping for 2s", :red)
47
+ ::Process.kill(sig, pid.to_i)
48
+ sleep 2
49
+ tries += 1
12
50
  end
13
51
  end
52
+
53
+ return unless found
54
+
55
+ if found && tries > 15
56
+ STDERR.puts "Failed to kill existing process - continuing anyway."
57
+ return
58
+ end
14
59
  end
15
60
  end
16
61
  end
@@ -1,5 +1,5 @@
1
1
  module Alt
2
2
  module Foreman
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
data/lib/alt-foreman.rb CHANGED
@@ -7,13 +7,17 @@ module Alt
7
7
  @mutex = Mutex.new
8
8
  @senders = {}
9
9
  @colours = [:blue, :red, :yellow, :cyan, :green]
10
- def self.log(sender, line)
10
+ def self.log(sender, line, line_colour = nil)
11
11
  @mutex.synchronize do
12
12
  colour = @senders[sender]
13
13
  unless colour
14
14
  colour = @senders[sender] = @colours[@senders.size % @colours.size]
15
15
  end
16
- puts Term::ANSIColor.send(colour, Term::ANSIColor.bold(sprintf '%s %15s | ', Time.now.strftime('%H:%M:%S'), sender) + line.chomp)
16
+ line = line.chomp
17
+ if line_colour
18
+ line = Term::ANSIColor.send(line_colour, line)
19
+ end
20
+ puts Term::ANSIColor.send(colour, Term::ANSIColor.bold(sprintf '%s %15s | ', Time.now.strftime('%H:%M:%S'), sender) + line)
17
21
  end
18
22
  end
19
23
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alt-foreman
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2012-10-26 00:00:00.000000000 Z
14
+ date: 2013-01-10 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: term-ansicolor
@@ -70,4 +70,3 @@ signing_key:
70
70
  specification_version: 3
71
71
  summary: rubbish dev version of foreman
72
72
  test_files: []
73
- has_rdoc: