Skip to main content


Use the Knapsack Pro binary

You can install Knapsack Pro globally as a binary if you don't want it as a dependency in your Gemfile:

gem install knapsack_pro

knapsack_pro queue:rspec "--tag MY_TAG --profile"

Set a custom logger

In your rails_helper.rb, you can set a custom Knapsack Pro logger:

# This line should be already in your rails_helper.rb
require File.expand_path('../../config/environment', __FILE__)

require 'logger'
KnapsackPro.logger ='log', "knapsack_pro_node_#{KnapsackPro::Config::Env.ci_node_index}.log"))
KnapsackPro.logger.level = Logger::DEBUG

The very first request to the Knapsack Pro API will still be sent to stdout because Knapsack Pro needs the subset of test files to execute before loading rails_helper.rb.

Run Knapsack Pro on a subset of parallel CI nodes (instead of all)

You may want to run Knapsack Pro only on a subset of parallel CI nodes, and use the others nodes for something else (e.g., linters).

For example, you could decide to run Knapsack Pro on all the CI nodes but the last one:

KNAPSACK_PRO_CI_NODE_TOTAL=$((MY_CI_NODE_TOTAL-1)) bundle exec rake knapsack_pro:queue:rspec

To find out which environment variable to use in place of MY_CI_NODE_TOTAL, take a look at what Knapsack Pro uses as node_total for your CI provider (e.g., for CircleCI it would be CIRCLE_NODE_TOTAL)

Fail the CI build if one of the test files exceeds a certain time limit

In the after_queue hook, retrieve the slowest test file and create a file on disk if it exceeds a given THRESHOLD:

KnapsackPro::Hooks::Queue.after_queue do |queue_id|
THRESHOLD = 120 # seconds

# all recorded test files by knapsack_pro gem on particular CI node index
slowest = Dir.glob(".knapsack_pro/queue/#{queue_id}/*.json")
.flat_map { |file| JSON.parse( }
.max_by { |test_file| test_file['time_execution'] }

if slowest['time_execution'].to_f > THRESHOLD
puts '!' * 50
puts "The slowest test file (#{slowest['path']}) took #{slowest['time_execution']} seconds and exceeded the threshold (#{THRESHOLD} seconds)."
puts '!' * 50'tmp/slowest_test_file_exceeded_threshold.txt', 'w+') do |file|

Then, in a bash script fail the CI build if the file exists:



if [ -f "$THRESHOLD_FILE_PATH" ]; then
echo "Slow test file exceeded threshold. Fail CI build."
exit 1

Run multiple test commands with one script


# Cucumber suite
bundle exec rake knapsack_pro:queue:cucumber

# RSpec suite
bundle exec rake knapsack_pro:queue:rspec

if [ "$CUCUMBER_EXIT_CODE" -ne "0" ]; then

if [ "$RSPEC_EXIT_CODE" -ne "0" ]; then

Run (and fail fast) multiple test commands with one script


CI nodes that fail on the first suite won't run the second suite: tests will be distributed to fewer CI nodes and the CI run will take longer.


set -e # exit on error

bundle exec rake knapsack_pro:queue:rspec
bundle exec rake knapsack_pro:queue:cucumber

This site uses cookies. By staying here you accept them. See our Cookie Policy for details.
For more information on how to turn off the use of cookies, please see this.
To refuse the use of cookies, please leave the page (more details here).