From 4c1f1c0e21d3859a2121cfb16a672aa90940461c Mon Sep 17 00:00:00 2001 From: Cyrille <1618640+cyri113@users.noreply.github.com> Date: Tue, 9 Jan 2024 13:23:04 +0100 Subject: [PATCH] mongo + healthcheck.js --- development/docker-compose.yml | 19 ++++--------------- development/mongo/healthcheck.js | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 15 deletions(-) create mode 100644 development/mongo/healthcheck.js diff --git a/development/docker-compose.yml b/development/docker-compose.yml index 6c3470c..92a9c77 100644 --- a/development/docker-compose.yml +++ b/development/docker-compose.yml @@ -12,29 +12,18 @@ x-logging: services: mongodb-dev: image: mongo:latest - container_name: mongodb-dev restart: unless-stopped - command: ["--replSet", "rs0", "--keyFile", "/etc/mongo/replica.key", "--bind_ip_all"] + command: ['--replSet', 'rs0'] ports: - 37017:27017 env_file: - ./.env.db volumes: - - ./mongo:/etc/mongo + - ./mongo:/etc/mongo:ro - mongodb_data_container:/data/db - - type: bind - source: ../init-mongo.sh - target: /docker-entrypoint-initdb.d/init-mongo.sh - read_only: true - # entrypoint: - # - bash - # - -c - # - | - # chmod 400 /etc/mongo/replica.key - # chown 999:999 /etc/mongo/replica.key + - ../init-mongo.sh:/docker-entrypoint-initdb.d/init-mongo.sh:ro healthcheck: - # test: echo 'db.stats().ok' | mongosh localhost:27017/test --quiet - test: echo "try { rs.status() } catch (err) { rs.initiate({_id:'rs0',members:[{_id:0,host:'localhost'}]}) }" | mongosh localhost:27017/test --quiet + test: test $(echo $(mongosh --quiet /etc/mongo/healthcheck.js)) -eq 1 interval: 60s timeout: 10s retries: 2 diff --git a/development/mongo/healthcheck.js b/development/mongo/healthcheck.js new file mode 100644 index 0000000..6ec56ad --- /dev/null +++ b/development/mongo/healthcheck.js @@ -0,0 +1,17 @@ +function run() { + let ok = 2 + try { + ok = rs.status().ok + } catch (error) { + var config = { + _id: "rs0", + members: [ + { _id: 0, host: "mongodb-dev" }, + ] + } + rs.initiate(config) + } + return ok + } + + console.log(run()) \ No newline at end of file