Commit 75328c7edae1921f91a215b6fae2958876a847f1

Authored by Adhidarma Hadiwinoto
1 parent c1e6950b52
Exists in master

Add mutex on config-save

Showing 3 changed files with 29 additions and 7 deletions Side-by-side Diff

api-server/config-save.js
1 1 const MODULE_NAME = 'KOMODO-SDK.API-SERVER.CONFIG-SAVE';
2 2  
3 3 const fs = require('fs');
4   -const logger = require('tektrans-logger');
  4 +const locks = require('locks');
5 5 const moment = require('moment');
  6 +const logger = require('tektrans-logger');
6 7 const config = require('../config');
7 8 const matrix = require('../matrix');
8 9  
9 10 if (!fs.existsSync('config-backup')) fs.mkdirSync('config-backup');
10 11  
  12 +const mutex = locks.createMutex();
  13 +
11 14 const backup = async (xid) => {
12 15 try {
13 16 const backupFilename = `config-backup/config_${moment().format('YYYYMMDD_HHmmss.SS')}.json`;
... ... @@ -33,12 +36,18 @@ const backup = async (xid) => {
33 36 };
34 37  
35 38 module.exports = async (xid) => {
36   - try {
37   - if (!matrix.config_is_dirty) {
38   - logger.verbose(`${MODULE_NAME} 4B263CB4: No need to save because config is not dirty`, { xid });
39   - return;
40   - }
  39 + if (!matrix.config_is_dirty) {
  40 + logger.verbose(`${MODULE_NAME} 4B263CB4: No need to save because config is not dirty`, { xid });
  41 + return;
  42 + }
  43 +
  44 + await new Promise((resolve) => {
  45 + mutex.lock(() => {
  46 + resolve();
  47 + });
  48 + });
41 49  
  50 + try {
42 51 const backupFilename = await backup(xid);
43 52  
44 53 await fs.promises.writeFile(
... ... @@ -61,5 +70,7 @@ module.exports = async (xid) => {
61 70 });
62 71  
63 72 throw newE;
  73 + } finally {
  74 + mutex.unlock();
64 75 }
65 76 };
... ... @@ -18,6 +18,7 @@
18 18 "fs-copy-file": "^2.1.2",
19 19 "json-query": "^2.2.2",
20 20 "json-stringify-pretty-compact": "^3.0.0",
  21 + "locks": "^0.2.2",
21 22 "lru-cache": "^4.1.1",
22 23 "macaddress": "^0.2.9",
23 24 "moment": "^2.24.0",
... ... @@ -28,7 +29,6 @@
28 29 "pkginfo": "^0.4.1",
29 30 "redis": "^3.1.2",
30 31 "request": "^2.88.2",
31   - "sd-notify": "*",
32 32 "sha1": "^1.1.1",
33 33 "simple-git": "^1.80.1",
34 34 "stack-trace": "0.0.10",
... ... @@ -2266,6 +2266,11 @@
2266 2266 "node": ">=4"
2267 2267 }
2268 2268 },
  2269 + "node_modules/locks": {
  2270 + "version": "0.2.2",
  2271 + "resolved": "https://registry.npmjs.org/locks/-/locks-0.2.2.tgz",
  2272 + "integrity": "sha1-JZkz0TJ8uvD9NmL4//3jaAnYTO0="
  2273 + },
2269 2274 "node_modules/lodash": {
2270 2275 "version": "4.17.21",
2271 2276 "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
... ... @@ -5457,6 +5462,11 @@
5457 5462 "path-exists": "^3.0.0"
5458 5463 }
5459 5464 },
  5465 + "locks": {
  5466 + "version": "0.2.2",
  5467 + "resolved": "https://registry.npmjs.org/locks/-/locks-0.2.2.tgz",
  5468 + "integrity": "sha1-JZkz0TJ8uvD9NmL4//3jaAnYTO0="
  5469 + },
5460 5470 "lodash": {
5461 5471 "version": "4.17.21",
5462 5472 "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
... ... @@ -29,6 +29,7 @@
29 29 "fs-copy-file": "^2.1.2",
30 30 "json-query": "^2.2.2",
31 31 "json-stringify-pretty-compact": "^3.0.0",
  32 + "locks": "^0.2.2",
32 33 "lru-cache": "^4.1.1",
33 34 "macaddress": "^0.2.9",
34 35 "moment": "^2.24.0",