From 89e9d29de224e91a8aec205bfaf930079eaa17cb Mon Sep 17 00:00:00 2001 From: Oanh Nguyen Date: Tue, 20 Apr 2021 15:08:32 +0700 Subject: [PATCH] Refactor health-check script --- .github/README-vi.md | 7 ++++-- .github/README.md | 2 +- .github/workflows/ci.yml | 27 +++++++++++---------- Dockerfile | 3 +-- bin/health-check | 39 +------------------------------ examples/laravel-echo-server.json | 25 ++++++++++++++++++++ examples/redis.yml | 12 +++++----- examples/sqlite.yml | 16 +++++++++---- src/healthcheck.js | 32 +++++++++++++++++++++++++ 9 files changed, 96 insertions(+), 67 deletions(-) create mode 100644 examples/laravel-echo-server.json create mode 100644 src/healthcheck.js diff --git a/.github/README-vi.md b/.github/README-vi.md index 155d1ad..b7e41f5 100644 --- a/.github/README-vi.md +++ b/.github/README-vi.md @@ -27,7 +27,7 @@ Image `oanhnn/laravel-echo-server` Image `ghcr.io/oanhnn/laravel-echo-server` - `edge` - the edge version, được tạo từ code mới nhất của nhánh `master` -- `nightly` - the nightly version, Được tạo hàng ngày từ code mới nhất của nhánh `master` vào 8:20 AM UTC +- `nightly` - the nightly version, được tạo hàng ngày từ code mới nhất của nhánh `master` vào 8:20 AM UTC > CHÚ Ý: Hãy sử dụng sermatic version cho sản phẩm (VD: `3.1`) @@ -136,6 +136,9 @@ Một số biến môi trường khi tồn tại (kể cả tải từ tệp `/a Bạn có thể xem thêm thông tin về ghi đè thiết lập cấu hình bằng biến môi trường ở [đây](https://github.com/tlaverdure/laravel-echo-server/blob/master/README.md#dotenv) +> **Chú ý** Tệp entrypoint chỉ hỗ trợ ghi đè một số biến môi trường. Một số biến môi trường khác được ghi đè bởi chinh `laravel-echo-server`. +> Nó thực hiện trong mã nguồn của mình để ghi đè các cấu hình từ file `laravel-echo-server.json` +> [Xem thêm](https://github.com/oanhnn/docker-laravel-echo-server/issues/18) ## Đóng góp @@ -149,5 +152,5 @@ Nếu bạn muốn giúp đỡ, hãy tìm kiếm trong [danh sách các vấn đ ## Giấy phép Dự án này được phát hành dưới giấy phép mã nguồn mở MIT. -Bản quyền thuộc © 2020 [Oanh Nguyen](https://github.com/oanhnn) +Bản quyền thuộc © 2021 [Oanh Nguyen](https://github.com/oanhnn) Xem file [License](https://github.com/oanhnn/docker-laravel-echo-server/blob/master/LICENSE) để biết thêm thông tin. diff --git a/.github/README.md b/.github/README.md index e30600d..7624ae8 100644 --- a/.github/README.md +++ b/.github/README.md @@ -149,5 +149,5 @@ If you would like to help take a look at the [list of issues](https://github.com ## License This project is released under the MIT License. -Copyright © 2020 [Oanh Nguyen](https://github.com/oanhnn) +Copyright © 2021 [Oanh Nguyen](https://github.com/oanhnn) Please see [License File](https://github.com/oanhnn/docker-laravel-echo-server/blob/master/LICENSE) for more information. diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 35f9866..9fd4625 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,12 +21,6 @@ jobs: runs-on: ubuntu-latest env: DOCKER_IMAGE: oanhnn/laravel-echo-server - strategy: - fail-fast: false - matrix: - database: - - redis - - sqlite steps: - name: Checkout uses: actions/checkout@v2 @@ -55,20 +49,25 @@ jobs: load: true tags: ${{ env.DOCKER_IMAGE }}:test - - name: Setup docker-compose + - name: Test with sqlite run: | - mkdir ./ci-test - cp ./examples/${{ matrix.database }}.yml ./ci-test/docker-compose.yml - sed -i "s|oanhnn/laravel-echo-server:.*|oanhnn/laravel-echo-server:test|g" ./ci-test/docker-compose.yml + sed -i "s|oanhnn/laravel-echo-server:.*|oanhnn/laravel-echo-server:test|g" ./sqlite.yml + docker-compose -f sqlite.yml up -d + sleep 10s + docker-compose -f sqlite.yml ps + curl --silent --show-error --fail http://127.0.0.1:6001/example.io/socket.io.js + docker-compose -f sqlite.yml down + working-directory: ./examples - name: Run with docker run: | - docker-compose up -d + sed -i "s|oanhnn/laravel-echo-server:.*|oanhnn/laravel-echo-server:test|g" ./redis.yml + docker-compose -f redis.yml up -d sleep 10s - docker-compose ps + docker-compose -f redis.yml ps curl --silent --show-error --fail http://127.0.0.1:6001/socket.io/socket.io.js - docker-compose down - working-directory: ./ci-test + docker-compose -f redis.yml down + working-directory: ./examples push: runs-on: ubuntu-latest diff --git a/Dockerfile b/Dockerfile index 2231f89..755acba 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,7 +7,6 @@ RUN set -eux; \ apk add --update --no-cache \ sqlite \ openssl \ - curl \ ; \ apk add --update --no-cache --virtual .build-deps \ build-base \ @@ -19,7 +18,7 @@ RUN set -eux; \ rm /usr/local/bin/docker-entrypoint.sh COPY bin/* /usr/local/bin/ -COPY src/laravel-echo-server.json /usr/local/src/ +COPY src/* /usr/local/src/ VOLUME /app EXPOSE 6001 diff --git a/bin/health-check b/bin/health-check index a8377d1..626477e 100755 --- a/bin/health-check +++ b/bin/health-check @@ -1,39 +1,2 @@ #!/bin/sh -set -x - -_init () { - scheme="http://" - address="$(netstat -nplt 2>/dev/null | awk ' /(.*\/laravel-echo-serv)/ { gsub(":::","127.0.0.1:",$4); print $4}')" - resource="/socket.io/socket.io.js" - start=$(stat -c "%Y" /proc/1) -} - -fn_health_check () { - # In distributed environment like Swarm, traffic is routed - # to a container only when it reports a `healthy` status. So, we exit - # with 0 to ensure healthy status till distributed service starts (120s). - # - # Refer: https://github.com/moby/moby/pull/28938#issuecomment-301753272 - if [[ $(( $(date +%s) - start )) -lt 120 ]]; then - exit 0 - else - # Get the http response code - http_response=$(curl -s -k -o /dev/null -w "%{http_code}" ${scheme}${address}${resource}) - - # Get the http response body - http_response_body=$(curl -k -s ${scheme}${address}${resource}) - - # server returns response 403 and body "SSL required" if non-TLS - # connection is attempted on a TLS-configured server. Change - # the scheme and try again - if [[ "$http_response" = "403" ]] && [[ "$http_response_body" = "SSL required" ]]; then - scheme="https://" - http_response=$(curl -s -k -o /dev/null -w "%{http_code}" ${scheme}${address}${resource}) - fi - - # If http_response is 200 - server is up. - [[ "$http_response" = "200" ]] - fi -} - -_init && fn_health_check +node /usr/local/src/healthcheck.js diff --git a/examples/laravel-echo-server.json b/examples/laravel-echo-server.json new file mode 100644 index 0000000..62b7546 --- /dev/null +++ b/examples/laravel-echo-server.json @@ -0,0 +1,25 @@ +{ + "apiOriginAllow": {}, + "authEndpoint": "/broadcasting/auth", + "authHost": "http://localhost", + "clients": [], + "database": "sqlite", + "databaseConfig": { + "sqlite": { + "databasePath": "/database/laravel-echo-server.sqlite" + }, + "publishPresence": true + }, + "devMode": false, + "host": null, + "port": 6001, + "protocol": "http", + "sslCertPath": "", + "sslKeyPath": "", + "sslCertChainPath": "", + "sslPassphrase": "", + "socketio": { + "path": "/example.io" + }, + "subscribers": {"http": true} +} diff --git a/examples/redis.yml b/examples/redis.yml index fb39362..06f0144 100644 --- a/examples/redis.yml +++ b/examples/redis.yml @@ -22,9 +22,9 @@ services: - redis-vol:/data healthcheck: test: redis-cli -h redis -p 6379 ping - start_period: 1s - interval: 1s - timeout: 3s + start_period: 3s + interval: 2s + timeout: 2s retries: 10 laravel-echo-server: @@ -46,8 +46,8 @@ services: - 6001:6001 restart: unless-stopped healthcheck: - test: /usr/local/bin/health-check - start_period: 1s - interval: 1s + test: node /usr/local/src/healthcheck.js + start_period: 5s + interval: 3s timeout: 3s retries: 10 diff --git a/examples/sqlite.yml b/examples/sqlite.yml index 6fb72c6..4fb94e6 100644 --- a/examples/sqlite.yml +++ b/examples/sqlite.yml @@ -6,12 +6,20 @@ volumes: services: laravel-echo-server: image: oanhnn/laravel-echo-server:latest - environment: - LARAVEL_ECHO_SERVER_AUTH_HOST: http://example.com - LARAVEL_ECHO_SERVER_DEBUG: 'true' - LARAVEL_ECHO_SERVER_DATABASE: sqlite + # environment: + # LARAVEL_ECHO_SERVER_AUTH_HOST: http://example.com + # LARAVEL_ECHO_SERVER_DEBUG: 'true' + # LARAVEL_ECHO_SERVER_DATABASE: sqlite ports: - 6001:6001 restart: unless-stopped volumes: - sqlite-vol:/database + - ./laravel-echo-server.json:/app/laravel-echo-server.json:ro + healthcheck: + # test: node /usr/local/src/healthcheck.js + test: /usr/local/bin/health-check + start_period: 5s + interval: 3s + timeout: 3s + retries: 10 diff --git a/src/healthcheck.js b/src/healthcheck.js new file mode 100644 index 0000000..098afa9 --- /dev/null +++ b/src/healthcheck.js @@ -0,0 +1,32 @@ +// See https://blog.sixeyed.com/docker-healthchecks-why-not-to-use-curl-or-iwr/ + +var http = require("http"); +var config = {}; + +try { + config = require("/app/laravel-echo-server.json"); +} catch (error) { +} + +var options = { + host : "localhost", + port : config.port | 6001, + path : `${config.socketio && config.socketio.path ? config.socketio.path : '/socket.io'}/socket.io.js`, + timeout : 2000 +}; + +var request = http.request(options, (res) => { + console.log(`STATUS: ${res.statusCode}`); + if (res.statusCode == 200) { + process.exit(0); + } else { + process.exit(1); + } +}); + +request.on('error', function(err) { + console.log('ERROR'); + process.exit(1); +}); + +request.end();