Class: Rake::RemoteTask::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/ext/rake/remote_task.rb

Overview

Action is used to run a task’s remote_actions in parallel on each of its hosts. Actions are created automatically in Rake::RemoteTask#enhance.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task, block) ⇒ Action

Creates a new Action that will run block for task.



485
486
487
488
489
# File 'lib/ext/rake/remote_task.rb', line 485

def initialize(task, block)
  @task  = task
  @block = block
  @workers = ThreadGroup.new
end

Instance Attribute Details

#blockObject (readonly)

The block this action will execute.



479
480
481
# File 'lib/ext/rake/remote_task.rb', line 479

def block
  @block
end

#taskObject (readonly)

The task this action is attached to.



476
477
478
# File 'lib/ext/rake/remote_task.rb', line 476

def task
  @task
end

#workersObject (readonly)

An Array of threads, one for each host this action executes on.



482
483
484
# File 'lib/ext/rake/remote_task.rb', line 482

def workers
  @workers
end

Instance Method Details

#==(other) ⇒ Object

:nodoc:



491
492
493
494
# File 'lib/ext/rake/remote_task.rb', line 491

def ==(other) # :nodoc:
  return false unless Action === other
  block == other.block && task == other.task
end

#execute(hosts, task, args) ⇒ Object

Execute this action on hosts in parallel. Returns when block has completed for each host.



498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
# File 'lib/ext/rake/remote_task.rb', line 498

def execute(hosts, task, args)
  hosts.each do |host|
    t = task.clone
    t.target_host = host
    thread = Thread.new(t) do |task|
      Thread.current.abort_on_exception = true
      Thread.current[:task] = task
      case block.arity
      when 1
        block.call task
      else
        block.call task, args
      end
      Thread.current[:task] = nil
    end
    @workers.add thread
  end
  @workers.list.each { |thr| thr.join }
  
end