Skip to content

Commit

Permalink
use queue job system to process voting from 3rd pt website
Browse files Browse the repository at this point in the history
worse performance than proxy forward but i have no choice for now
  • Loading branch information
SaitDev committed Sep 10, 2018
1 parent 89027a7 commit 650701b
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 100 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Expand Up @@ -10,7 +10,7 @@
"name": "development",
"runtimeExecutable": "node",
"env": {
"NODE_ENV": "development"
"CHITANDA_ENV": "development"
},
"runtimeArgs": [
"node_modules/nodemon/bin/nodemon.js",
Expand Down
3 changes: 2 additions & 1 deletion bot.js
Expand Up @@ -15,7 +15,8 @@ client.once('ready', function() {
client.commandManager.loadCommands();
client.database.connect()
.then(_ => {
client.database.guildSettingManager.loadSettings();
client.schedulerManager.startAll();
// client.database.guildSettingManager.loadSettings();
});
client.presenceManager.start();
});
Expand Down
8 changes: 6 additions & 2 deletions chitanda.js
@@ -1,9 +1,11 @@
const Discord = require('discord.js')

const config = require('./config.json')
const DatabaseFactory = require('./database/databaseFactory')
const CommandManager = require('./managers/commandManager.js')
const PresenceManager = require('./managers/presenceManager.js')
const BotListingManager = require('./managers/botListingManager')
const SchedulerManager = require('./managers/schedulerManager')
const ErrorLog = require('./logger/errorlog')
const CommandLog = require('./logger/commandLog')
const ChatLog = require('./logger/chatLog')
Expand All @@ -17,16 +19,18 @@ class Chitanda extends Discord.Client {
*/
constructor(cmdPath, options) {
super(options);
this.ready = false
this.ready = false;
this.messageUtil = new MessageUtil(this);
this.errorLogger = new ErrorLog(this);
this.database = new DatabaseFactory(this);
this.commandManager = new CommandManager(this, cmdPath);
this.presenceManager = new PresenceManager(this, config);
// this.botListingManager = new BotListingManager(this);
this.delayCreateBotListingManager();
this.commandLogger = new CommandLog(this);
this.chatLogger = new ChatLog(this);
this.guildLogger = new GuildLog(this);
this.messageUtil = new MessageUtil(this);
this.schedulerManager = new SchedulerManager(this);
}

delayCreateBotListingManager() {
Expand Down
1 change: 1 addition & 0 deletions config.json
Expand Up @@ -10,6 +10,7 @@
"dmCommand": true,
"tracking": [],
"botname": "BotName",
"database": "mongodb-connection-uri",
"cleverbot": {
"user": "cleverbot.io_api_user",
"key": "cleverbot.io_api_key"
Expand Down
26 changes: 26 additions & 0 deletions database/databaseFactory.js
@@ -0,0 +1,26 @@
const mongoose = require('mongoose')

const config = require('../config.json')

class DatabaseFactory {
/**
* @param {import('../chitanda')} client
*/
constructor(client) {
this.client = client;
}

connect() {
return mongoose.connect(config.database, { useNewUrlParser: true })
.then(() => {
this.client.errorLogger.info('Connected to mongodb', true);
return true;
})
.catch(err => {
this.client.errorLogger.error(err);
process.exit(1);
});
}
}

module.exports = DatabaseFactory;
26 changes: 7 additions & 19 deletions logger/errorlog.js
@@ -1,6 +1,6 @@
const config = require('../config.json');
const prodEnv = "PRODUCTION";
const textChat = ['text', 'dm', 'group'];
const config = require('../config.json')
const util = require('../util/commonUtil')
const textChat = ['text', 'dm', 'group']

//not sending these log to discord channel
const ignore = {
Expand All @@ -25,10 +25,7 @@ class ErrorLog {
console.log('[Info] ' + message);

try {
if (consoleOnly ||
!process.env.CHITANDA_ENV ||
process.env.CHITANDA_ENV.toUpperCase() != prodEnv
) {
if (consoleOnly || !util.isProduction()) {
return;
}

Expand All @@ -50,10 +47,7 @@ class ErrorLog {
console.warn('[Warn] ' + message);

try {
if (consoleOnly ||
!process.env.CHITANDA_ENV ||
process.env.CHITANDA_ENV.toUpperCase() != prodEnv
) {
if (consoleOnly || !util.isProduction()) {
return;
}

Expand Down Expand Up @@ -81,10 +75,7 @@ class ErrorLog {
}

try {
if (consoleOnly ||
!process.env.CHITANDA_ENV ||
process.env.CHITANDA_ENV.toUpperCase() != prodEnv
) {
if (consoleOnly || !util.isProduction()) {
return;
}

Expand Down Expand Up @@ -112,10 +103,7 @@ class ErrorLog {
console.error(err.stack);

try {
if (consoleOnly ||
!process.env.CHITANDA_ENV ||
process.env.CHITANDA_ENV.toUpperCase() != prodEnv
) {
if (consoleOnly || !util.isProduction()) {
return;
}

Expand Down
45 changes: 0 additions & 45 deletions managers/botListingManager.js
@@ -1,11 +1,6 @@
const DiscordBotOrg = require('../services/discordBots.org')
const BotDiscordPw = require('../services/botsDiscord.Pw')

const messages = [
'Hey hey. I just got voted. I\'m curious, who did vote me?',
'Thank for voting me. Please keep the flame'
]

class BotListingManager {
/**
* @param {import('../chitanda')} client
Expand All @@ -14,47 +9,7 @@ class BotListingManager {
this.client = client;
this.discordBotOrg = new DiscordBotOrg(client);
this.botDiscordPw = new BotDiscordPw(client);

this.discordBotOrg.onVote((userId, isWeekend) => {
try {
this.thankVote(userId, isWeekend);
} catch (err) {
this.client.errorLogger.error(err);
}
});
}

thankVote(userId, isWeekend) {
//TODO: remove these debug after success on production
console.log(userId)
console.log(isWeekend)
this.client.fetchUser(userId)
.then(user => {
if (user.dmChannel) {
sendThank(this.client, user.dmChannel, user);
} else {
user.createDM().then(dm => {
sendThank(this.client, dm, user);
})
}
})
.catch(err => {
this.client.errorLogger.error(err);
})
}
}

/**
*
* @param {import('../chitanda')} client
* @param {import('discord.js').DMChannel} dmChannel
* @param {import('discord.js').User} user
*/
var sendThank = function(client, dmChannel, user) {
client.messageUtil.sendFromChannel(
dmChannel,
messages[Math.floor(Math.random() * messages.length)]
);
}

module.exports = BotListingManager;
17 changes: 17 additions & 0 deletions managers/schedulerManager.js
@@ -0,0 +1,17 @@
const VoteScheduler = require('../schedulers/voteScheduler')

class SchedulerManager {
/**
* @param {import('../chitanda')} client
*/
constructor(client) {
this.client = client;
this.voteScheduler = new VoteScheduler(client);
}

startAll() {
this.voteScheduler.start();
}
}

module.exports = SchedulerManager;
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -18,14 +18,16 @@
},
"dependencies": {
"@types/node": "^9.6.1",
"agenda": "^2.0.1",
"bluebird": "^3.5.1",
"body-parser": "^1.18.3",
"cleverbot.io": "^1.0.4",
"dblapi.js": "^2.2.0",
"discord.js": "^11.3.2",
"express": "^4.16.3",
"express-http-proxy": "^1.2.0",
"leetscript": "^1.1.0",
"moment": "^2.22.2",
"mongoose": "^5.2.13",
"sync-request": "^6.0.0"
},
"devDependencies": {
Expand Down
29 changes: 1 addition & 28 deletions services/discordBots.org.js
Expand Up @@ -9,13 +9,7 @@ class DiscordBotOrg {
*/
constructor(client) {
this.listeners = [];
this.service = new DBL(config.discordBotOrg.token,
{
webhookPort: 5000,
webhookAuth: config.discordBotOrg.webhookAuth
},
client
);
this.service = new DBL(config.discordBotOrg.token, client);

this.service.on('posted', () => {
client.errorLogger.info(message, true);
Expand All @@ -24,21 +18,6 @@ class DiscordBotOrg {
client.errorLogger.error(err);
});

this.service.webhook.on('ready', hook => {
client.errorLogger.info(`Internal webhook running at http://${hook.hostname}:${hook.port}${hook.path}`, true);
});
this.service.webhook.on('vote', vote => {
//TODO: remove these debug after success on production
console.log(vote)
this.listeners.forEach(listener => {
try {
listener(vote.user, vote.isWeekend);
} catch (err) {
client.errorLogger.error(err);
}
})
});

this.service.postStats(client.guilds.size)
.then(() => {
client.errorLogger.info(message, true);
Expand All @@ -47,12 +26,6 @@ class DiscordBotOrg {
client.errorLogger.error(err);
});
}

onVote(listener) {
if (typeof listener == 'function') {
this.listeners.push(listener);
}
}
}

module.exports = DiscordBotOrg;
6 changes: 6 additions & 0 deletions util/commonUtil.js
@@ -1,3 +1,9 @@
const prodEnv = "PRODUCTION";

exports.randomTrue = () => {
return Math.random() > 0.5;
}

exports.isProduction = () => {
return process.env.CHITANDA_ENV && process.env.CHITANDA_ENV.toUpperCase() == prodEnv;
}
33 changes: 30 additions & 3 deletions web.js
@@ -1,20 +1,47 @@
/**======= Copy right
Sait 2018
Sait 2018
=====================*/
const express = require('express')
const proxy = require('express-http-proxy')
const bodyParser = require('body-parser')
const agenda = require('agenda')

const app = express()
const jobQueue = new agenda()

const config = require('./config.json')
const VoteScheduler = require('./schedulers/voteScheduler')
const path = '/dblwebhook'
const port = process.env.PORT || 5555

app.use(path, proxy('localhost:5000'));
jobQueue.database(config.database, VoteScheduler.voteCollection, { useNewUrlParser: true });

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.get('/', (req, res) => {
res.send('How did you find this owo');
})

app.use(path, async (req, res) => {
var auth = config.discordBotOrg.webhookAuth;
if (auth && auth !== req.headers.authorization) {
res.status(401).end();
return;
}

await jobQueue.start();
jobQueue.now(VoteScheduler.voteJob, {
header: {
host: req.headers.host,
origin: req.headers.origin,
// authorization: req.headers.authorization
},
body: req.body
}).then(() => {
res.status(200).end();
})
})

app.listen(port, () => {
console.log(`[Info] Webhook running at ${path} on port ${port}`);
})

0 comments on commit 650701b

Please sign in to comment.