Class: SpecistentServer

Inherits:
EM::Protocols::LineAndTextProtocol
  • Object
show all
Includes:
EM::Protocols::LineText2
Defined in:
lib/specistent/specistent_server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSpecistentServer

Returns a new instance of SpecistentServer.



28
29
30
31
32
33
34
35
36
37
# File 'lib/specistent/specistent_server.rb', line 28

def initialize
  @workers = 1
  @migrate = true
  @databuf = []  
  @g = nil
  @directory = nil
  @files = []
  @worker_files = []
  @running_workers = 0
end

Instance Attribute Details

#migrateObject

Returns the value of attribute migrate.



26
27
28
# File 'lib/specistent/specistent_server.rb', line 26

def migrate
  @migrate
end

#workersObject

Returns the value of attribute workers.



26
27
28
# File 'lib/specistent/specistent_server.rb', line 26

def workers
  @workers
end

Instance Method Details

#command(cmd) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/specistent/specistent_server.rb', line 39

def command(cmd)
  case cmd
  when /workers/
    send_data(@workers)
  when /run\s(\w*)\/(\w*)\s(.*)/
    @g = Git.open(Dir.pwd)
    puts @g.pull($1, "#{$1}/#{$2}", "#{$1} pull")
    @files = $3.split(' ')
    run_enviroments
  else
    send_data("unknown command #{cmd.inspect}\r\n")
  end
end

#receive_data(data) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/specistent/specistent_server.rb', line 53

def receive_data(data)
  @databuf << data
  if (/(.*)\;/ =~ @databuf.to_s)
    command($1)
    reset_databuf()
  end
end

#run_enviromentsObject



61
62
63
64
65
66
67
68
69
# File 'lib/specistent/specistent_server.rb', line 61

def run_enviroments
  split_files
  @workers.times do |i|
    test_env_number = i < 1 ? nil : i
    puts system("RAILS_ENV=test TEST_ENV_NUMBER=#{test_env_number} rake db:migrate") if @migrate
    EM.popen("/bin/sh -c 'TEST_ENV_NUMBER=#{test_env_number} spec #{@worker_files[i].join(' ')}'", SpecProcess, self)
    @running_workers += 1
  end
end

#split_filesObject



71
72
73
74
75
76
77
# File 'lib/specistent/specistent_server.rb', line 71

def split_files
  @workers.times { @worker_files << [] }
  @files.each_with_index do |f, i|
    worker_index = (i % @workers)
    @worker_files[worker_index] << f
  end
end

#worker_completedObject



79
80
81
82
83
84
85
# File 'lib/specistent/specistent_server.rb', line 79

def worker_completed
  @running_workers -= 1
  puts "Working Completed #{@running_workers}"
  if 0 == @running_workers
    close_connection(true)
  end
end