Browse Source

Merge pull request #86 from stakwork/healthcheck

Healthcheck
loopout2 v1.0.3
Evan Feenstra 4 years ago
committed by GitHub
parent
commit
72783e51fb
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      dist/src/controllers/bots.js
  2. 2
      dist/src/controllers/bots.js.map
  3. 5
      dist/src/controllers/chatTribes.js
  4. 2
      dist/src/controllers/chatTribes.js.map
  5. 56
      dist/src/controllers/confirmations.js
  6. 2
      dist/src/controllers/confirmations.js.map
  7. 3
      dist/src/controllers/index.js
  8. 2
      dist/src/controllers/index.js.map
  9. 7
      dist/src/network/intercept.js
  10. 2
      dist/src/network/intercept.js.map
  11. 4
      dist/src/utils/case.js
  12. 2
      dist/src/utils/case.js.map
  13. 3
      src/controllers/bots.ts
  14. 5
      src/controllers/chatTribes.ts
  15. 65
      src/controllers/confirmations.ts
  16. 4
      src/controllers/index.ts
  17. 7
      src/network/intercept.ts
  18. 3
      src/utils/case.ts

3
dist/src/controllers/bots.js

@ -270,9 +270,10 @@ function postToBotServer(msg, bot, route) {
}
exports.postToBotServer = postToBotServer;
function buildBotPayload(msg) {
const chat_uuid = msg.chat && msg.chat.uuid;
const m = {
channel: {
id: msg.chat.uuid,
id: chat_uuid,
send: function () { },
},
reply: function () { },

2
dist/src/controllers/bots.js.map

File diff suppressed because one or more lines are too long

5
dist/src/controllers/chatTribes.js

@ -438,7 +438,10 @@ function replayChatHistory(chat, contact) {
content = JSON.parse(m.remoteMessageContent);
}
catch (e) { }
const dateString = m.date && m.date.toISOString();
let mdate = m.date;
if (!mdate)
mdate = new Date();
const dateString = mdate.toISOString();
let mediaKeyMap;
let newMediaTerms;
if (m.type === constants.message_types.attachment) {

2
dist/src/controllers/chatTribes.js.map

File diff suppressed because one or more lines are too long

56
dist/src/controllers/confirmations.js

@ -15,6 +15,7 @@ const socket = require("../utils/socket");
const jsonUtils = require("../utils/json");
const network = require("../network");
const path = require("path");
const res_1 = require("../utils/res");
const constants = require(path.join(__dirname, '../../config/constants.json'));
function sendConfirmation({ chat, sender, msg_id }) {
if (!msg_id)
@ -146,4 +147,59 @@ function receiveHeartbeat(payload) {
});
}
exports.receiveHeartbeat = receiveHeartbeat;
let heartbeats = {};
function healthcheck(req, res) {
return __awaiter(this, void 0, void 0, function* () {
const pubkey = req.query.pubkey;
if (!(pubkey && pubkey.length === 66)) {
return res_1.failure(res, 'missing pubkey');
}
const owner = yield models_1.models.Contact.findOne({ where: { isOwner: true } });
const amt = 10;
const opts = {
amt,
dest: pubkey,
data: {
type: constants.message_types.heartbeat,
message: {
amount: amt,
},
sender: { pub_key: owner.publicKey }
}
};
try {
yield network.signAndSend(opts);
}
catch (e) {
res_1.failure(res, e);
return;
}
let i = 0;
let interval = setInterval(() => {
if (i >= 15) {
clearInterval(interval);
delete heartbeats[pubkey];
res_1.failure(res, 'no confimration received');
return;
}
if (heartbeats[pubkey]) {
res_1.success(res, 'success');
clearInterval(interval);
delete heartbeats[pubkey];
return;
}
i++;
}, 1000);
});
}
exports.healthcheck = healthcheck;
function receiveHeartbeatConfirmation(payload) {
return __awaiter(this, void 0, void 0, function* () {
console.log('=> received heartbeat confirmation');
const dat = payload.content || payload;
const sender_pub_key = dat.sender.pub_key;
heartbeats[sender_pub_key] = true;
});
}
exports.receiveHeartbeatConfirmation = receiveHeartbeatConfirmation;
//# sourceMappingURL=confirmations.js.map

2
dist/src/controllers/confirmations.js.map

File diff suppressed because one or more lines are too long

3
dist/src/controllers/index.js

@ -97,6 +97,7 @@ function set(app) {
app.get('/bots', bots.getBots);
app.post('/bot', bots.createBot);
app.delete('/bot/:id', bots.deleteBot);
app.get('/healthcheck', confirmations.healthcheck);
app.get('/version', function (req, res) {
return __awaiter(this, void 0, void 0, function* () {
const version = yield gitinfo_1.checkTag();
@ -148,6 +149,6 @@ exports.ACTIONS = {
[msgtypes.bot_cmd]: bots.receiveBotCmd,
[msgtypes.bot_res]: bots.receiveBotRes,
[msgtypes.heartbeat]: confirmations.receiveHeartbeat,
[msgtypes.heartbeat_confirmation]: () => { },
[msgtypes.heartbeat_confirmation]: confirmations.receiveHeartbeatConfirmation,
};
//# sourceMappingURL=index.js.map

2
dist/src/controllers/index.js.map

File diff suppressed because one or more lines are too long

7
dist/src/network/intercept.js

@ -27,10 +27,11 @@ function isBotMsg(msg, sentByMe) {
if (msgType === constants.message_types.bot_res) {
return false; // bot res msg type not for processing
}
const uuid = msg.chat && msg.chat.uuid;
if (!uuid)
return false;
const chat = yield models_1.models.Chat.findOne({
where: {
uuid: msg.chat.uuid
}
where: { uuid }
});
if (!chat)
return false;

2
dist/src/network/intercept.js.map

@ -1 +1 @@
{"version":3,"file":"intercept.js","sourceRoot":"","sources":["../../../src/network/intercept.ts"],"names":[],"mappings":";;;;;;;;;;;AACA,sCAAkC;AAClC,wCAA2C;AAC3C,8CAAoE;AACpE,6BAA4B;AAC5B,wCAAuC;AAEvC,MAAM,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,6BAA6B,CAAC,CAAC,CAAA;AAE9E;;;EAGE;AAEF,kDAAkD;AAClD,SAAsB,QAAQ,CAAC,GAAQ,EAAE,QAAiB;;QACxD,MAAM,GAAG,GAAG,GAAG,CAAC,OAAO,IAAI,GAAG,CAAC,OAAO,CAAC,OAAO,CAAA;QAE9C,MAAM,OAAO,GAAG,GAAG,CAAC,IAAI,CAAA;QACxB,IAAI,OAAO,KAAK,SAAS,CAAC,aAAa,CAAC,OAAO,EAAE;YAC/C,OAAO,KAAK,CAAA,CAAC,sCAAsC;SACpD;QACD,MAAM,IAAI,GAAG,MAAM,eAAM,CAAC,IAAI,CAAC,OAAO,CAAC;YACrC,KAAK,EAAE;gBACL,IAAI,EAAE,GAAG,CAAC,IAAI,CAAC,IAAI;aACpB;SACF,CAAC,CAAA;QACF,IAAI,CAAC,IAAI;YAAE,OAAO,KAAK,CAAA;QAEvB,IAAI,OAAO,GAAG,KAAK,CAAA;QAEnB,IAAI,GAAG,IAAI,GAAG,CAAC,U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E,UAAU,CAAC,OAAO;qBACzB;iBACF,CAAC,CAAA;gBACF,OAAO,sBAAe,CAAC,GAAG,EAAE,GAAG,EAAE,SAAS,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAA;YAC9D,KAAK,SAAS,CAAC,SAAS,CAAC,MAAM;gBAC7B,OAAO,oBAAa,CAAC,GAAG,EAAE,UAAU,CAAC,CAAA;YACvC;gBACE,OAAO,KAAK,CAAA;SACf;IACH,CAAC;CAAA;AAED,SAAe,YAAY,CAAC,KAAK,EAAE,QAAQ;;QACzC,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,KAAK,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE;YACjD,MAAM,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC;SAC5C;IACH,CAAC;CAAA"}

4
dist/src/utils/case.js

@ -8,7 +8,9 @@ function toSnake(obj) {
for (let [key, value] of Object.entries(obj)) {
if (dateKeys.includes(key) && value) {
const v = value;
const d = new Date(v);
let d = new Date(v);
if (isNaN(d.getTime()))
d = new Date();
ret[changeCase.snakeCase(key)] = d.toISOString();
}
else if (boolKeys.includes(key)) {

2
dist/src/utils/case.js.map

@ -1 +1 @@
{"version":3,"file":"case.js","sourceRoot":"","sources":["../../../src/utils/case.ts"],"names":[],"mappings":";;AAAA,0CAA0C;AAE1C,MAAM,QAAQ,GAAG,CAAC,MAAM,EAAC,WAAW,EAAC,WAAW,EAAC,YAAY,EAAC,YAAY,CAAC,CAAA;AAC3E,MAAM,QAAQ,GAAG,CAAC,WAAW,EAAC,SAAS,EAAC,SAAS,CAAC,CAAA;AAElD,SAAS,OAAO,CAAC,GAAG;IAChB,MAAM,GAAG,GAAuB,EAAE,CAAA;IAClC,KAAK,IAAI,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;QAC1C,IAAG,QAAQ,CAAC,QAAQ,C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C,KAAK,IAAI,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;QAC1C,IAAG,QAAQ,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,KAAK,EAAC;YAC/B,MAAM,CAAC,GAAQ,KAAK,CAAA;YACpB,IAAI,CAAC,GAAG,IAAI,IAAI,CAAC,CAAC,CAAC,CAAA;YACnB,IAAG,KAAK,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC;gBAAE,CAAC,GAAC,IAAI,IAAI,EAAE,CAAA;YACnC,GAAG,CAAC,UAAU,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,WAAW,EAAE,CAAA;SACnD;aAAM,IAAG,QAAQ,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE;YAC9B,GAAG,CAAC,UAAU,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,IAAE,KAAK,KAAG,GAAG,CAAC,CAAA,CAAC,CAAA,CAAC,CAAA,CAAC,CAAA,CAAC,CAAA;SAC7D;aAAM;YACH,GAAG,CAAC,UAAU,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,GAAG,KAAK,CAAA;SACzC;KACJ;IACD,OAAO,GAAG,CAAA;AACd,CAAC;AAUO,0BAAO;AARf,SAAS,OAAO,CAAC,GAAG;IAChB,MAAM,GAAG,GAAuB,EAAE,CAAA;IAClC,KAAK,IAAI,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;QAC1C,GAAG,CAAC,UAAU,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,GAAG,KAAK,CAAA;KACzC;IACD,OAAO,GAAG,CAAA;AACd,CAAC;AAEgB,0BAAO"}

3
src/controllers/bots.ts

@ -261,9 +261,10 @@ export async function postToBotServer(msg, bot, route:string): Promise<boolean>
}
export function buildBotPayload(msg:Msg): SphinxBot.Message {
const chat_uuid = msg.chat&&msg.chat.uuid
const m = <SphinxBot.Message>{
channel:{
id: msg.chat.uuid,
id: chat_uuid,
send:function(){},
},
reply:function(){},

5
src/controllers/chatTribes.ts

@ -440,7 +440,10 @@ export async function replayChatHistory(chat, contact) {
let content = ''
try {content = JSON.parse(m.remoteMessageContent)} catch(e) {}
const dateString = m.date&&m.date.toISOString()
let mdate = m.date
if(!mdate) mdate = new Date()
const dateString = mdate.toISOString()
let mediaKeyMap
let newMediaTerms
if(m.type===constants.message_types.attachment) {

65
src/controllers/confirmations.ts

@ -4,6 +4,7 @@ import * as socket from '../utils/socket'
import * as jsonUtils from '../utils/json'
import * as network from '../network'
import * as path from 'path'
import { failure, success } from '../utils/res'
const constants = require(path.join(__dirname, '../../config/constants.json'))
@ -108,21 +109,21 @@ export async function receiveHeartbeat(payload) {
const sender_pub_key = dat.sender.pub_key
const receivedAmount = dat.message.amount
if (!(sender_pub_key && sender_pub_key.length===66)) return console.log('no sender')
if (!(sender_pub_key && sender_pub_key.length === 66)) return console.log('no sender')
if (!receivedAmount) return console.log('no amount')
const owner = await models.Contact.findOne({ where: { isOwner: true } })
const amount = Math.round(receivedAmount/2)
const amount = Math.round(receivedAmount / 2)
const MIN_SATS = 3
const amt = Math.max(amount || MIN_SATS)
const opts = {
amt,
dest:sender_pub_key,
dest: sender_pub_key,
data: <network.Msg>{
type: constants.message_types.heartbeat_confirmation,
message: { amount: amt },
sender: {pub_key: owner.publicKey}
sender: { pub_key: owner.publicKey }
}
}
try {
@ -131,4 +132,60 @@ export async function receiveHeartbeat(payload) {
} catch (e) {
return false
}
}
let heartbeats:{[k:string]:boolean} = {}
export async function healthcheck(req, res) {
const pubkey:string = req.query.pubkey
if (!(pubkey&&pubkey.length===66)) {
return failure(res, 'missing pubkey')
}
const owner = await models.Contact.findOne({ where: { isOwner: true } })
const amt = 10
const opts = {
amt,
dest: pubkey,
data: <network.Msg>{
type: constants.message_types.heartbeat,
message: {
amount: amt,
},
sender: { pub_key: owner.publicKey }
}
}
try {
await network.signAndSend(opts)
} catch (e) {
failure(res, e)
return
}
let i = 0
let interval = setInterval(()=>{
if(i>=15) {
clearInterval(interval)
delete heartbeats[pubkey]
failure(res, 'no confimration received')
return
}
if(heartbeats[pubkey]) {
success(res, 'success')
clearInterval(interval)
delete heartbeats[pubkey]
return
}
i ++
}, 1000)
}
export async function receiveHeartbeatConfirmation(payload) {
console.log('=> received heartbeat confirmation')
const dat = payload.content || payload
const sender_pub_key = dat.sender.pub_key
heartbeats[sender_pub_key] = true
}

4
src/controllers/index.ts

@ -101,6 +101,8 @@ export async function set(app) {
app.post('/bot', bots.createBot)
app.delete('/bot/:id', bots.deleteBot)
app.get('/healthcheck', confirmations.healthcheck)
app.get('/version', async function(req,res) {
const version = await checkTag()
res.send({version})
@ -148,5 +150,5 @@ export const ACTIONS = {
[msgtypes.bot_cmd]: bots.receiveBotCmd,
[msgtypes.bot_res]: bots.receiveBotRes,
[msgtypes.heartbeat]: confirmations.receiveHeartbeat,
[msgtypes.heartbeat_confirmation]: ()=>{},
[msgtypes.heartbeat_confirmation]: confirmations.receiveHeartbeatConfirmation,
}

7
src/network/intercept.ts

@ -20,10 +20,11 @@ export async function isBotMsg(msg: Msg, sentByMe: boolean): Promise<boolean> {
if (msgType === constants.message_types.bot_res) {
return false // bot res msg type not for processing
}
const uuid = msg.chat && msg.chat.uuid
if(!uuid) return false
const chat = await models.Chat.findOne({
where: {
uuid: msg.chat.uuid
}
where: {uuid}
})
if (!chat) return false

3
src/utils/case.ts

@ -8,7 +8,8 @@ function toSnake(obj) {
for (let [key, value] of Object.entries(obj)) {
if(dateKeys.includes(key) && value){
const v: any = value
const d = new Date(v)
let d = new Date(v)
if(isNaN(d.getTime())) d=new Date()
ret[changeCase.snakeCase(key)] = d.toISOString()
} else if(boolKeys.includes(key)) {
ret[changeCase.snakeCase(key)] = (!value||value==='0')?0:1

Loading…
Cancel
Save