Class: Rake::Subproject::TaskRunner

Inherits:
Object
  • Object
show all
Includes:
FileUtils, Client
Defined in:
lib/rake/subproject/task_runner.rb

Overview

:nodoc: all

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ TaskRunner

Returns a new instance of TaskRunner.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rake/subproject/task_runner.rb', line 9

def initialize(path)
  raise "Subproject path '#{path}' does not exist" unless File.exist?(path)
  if File.directory?(path)
    @directory = path
  else
    rakefile = path
    @directory = File.dirname(path)
  end

  @@rake_env ||= ARGV.each_with_object({}) do |arg, hash|
    hash[$1] = $2 if arg =~ /^(\w+)=(.*)$/m
  end
  
  child_socket, parent_socket  = Socket.pair(:UNIX, :STREAM, 0)
  port = Port.new(parent_socket, "client")
  @session_manager = SessionManager.new(port)
  thread = Thread.new { @session_manager.start } 
  
  at_exit do
    @session_manager.close
    port.close
    thread.join
  end

  rake_args = [
      "bundle", "exec", "--keep-file-descriptors",
      "rake",
        # Do not search parent directories for the Rakefile.
      "--no-search",
        # Include LIBDIR in the search path for required modules.
      "--libdir", File.dirname(__FILE__)+ "/server",
        # Require MODULE before executing rakefile.
      "-r", "task", "subproject:server:start[#{child_socket.fileno}]",
      
  ]
  rake_args = rake_args + ['--rakefile', File.basename(rakefile)] unless rakefile.nil?
  rake_args << {child_socket.fileno => child_socket, :chdir => @directory}

  Bundler.with_clean_env do
    @server_pid = Process.spawn(*rake_args)
  end

  ['TERM', 'KILL', 'INT'].each do |sig|
    Signal.trap(sig) do
      Process.kill(sig, @server_pid)
    end
  end
  
  child_socket.close
end

Instance Attribute Details

#directoryObject (readonly)

Returns the value of attribute directory.



4
5
6
# File 'lib/rake/subproject/task_runner.rb', line 4

def directory
  @directory
end

Instance Method Details

#invoke_task(name, args) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rake/subproject/task_runner.rb', line 60

def invoke_task(name, args)
  log "TaskRunner#invoke_task: #{name} with #{args}"
  session = @session_manager.with_session do |session|
    session.write(message: 'invoke_task', name: name, args: {hash: args.to_hash, array: args.to_a})
    response = session.read
    if (response['message'] == 'task_failed')
      e = ::Rake::Subproject::Error.new(response['exception']['message'])
      e.set_backtrace(response['exception']['backtrace'] + Thread.current.backtrace)
      raise e
    end
  end
end