Skip to content

Commit ab66bee

Browse files
Refactor schedule generation to use platform-specific path and add timeout for MatchMaker process
1 parent 5c590bb commit ab66bee

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/commands/generate_schedule.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
import { spawn } from "child_process";
77
import fs from "fs/promises";
88
import fsSync from "fs";
9+
import path from "path";
910
import { postSchedule } from "../lib/googleSheet";
1011
import fetch from "node-fetch";
1112
import logger from "../config/logger";
@@ -68,7 +69,11 @@ export const execute = async (interaction: ChatInputCommandInteraction) => {
6869
);
6970

7071
// Call MatchMaker by Idle Loop to generate a schedule
71-
const matchMaker = spawn("./MatchMaker", [
72+
const matchMakerPath = path.join(
73+
process.cwd(),
74+
`MatchMaker${process.platform === "win32" ? ".exe" : ""}`
75+
);
76+
const matchMaker = spawn(matchMakerPath, [
7277
"-t",
7378
String(participantCount),
7479
"-r",
@@ -80,20 +85,28 @@ export const execute = async (interaction: ChatInputCommandInteraction) => {
8085
]);
8186

8287
// Pipe the output of MatchMaker to a file
83-
const out = fsSync.createWriteStream("./schedule.txt");
84-
matchMaker.stdout.pipe(out);
88+
const schedulePath = path.join(process.cwd(), "schedule.txt");
89+
const out = fsSync.createWriteStream(schedulePath);
90+
matchMaker.stdout.pipe(out as any);
8591

86-
// Wait for MatchMaker to finish
92+
// Wait for MatchMaker to finish or timeout after 15 seconds
8793
await new Promise((resolve) => {
88-
matchMaker.once("close", resolve);
94+
const timeout = setTimeout(() => {
95+
resolve(undefined);
96+
}, 15000);
97+
98+
matchMaker.once("close", () => {
99+
clearTimeout(timeout);
100+
resolve(undefined);
101+
});
89102
});
90103

91104
interaction.editReply(
92105
`Generated a schedule for ${participantCount} participants playing ${rounds} rounds. Saving...`
93106
);
94107

95108
// Handle the output of MatchMaker
96-
const schedule = await fs.readFile("./schedule.txt", "utf-8");
109+
const schedule = await fs.readFile(schedulePath, "utf-8");
97110
const lines = schedule.split("\n");
98111

99112
let i = 0;
@@ -118,7 +131,7 @@ export const execute = async (interaction: ChatInputCommandInteraction) => {
118131
}
119132

120133
// Delete the schedule file
121-
await fs.unlink("./schedule.txt");
134+
await fs.unlink(schedulePath);
122135

123136
// Create list of discord user ids and display names
124137
const playerIds = players.map((player) => player.id);

0 commit comments

Comments
 (0)