Elasticsearch在CircleCI上尚未准备好

时间:2019-05-13 15:50:51

标签: docker elasticsearch circleci circleci-2.0

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

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

这是我的docker文件

dockerize

请注意,我也尝试过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 2m无效。

1 个答案:

答案 0 :(得分:0)

我尝试添加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