Circle CI 2.0 Rails Redis Resque - Rspec Redis Server Not Found Error

When our test suite runs we are getting the following issue regarding redis-server. No matter what we have tried, nothing seems to get past this error. We have validated via dockerize that the containers are live by waiting as seen below, but this error still occurs.

Any thoughts would be greatly appreciated!

enter image description here

Resque Initializer

require 'resque'
require 'redis'
require 'yaml'

# Resque Plugins
require 'resque/plugins/retry'
require 'resque-retry'
require 'resque-retry/server'
require 'resque-lock-timeout'
require 'resque-scheduler'
require 'resque/failure/multiple'
require 'resque/failure/redis'
require 'resque-job-stats/server'
require 'resque/rollbar'

if AppUnsecure.settings[:active_db_services].include?('redis')
  uri = URI.parse(ENV["REDIS_URL"])
  config = {
    host: uri.host,
    port: uri.port,
    password: uri.password
  }
  Resque::Failure::Multiple.classes = [ Resque::Failure::Redis, Resque::Failure::Rollbar ]
  Resque::Failure.backend = Resque::Failure::Multiple
  Resque.redis = Redis.new(config)
elsif AppUnsecure.settings[:active_db_services].include?('redis-continous-integration')
  Resque::Failure::MultipleWithRetrySuppression.classes = [Resque::Failure::Redis]
  Resque.redis = Redis.new(host: 'redis://localhost', port: 6391)
else
  Resque::Failure::MultipleWithRetrySuppression.classes = [Resque::Failure::Redis]
  Resque.redis = Redis.new
end

Resque.redis.namespace = 'resque:GathrlySmartforms'

# Ignores Resque when processing jobs if activated!
Resque.inline = true if AppUnsecure.settings[:process_redis_inline]

# Setup Scheduler
# https://github.com/resque/resque-scheduler/issues/118
# https://github.com/resque/resque-scheduler/issues/581
Resque::Scheduler.configure do |c|
  c.quiet = false
  c.verbose = false
  c.logfile = File.join(Rails.root, 'log', "#{Rails.env}_resque_scheduler.log")
  c.logformat = 'text'
end
Resque::Scheduler.dynamic = true

schedules = {}
global = YAML.load_file("#{Rails.root}/config/resque_schedule.yml")
schedules.merge!(global) if global
# http://stackoverflow.com/questions/12158226/how-do-i-skip-loading-of-rails-initializers-when-running-a-rake-task
unless defined?(is_running_migration?) && is_running_migration?
  Resque.schedule = schedules if schedules.present?
end

Resque::Server.class_eval do
  use Rack::Auth::Basic do |username, password|
    [username, password] == [Rails.application.secrets.my_resque_username, Rails.application.secrets.my_resque_password]
  end
end

Circle Configuration

version: 2
jobs:
  build:
    working_directory: ~/DIR_NAME
    docker:
      - image: circleci/ruby:2.4.1-node
        environment:
          RAILS_ENV: continous_integration
          PGHOST: 127.0.0.1
          PGUSER: rails_test_user

      - image: circleci/postgres:9.6.3-alpine
        environment:
          POSTGRES_USER: rails_test_user
          POSTGRES_PASSWORD: ""
          POSTGRES_DB: continous_integration

      - image: redis:4.0.6

    steps:
      - checkout

      - run:
          name: Dockerize v0.6.0
          command: |
            wget https://github.com/jwilder/dockerize/releases/download/v0.6.0/dockerize-linux-amd64-v0.6.0.tar.gz
            sudo rm -rf /usr/local/bin/dockerize
            sudo tar -C /usr/local/bin -xzvf dockerize-linux-amd64-v0.6.0.tar.gz
            rm dockerize-linux-amd64-v0.6.0.tar.gz

      - run:
          name: Wait for PG
          command: dockerize -wait tcp://localhost:5432 -timeout 2m

      - run:
          name: Wait for Redis
          command: |
            dockerize -wait tcp://localhost:6379 -timeout 2m

      - restore_cache:
          keys:
            - DIR_NAME-{{ checksum "Gemfile.lock" }}
            - DIR_NAME-

      - save_cache:
          key: rails-demo-{{ checksum "Gemfile.lock" }}
          paths:
            - vendor/bundle

      - run:
          name: Setup Bundler and Gems
          command: |
            gem install bundler
            gem update bundler
            gem install brakeman
            gem install rubocop
            gem install rubocop-rspec
            gem install scss_lint
            gem install eslint-rails
            gem install execjs
            bundle config without development:test
            bundle check --path=vendor/bundle || bundle install --without development test --path=vendor/bundle --jobs 4 --retry 3

      - run:
          name: Install Phantom Js
          command: |
            sudo curl --output /tmp/phantomjs https://s3.amazonaws.com/circle-downloads/phantomjs-2.1.1
            sudo chmod ugo+x /tmp/phantomjs
            sudo ln -sf /tmp/phantomjs /usr/local/bin/phantomjs

      - run:
          name: Install Postgres Tools
          command: |
            sudo apt-get update
            sudo apt-get install postgresql-client

      - run:
          name: Install Redis Tools
          command: |
            sudo apt-get install redis-tools ; while ! redis-cli ping 2>/dev/null ; do sleep 1 ; done

      - run:
          name: Build Rails Database Yaml
          command: |
            cp config/database_example.yml config/database.yml

      - run:
          name: Setup Rails Database
          command: |
            bundle exec rake db:drop
            bundle exec rake db:setup

      - run:
          name: Run Rspec
          timeout: 60
          command: |
            RAILS_ENV=continous_integration bundle exec rspec --format RspecJunitFormatter -o /tmp/test-results/rspec.xml

      - run:
          name: Run Brakeman
          command: |
            brakeman -z

      - run:
          name: Run Rubocop
          command: |
            bundle exec rubocop --format fuubar --require rubocop-rspec --config .rubocop.yml

      - run:
          name: Run the SCSS Linter
          command: |
            bundle exec scss-lint --config=config/scsslint.yml

      - run:
          name: Run the Eslint Linter for JS
          command: |
            bundle exec rake eslint:run_all


      - store_test_results:
          path: /tmp/test-results

UPDATE

On various test runs it may occasionally work…however, the error is still the same, this must be the cause…

enter image description here

It’s not your CircleCI configuration, it’s going to be in your bundle config. Based on the command you ran and the error you got, your specs are misconfigured to bring up Redis on start time.

There are two solutions:

  1. Remove redis-server from your bundle config
  2. Remove the Redis image from the top of the config.yml and install redis-server.

The first one is more ideal.

rohara,

Thanks for your reply! My name is Andrew and I work with Chris as a junior on the project. I don’t have experience setting up a circle.yml before tooling with this configuration so you may have to be extra clear so that I can be sure what you mean.

Inside the circle.yml, we want to remove the redis image and install ‘redis-server’ as a step under a ‘run’ command?

When you say remove it from the bundle config, are you meaning the ‘.bundle’ bundler config file?

Thanks for your help!

I have no insight into your project but the command you’re running in that screenshot has nothing to do with Redis. Since you’re hitting an error with trying to start redis-server, it tells me you have bundle/rspec configured to start Redis when specs are run.

I took the whole “before suite” block out of the redis spec config. The suite is running and passing, and we’re still getting output about the kill -q being invalid, and the .pid not existing, but no error about redis-server not being found.

# frozen_string_literal: true

# https://github.com/resque/resque/wiki/RSpec-and-Resque
RSpec.configure do |config|
  # rubocop:disable Rails/FilePath
  REDIS_PID = "#{Rails.root}/tmp/pids/redis-test.pid"
  REDIS_CACHE_PATH = "#{Rails.root}/tmp/cache/"
  # rubocop:enable all

  config.before(:suite) do
    redis_options = {
      'daemonize'     => 'yes',
      'pidfile'       => REDIS_PID,
      'port'          => 9_736,
      'timeout'       => 300,
      'save 900'      => 1,
      'save 300'      => 1,
      'save 60'       => 10_000,
      'dbfilename'    => 'dump.rdb',
      'dir'           => REDIS_CACHE_PATH,
      'loglevel'      => 'debug',
      'logfile'       => 'stdout',
      'databases'     => 16
    }.map { |k, v| "#{k} \"#{v}\"" }.join("\n")
    `echo '#{redis_options}' | redis-server -`
  end

  config.after(:suite) do
    `
      cat "#{REDIS_PID}" | xargs kill -QUIT
      rm -f "#{REDIS_CACHE_PATH}dump.rdb"
    `
  end

  config.before do
    # https://github.com/resque/redis-namespace/issues/132
    Redis.current.flushdb
  end
end

What is the exact command you’re running for kill, and what is running it? I assume it’s run by Rspec, since it’s not in your commands above.

In any case, I wonder if this would be a better question for Stack Overflow - it does not seem to be a CircleCI-specific problem, and there would be a wider audience over there to give you Rails-specific answers.