Class: Specjour::Manager

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Manager

Returns a new instance of Manager.



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

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.



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

def batch_size
  @batch_size
end

#bonjour_serviceObject (readonly)

Returns the value of attribute bonjour_service.



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

def bonjour_service
  @bonjour_service
end

#dispatcher_uriObject

Returns the value of attribute dispatcher_uri.



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

def dispatcher_uri
  @dispatcher_uri
end

#project_nameObject

Returns the value of attribute project_name.



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

def project_name
  @project_name
end

#registered_projectsObject (readonly)

Returns the value of attribute registered_projects.



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

def registered_projects
  @registered_projects
end

#specs_to_runObject

Returns the value of attribute specs_to_run.



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

def specs_to_run
  @specs_to_run
end

#worker_pidsObject (readonly)

Returns the value of attribute worker_pids.



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

def worker_pids
  @worker_pids
end

#worker_sizeObject (readonly)

Returns the value of attribute worker_size.



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

def worker_size
  @worker_size
end

Instance Method Details

#available_for?(project_name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#bundle_installObject



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

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

#dispatchObject



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

def dispatch
  suspend_bonjour do
    sync
    bundle_install
    dispatch_workers
  end
end

#dispatch_workersObject



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

def dispatch_workers
  (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



67
68
69
70
71
# File 'lib/specjour/manager.rb', line 67

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

#kill_worker_processesObject



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

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

#project_pathObject



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

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

#startObject



60
61
62
63
64
65
# File 'lib/specjour/manager.rb', line 60

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

#syncObject



73
74
75
# File 'lib/specjour/manager.rb', line 73

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