Class: Rake::RemoteTask::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/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.



564
565
566
567
568
# File 'lib/rake_remote_task.rb', line 564

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

Instance Attribute Details

#blockObject (readonly)

The block this action will execute.



554
555
556
# File 'lib/rake_remote_task.rb', line 554

def block
  @block
end

#taskObject (readonly)

The task this action is attached to.



549
550
551
# File 'lib/rake_remote_task.rb', line 549

def task
  @task
end

#workersObject (readonly)

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



559
560
561
# File 'lib/rake_remote_task.rb', line 559

def workers
  @workers
end

Instance Method Details

#==(other) ⇒ Object

:nodoc:



570
571
572
573
# File 'lib/rake_remote_task.rb', line 570

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.



579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
# File 'lib/rake_remote_task.rb', line 579

def execute hosts, task, args
  hosts.each do |host|
    t = task.clone
    t.target_host = host
    thread = Thread.new(t) do |task|
      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