CIRCLE CI: --- Redis::CannotConnectError:

Posted

技术标签:

【中文标题】CIRCLE CI: --- Redis::CannotConnectError:【英文标题】: 【发布时间】:2021-12-21 07:05:29 【问题描述】:

当我在本地运行我的 rspec 测试时,一切都通过了,但是当我在 Circle CI 上运行它们时,我得到了 rspec 错误:Redis::CannotConnectError:

我最近为 SideKiq 添加了功能测试。它使用 Redis 和 Chrome 驱动程序。

我想我需要将 redis 构建添加到我的 cimg 中,但不知道该怎么做。

Rspec 测试结果

Failures:

  1) Sidekiq::Web with admin user can able to access sidekiq GUI
     Failure/Error: raise CannotConnectError, "Error connecting to Redis on #location (#error.class)"
     
     Redis::CannotConnectError:
       Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED)
     # /home/circleci/project/vendor/bundle/ruby/2.7.0/gems/redis-4.2.2/lib/redis/client.rb:362:in `rescue in establish_connection'
     # /home/circleci/project/vendor/bundle/ruby/2.7.0/gems/redis-4.2.2/lib/redis/client.rb:343:in `establish_connection'
     # /home/circleci/project/vendor/bundle/ruby/2.7.0/gems/redis-4.2.2/lib/redis/client.rb:107:in `block in connect'
     # /home/circleci/project/vendor/bundle/ruby/2.7.0/gems/redis-4.2.2/lib/redis/client.rb:308:in `with_reconnect'
     # /home/circleci/project/vendor/bundle/ruby/2.7.0/gems/redis-4.2.2/lib/redis/client.rb:106:in `connect'
     # /home/circleci/project/vendor/bundle/ruby/2.7.0/gems/redis-4.2.2/lib/redis/client.rb:381:in `ensure_connected'
     # /home/circleci/project/vendor/bundle/ruby/2.7.0/gems/redis-4.2.2/lib/redis/client.rb:233:in `block in process'
     # /home/circleci/project/vendor/bundle/ruby/2.7.0/gems/redis-4.2.2/lib/redis/client.rb:320:in `logging'
     # /home/circleci/project/vendor/bundle/ruby/2.7.0/gems/redis-4.2.2/lib/redis/client.rb:232:in `process'
     # /home/circleci/project/vendor/bundle/ruby/2.7.0/gems/redis-4.2.2/lib/redis/client.rb:126:in `call'
     # /home/circleci/project/vendor/bundle/ruby/2.7.0/gems/redis-4.2.2/lib/redis.rb:305:in `block in info'
     # /home/circleci/project/vendor/bundle/ruby/2.7.0/gems/redis-4.2.2/lib/redis.rb:69:in `block in synchronize'
     # /usr/local/lib/ruby/2.7.0/monitor.rb:202:in `synchronize'
     # /usr/local/lib/ruby/2.7.0/monitor.rb:202:in `mon_synchronize'
     # /home/circleci/project/vendor/bundle/ruby/2.7.0/gems/redis-4.2.2/lib/redis.rb:69:in `synchronize'
     # /home/circleci/project/vendor/bundle/ruby/2.7.0/gems/redis-4.2.2/lib/redis.rb:304:in `info'
     # /home/circleci/project/vendor/bundle/ruby/2.7.0/gems/sidekiq-6.1.2/lib/sidekiq.rb:120:in `block in redis_info'
     # /home/circleci/project/vendor/bundle/ruby/2.7.0/gems/sidekiq-6.1.2/lib/sidekiq.rb:98:in `block in redis'
     # /home/circleci/project/vendor/bundle/ruby/2.7.0/gems/connection_pool-2.2.3/lib/connection_pool.rb:63:in `block (2 levels) in with'
     # /home/circleci/project/vendor/bundle/ruby/2.7.0/gems/connection_pool-2.2.3/lib/connection_pool.rb:62:in `handle_interrupt'
     # /home/circleci/project/vendor/bundle/ruby/2.7.0/gems/connection_pool-2.2.3/lib/connection_pool.rb:62:in `block in with'
     # /home/circleci/project/vendor/bundle/ruby/2.7.0/gems/connection_pool-2.2.3/lib/connection_pool.rb:59:in `handle_interrupt'
     # /home/circleci/project/vendor/bundle/ruby/2.7.0/gems/connection_pool-2.2.3/lib/connection_pool.rb:59:in `with'
     # /home/circleci/project/vendor/bundle/ruby/2.7.0/gems/sidekiq-6.1.2/lib/sidekiq.rb:95:in `redis'
     # /home/circleci/project/vendor/bundle/ruby/2.7.0/gems/sidekiq-6.1.2/lib/sidekiq.rb:114:in `redis_info'
     # /home/circleci/project/vendor/bundle/ruby/2.7.0/gems/sidekiq-6.1.2/lib/sidekiq/web/helpers.rb:171:in `redis_info'
     # /home/circleci/project/vendor/bundle/ruby/2.7.0/gems/sidekiq-6.1.2/lib/sidekiq/web/application.rb:46:in `block in <class:WebApplication>'
     # /home/circleci/project/vendor/bundle/ruby/2.7.0/gems/sidekiq-6.1.2/lib/sidekiq/web/application.rb:296:in `instance_exec'
     # /home/circleci/project/vendor/bundle/ruby/2.7.0/gems/sidekiq-6.1.2/lib/sidekiq/web/application.rb:296:in `block in call'
     # /home/circleci/project/vendor/bundle/ruby/2.7.0/gems/sidekiq-6.1.2/lib/sidekiq/web/application.rb:294:in `catch'
     # /home/circleci/project/vendor/bundle/ruby/2.7.0/gems/sidekiq-6.1.2/lib/sidekiq/web/application.rb:294:in `call'
     # /home/circleci/project/vendor/bundle/ruby/2.7.0/gems/sidekiq-6.1.2/lib/sidekiq/web/csrf_protection.rb:52:in `admit'
     # /home/circleci/project/vendor/bundle/ruby/2.7.0/gems/sidekiq-6.1.2/lib/sidekiq/web/csrf_protection.rb:41:in `call'

     # --- Caused by: ---
     # IO::EINPROGRESSWaitWritable:
     #   Operation now in progress - connect(2) would block
     #   /usr/local/lib/ruby/2.7.0/socket.rb:1214:in `__connect_nonblock'

用于 CirclCI 的 config.yml

version: 2.1
orbs:
  browser-tools: circleci/browser-tools@1.1
jobs:
  build:
    parallelism: 1
    docker:
      - image: cimg/ruby:2.7.3-browsers
        environment:
          BUNDLE_JOBS: 3
          BUNDLE_RETRY: 3
          BUNDLE_PATH: vendor/bundle
          PGHOST: 127.0.0.1
          PGUSER: myapp
          RAILS_ENV: test
          CAPYBARA_DEFAULT_MAX_WAIT_TIME: 10
      - image: circleci/postgres:9.5-alpine # database image
        environment: # environment variables for database
          POSTGRES_USER: myapp
          POSTGRES_DB: myapp
          POSTGRES_PASSWORD: mypass
    steps:
      - browser-tools/install-browser-tools
      - checkout

      - run:
          name: Install NVM
          command: |
            curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash && \
            export NVM_DIR="$HOME/.nvm" && \
            echo 'export NVM_DIR=$HOME/.nvm' >> $BASH_ENV && echo 'source $NVM_DIR/nvm.sh' >> $BASH_ENV && \
            [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" && \
            [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"

      - run:
          name: Install NodeJS
          command: nvm install 8.9.1

      - run:
          name: Install bundler 2.0.2
          command: gem install bundler:2.0.2

      - run:
          name: Which bundler?
          command: bundle -v

      - run:
          name: Which Google Chrome?
          command: google-chrome --version

      - restore_cache:
          keys:
            - myapp- checksum "Gemfile.lock" 
            - myapp-

      - run:
          name: Bundle Install
          command: bundle check || bundle install

      - save_cache:
          key: myapp- checksum "Gemfile.lock" 
          paths:
            - vendor/bundle

      - run:
          name: Wait for DB
          command: dockerize -wait tcp://localhost:5432 -timeout 1m

      - run:
          name: Database setup
          command: bin/rails db:drop && bin/rails db:create && bin/rails db:migrate
      
      - run:
          name: Precompile Assets
          command: bundle exec rake assets:precompile
          
      - run:
          name: Run rspec
          command: bundle exec rspec spec

【问题讨论】:

【参考方案1】:

在测试环境中,通常最好不要在 Redis 中排队作业。该文档包含有关如何在测试中配置 Sidekiq 的信息。

https://github-wiki-see.page/m/mperham/sidekiq/wiki/Testing

【讨论】:

以上是关于CIRCLE CI: --- Redis::CannotConnectError:的主要内容,如果未能解决你的问题,请参考以下文章

Circle CI Docker 镜像:Php + Java

排毒不会在 Circle CI 上启动 iOS 模拟器

Circle ci 触发构建拉取请求创建或更新

Circle CI 上的 Rspec 测试失败(Ruby on Rails 5.2.3)

从 circle ci 访问 MySQL GCP 以自动化数据库迁移?

什么是 github 操作相当于 circle ci 的 add_ssh_keys