Browse Source

Adding shutdown hook

* Migrated some code into Bot class
* Added shutdown hook with async pattern to send shutdown message
* Added utility module
* Added buildInfo version on startup and in startup message
pull/10/head
Drew Short 4 years ago
parent
commit
c8f86ae762
  1. 99
      bot/bot.js
  2. 18
      bot/utility.js
  3. 6
      index.js

99
bot/bot.js

@ -1,9 +1,59 @@
let fs = require('fs');
let sdk = require("matrix-js-sdk");
let message = require("./message.js");
let utility = require("./utility.js");
class Bot {
constructor(config) {
this.config = config
constructor(config, buildInfo) {
this.config = config;
this.buildInfo = buildInfo;
this.connected = false;
}
sendStatusStartup() {
let promises = [Promise.resolve(true)]
if (this.connected) {
this.config.statusRooms.forEach(roomId => {
console.log("Notifying %s", roomId, "of startup");
promises.push(this.client.sendMessage(
roomId, message.createBasic("Started with version: " + this.buildInfo)
).then(function () {
console.log("Notified %s", roomId, "of startup");
}));
});
} else {
console.log("Attempting to send startup message while disconnected");
}
return Promise.all(promises);
}
sendStatusShutdown() {
let promises = [Promise.resolve(true)]
if (this.connected) {
this.config.statusRooms.forEach(roomId => {
console.log("Notifying %s", roomId, "of Shutdown");
promises.push(this.client.sendMessage(
roomId, message.createBasic("Shutting down")
).then(function () {
console.log("Notified %s", roomId, "of shutdown");
}));
});
} else {
console.log("Attempting to send shutdown message while disconnected");
}
return Promise.all(promises);
}
}
function getBuildInfo(buildInfoPath) {
try {
return fs.readFileSync(buildInfoPath, "utf8");
} catch (err) {
if (err.code === 'ENOENT') {
return "UNKNOWN_" + utility.toISODateString(new Date());
} else {
console.log("Unexpected Error! " + err);
}
}
}
@ -11,7 +61,10 @@ function create(configFile) {
let config = require(configFile);
console.log("Running with config:");
console.log(config);
return new Bot(config);
let buildInfo = getBuildInfo("../build.info")
console.log("Running version:");
console.log(buildInfo);
return new Bot(config, buildInfo);
}
function init(bot) {
@ -22,39 +75,49 @@ function init(bot) {
userId: bot.config.userId
});
function sendClientStatusUpdate() {
bot.config.statusRooms.forEach(roomId => {
console.log("Notifying %s", roomId);
bot.client.sendMessage(roomId, message.createBasic("Started!")).done(function () {
console.log("Notified %s", roomId);
})
});
}
// Prep the bot
bot.client.on("sync", function (state, previousState, data) {
bot.client.on("sync", async function (state, previousState, data) {
switch (state) {
case "PREPARED":
sendClientStatusUpdate();
bot.connected = true;
await bot.sendStatusStartup();
break;
}
});
// auto join rooms that an admin user has invited the bot to
bot.client.on("RoomMember.membership", function (event, member) {
if (member.membership === "invite"
&& bot.config.admin.indexOf(ember.userId) >= 0) {
bot.client.joinRoom(member.roomId).done(function () {
bot.client.joinRoom(member.roomId).done(function () {
console.log("Auto-joined %s", member.roomId);
});
}
});
/* Capture Exit Conditions */
["SIGINT", "SIGTERM"].forEach((signature) => {
process.on(signature, async () => {
await bot.sendStatusShutdown()
.then(function () {
console.log("Gracefully stopping Matrix SDK Client")
bot.client.stopClient();
});
process.exit(0);
});
});
process.on('exit', async function () {
console.log("Shutting Down");
});
return bot;
}
function run(bot) {
bot.client.startClient()
// console.log("Initializing Crypto");
// await bot.client.initCrypto();
console.log("Starting Matrix SDK Client");
bot.client.startClient();
}
exports.create = create;

18
bot/utility.js

@ -0,0 +1,18 @@
function toISODateString(d) {
function pad(n) { return n < 10 ? '0' + n : n }
return d.getUTCFullYear() + '-'
+ pad(d.getUTCMonth() + 1) + '-'
+ pad(d.getUTCDate()) + 'T'
+ pad(d.getUTCHours()) + ':'
+ pad(d.getUTCMinutes()) + ':'
+ pad(d.getUTCSeconds()) + 'Z'
}
function sleep(ms){
return new Promise(resolve=>{
setTimeout(resolve,ms)
})
}
exports.toISODateString = toISODateString;
exports.sleep = sleep;

6
index.js

@ -1,2 +1,6 @@
let bot = require('./bot/bot.js');
bot.run(bot.init(bot.create("../data/config.json")));
bot.run(
bot.init(
bot.create("../data/config.json")
)
);
Loading…
Cancel
Save