Class: ActiveSupport::Testing::Parallelization

Inherits:
Object
  • Object
show all
Defined in:
activesupport/lib/active_support/testing/parallelization.rb,
activesupport/lib/active_support/testing/parallelization/server.rb,
activesupport/lib/active_support/testing/parallelization/worker.rb,
activesupport/lib/active_support/testing/parallelization/test_distributor.rb,
activesupport/lib/active_support/testing/parallelization/thread_pool_executor.rb

Overview

:nodoc:

Defined Under Namespace

Classes: PrerecordResultClass, RoundRobinDistributor, RoundRobinWorkStealingDistributor, Server, SharedQueueDistributor, TestDistributor, ThreadPoolExecutor, Worker

Constant Summary collapse

@@before_fork_hooks =
[]
@@after_fork_hooks =
[]
@@run_cleanup_hooks =
[]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(worker_count, work_stealing: false) ⇒ Parallelization

Returns a new instance of Parallelization.



38
39
40
41
42
43
44
45
46
47
# File 'activesupport/lib/active_support/testing/parallelization.rb', line 38

def initialize(worker_count, work_stealing: false)
  @worker_count = worker_count

  distributor = (work_stealing ? RoundRobinWorkStealingDistributor : RoundRobinDistributor).new \
    worker_count: worker_count

  @queue_server = Server.new(distributor: distributor)
  @worker_pool = []
  @url = DRb.start_service("drbunix:", @queue_server).uri
end

Class Method Details

.after_fork_hook(&blk) ⇒ Object



24
25
26
# File 'activesupport/lib/active_support/testing/parallelization.rb', line 24

def self.after_fork_hook(&blk)
  @@after_fork_hooks << blk
end

.before_fork_hook(&blk) ⇒ Object



16
17
18
# File 'activesupport/lib/active_support/testing/parallelization.rb', line 16

def self.before_fork_hook(&blk)
  @@before_fork_hooks << blk
end

.run_cleanup_hook(&blk) ⇒ Object



32
33
34
# File 'activesupport/lib/active_support/testing/parallelization.rb', line 32

def self.run_cleanup_hook(&blk)
  @@run_cleanup_hooks << blk
end

Instance Method Details

#<<(work) ⇒ Object



60
61
62
# File 'activesupport/lib/active_support/testing/parallelization.rb', line 60

def <<(work)
  @queue_server << work
end

#before_forkObject



49
50
51
# File 'activesupport/lib/active_support/testing/parallelization.rb', line 49

def before_fork
  Parallelization.before_fork_hooks.each(&:call)
end

#shutdownObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'activesupport/lib/active_support/testing/parallelization.rb', line 68

def shutdown
  dead_worker_pids = @worker_pool.filter_map do |pid|
    Process.waitpid(pid, Process::WNOHANG)
  rescue Errno::ECHILD
    pid
  end
  @queue_server.remove_dead_workers(dead_worker_pids)

  @queue_server.shutdown
  @worker_pool.each do |pid|
    Process.waitpid(pid)
  rescue Errno::ECHILD
    nil
  end
end

#sizeObject



64
65
66
# File 'activesupport/lib/active_support/testing/parallelization.rb', line 64

def size
  @worker_count
end

#startObject



53
54
55
56
57
58
# File 'activesupport/lib/active_support/testing/parallelization.rb', line 53

def start
  before_fork
  @worker_pool = @worker_count.times.map do |worker|
    Worker.new(worker, @url).start
  end
end