Error connecting to Redis on localhost:6379 (Errno::ECONNREFUSED)

I have a Gitlab CI build with Redis and one of the tests fails on connecting to Redis. I have followed Gitlab documentation, I have even searched the whole oblivion of internet and can’t figure out what I am doing wrong.

My .gitlab-ci.yml

image: "ruby:2.4.0"

services:
  - postgres:latest
  - redis:latest
  - name: docker.elastic.co/elasticsearch/elasticsearch:6.5.4
    alias: elasticsearch

variables:
  POSTGRES_DB: lauber_backend_test
  POSTGRES_USER: runner
  POSTGRES_PASSWORD: ""
  REDIS_HOST: redis
  ELASTICSEARCH_URL: "http://elasticsearch:9200"

before_script:
  - apt-get update -qq && apt-get install -y -qq nodejs libpq-dev cmake libicu-dev
  - echo "$RAILS_MASTER_KEY" -> config/master.key
  - RAILS_ENV=test bundle install --jobs $(nproc)  "${FLAGS[@]}"
  - cp config/database.yml.example config/database.yml
  - RAILS_ENV=test rails db:create

rspec:
  script:
    - bundle exec rspec

config/initializers/sidekiq.rb

redis = { db: 4, url: 'redis://localhost:6379/0' }

Sidekiq.configure_client do |config|
  config.redis = redis
end

Sidekiq.configure_server do |config|
  config.redis = redis
end

And this is the full error message I am getting:

Failure/Error: ss = Sidekiq::ScheduledSet.new

     Redis::CannotConnectError:
       Error connecting to Redis on localhost:6379 (Errno::ECONNREFUSED)
     # /usr/local/bundle/gems/redis-4.1.2/lib/redis/client.rb:362:in `rescue in establish_connection'
     # /usr/local/bundle/gems/redis-4.1.2/lib/redis/client.rb:344:in `establish_connection'
     # /usr/local/bundle/gems/redis-4.1.2/lib/redis/client.rb:106:in `block in connect'
     # /usr/local/bundle/gems/redis-4.1.2/lib/redis/client.rb:306:in `with_reconnect'
     # /usr/local/bundle/gems/redis-4.1.2/lib/redis/client.rb:105:in `connect'
     # /usr/local/bundle/gems/redis-4.1.2/lib/redis/client.rb:381:in `ensure_connected'
     # /usr/local/bundle/gems/redis-4.1.2/lib/redis/client.rb:231:in `block in process'
     # /usr/local/bundle/gems/redis-4.1.2/lib/redis/client.rb:319:in `logging'
     # /usr/local/bundle/gems/redis-4.1.2/lib/redis/client.rb:230:in `process'
     # /usr/local/bundle/gems/redis-4.1.2/lib/redis/client.rb:125:in `call'
     # /usr/local/bundle/gems/redis-4.1.2/lib/redis.rb:1524:in `block in zcard'
     # /usr/local/bundle/gems/redis-4.1.2/lib/redis.rb:52:in `block in synchronize'
     # /usr/local/bundle/gems/redis-4.1.2/lib/redis.rb:52:in `synchronize'
     # /usr/local/bundle/gems/redis-4.1.2/lib/redis.rb:1523:in `zcard'
     # /usr/local/bundle/gems/sidekiq-5.2.7/lib/sidekiq/api.rb:542:in `block in size'
     # /usr/local/bundle/gems/sidekiq-5.2.7/lib/sidekiq.rb:97:in `block in redis'
     # /usr/local/bundle/gems/connection_pool-2.2.2/lib/connection_pool.rb:65:in `block (2 levels) in with'
     # /usr/local/bundle/gems/connection_pool-2.2.2/lib/connection_pool.rb:64:in `handle_interrupt'
     # /usr/local/bundle/gems/connection_pool-2.2.2/lib/connection_pool.rb:64:in `block in with'
     # /usr/local/bundle/gems/connection_pool-2.2.2/lib/connection_pool.rb:61:in `handle_interrupt'
     # /usr/local/bundle/gems/connection_pool-2.2.2/lib/connection_pool.rb:61:in `with'
     # /usr/local/bundle/gems/sidekiq-5.2.7/lib/sidekiq.rb:94:in `redis'
     # /usr/local/bundle/gems/sidekiq-5.2.7/lib/sidekiq/api.rb:542:in `size'
     # /usr/local/bundle/gems/sidekiq-5.2.7/lib/sidekiq/api.rb:538:in `initialize'
     # /usr/local/bundle/gems/sidekiq-5.2.7/lib/sidekiq/api.rb:647:in `initialize'
     # ./app/services/external_data/lauber_catalogue/reload_database_scheduler.rb:19:in `new'
     # ./app/services/external_data/lauber_catalogue/reload_database_scheduler.rb:19:in `already_scheduled?'
     # ./app/services/external_data/lauber_catalogue/reload_database_scheduler.rb:9:in `execute'
     # ./app/controllers/api/v1/reload_database_controller.rb:8:in `index'
     # /usr/local/bundle/gems/searchkick-4.1.0/lib/searchkick/logging.rb:211:in `process_action'
     # /usr/local/bundle/gems/warden-1.2.8/lib/warden/manager.rb:36:in `block in call'
     # /usr/local/bundle/gems/warden-1.2.8/lib/warden/manager.rb:34:in `catch'
     # /usr/local/bundle/gems/warden-1.2.8/lib/warden/manager.rb:34:in `call'
     # /usr/local/bundle/gems/rack-2.0.7/lib/rack/etag.rb:25:in `call'
     # /usr/local/bundle/gems/rack-2.0.7/lib/rack/conditional_get.rb:25:in `call'
     # /usr/local/bundle/gems/rack-2.0.7/lib/rack/head.rb:12:in `call'
     # /usr/local/bundle/gems/rack-2.0.7/lib/rack/session/abstract/id.rb:232:in `context'
     # /usr/local/bundle/gems/rack-2.0.7/lib/rack/session/abstract/id.rb:226:in `call'
     # /usr/local/bundle/gems/rollbar-2.22.1/lib/rollbar/middleware/rails/rollbar.rb:25:in `block in call'
     # /usr/local/bundle/gems/rollbar-2.22.1/lib/rollbar.rb:145:in `scoped'
     # /usr/local/bundle/gems/rollbar-2.22.1/lib/rollbar/middleware/rails/rollbar.rb:22:in `call'
     # /usr/local/bundle/gems/rollbar-2.22.1/lib/rollbar/middleware/rails/show_exceptions.rb:22:in `call_with_rollbar'
     # /usr/local/bundle/gems/railties-5.1.7/lib/rails/rack/logger.rb:36:in `call_app'
     # /usr/local/bundle/gems/railties-5.1.7/lib/rails/rack/logger.rb:24:in `block in call'
     # /usr/local/bundle/gems/railties-5.1.7/lib/rails/rack/logger.rb:24:in `call'
     # /usr/local/bundle/gems/rack-2.0.7/lib/rack/method_override.rb:22:in `call'
     # /usr/local/bundle/gems/rack-2.0.7/lib/rack/runtime.rb:22:in `call'
     # /usr/local/bundle/gems/rack-2.0.7/lib/rack/sendfile.rb:111:in `call'
     # /usr/local/bundle/gems/rack-cors-1.0.3/lib/rack/cors.rb:95:in `call'
     # /usr/local/bundle/gems/railties-5.1.7/lib/rails/engine.rb:522:in `call'
     # /usr/local/bundle/gems/rack-test-1.1.0/lib/rack/mock_session.rb:29:in `request'
     # /usr/local/bundle/gems/rack-test-1.1.0/lib/rack/test.rb:266:in `process_request'
     # /usr/local/bundle/gems/rack-test-1.1.0/lib/rack/test.rb:129:in `custom_request'
     # /usr/local/bundle/gems/rack-test-1.1.0/lib/rack/test.rb:58:in `get'
     # /usr/local/bundle/gems/rspec_api_documentation-6.1.0/lib/rspec_api_documentation/rack_test_client.rb:38:in `do_request'
     # /usr/local/bundle/gems/rspec_api_documentation-6.1.0/lib/rspec_api_documentation/client_base.rb:42:in `process'
     # /usr/local/bundle/gems/rspec_api_documentation-6.1.0/lib/rspec_api_documentation/client_base.rb:12:in `get'
     # ./spec/requests/api/v1/reload_database_spec.rb:19:in `block (3 levels) in <top (required)>'
     # ------------------
     # --- Caused by: ---
     # IO::EINPROGRESSWaitWritable:
     #   Operation now in progress - connect(2) would block
     #   /usr/local/bundle/gems/redis-4.1.2/lib/redis/connection/ruby.rb:180:in `connect_addrinfo'

This is the code where Sidekiq is being called.

require 'sidekiq/api'

module ExternalData
  module LauberCatalogue
    class ReloadDatabaseScheduler < Service
      def call(worker:)
        worker.perform_in(4.hours + 30.minutes) unless already_scheduled?
      end

      private

      def already_scheduled?
        ss = Sidekiq::ScheduledSet.new
        ss.select { |job| job.klass == 'ReloadDatabaseWorker' }.size.positive?
      end
    end
  end
end

On my local machine all works fine.

I had a same trouble

I fixed it like this

ss = begin
    Sidekiq::ScheduledSet.new
  rescue
    sleep 0.5
    Sidekiq::ScheduledSet.new
end
1 Like