specistent 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ require 'spec/rake/spectask'
6
6
 
7
7
  GEM = 'specistent'
8
8
  GEM_NAME = 'specistent'
9
- GEM_VERSION = '0.0.1'
9
+ GEM_VERSION = '0.0.2'
10
10
  AUTHORS = ['James Pozdena']
11
11
  EMAIL = "jpoz@jpoz.net"
12
12
  HOMEPAGE = "http://github.com/jpoz/specistent"
data/bin/specistent CHANGED
@@ -1,6 +1,11 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'specistent'
3
+ # require 'specistent'
4
+
5
+ # development
6
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
7
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'specistent')
8
+
4
9
  require 'optparse'
5
10
  require 'yaml'
6
11
 
@@ -10,7 +15,9 @@ options = {
10
15
  :port => 8081,
11
16
  :env => 'test',
12
17
  :remote => 'origin',
13
- :yml => 'specistent.yml'
18
+ :yml => 'specistent.yml',
19
+ :workers => 1,
20
+ :migrate => true
14
21
  }
15
22
 
16
23
  OptionParser.new do |opts|
@@ -28,14 +35,18 @@ OptionParser.new do |opts|
28
35
  options[:port] = p.to_i
29
36
  end
30
37
 
31
- opts.on("-e","--env ENV", "Environment") do |e|
32
- options[:env] = e
38
+ opts.on('-w', '--workers [WORKERS]', Numeric, "workers (defaults to 1)") do |w|
39
+ options[:workers] = w
33
40
  end
34
41
 
35
42
  opts.on("-b","--branch BRANCH", "git branch to pull from (defaults to current branch)") do |b|
36
43
  options[:branch] = b
37
44
  end
38
45
 
46
+ opts.on("-m","--no-migrate", "do not migrate the database") do |m|
47
+ options[:migrate] = false
48
+ end
49
+
39
50
  opts.on("-r","--remote REMOTE", "git remote to pull from (default origin)") do |r|
40
51
  options[:remote] = r
41
52
  end
@@ -48,14 +59,17 @@ end.parse!
48
59
 
49
60
  if (SpecistentServer == options[:klass])
50
61
  g = Git.open(Dir.pwd)
51
- RENV = options[:env]
52
62
  EventMachine::run {
53
- puts "Started server on port #{options[:port]}"
54
- EventMachine::start_server options[:host], options[:port], SpecistentServer
63
+ puts "Started server on #{options[:host]}:#{options[:port]} with #{options[:workers]} workers"
64
+ EventMachine::start_server( options[:host], options[:port], SpecistentServer ) { |s|
65
+ s.workers = options[:workers]
66
+ s.migrate = options[:migrate]
67
+ }
55
68
  }
56
69
  else
57
70
  servers = YAML::load(File.read(options[:yml])).map { |s| s.split(":") << [] }
58
71
  options[:branch] ||= Git.open(Dir.pwd).branch.name
72
+ @connections = []
59
73
  EM.run {
60
74
  queue = EM::Queue.new
61
75
  processor = proc{ |connection_array|
@@ -64,7 +78,9 @@ else
64
78
  c.branch = options[:branch]
65
79
  c.remote = options[:remote]
66
80
  c.start_spec
81
+ @connections << c
67
82
  }
83
+
68
84
  queue.pop(&processor)
69
85
  }
70
86
  queue.pop(&processor)
@@ -75,6 +91,10 @@ else
75
91
  servers[server_index][2] << f
76
92
  end
77
93
 
94
+ EventMachine::PeriodicTimer.new(5) do
95
+ EM.stop if @connections.all? { |c| !c.connected }
96
+ end
97
+
78
98
  servers.each do |s|
79
99
  queue.push(s)
80
100
  end
@@ -3,9 +3,14 @@ require 'eventmachine'
3
3
  require 'logger'
4
4
  require 'git'
5
5
 
6
- module SpecistentClient # < EventMachine::Connection
6
+ module SpecistentClient
7
7
 
8
8
  attr_accessor :files, :branch, :remote
9
+ attr_reader :connected
10
+
11
+ def initialize()
12
+ @connected = true
13
+ end
9
14
 
10
15
  def start_spec
11
16
  return puts "NO files" if files.empty?
@@ -13,12 +18,12 @@ module SpecistentClient # < EventMachine::Connection
13
18
  end
14
19
 
15
20
  def receive_data(data)
16
- print data
17
- STDOUT.flush
21
+ print data
22
+ STDOUT.flush
18
23
  end
19
24
 
20
25
  def unbind
21
- puts "A connection has terminated"
26
+ @connected = false
22
27
  end
23
28
 
24
29
  end
@@ -10,27 +10,41 @@ module SpecProcess
10
10
  end
11
11
 
12
12
  def receive_data(data)
13
- puts "sent #{data}"
13
+ print data
14
14
  @socket.send_data(data)
15
15
  end
16
16
 
17
+ def unbind
18
+ @socket.worker_completed
19
+ end
20
+
17
21
  end
18
22
 
19
23
  class SpecistentServer < EM::Protocols::LineAndTextProtocol
20
24
  include EM::Protocols::LineText2
21
25
 
26
+ attr_accessor :workers, :migrate
27
+
22
28
  def initialize
23
- @databuf = []
29
+ @workers = 1
30
+ @migrate = true
31
+ @databuf = []
24
32
  @g = nil
25
33
  @directory = nil
34
+ @files = []
35
+ @worker_files = []
36
+ @running_workers = 0
26
37
  end
27
38
 
28
39
  def command(cmd)
29
40
  case cmd
41
+ when /workers/
42
+ send_data(@workers)
30
43
  when /run\s(\w*)\/(\w*)\s(.*)/
31
44
  @g = Git.open(Dir.pwd)
32
45
  puts @g.pull($1, "#{$1}/#{$2}", "#{$1} pull")
33
- EM.popen("/bin/sh -c 'RAILS_ENV=#{RENV} spec #{$3}'", SpecProcess, self)
46
+ @files = $3.split(' ')
47
+ run_enviroments
34
48
  else
35
49
  send_data("unknown command #{cmd.inspect}\r\n")
36
50
  end
@@ -38,12 +52,37 @@ class SpecistentServer < EM::Protocols::LineAndTextProtocol
38
52
 
39
53
  def receive_data(data)
40
54
  @databuf << data
41
- puts data
42
55
  if (/(.*)\;/ =~ @databuf.to_s)
43
56
  command($1)
44
57
  reset_databuf()
45
58
  end
46
59
  end
60
+
61
+ def run_enviroments
62
+ split_files
63
+ @workers.times do |i|
64
+ test_env_number = i < 1 ? nil : i
65
+ puts system("RAILS_ENV=test TEST_ENV_NUMBER=#{test_env_number} rake db:migrate") if @migrate
66
+ EM.popen("/bin/sh -c 'TEST_ENV_NUMBER=#{test_env_number} spec #{@worker_files[i].join(' ')}'", SpecProcess, self)
67
+ @running_workers += 1
68
+ end
69
+ end
70
+
71
+ def split_files
72
+ @workers.times { @worker_files << [] }
73
+ @files.each_with_index do |f, i|
74
+ worker_index = (i % @workers)
75
+ @worker_files[worker_index] << f
76
+ end
77
+ end
78
+
79
+ def worker_completed
80
+ @running_workers -= 1
81
+ puts "Working Completed #{@running_workers}"
82
+ if 0 == @running_workers
83
+ close_connection(true)
84
+ end
85
+ end
47
86
 
48
87
  private
49
88
  def reset_databuf
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - James Pozdena
@@ -14,8 +14,8 @@ autorequire: specistent
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-01 00:00:00 -07:00
18
- default_executable: specistent
17
+ date: 2010-04-02 00:00:00 -07:00
18
+ default_executable:
19
19
  dependencies: []
20
20
 
21
21
  description: ""