Repository is archived
No commit activity in last 3 years
No release in over 3 years
Provides Ruby APIs for syncing CircleCI parallel nodes and transferring files between the nodes
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.12
 Project Readme

Gem Version Dependency Status CircleCI Coverage Status Code Climate

CircleCI::Parallel

CircleCI::Parallel provides simple Ruby APIs for syncing CircleCI parallel nodes and transferring all node data to a single master node.

Installation

Add this line to your application's Gemfile:

gem 'circleci-parallel'

And then execute:

$ bundle install

Usage

Before using CircleCI::Parallel:

CircleCI::Parallel uses SSH for syncing nodes and transferring data between nodes. All data will be transferred to the master node (where CIRCLE_NODE_INDEX is 0).

# circle.yml
test:
  override:
    - ruby test.rb:
        parallel: true
# test.rb
require 'circleci/parallel'

CircleCI::Parallel.configure do |config|
  # This hook will be invoked on every node.
  # The current working directory in this hook is set to the local data directory
  # where node specific data should be saved in.
  config.on_every_node.before_sync do
    data = { 'foo' => ENV['CIRCLE_NODE_INDEX'].to_i * 10 }
    json = JSON.generate(data)
    File.write('data.json', json)
  end

  # This hook will be invoked only on the master node after downloading all data from slave nodes.
  # The current working directory in this hook is set to the download data directory
  # where all node data are gathered into.
  # The directory structure on the master node will be the following:
  #
  #     .
  #     ├── node0
  #     │   └── node_specific_data_you_saved_on_node0.txt
  #     ├── node1
  #     │   └── node_specific_data_you_saved_on_node1.txt
  #     └── node2
  #         └── node_specific_data_you_saved_on_node2.txt
  config.on_master_node.after_download do
    merged_data = {}

    Dir.glob('*/data.json') do |path|
      json = File.read(path)
      data = JSON.parse(json)
      node_name = File.dirname(path)
      merged_data[node_name] = data
    end

    p merged_data
    # {
    #   "node0" => { "foo" => 0 },
    #   "node1" => { "foo" => 10 },
    #   "node2" => { "foo" => 20 }
    # }
  end
end

# Sync all nodes in the same build and gather all node data into the master node.
# Invoking this method blocks until the sync and the data transfer is complete.
CircleCI::Parallel.sync

See API docs for more details.

License

The gem is available as open source under the terms of the MIT License.