Commit 2cf4686d2ba94aed463549650d8a492736aa6c59

Authored by Adhidarma Hadiwinoto
1 parent 59ef709f19
Exists in master and in 1 other branch webadmin

Lock modem for 2 secs

Showing 4 changed files with 41 additions and 0 deletions Side-by-side Diff

... ... @@ -0,0 +1,21 @@
  1 +const locks = require('locks');
  2 +
  3 +const mutexes = {};
  4 +
  5 +exports.lock = (name) => new Promise((resolve) => {
  6 + if (!mutexes[name]) {
  7 + mutexes[name] = locks.createMutex();
  8 + }
  9 +
  10 + mutexes[name].lock(() => {
  11 + resolve(true);
  12 + });
  13 +});
  14 +
  15 +exports.unlock = (name) => {
  16 + try {
  17 + if (mutexes[name]) mutexes[name].unlock();
  18 + } catch (e) {
  19 + //
  20 + }
  21 +};
... ... @@ -8,6 +8,15 @@ const messagingClient = require('komodo-center-messaging-client-lib');
8 8  
9 9 const modems = require('./modems');
10 10 const messageSplitter = require('./message-splitter');
  11 +const modemLocks = require('./modem-locks');
  12 +
  13 +async function sleep(ms) {
  14 + return new Promise((resolve) => {
  15 + setTimeout(() => {
  16 + resolve();
  17 + }, ms);
  18 + });
  19 +}
11 20  
12 21 async function sendToModem(partner, msg, modem, parentXid) {
13 22 const xid = parentXid || uniqid();
... ... @@ -24,6 +33,8 @@ async function sendToModem(partner, msg, modem, parentXid) {
24 33 modem: modem.name,
25 34 });
26 35  
  36 + await modemLocks.lock(modem.name);
  37 +
27 38 try {
28 39 await axios.get(config.sender.url, {
29 40 params: {
... ... @@ -56,6 +67,9 @@ async function sendToModem(partner, msg, modem, parentXid) {
56 67 is_outgoing: true,
57 68 });
58 69  
  70 + await sleep(2000);
  71 + modemLocks.unlock(modem.name);
  72 +
59 73 if (msgTail) {
60 74 await sendToModem(partner, msgTail, modem);
61 75 }
... ... @@ -2713,6 +2713,11 @@
2713 2713 "path-exists": "^3.0.0"
2714 2714 }
2715 2715 },
  2716 + "locks": {
  2717 + "version": "0.2.2",
  2718 + "resolved": "https://registry.npmjs.org/locks/-/locks-0.2.2.tgz",
  2719 + "integrity": "sha1-JZkz0TJ8uvD9NmL4//3jaAnYTO0="
  2720 + },
2716 2721 "lodash": {
2717 2722 "version": "4.17.15",
2718 2723 "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz",
... ... @@ -24,6 +24,7 @@
24 24 "express": "^4.17.1",
25 25 "komodo-center-messaging-client-lib": "git+http://gitlab.kodesumber.com/komodo/komodo-center-messaging-client-lib.git",
26 26 "komodo-sdk": "git+http://gitlab.kodesumber.com/komodo/komodo-sdk.git",
  27 + "locks": "^0.2.2",
27 28 "moment": "^2.24.0",
28 29 "uniqid": "^5.2.0"
29 30 },