Skip to content

Commit

Permalink
fix tests hanging again
Browse files Browse the repository at this point in the history
  • Loading branch information
MrBrax committed Jun 22, 2023
1 parent b337751 commit bb6f09b
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 43 deletions.
2 changes: 1 addition & 1 deletion server/src/Controllers/Channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { TwitchVODChapterJSON } from "../Storage/JSON";
import { Job } from "../Core/Job";
import { Exporter, GetExporter } from "./Exporter";
import { ExporterOptions } from "@common/Exporter";
import { KickChannel } from "Core/Providers/Kick/KickChannel";
import { KickChannel } from "../Core/Providers/Kick/KickChannel";

export async function ListChannels(req: express.Request, res: express.Response): Promise<void> {

Expand Down
2 changes: 1 addition & 1 deletion server/src/Controllers/KickAPI.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { KickChannel, KickUser, KickChannelVideo, KickChannelLivestream, KickChannelLivestreamResponse } from "@common/KickAPI/Kick";
import { GetUser } from "Providers/Kick";
import { GetUser } from "../Providers/Kick";
import express from "express";
import { ApiErrorResponse } from "@common/Api/Api";

Expand Down
90 changes: 52 additions & 38 deletions server/src/Core/LiveStreamDVR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ export class LiveStreamDVR {
return false;
}

public static shutdown(reason: string) {
public static shutdown(reason: string, doNotActuallyShutdown?: boolean): void {
this.shutting_down = true;
console.log(chalk.red(`[${new Date().toISOString()}] Shutting down (${reason})...`));

Expand All @@ -445,17 +445,23 @@ export class LiveStreamDVR {
});
}

let timeout: NodeJS.Timeout | undefined = undefined;
// introduced in node 18.2
if ("closeAllConnections" in this.server) {
console.log("closeAllConnections is available, using it");
this.server.closeAllConnections();
} else {
// bad workaround
timeout = xTimeout(() => {
console.log(chalk.red("Force exiting server, 10 seconds have passed without close event."));
process.exit(1);
}, 10000);
if (this.server) {

// let timeout: NodeJS.Timeout | undefined = undefined;
// introduced in node 18.2
if ("closeAllConnections" in this.server) {
console.log("closeAllConnections is available, using it");
this.server.closeAllConnections();
} else {
console.error("closeAllConnections is not available");
// bad workaround
/*
timeout = xTimeout(() => {
console.log(chalk.red("Force exiting server, 10 seconds have passed without close event."));
if (!doNotActuallyShutdown) process.exit(1);
}, 10000);*/
}

}

if (this.debugConnectionInterval) {
Expand All @@ -465,32 +471,40 @@ export class LiveStreamDVR {
clearAllTimeoutsAndIntervals();

// this will not be called until all connections are closed
this.server.close(async (error) => {
if (error) {
console.log(chalk.red(error));
} else {
console.log(chalk.red("express server is now down"));
}
if (this.websocketServer) this.websocketServer.close();
Scheduler.removeAllJobs();
for (const c of LiveStreamDVR.getInstance().channels) {
await c.stopWatching();
}
for (const v of LiveStreamDVR.getInstance().vods) {
await v.stopWatching();
}
for (const j of Job.jobs) {
await j.kill();
}
ClientBroker.wss = undefined;
Config.getInstance().stopWatchingConfig();
TwitchHelper.removeAllEventWebsockets();
if (timeout !== undefined) clearTimeout(timeout);
if (LiveStreamDVR.getInstance().diskSpaceInterval) clearInterval(LiveStreamDVR.getInstance().diskSpaceInterval);
if (fs.existsSync(path.join(BaseConfigCacheFolder.cache, "is_running"))) fs.unlinkSync(path.join(BaseConfigCacheFolder.cache, "is_running"));
console.log(chalk.red("Finished tasks, bye bye."));
// process.exit(0);
});
if (this.server) {
this.server.close(async (error) => {
if (error) {
console.log(chalk.red(error));
} else {
console.log(chalk.red("express server is now down"));
}
await this.shutdownRest();
});
} else {
this.shutdownRest();
}
}

private static async shutdownRest(): Promise<void> {
if (this.websocketServer) this.websocketServer.close();
Scheduler.removeAllJobs();
for (const c of LiveStreamDVR.getInstance().channels) {
await c.stopWatching();
}
for (const v of LiveStreamDVR.getInstance().vods) {
await v.stopWatching();
}
for (const j of Job.jobs) {
await j.kill();
}
ClientBroker.wss = undefined;
Config.getInstance().stopWatchingConfig();
TwitchHelper.removeAllEventWebsockets();
// if (timeout !== undefined) clearTimeout(timeout);
if (LiveStreamDVR.getInstance().diskSpaceInterval) clearInterval(LiveStreamDVR.getInstance().diskSpaceInterval);
if (fs.existsSync(path.join(BaseConfigCacheFolder.cache, "is_running"))) fs.unlinkSync(path.join(BaseConfigCacheFolder.cache, "is_running"));
console.log(chalk.red("Finished tasks, bye bye."));
// process.exit(0);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion server/src/Core/Scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class Scheduler {
if (this.hasJob(name)) {
this.removeJob(name);
}
const job = new cron.CronJob(cronTime, callback);
const job = new cron.CronJob(cronTime, callback, undefined, false);
this.jobs[name] = job;
job.start();
Log.logAdvanced(Log.Level.INFO, "scheduler.schedule", `Scheduled job '${name}' with cronTime '${cronTime}'`);
Expand Down
1 change: 1 addition & 0 deletions server/tests/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ beforeAll(async () => {

afterAll(() => {
Config.destroyInstance();
LiveStreamDVR.shutdown("test", true);
app = undefined;
spy1?.mockRestore();
jest.restoreAllMocks();
Expand Down
7 changes: 5 additions & 2 deletions server/tests/scheduler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ jest.mock("../src/Controllers/Cron", () => {
};
});

describe("Scheduler", () => {

beforeAll(() => {
Config.getInstance().config = {};
Scheduler.defaultJobs();
});

describe("Scheduler", () => {
it("should run the function inside the schedule if the config is set to true", () => {
const config = Config.getInstance();
config.config = {};
Expand Down

0 comments on commit bb6f09b

Please sign in to comment.