Fix some race conditions with the 3.2 update
This commit is contained in:
benji7425 2017-12-09 01:38:28 +00:00
parent 30ed030b62
commit f54f610c01
5 changed files with 40 additions and 39 deletions

View File

@ -1,4 +1,9 @@
# Changelog # Changelog
## v3.2.0-b3
### Fixed
- Fixed some race condtions with the 3.2 update
## v3.2.0-b2
### Fixed ### Fixed
- Fixed upgrader script not including channel ID in new database - Fixed upgrader script not including channel ID in new database

View File

@ -47,7 +47,7 @@ client.bootstrap();
function doGuildIteration() { function doGuildIteration() {
const guild = guildsIterator.next().value; const guild = guildsIterator.next().value;
guild && client.guildDataModel.findOne({ guildID: guild.id }) guild && client.guildDataModel.findOne({ guildID: guild.id })
.then(guildData => guildData && guildData.checkFeeds(guild)); .then(guildData => guildData && guildData.checkFeeds(guild).then(() => guildData.save()));
} }
function parseLinksInGuilds() { function parseLinksInGuilds() {

View File

@ -5,6 +5,7 @@ const Dns = require("dns"); //for host resolution checking
const Url = require("url"); //for url parsing const Url = require("url"); //for url parsing
const { promisify } = require("util"); const { promisify } = require("util");
const FeedReadPromise = promisify(require("feed-read")); //for extracing new links from RSS feeds const FeedReadPromise = promisify(require("feed-read")); //for extracing new links from RSS feeds
const DnsResolvePromise = promisify(Dns.resolve);
const GetUrls = require("get-urls"); //for extracting urls from messages const GetUrls = require("get-urls"); //for extracting urls from messages
module.exports = class FeedData extends Camo.EmbeddedDocument { module.exports = class FeedData extends Camo.EmbeddedDocument {
@ -50,12 +51,11 @@ module.exports = class FeedData extends Camo.EmbeddedDocument {
} }
fetchLatest(guild) { fetchLatest(guild) {
Dns.resolve(Url.parse(this.url).host || "", err => { const dnsPromise = DnsResolvePromise(Url.parse(this.url).host).then(() => this._doFetchRSS(guild));
if (err)
DiscordUtil.dateDebugError("Connection Error: Can't resolve host", err.message || err); dnsPromise.catch(err => DiscordUtil.dateError("Connection error: Can't resolve host", err.message || err));
else
this._doFetchRSS(guild); return dnsPromise;
});
} }
toString() { toString() {
@ -64,25 +64,29 @@ module.exports = class FeedData extends Camo.EmbeddedDocument {
} }
_doFetchRSS(guild) { _doFetchRSS(guild) {
const that = this; const feedPromise = FeedReadPromise(this.url).then(articles => this._processLatestArticle(guild, articles));
FeedReadPromise(this.url)
.then(articles => {
if (articles.length > 0 && articles[0].link) {
const latest = normaliseUrl(articles[0].link);
if (!that.cachedLinks.includes(latest)) { feedPromise.catch(err => DiscordUtil.dateDebugError([`Error reading feed ${this.url}`, err]));
that.cache(latest);
const channel = guild.channels.get(that.channelID), return feedPromise;
role = guild.roles.get(that.roleID); }
channel.send((role || "") + formatPost(articles[0])) _processLatestArticle(guild, articles) {
.catch(err => DiscordUtil.dateDebugError(`Error posting in ${channel.id}: ${err.message || err}`)); if (articles.length === 0 || !articles[0].link)
} return;
}
}) const latest = normaliseUrl(articles[0].link);
.catch(err =>
DiscordUtil.dateDebugError(`Error reading feed ${that.url}`, err)); if (this.cachedLinks.includes(latest))
return;
this.cache(latest);
const channel = guild.channels.get(this.channelID),
role = guild.roles.get(this.roleID);
channel.send((role || "") + formatPost(articles[0]))
.catch(err => DiscordUtil.dateDebugError(`Error posting in ${channel.id}: ${err.message || err}`));
} }
}; };

View File

@ -1,4 +1,3 @@
const DiscordUtil = require("../../discord-bot-core").util;
const Core = require("../../discord-bot-core"); const Core = require("../../discord-bot-core");
const FeedData = require("./feed-data.js"); const FeedData = require("./feed-data.js");
@ -10,22 +9,15 @@ module.exports = class GuildData extends Core.BaseGuildData {
} }
cachePastPostedLinks(guild) { cachePastPostedLinks(guild) {
const promises = []; return Promise.all(
this.feeds.map(feed => guild.channels.get(feed.channelID) ? feed.updatePastPostedLinks(guild) : Promise.resolve())
this.feeds.forEach(feed => { );
if (guild.channels.get(feed.channelID))
promises.push(
feed.updatePastPostedLinks(guild)
.catch(err => DiscordUtil.dateError(`Error reading history in channel ${feed.channelID}: ${err.message || err}`)));
});
return Promise.all(promises);
} }
checkFeeds(guild) { checkFeeds(guild) {
this.feeds.forEach(feed => { return Promise.all(
if (guild.channels.get(feed.channelID)) this.feeds.map(feed =>
feed.fetchLatest(guild); guild.channels.get(feed.channelID) ? feed.fetchLatest(guild) : Promise.resolve())
}); );
} }
}; };

View File

@ -1,5 +1,5 @@
{ {
"version": "3.2.0-b2", "version": "3.2.0-b3",
"main": "app/index.js", "main": "app/index.js",
"scripts": { "scripts": {
"postinstall": "cd ./discord-bot-core && npm install", "postinstall": "cd ./discord-bot-core && npm install",