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.



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

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.



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

def batch_size
  @batch_size
end

#bonjour_serviceObject (readonly)

Returns the value of attribute bonjour_service.



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

def bonjour_service
  @bonjour_service
end

#dispatcher_uriObject

Returns the value of attribute dispatcher_uri.



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

def dispatcher_uri
  @dispatcher_uri
end

#project_nameObject

Returns the value of attribute project_name.



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

def project_name
  @project_name
end

#registered_projectsObject (readonly)

Returns the value of attribute registered_projects.



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

def registered_projects
  @registered_projects
end

#specs_to_runObject

Returns the value of attribute specs_to_run.



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

def specs_to_run
  @specs_to_run
end

#worker_pidsObject (readonly)

Returns the value of attribute worker_pids.



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

def worker_pids
  @worker_pids
end

#worker_sizeObject (readonly)

Returns the value of attribute worker_size.



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

def worker_size
  @worker_size
end

Instance Method Details

#available_for?(project_name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#bundle_installObject



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

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

#compile_fastorObject



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

def compile_fastor
  # Only compile fastor if there are new changes since the last compilation
  # sha_tool = RUBY_PLATFORM =~ /darwin/ ? "shasum" : "sha1sum"
  # files = Dir["#{project_path}/lib/fastor/*.{rb,h,c,y,sql}"].sort.join(' ')
  # sha_file = "/tmp/fastor_sha_#{project_name}"
  # return if File.exists?("#{project_path}/lib/fastor/fastor.so") && File.exists?(sha_file) &&
  #  `cd #{project_path} && cat #{files} | #{sha_tool}` == File.new(sha_file).readlines.first

  puts "Compiling fastor..."
  system "cd #{project_path} && rake fastor"
  # system "cd #{project_path} && cat #{files} | #{sha_tool} > #{sha_file}"
end

#dispatchObject



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

def dispatch
  suspend_bonjour do
    sync
    FileUtils.mkdir_p("#{project_path}/tmp/cache")
    compile_fastor
    dispatch_workers
  end
end

#dispatch_workersObject



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/specjour/manager.rb', line 65

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



85
86
87
88
89
# File 'lib/specjour/manager.rb', line 85

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

#kill_worker_processesObject



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

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

#project_pathObject



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

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

#startObject



77
78
79
80
81
82
83
# File 'lib/specjour/manager.rb', line 77

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



91
92
93
# File 'lib/specjour/manager.rb', line 91

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