mirror of
https://github.com/dec0dOS/zero-ui.git
synced 2025-07-05 20:41:43 -07:00
feat(backend): add cron and ping members
This commit is contained in:
parent
54ec7677db
commit
d3fdac61bd
4 changed files with 676 additions and 18901 deletions
|
@ -4,9 +4,11 @@ const logger = require("morgan");
|
|||
const compression = require("compression");
|
||||
const bearerToken = require("express-bearer-token");
|
||||
const helmet = require("helmet");
|
||||
const cron = require("node-cron");
|
||||
|
||||
const db = require("./utils/db");
|
||||
const initAdmin = require("./utils/init-admin");
|
||||
const pingAll = require("./utils/ping");
|
||||
|
||||
const authRoutes = require("./routes/auth");
|
||||
const networkRoutes = require("./routes/network");
|
||||
|
@ -74,4 +76,13 @@ app.use(async function (err, req, res) {
|
|||
res.status(500).json({ error: "500 Internal server error" });
|
||||
});
|
||||
|
||||
// ping all networks every 5 minutes
|
||||
cron.schedule('5 * * * *', () => {
|
||||
const networks = db.get('networks').value();
|
||||
networks.forEach((network) => {
|
||||
pingAll(network);
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
module.exports = app;
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
"lodash": "^4.17.21",
|
||||
"lowdb": "^1.0.0",
|
||||
"morgan": "~1.10.0",
|
||||
"node-cron": "^3.0.2",
|
||||
"pbkdf2-wrapper": "^1.3.4"
|
||||
}
|
||||
}
|
||||
|
|
22
backend/utils/ping.js
Normal file
22
backend/utils/ping.js
Normal file
|
@ -0,0 +1,22 @@
|
|||
const api = require("./controller-api");
|
||||
const db = require("./db");
|
||||
|
||||
export default pingAll;
|
||||
const pingAll = async (network) => {
|
||||
await Promise.all(network.members.map((async (member) => {
|
||||
try {
|
||||
await api.get("peer/" + member.id);
|
||||
// write lastOnelineTime field in db
|
||||
db.get("networks")
|
||||
.filter({ id: network.id })
|
||||
.map("members")
|
||||
.first()
|
||||
.filter({ id: member.id })
|
||||
.first()
|
||||
.set("lastOnlineTime", new Date().getTime())
|
||||
.write();
|
||||
} catch (err) {
|
||||
return;
|
||||
}
|
||||
})));
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue