Skip to content

Commit

Permalink
Refactor health-check script
Browse files Browse the repository at this point in the history
  • Loading branch information
oanhnn committed Apr 20, 2021
1 parent dd4d532 commit 89e9d29
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 67 deletions.
7 changes: 5 additions & 2 deletions .github/README-vi.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down Expand Up @@ -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

Expand All @@ -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.
2 changes: 1 addition & 1 deletion .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
27 changes: 13 additions & 14 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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
Expand Down
39 changes: 1 addition & 38 deletions bin/health-check
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions examples/laravel-echo-server.json
Original file line number Diff line number Diff line change
@@ -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}
}
12 changes: 6 additions & 6 deletions examples/redis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
16 changes: 12 additions & 4 deletions examples/sqlite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
32 changes: 32 additions & 0 deletions src/healthcheck.js
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit 89e9d29

Please sign in to comment.