gpt4 book ai didi

docker - Elasticsearch在CircleCI上尚未准备好

转载 作者:行者123 更新时间:2023-12-02 19:58:34 25 4
gpt4 key购买 nike

当我运行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 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

关于docker - Elasticsearch在CircleCI上尚未准备好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56115853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com