Class: Specjour::Manager

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Fork

fork, fork_quietly

Methods included from SocketHelper

#current_uri, #hostname, #ip_from_hostname, #new_uri

Constructor Details

#initialize(options = {}) ⇒ Manager

Returns a new instance of Manager.



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

def initialize(options = {})
  @options = options
  @worker_size = options[:worker_size]
  @worker_task = options[:worker_task]
  @registered_projects = options[:registered_projects]
  @rsync_port = options[:rsync_port]
  at_exit { kill_loader_process }
end

Instance Attribute Details

#dispatcher_uriObject

Returns the value of attribute dispatcher_uri.



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

def dispatcher_uri
  @dispatcher_uri
end

#loader_pidObject (readonly)

Returns the value of attribute loader_pid.



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

def loader_pid
  @loader_pid
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#pidObject

Returns the value of attribute pid.



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

def pid
  @pid
end

#project_nameObject

Returns the value of attribute project_name.



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

def project_name
  @project_name
end

#registered_projectsObject (readonly)

Returns the value of attribute registered_projects.



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

def registered_projects
  @registered_projects
end

#rsync_portObject (readonly)

Returns the value of attribute rsync_port.



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

def rsync_port
  @rsync_port
end

#test_pathsObject

Returns the value of attribute test_paths.



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

def test_paths
  @test_paths
end

#worker_sizeObject (readonly)

Returns the value of attribute worker_size.



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

def worker_size
  @worker_size
end

#worker_taskObject

Returns the value of attribute worker_task.



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

def worker_task
  @worker_task
end

Class Method Details

.start_quietly(options) ⇒ Object



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

def self.start_quietly(options)
  manager = new options.merge(:quiet => true)
  manager.drb_uri
  manager.pid = Fork.fork_quietly { manager.start }
  manager
end

Instance Method Details

#available_for?(project_name) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/specjour/manager.rb', line 28

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

#dispatchObject



37
38
39
40
41
42
43
44
45
# File 'lib/specjour/manager.rb', line 37

def dispatch
  suspend_bonjour do
    sync
    with_clean_env do
      execute_before_fork
      dispatch_loader
    end
  end
end

#dispatch_loaderObject



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

def dispatch_loader
  @loader_pid = fork do
    exec_cmd = "load --printer-uri #{dispatcher_uri} --workers #{worker_size} --task #{worker_task} --project-path #{project_path}"
    exec_cmd << " --test-paths #{test_paths.join(" ")}" if test_paths.any?
    exec_cmd << " --log" if Specjour.log?
    exec_cmd << " --quiet" if quiet?
    exec_ruby = "Specjour::CLI.start(#{exec_cmd.split(' ').inspect})"
    load_path = ''
    $LOAD_PATH.each {|p| load_path << "-I#{p} " if p =~ /specjour/}
    exec_cmd = "ruby #{load_path} -rspecjour -e '#{exec_ruby}'"
    exec exec_cmd
  end
  Process.waitall
ensure
  kill_loader_process
end

#drb_startObject



47
48
49
50
51
# File 'lib/specjour/manager.rb', line 47

def drb_start
  $PROGRAM_NAME = "specjour listen" if quiet?
  DRb.start_service drb_uri.to_s, self
  at_exit { DRb.stop_service }
end

#drb_uriObject



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

def drb_uri
  @drb_uri ||= begin
    current_uri.scheme = "druby"
    current_uri
  end
end

#in_project(&block) ⇒ Object



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

def in_project(&block)
  Dir.chdir(project_path, &block)
end

#interrupted=(bool) ⇒ Object



81
82
83
84
# File 'lib/specjour/manager.rb', line 81

def interrupted=(bool)
  Specjour.interrupted = bool
  kill_loader_process
end

#kill_loader_processObject



86
87
88
89
90
91
92
# File 'lib/specjour/manager.rb', line 86

def kill_loader_process
  if Specjour.interrupted?
    Process.kill('INT', loader_pid) rescue Errno::ESRCH
  else
    Process.kill('TERM', loader_pid) rescue Errno::ESRCH
  end
end

#project_pathObject



98
99
100
# File 'lib/specjour/manager.rb', line 98

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

#quiet?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/specjour/manager.rb', line 109

def quiet?
  options.has_key? :quiet
end

#startObject



102
103
104
105
106
107
# File 'lib/specjour/manager.rb', line 102

def start
  drb_start
  bonjour_announce
  at_exit { stop_bonjour }
  DRb.thread.join
end

#syncObject



113
114
115
116
# File 'lib/specjour/manager.rb', line 113

def sync
  cmd "rsync -aL --delete --ignore-errors --port=#{rsync_port} #{dispatcher_uri.host}::#{project_name} #{project_path}"
  puts "rsync complete"
end