Class: MixedGauge::AllShardsInParallel

Inherits:
Object
  • Object
show all
Defined in:
lib/mixed_gauge/all_shards_in_parallel.rb

Overview

Support parallel execution with each shard and deal with AR connection management in parallel execution.

Instance Method Summary collapse

Constructor Details

#initialize(shards, service:) ⇒ AllShardsInParallel

Returns a new instance of AllShardsInParallel.

Parameters:

  • shards (Array<Class>)

    An array of shard model class

  • service (Expeditor::Service)


7
8
9
10
# File 'lib/mixed_gauge/all_shards_in_parallel.rb', line 7

def initialize(shards, service:)
  @shards = shards
  @service = service
end

Instance Method Details

#each {|Class| ... } ⇒ MixedGauge::AllShardsInParallel

Examples:

User.all_shards_in_parallel.each {|m| puts m.count }

Yields:

  • (Class)

    A shard model class

Returns:



36
37
38
39
# File 'lib/mixed_gauge/all_shards_in_parallel.rb', line 36

def each(&block)
  map(&block) if block_given?
  self
end

#flat_map {|Class| ... } ⇒ Array

Returns A result.

Examples:

User.all_shards_in_parallel.flat_map {|m| m.where(age: 1) }

Yields:

  • (Class)

    A shard model class

Returns:

  • (Array)

    A result



28
29
30
# File 'lib/mixed_gauge/all_shards_in_parallel.rb', line 28

def flat_map(&block)
  map(&block).flatten
end

#map {|Class| ... } ⇒ Array

Returns A result.

Examples:

User.all_shards_in_parallel.map(&:count).reduce(&:+)

Yields:

  • (Class)

    A shard model class

Returns:

  • (Array)

    A result



16
17
18
19
20
21
22
# File 'lib/mixed_gauge/all_shards_in_parallel.rb', line 16

def map
  commands = @shards.map do |m|
    Expeditor::Command.new(service: @service) { m.connection_pool.with_connection { yield m } }
  end
  commands.each(&:start)
  commands.map(&:get)
end