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
## v3.2.0-b3
### Fixed
- Fixed some race condtions with the 3.2 update
## v3.2.0-b2
### Fixed
- Fixed upgrader script not including channel ID in new database

View File

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

View File

@ -5,6 +5,7 @@ const Dns = require("dns"); //for host resolution checking
const Url = require("url"); //for url parsing
const { promisify } = require("util");
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
module.exports = class FeedData extends Camo.EmbeddedDocument {
@ -50,12 +51,11 @@ module.exports = class FeedData extends Camo.EmbeddedDocument {
}
fetchLatest(guild) {
Dns.resolve(Url.parse(this.url).host || "", err => {
if (err)
DiscordUtil.dateDebugError("Connection Error: Can't resolve host", err.message || err);
else
this._doFetchRSS(guild);
});
const dnsPromise = DnsResolvePromise(Url.parse(this.url).host).then(() => this._doFetchRSS(guild));
dnsPromise.catch(err => DiscordUtil.dateError("Connection error: Can't resolve host", err.message || err));
return dnsPromise;
}
toString() {
@ -64,25 +64,29 @@ module.exports = class FeedData extends Camo.EmbeddedDocument {
}
_doFetchRSS(guild) {
const that = this;
FeedReadPromise(this.url)
.then(articles => {
if (articles.length > 0 && articles[0].link) {
const latest = normaliseUrl(articles[0].link);
const feedPromise = FeedReadPromise(this.url).then(articles => this._processLatestArticle(guild, articles));
if (!that.cachedLinks.includes(latest)) {
that.cache(latest);
feedPromise.catch(err => DiscordUtil.dateDebugError([`Error reading feed ${this.url}`, err]));
const channel = guild.channels.get(that.channelID),
role = guild.roles.get(that.roleID);
return feedPromise;
}
channel.send((role || "") + formatPost(articles[0]))
.catch(err => DiscordUtil.dateDebugError(`Error posting in ${channel.id}: ${err.message || err}`));
}
}
})
.catch(err =>
DiscordUtil.dateDebugError(`Error reading feed ${that.url}`, err));
_processLatestArticle(guild, articles) {
if (articles.length === 0 || !articles[0].link)
return;
const latest = normaliseUrl(articles[0].link);
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 FeedData = require("./feed-data.js");
@ -10,22 +9,15 @@ module.exports = class GuildData extends Core.BaseGuildData {
}
cachePastPostedLinks(guild) {
const promises = [];
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);
return Promise.all(
this.feeds.map(feed => guild.channels.get(feed.channelID) ? feed.updatePastPostedLinks(guild) : Promise.resolve())
);
}
checkFeeds(guild) {
this.feeds.forEach(feed => {
if (guild.channels.get(feed.channelID))
feed.fetchLatest(guild);
});
return Promise.all(
this.feeds.map(feed =>
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",
"scripts": {
"postinstall": "cd ./discord-bot-core && npm install",