Fix bug in `fetchPendingKudos` logic which was preventing it from processing new kudos entries until a full page was available

This commit is contained in:
Pedro Pombeiro 2018-04-10 18:34:38 +02:00
parent 029dc531f8
commit 51ff592e08
No known key found for this signature in database
GPG Key ID: A65DEB11E4BBC647
1 changed files with 8 additions and 8 deletions

View File

@ -51,7 +51,7 @@ async function processKudosChannelUpdates (robot) {
isCheckingUpdates = true isCheckingUpdates = true
try { try {
const mc = robot['memcache'] const mc = robot['memcache']
const data = await getSavedData(mc) const data = await getSavedData(robot, mc)
await fetchPendingKudos(robot, data) await fetchPendingKudos(robot, data)
@ -67,13 +67,14 @@ async function processKudosChannelUpdates (robot) {
} }
} }
async function getSavedData (mc) { async function getSavedData (robot, mc) {
const json = await mc.get(kudosBotDataMemcachedKey) const json = await mc.get(kudosBotDataMemcachedKey)
if (json.value) { if (json.value) {
const data = JSON.parse(json.value) const data = JSON.parse(json.value)
if (!data.hasOwnProperty('lastMessageTimestamp') || !data.hasOwnProperty('userPendingPayouts')) { if (!data.hasOwnProperty('lastMessageTimestamp') || !data.hasOwnProperty('userPendingPayouts')) {
throw new Error(`${botName} - Invalid cached data`) throw new Error(`${botName} - Invalid cached data`)
} }
robot.log.debug(`${botName} - Loaded existing state: lastMessageTimestamp=${new Date(data.lastMessageTimestamp * 1000).toISOString()}`)
return data return data
} }
@ -100,11 +101,6 @@ async function fetchPendingKudos (robot, data) {
while (true) { while (true) {
const historyPayload = await slackWeb.channels.history(kudosChannelId, { oldest: data.lastMessageTimestamp }) const historyPayload = await slackWeb.channels.history(kudosChannelId, { oldest: data.lastMessageTimestamp })
if (historyPayload.ok) { if (historyPayload.ok) {
if (!historyPayload.has_more && newMessagesProcessed === 0) {
robot.log.debug(`${botName} - No new entries in ${kudosChannelId} channel history`)
break
}
for (const message of historyPayload.messages.reverse()) { for (const message of historyPayload.messages.reverse()) {
const messageTs = parseFloat(message.ts) const messageTs = parseFloat(message.ts)
if (messageTs >= thresholdTs) { if (messageTs >= thresholdTs) {
@ -154,7 +150,11 @@ async function fetchPendingKudos (robot, data) {
} }
if (!historyPayload.has_more) { if (!historyPayload.has_more) {
if (newMessagesProcessed === 0) {
robot.log.debug(`${botName} - No new entries in ${kudosChannelId} channel history`)
} else {
robot.log.debug(`${botName} - Reached end of ${kudosChannelId} channel history`) robot.log.debug(`${botName} - Reached end of ${kudosChannelId} channel history`)
}
break break
} }
} else { } else {