From 6cd07ed696bc50c07d13766a3d2d9ef5ff4fe5b7 Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Thu, 28 May 2020 02:45:07 +0300 Subject: [PATCH] =?UTF-8?q?Don'=C3=83t=20run=20message=20cleanup=20queries?= =?UTF-8?q?=20in=20the=20API=20process?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/api/index.ts | 3 +++ backend/src/data/GuildSavedMessages.ts | 7 +++++-- backend/src/globals.ts | 9 +++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 backend/src/globals.ts diff --git a/backend/src/api/index.ts b/backend/src/api/index.ts index 575b8b51..97049892 100644 --- a/backend/src/api/index.ts +++ b/backend/src/api/index.ts @@ -1,5 +1,6 @@ import { connect } from "../data/db"; import path from "path"; +import { setIsAPI } from "../globals"; require("dotenv").config({ path: path.resolve(process.cwd(), "api.env") }); @@ -10,6 +11,8 @@ function errorHandler(err) { process.on("unhandledRejection", errorHandler); +setIsAPI(true); + // Connect to the database before loading the rest of the code (that depend on the database connection) console.log("Connecting to database..."); // tslint:disable-line connect().then(() => { diff --git a/backend/src/data/GuildSavedMessages.ts b/backend/src/data/GuildSavedMessages.ts index ab79fdff..58e5210b 100644 --- a/backend/src/data/GuildSavedMessages.ts +++ b/backend/src/data/GuildSavedMessages.ts @@ -5,6 +5,7 @@ import { QueuedEventEmitter } from "../QueuedEventEmitter"; import { GuildChannel, Message } from "eris"; import moment from "moment-timezone"; import { DAYS, MINUTES } from "../utils"; +import { isAPI } from "../globals"; const CLEANUP_INTERVAL = 5 * MINUTES; @@ -39,8 +40,10 @@ async function cleanup() { setTimeout(cleanup, CLEANUP_INTERVAL); } -// Start first cleanup 30 seconds after startup -setTimeout(cleanup, 30 * 1000); +if (!isAPI()) { + // Start first cleanup 30 seconds after startup + setTimeout(cleanup, 30 * 1000); +} export class GuildSavedMessages extends BaseGuildRepository { private messages: Repository; diff --git a/backend/src/globals.ts b/backend/src/globals.ts new file mode 100644 index 00000000..0c5abcd4 --- /dev/null +++ b/backend/src/globals.ts @@ -0,0 +1,9 @@ +let isAPIValue = false; + +export function isAPI() { + return isAPIValue; +} + +export function setIsAPI(value: boolean) { + isAPIValue = value; +}