Skip to content

Commit

Permalink
Fix server start failing after install in a hacky way
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Feb 3, 2021
1 parent c946bab commit 0331925
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
21 changes: 20 additions & 1 deletion bin/generate-mod-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,26 @@ async function run(command: string, configPath: string) {
if (!modLoader.areModsInstalled()) {
await modLoader.installMods();
}
await modLoader.startServer();

// Multiple attempts for starting the server
// Needed because for some reason this can fail when happening right after Forge installation
let attempts = 0;
let lastError: Error;
do {
try {
await modLoader.startServer();
lastError = undefined;
break;
} catch (error) {
lastError = error;
process.stdout.write('Failed to start server, retrying after delay...\n');
await new Promise((resolve) => setTimeout(resolve, 5000));
}
} while (attempts++ < 5);
if (lastError) {
throw lastError;
}

await modLoader.copyRegistries(join(process.cwd(), 'registries'));
await modLoader.extractMinecraftAssets();
await modLoader.extractModsAssets();
Expand Down
13 changes: 12 additions & 1 deletion lib/modloader/ModLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ export class ModLoader {
await new Promise((resolve, reject) => exec(
`cd ${this.path} && java -jar forge-installer.jar --installServer`).on('exit', resolve));

// Wait a bit, because otherwise some files don't exist yet (while they should...)
process.stdout.write('Wait a bit after Forge installation...\n');
await new Promise((resolve) => setTimeout(resolve, 10000));

// Cleanup
process.stdout.write('Cleaning up...\n');
await fs.promises.unlink(installerFile);
Expand Down Expand Up @@ -119,11 +123,18 @@ export class ModLoader {
public async startServer() {
// Start the Forge server
process.stdout.write('Starting server...\n');

const proc = exec(`cd ${this.path} && java -jar forge-*.jar nogui`);
// Ignore stdout: proc.stdout.pipe(process.stdout);
proc.stderr.pipe(process.stderr);
const onDone = new Promise((resolve, reject) => {
proc.addListener('exit', resolve);
proc.addListener('exit', (code) => {
if (code === 0) {
resolve();
} else {
reject('Server closed with non-zero exit code');
}
});
proc.addListener('error', reject);
});

Expand Down

0 comments on commit 0331925

Please sign in to comment.