diff --git a/CHANGELOG.md b/CHANGELOG.md index 2efb90b..49a130a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/app/index.js b/app/index.js index c58bd1e..21a2779 100644 --- a/app/index.js +++ b/app/index.js @@ -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() { diff --git a/app/models/feed-data.js b/app/models/feed-data.js index e1694d7..243548b 100644 --- a/app/models/feed-data.js +++ b/app/models/feed-data.js @@ -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}`)); } }; diff --git a/app/models/guild-data.js b/app/models/guild-data.js index 6cde8df..1eb4e8d 100644 --- a/app/models/guild-data.js +++ b/app/models/guild-data.js @@ -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()) + ); } }; \ No newline at end of file diff --git a/package.json b/package.json index bbd0191..551dfd8 100644 --- a/package.json +++ b/package.json @@ -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",