catch bot rate limiting error, update tests

This commit is contained in:
Sharon Kennedy
2020-03-18 03:00:43 -04:00
parent 260bbbaf80
commit ef3c44f0d5
7 changed files with 156 additions and 242 deletions

View File

@@ -198,14 +198,15 @@ class OcrccBot {
}
deleteOldDevices() {
return this.client.getDevices().then(data => {
const currentDeviceId = this.client.getDeviceId();
const allDeviceIds = data.devices.map(d => d.device_id);
const oldDevices = allDeviceIds.filter(id => id !== currentDeviceId);
logger.log("info", `DELETING OLD DEVICES: ${oldDevices}`);
this.client
.deleteMultipleDevices(oldDevices)
.catch(err => {
const doDelete = (oldDevices, auth=null, retries=0) => {
if (retries > MAX_RETRIES) {
throw new Error("Exceeded max retries deleting old devices")
}
logger.log("info", `ATTEMPTING TO DELETE OLD DEVICES: ${oldDevices}`);
this.client.deleteMultipleDevices(oldDevices, auth)
.then(() => logger.log("info", "DELETED OLD DEVICES"))
.catch(err => {
if (err['errcode'] === undefined && err['data']) {
const auth = {
session: err.data.session,
type: "m.login.password",
@@ -213,23 +214,67 @@ class OcrccBot {
identifier: { type: "m.id.user", user: process.env.BOT_USERID },
password: process.env.BOT_PASSWORD
};
this.client
.deleteMultipleDevices(oldDevices, auth)
.then(() => logger.log("info", "DELETED OLD DEVICES"))
.catch(err =>
// TODO: catch rate limiting error and retry
// if (err.errcode === "M_LIMIT_EXCEEDED") {
// setTimeout(err.retry_after_ms)
// }
logger.log(
"error",
`ERROR DELETING OLD DEVICES: ${JSON.stringify(err.data)}`
)
);
doDelete(oldDevices, auth)
} else if (err['errcode'] === 'M_LIMIT_EXCEEDED') {
const retryCount = retries + 1
const delay = err['retry_after_ms'] ? err['retry_after_ms'] : retryCount * 1000
logger.log("error", `RATE LIMIT EXCEEDED, RETRYING IN ${delay} MS`);
setTimeout(() => {
doDelete(oldDevices, auth, retryCount)
}, delay)
} else {
logger.log("error", `ERROR DELETING OLD DEVICES ON RETRY ${retries}: ${JSON.stringify(err)}`)
doDelete(oldDevices, auth, retries + 1)
}
})
}
return this.client.getDevices()
.then(data => {
const currentDeviceId = this.client.getDeviceId();
const allDeviceIds = data.devices.map(d => d.device_id);
const oldDevices = allDeviceIds.filter(id => id !== currentDeviceId);
doDelete(oldDevices)
})
.catch(err => {
this.handleBotCrash(undefined, err);
logger.log(
"error",
`ERROR DELETING OLD DEVICES: ${JSON.stringify(err.data)}`
)
});
});
}
// deleteOldDevices() {
// return this.client.getDevices().then(data => {
// const currentDeviceId = this.client.getDeviceId();
// const allDeviceIds = data.devices.map(d => d.device_id);
// const oldDevices = allDeviceIds.filter(id => id !== currentDeviceId);
// logger.log("info", `DELETING OLD DEVICES: ${oldDevices}`);
// this.client
// .deleteMultipleDevices(oldDevices)
// .catch(err => {
// const auth = {
// session: err.data.session,
// type: "m.login.password",
// user: process.env.BOT_USERID,
// identifier: { type: "m.id.user", user: process.env.BOT_USERID },
// password: process.env.BOT_PASSWORD
// };
// this.client
// .deleteMultipleDevices(oldDevices, auth)
// .then(() => logger.log("info", "DELETED OLD DEVICES"))
// .catch(err =>
// logger.log(
// "error",
// `ERROR DELETING OLD DEVICES: ${JSON.stringify(err.data)}`
// )
// );
// });
// });
// }
setMembershipListeners() {
// Automatically accept all room invitations
return this.client.on("RoomMember.membership", (event, member) => {

View File

@@ -35,8 +35,11 @@ import {
import OcrccBot from './bot'
describe('OcrccBot', () => {
const mockAppendFileSync = jest.fn()
fs.appendFileSync = mockAppendFileSync
describe('OcrccBot', () => {
beforeEach(() => {
createClient.mockClear()
mockInitCrypto.mockClear()
@@ -61,6 +64,7 @@ describe('OcrccBot', () => {
mockGetJoinedRoomMembers.mockClear()
mockGetUser.mockClear()
mockSendTextMessage.mockClear()
mockAppendFileSync.mockClear()
})
test('constructor should inititialize matrix client', () => {
@@ -196,11 +200,10 @@ describe('OcrccBot', () => {
})
test('#writeToTranscript should parse event and write to transcript file', () => {
const appendFileSyncSpy = jest.spyOn(fs, 'appendFileSync')
const bot = new OcrccBot()
bot.start()
bot.activeChatrooms['test_room_id'] = { transcriptFile: 'test filepath' }
bot.activeChatrooms['test_room_id'] = { transcriptFile: '__mocks__/test_transcript.txt' }
waitForExpect(() => {
expect(mockStartClient).toHaveBeenCalled()
@@ -216,7 +219,7 @@ describe('OcrccBot', () => {
bot.writeToTranscript(mockEvent)
waitForExpect(() => {
expect(appendFileSyncSpy).toHaveBeenCalledWith('test filepath', 'test_sender [00:00:00]: test content', 'utf8')
expect(mockAppendFileSync).toHaveBeenCalledWith('__mocks__/test_transcript.txt', 'test_sender [00:00:00]: test content', 'utf8')
})
})