【问题标题】:Elasticsearch not ready on CircleCICircleCI 上的 Elasticsearch 尚未准备好
【发布时间】:2019-05-13 15:50:51
【问题描述】:

当我运行 CircleCI 时,前几个测试由于 Elasticsearch 尚未完全设置而失败。

通常我会使用 dockerize 库来等待 Elasticsearch 完成,但这似乎无法检测到 Elasticsearch。任何想法为什么? dockerize 命令只是超时。但是,Elasticsearch 容器似乎正在运行,因为如果我不等待,最终 Elasticsearch 会开始处理测试。

这是我的 docker 文件

version: 2
jobs:
  build:
    parallelism: 3
    working_directory: ~/export-opportunities
    docker:
      - image: circleci/ruby:2.5.5-node
        environment:
          BUNDLE_JOBS: 3
          BUNDLE_RETRY: 3
          BUNDLE_PATH: vendor/bundle
          PGHOST: localhost
          PGUSER: user
          RAILS_ENV: test
      - image: circleci/postgres:latest
        environment:
          POSTGRES_USER: user
          POSTGRES_DB:  circle_test
          POSTGRES_PASSWORD: $POSTGRES_PASSWORD
      - image: circleci/redis:4.0.9
        environment:
          REDIS_URL: "redis://localhost:6379/"
      - image: docker.elastic.co/elasticsearch/elasticsearch:6.3.2
        environment:
          cluster.name: elasticsearch
          xpack.security.enabled: false
          transport.host: localhost
          network.host: 127.0.0.1
          http.port: 9200
          discovery.type: single-node
    branches:
      only: chore/XOT-597-circleci
    steps:
      - checkout # check out the code in the project directory
      # restore bundle cache  
      - restore_cache:
          keys:
            - exops-{{ checksum "Gemfile.lock" }}

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

      # store bundle cache
      - save_cache:
          key: exops-{{ checksum "Gemfile.lock" }}
          paths: 
            - vendor/bundle

      # Database setup
      - run:
          name: install dockerize
          command: wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && sudo tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz
          environment:
            DOCKERIZE_VERSION: v0.3.0

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

      - run:
          name: Database setup
          command: |
            bundle exec rake db:create
            bundle exec rake db:migrate

      # Redis setup
      - run:
          name: Wait for Redis
          command: dockerize -wait tcp://localhost:6379 -timeout 1m
      # DOES NOT WORK:
      - run:
          name: Wait for Elasticsearch
          command: dockerize -wait http://localhost:9200 -timeout 2m

      # Run rspec in parallel 
      - run: |
          echo Running test ...
          bundle exec rspec --profile 10 \
                      --format RspecJunitFormatter \
                      --out test_results/rspec.xml \
                      --format progress \
                      $(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)

      # Save test results for timing analysis
      - store_test_results:
          path: test_results 

注意我也试过dockerize -wait tcp://localhost:9200 -timeout 2mdockerize -wait http://127.0.0.1:9200 -timeout 2mdockerize -wait tcp://127.0.0.1:9200 -timeout 2m,但没有效果。

【问题讨论】:

  • 嗯,奇怪。为了让您继续前进,请使用sleep 10 代替不工作的dockerize。然后,当您有时间时,我想知道是否值得尝试任何类似的工具,例如waitforit 脚本。
  • Elasticsearch 需要多长时间才能做好准备?我想知道你是否有一个需要超过 2m 才能启动的极端情况。

标签: docker elasticsearch circleci circleci-2.0


【解决方案1】:

我尝试添加sleep 10sleep 100,但问题仍然存在。

问题是测试在创建索引之前运行。索引创建是由第一个运行的测试触发的,但需要几秒钟,所以前几个测试总是失败。

我的解决方案是添加以下代码以触发在启动 rails 环境时运行的 rails_helper.rb 中构建索引(如果不存在)。对于大多数其他环境,索引确实存在,因此它不会减慢其他进程。

# Build initial indices if not present, e.g. CircleCI
  [Opportunity, Subscription].each do |model|
    unless model.__elasticsearch__.index_exists? index: model.__elasticsearch__.index_name
      model.__elasticsearch__.create_index!(force: true)
      sleep 2
    end
  end

【讨论】:

    猜你喜欢
    • 2016-01-24
    • 2017-12-19
    • 2019-08-14
    • 2018-06-05
    • 1970-01-01
    • 2016-02-21
    • 2018-05-11
    • 2011-06-27
    • 1970-01-01
    相关资源
    最近更新 更多