Browse Source

3.2.0-b3

Fix some race conditions with the 3.2 update
shorten
benji7425 3 years ago
parent
commit
784ef72392
  1. 5
      CHANGELOG.md
  2. 2
      app/index.js
  3. 54
      app/models/feed-data.js
  4. 22
      app/models/guild-data.js
  5. 2
      package.json

5
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

2
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() {

54
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);
if (!that.cachedLinks.includes(latest)) {
that.cache(latest);
const channel = guild.channels.get(that.channelID),
role = guild.roles.get(that.roleID);
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));
const feedPromise = FeedReadPromise(this.url).then(articles => this._processLatestArticle(guild, articles));
feedPromise.catch(err => DiscordUtil.dateDebugError([`Error reading feed ${this.url}`, err]));
return feedPromise;
}
_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}`));
}
};

22
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())
);
}
};

2
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",

Loading…
Cancel
Save