From 6522eadf195c38f9b4470f3e0f42b98e820536ff Mon Sep 17 00:00:00 2001 From: Matthew R Date: Tue, 16 Jun 2020 17:32:01 -0400 Subject: [PATCH] fixes to try to fix memory leak --- src/class/Client.ts | 2 +- src/intervals/departmentPager.ts | 70 +++++++++++++++----------------- src/intervals/fetchMembers.ts | 2 +- 3 files changed, 34 insertions(+), 40 deletions(-) diff --git a/src/class/Client.ts b/src/class/Client.ts index 2d25fb8..2845927 100644 --- a/src/class/Client.ts +++ b/src/class/Client.ts @@ -28,7 +28,7 @@ export default class Client extends eris.Client { } public async loadDatabase() { - await mongoose.connect(this.config.mongoDB, { useNewUrlParser: true, useUnifiedTopology: true }); + await mongoose.connect(this.config.mongoDB, { useNewUrlParser: true, useUnifiedTopology: true, poolSize: 50 }); } public loadPlugins() { diff --git a/src/intervals/departmentPager.ts b/src/intervals/departmentPager.ts index cc24275..a1c82cc 100644 --- a/src/intervals/departmentPager.ts +++ b/src/intervals/departmentPager.ts @@ -1,16 +1,15 @@ /* eslint-disable no-await-in-loop */ import { Client } from '../class'; -import { PagerNumberInterface } from '../models'; let interval: NodeJS.Timeout; async function setupDepartmentCodes(client: Client): Promise { - const directorPagers = await client.db.PagerNumber.findOne({ num: '00' }); - const supervisorPagers = await client.db.PagerNumber.findOne({ num: '01' }); - const technicianPagers = await client.db.PagerNumber.findOne({ num: '10' }); - const moderatorPagers = await client.db.PagerNumber.findOne({ num: '20' }); - const coreTeamPagers = await client.db.PagerNumber.findOne({ num: '21' }); - const associatePagers = await client.db.PagerNumber.findOne({ num: '22' }); + const directorPagers = await client.db.PagerNumber.findOne({ num: '00' }).lean().exec(); + const supervisorPagers = await client.db.PagerNumber.findOne({ num: '01' }).lean().exec(); + const technicianPagers = await client.db.PagerNumber.findOne({ num: '10' }).lean().exec(); + const moderatorPagers = await client.db.PagerNumber.findOne({ num: '20' }).lean().exec(); + const coreTeamPagers = await client.db.PagerNumber.findOne({ num: '21' }).lean().exec(); + const associatePagers = await client.db.PagerNumber.findOne({ num: '22' }).lean().exec(); if (!directorPagers) { const setup = new client.db.PagerNumber({ @@ -80,8 +79,8 @@ export default function departmentPager(client: Client): NodeJS.Timeout { // const takenPagers = new Set(); - members.forEach(async (member) => { - let pager = await client.db.PagerNumber.findOne({ individualAssignID: member.id }); + for (const member of members.values()) { + let pager = await client.db.PagerNumber.findOne({ individualAssignID: member.id }).lean().exec(); // Directors if (!pager && member.roles.includes('662163685439045632')) { let randomPagerNumber: string; @@ -106,9 +105,8 @@ export default function departmentPager(client: Client): NodeJS.Timeout { client.getDMChannel(member.id).then((chan) => { chan.createMessage(`__**Pager Number Creation**__\nYour individual pager number has been automatically created. Your number (PN) is ${randomPagerNumber}.`); }); - } - // Supervisors - if (!pager && member.roles.includes('701454855952138300')) { + } else if (!pager && member.roles.includes('701454855952138300')) { + // Supervisors let randomPagerNumber: string; let status = true; // eslint-disable-next-line no-constant-condition @@ -130,9 +128,8 @@ export default function departmentPager(client: Client): NodeJS.Timeout { client.getDMChannel(member.id).then((chan) => { chan.createMessage(`__**Pager Number Creation**__\nYour individual pager number has been automatically created. Your number (PN) is ${randomPagerNumber}.`); }); - } - // Technicians - if (!pager && member.roles.includes('701454780828221450')) { + } else if (!pager && member.roles.includes('701454780828221450')) { + // Technicians let randomPagerNumber: string; let status = true; // eslint-disable-next-line no-constant-condition @@ -155,9 +152,8 @@ export default function departmentPager(client: Client): NodeJS.Timeout { client.getDMChannel(member.id).then((chan) => { chan.createMessage(`__**Pager Number Creation**__\nYour individual pager number has been automatically created. Your number (PN) is ${randomPagerNumber}.`); }); - } - // Moderators - if (!pager && member.roles.includes('455972169449734144')) { + } else if (!pager && member.roles.includes('455972169449734144')) { + // Moderators let randomPagerNumber: string; let status = true; // eslint-disable-next-line no-constant-condition @@ -180,9 +176,8 @@ export default function departmentPager(client: Client): NodeJS.Timeout { client.getDMChannel(member.id).then((chan) => { chan.createMessage(`__**Pager Number Creation**__\nYour individual pager number has been automatically created. Your number (PN) is ${randomPagerNumber}.`); }); - } - // Core Team - if (!pager && member.roles.includes('453689940140883988')) { + } else if (!pager && member.roles.includes('453689940140883988')) { + // Core Team let randomPagerNumber: string; let status = true; // eslint-disable-next-line no-constant-condition @@ -205,9 +200,8 @@ export default function departmentPager(client: Client): NodeJS.Timeout { client.getDMChannel(member.id).then((chan) => { chan.createMessage(`__**Pager Number Creation**__\nYour individual pager number has been automatically created. Your number (PN) is ${randomPagerNumber}.`); }); - } - // Associates - if (!pager && member.roles.includes('701481967149121627')) { + } else if (!pager && member.roles.includes('701481967149121627')) { + // Associates let randomPagerNumber: string; let status = true; // eslint-disable-next-line no-constant-condition @@ -231,11 +225,11 @@ export default function departmentPager(client: Client): NodeJS.Timeout { chan.createMessage(`__**Pager Number Creation**__\nYour individual pager number has been automatically created. Your number (PN) is ${randomPagerNumber}.`); }); } - }); + } // Associates const associatePagers = await client.db.PagerNumber.findOne({ num: '22' }); - members.forEach(async (member) => { + for (const member of members.values()) { if (member.roles.includes('701481967149121627') && !associatePagers.discordIDs.includes(member.id)) { await associatePagers.updateOne({ $addToSet: { discordIDs: member.id } }); const acknowledgement = resolveStaffInformation(member.id); @@ -246,10 +240,10 @@ export default function departmentPager(client: Client): NodeJS.Timeout { const acknowledgement = resolveStaffInformation(member.id); if (acknowledgement?.emailAddress) await associatePagers.updateOne({ $pull: { emailAddresses: acknowledgement.emailAddress } }); } - }); + } // Core Team const coreTeamPagers = await client.db.PagerNumber.findOne({ num: '21' }); - members.forEach(async (member) => { + for (const member of members.values()) { if (member.roles.includes('453689940140883988') && !coreTeamPagers.discordIDs.includes(member.id)) { await coreTeamPagers.updateOne({ $addToSet: { discordIDs: member.id } }); const acknowledgement = resolveStaffInformation(member.id); @@ -260,10 +254,10 @@ export default function departmentPager(client: Client): NodeJS.Timeout { const acknowledgement = resolveStaffInformation(member.id); if (acknowledgement?.emailAddress) await coreTeamPagers.updateOne({ $pull: { emailAddresses: acknowledgement.emailAddress } }); } - }); + } // Moderator const moderatorPagers = await client.db.PagerNumber.findOne({ num: '20' }); - members.forEach(async (member) => { + for (const member of members.values()) { if (member.roles.includes('455972169449734144') && !moderatorPagers.discordIDs.includes(member.id)) { await moderatorPagers.updateOne({ $addToSet: { discordIDs: member.id } }); const acknowledgement = resolveStaffInformation(member.id); @@ -274,10 +268,10 @@ export default function departmentPager(client: Client): NodeJS.Timeout { const acknowledgement = resolveStaffInformation(member.id); if (acknowledgement?.emailAddress) await moderatorPagers.updateOne({ $pull: { emailAddresses: acknowledgement.emailAddress } }); } - }); + } // Technician const technicianPagers = await client.db.PagerNumber.findOne({ num: '10' }); - members.forEach(async (member) => { + for (const member of members.values()) { if (member.roles.includes('701454780828221450') && !technicianPagers.discordIDs.includes(member.id)) { await technicianPagers.updateOne({ $addToSet: { discordIDs: member.id } }); const acknowledgement = resolveStaffInformation(member.id); @@ -288,10 +282,10 @@ export default function departmentPager(client: Client): NodeJS.Timeout { const acknowledgement = resolveStaffInformation(member.id); if (acknowledgement?.emailAddress) await technicianPagers.updateOne({ $pull: { emailAddresses: acknowledgement.emailAddress } }); } - }); + } // Supervisor const supervisorPagers = await client.db.PagerNumber.findOne({ num: '01' }); - members.forEach(async (member) => { + for (const member of members.values()) { if (member.roles.includes('701454855952138300') && !supervisorPagers.discordIDs.includes(member.id)) { await supervisorPagers.updateOne({ $addToSet: { discordIDs: member.id } }); const acknowledgement = resolveStaffInformation(member.id); @@ -302,10 +296,10 @@ export default function departmentPager(client: Client): NodeJS.Timeout { const acknowledgement = resolveStaffInformation(member.id); if (acknowledgement?.emailAddress) await supervisorPagers.updateOne({ $pull: { emailAddresses: acknowledgement.emailAddress } }); } - }); + } // Board of Directors const directorPagers = await client.db.PagerNumber.findOne({ num: '00' }); - members.forEach(async (member) => { + for (const member of members.values()) { if (member.roles.includes('662163685439045632') && !directorPagers.discordIDs.includes(member.id)) { await directorPagers.updateOne({ $addToSet: { discordIDs: member.id } }); const acknowledgement = resolveStaffInformation(member.id); @@ -316,7 +310,7 @@ export default function departmentPager(client: Client): NodeJS.Timeout { const acknowledgement = resolveStaffInformation(member.id); if (acknowledgement?.emailAddress) await directorPagers.updateOne({ $pull: { emailAddresses: acknowledgement.emailAddress } }); } - }); - }, 10000); + } + }, 1200000); return interval; } diff --git a/src/intervals/fetchMembers.ts b/src/intervals/fetchMembers.ts index fdfd1f1..54268e5 100644 --- a/src/intervals/fetchMembers.ts +++ b/src/intervals/fetchMembers.ts @@ -5,7 +5,7 @@ let interval: NodeJS.Timeout; export default async function fetchMembers(client: Client): Promise { await client.guilds.get(client.config.guildID)?.fetchAllMembers(); interval = setInterval(async () => { - await client.guilds.get(client.config.guildID).fetchAllMembers(); + // await client.guilds.get(client.config.guildID).fetchAllMembers(); }, 1800000); return interval; }