Class: Specjour::Manager

Inherits:
Object
  • Object
show all
Includes:
DRbUndumped, SocketHelpers
Defined in:
lib/specjour/manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SocketHelpers

#hostname, #ip_from_hostname

Constructor Details

#initialize(options = {}) ⇒ Manager

Returns a new instance of Manager.



10
11
12
13
14
15
# File 'lib/specjour/manager.rb', line 10

def initialize(options = {})
  @worker_size = options[:worker_size]
  @batch_size = options[:batch_size]
  @registered_projects = options[:registered_projects]
  @worker_pids = []
end

Instance Attribute Details

#batch_sizeObject (readonly)

Returns the value of attribute batch_size.



8
9
10
# File 'lib/specjour/manager.rb', line 8

def batch_size
  @batch_size
end

#bonjour_serviceObject (readonly)

Returns the value of attribute bonjour_service.



8
9
10
# File 'lib/specjour/manager.rb', line 8

def bonjour_service
  @bonjour_service
end

#dispatcher_uriObject

Returns the value of attribute dispatcher_uri.



8
9
10
# File 'lib/specjour/manager.rb', line 8

def dispatcher_uri
  @dispatcher_uri
end

#project_nameObject

Returns the value of attribute project_name.



7
8
9
# File 'lib/specjour/manager.rb', line 7

def project_name
  @project_name
end

#registered_projectsObject (readonly)

Returns the value of attribute registered_projects.



8
9
10
# File 'lib/specjour/manager.rb', line 8

def registered_projects
  @registered_projects
end

#specs_to_runObject

Returns the value of attribute specs_to_run.



7
8
9
# File 'lib/specjour/manager.rb', line 7

def specs_to_run
  @specs_to_run
end

#worker_pidsObject (readonly)

Returns the value of attribute worker_pids.



8
9
10
# File 'lib/specjour/manager.rb', line 8

def worker_pids
  @worker_pids
end

#worker_sizeObject (readonly)

Returns the value of attribute worker_size.



8
9
10
# File 'lib/specjour/manager.rb', line 8

def worker_size
  @worker_size
end

Instance Method Details

#available_for?(project_name) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/specjour/manager.rb', line 17

def available_for?(project_name)
   registered_projects ? registered_projects.include?(project_name) : true
end

#bundle_installObject



21
22
23
24
25
26
27
# File 'lib/specjour/manager.rb', line 21

def bundle_install
  Dir.chdir(project_path) do
    unless system('bundle check > /dev/null')
      system("bundle install --relock > /dev/null")
    end
  end
end

#dispatchObject



42
43
44
45
46
47
48
# File 'lib/specjour/manager.rb', line 42

def dispatch
  suspend_bonjour do
    sync
    bundle_install
    dispatch_workers
  end
end

#dispatch_workersObject



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/specjour/manager.rb', line 50

def dispatch_workers
  GC.copy_on_write_friendly = true if GC.respond_to?(:copy_on_write_friendly=)
  (1..worker_size).each do |index|
    worker_pids << fork do
      exec("specjour --batch-size #{batch_size} #{'--log' if Specjour.log?} --do-work #{project_path},#{dispatcher_uri},#{index}")
      Kernel.exit!
    end
  end
  at_exit { kill_worker_processes }
  Process.waitall
end

#drb_startObject



70
71
72
73
74
# File 'lib/specjour/manager.rb', line 70

def drb_start
  DRb.start_service nil, self
  puts "Manager started at #{drb_uri}"
  at_exit { DRb.stop_service }
end

#kill_worker_processesObject



34
35
36
# File 'lib/specjour/manager.rb', line 34

def kill_worker_processes
  Process.kill('TERM', *worker_pids) rescue nil
end

#project_pathObject



38
39
40
# File 'lib/specjour/manager.rb', line 38

def project_path
  File.join("/tmp", project_name)
end

#startObject



62
63
64
65
66
67
68
# File 'lib/specjour/manager.rb', line 62

def start
  drb_start
  puts "Workers ready: #{worker_size}"
  bonjour_announce
  Signal.trap('INT') { puts; puts "Shutting down manager..."; exit }
  DRb.thread.join
end

#syncObject



76
77
78
# File 'lib/specjour/manager.rb', line 76

def sync
  cmd "rsync -aL --delete --port=8989 #{dispatcher_uri.host}::#{project_name} #{project_path}"
end