From 9f5a9be29e1d66a5762f940b5b51cf43391cefb9 Mon Sep 17 00:00:00 2001 From: Martin Dimitrov Date: Fri, 3 May 2024 16:08:53 -0700 Subject: [PATCH] exit redis client after exit --- .gitea/workflows/docker-image.yaml | 12 ++++++------ packages/serverless/functions/api/door/auth.js | 16 ++++++++-------- packages/serverless/functions/api/door/info.js | 12 ++++-------- packages/serverless/functions/api/door/status.js | 7 ++++--- 4 files changed, 22 insertions(+), 25 deletions(-) diff --git a/.gitea/workflows/docker-image.yaml b/.gitea/workflows/docker-image.yaml index dc1ef4a..ee655ab 100644 --- a/.gitea/workflows/docker-image.yaml +++ b/.gitea/workflows/docker-image.yaml @@ -17,9 +17,9 @@ jobs: - name: Login and Push Docker image run: echo -n '${{ secrets.PASSWORD }}' | docker login gitea.chromart.dedyn.io --username ${{ secrets.USERNAME }} --password-stdin && docker image push --all-tags gitea.chromart.dedyn.io/martin/doorman - deploy-portainer: - needs: docker - runs-on: ubuntu-22.04 - steps: - - name: Call Portainer stack webhook - run: curl --request POST http://192.168.1.150:9000/api/stacks/webhooks/42b3ae11-45bb-4021-b274-bedcb1250892 \ No newline at end of file + # deploy-portainer: + # needs: docker + # runs-on: ubuntu-22.04 + # steps: + # - name: Call Portainer stack webhook + # run: curl --request POST http://192.168.1.150:9000/api/stacks/webhooks/42b3ae11-45bb-4021-b274-bedcb1250892 \ No newline at end of file diff --git a/packages/serverless/functions/api/door/auth.js b/packages/serverless/functions/api/door/auth.js index 51afa1b..8c60fc8 100644 --- a/packages/serverless/functions/api/door/auth.js +++ b/packages/serverless/functions/api/door/auth.js @@ -23,29 +23,29 @@ exports.handler = function(context, event, callback) { return callback(null, response); } - door = door.toUpperCase(); - - if (!context['FIXED_PIN_' + door]) { + if (!context['FIXED_PIN_' + door.toUpperCase()]) { response.setStatusCode(404); return callback(null, response); } - let correctPin = context['FIXED_PIN' + door]; + let correctPin = context['FIXED_PIN_' + door.toUpperCase()]; if (correctPin !== pin) { response.setStatusCode(401); return callback(null, response); } - let client = new redis.RedisDbClient((err) => console.error(err), { url: context.REDIS_CONNECT_URL }); + let client = redis.createClient({ url: context.REDIS_CONNECT_URL }); client.connect() .then(async () => { const statusKey = doorStatusKey(door); const fingerprint = { method: "PIN" }; - const timeout = context['OPEN_TIMEOUT_' + door] || 60; + const timeout = context['OPEN_TIMEOUT_' + door.toUpperCase()] || 60; + + await client.set(statusKey, JSON.stringify(fingerprint)); + await client.expire(statusKey, timeout); + await client.quit(); - await client.put(statusKey, JSON.stringify(fingerprint)); - await client.getClient().expire(statusKey, timeout); response .setStatusCode(200) .appendHeader('Content-Type', 'application/json') diff --git a/packages/serverless/functions/api/door/info.js b/packages/serverless/functions/api/door/info.js index 873e235..2b304ce 100644 --- a/packages/serverless/functions/api/door/info.js +++ b/packages/serverless/functions/api/door/info.js @@ -22,22 +22,18 @@ exports.handler = function(context, event, callback) { return callback(null, response); } - door = door.toUpperCase(); + const timeout = context['OPEN_TIMEOUT_' + door.toUpperCase()] || 60; - const timeout = context['OPEN_TIMEOUT_' + door] || 60; - - - res.status(200).json(); - - let client = new RedisDbClient((err) => console.error(err), { url: context.REDIS_CONNECT_URL }); + let client = redis.createClient({ url: context.REDIS_CONNECT_URL }); client.connect() .then(async () => { const status = await client.get(doorStatusKey(door)) ? "OPEN": "CLOSED"; + await client.quit(); response .setStatusCode(200) .appendHeader('Content-Type', 'application/json') - .setBody({ id: doorId, timeout, status }); + .setBody({ id: door, timeout, status }); return callback(null, response); }); diff --git a/packages/serverless/functions/api/door/status.js b/packages/serverless/functions/api/door/status.js index b15e13d..8635d99 100644 --- a/packages/serverless/functions/api/door/status.js +++ b/packages/serverless/functions/api/door/status.js @@ -22,7 +22,7 @@ exports.handler = function(context, event, callback) { return callback(null, response); } - let client = new RedisDbClient((err) => console.error(err), { url: context.REDIS_CONNECT_URL }); + let client = redis.createClient({ url: context.REDIS_CONNECT_URL }); client.connect() .then(async () => { const isOpen = await client.get(doorStatusKey(door)); @@ -34,10 +34,11 @@ exports.handler = function(context, event, callback) { .setStatusCode(200) .appendHeader('Content-Type', 'application/json') .setBody({ - status: "OPEN", + status: "OPEN", fingerprint, }); - await client.remove(doorStatusKey(req.params.id)); + await client.getDel(doorStatusKey(door)); + await client.quit(); return callback(null, response); }