Skip to content

Commit 2b0eedc

Browse files
wip
1 parent 13e702d commit 2b0eedc

4 files changed

Lines changed: 36 additions & 11 deletions

File tree

src/hooks.server.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
11
import type { Handle } from '@sveltejs/kit';
2+
import { env } from '$env/dynamic/private';
3+
import { startInstance } from '$lib/instances';
4+
import fs from 'fs/promises';
5+
6+
(async () => {
7+
try {
8+
if (!env.INSTANCE_FOLDER) {
9+
throw new Error('INSTANCE_FOLDER environment variable is not set.');
10+
}
11+
const entries = await fs.readdir(env.INSTANCE_FOLDER, { withFileTypes: true });
12+
for (const entry of entries) {
13+
if (entry.isDirectory() && /^\d+$/.test(entry.name)) {
14+
await startInstance(Number(entry.name));
15+
}
16+
}
17+
} catch (err) {
18+
console.error('Error reading INSTANCE_FOLDER:', err);
19+
}
20+
})();
221

322
export const handle: Handle = async ({ event, resolve }) => {
423
const regex = /^\/instances\/(\d+)\/(.*)$/;

src/lib/instances.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
1+
import { env } from "$env/dynamic/private";
2+
import { spawn } from "child_process";
3+
14
export const motisInstances = new Map<number, ReturnType<typeof spawn>>();
5+
6+
export const startInstance = async (port: number) => {
7+
const motisProcess = spawn(`${env.MOTIS_FOLDER}/motis/motis`, ['server', '-d', `${env.INSTANCE_FOLDER}/${port}/data`]);
8+
motisProcess.stdout.on('data', (data) => {
9+
console.log(`stdout[${port}]: ${data}`);
10+
});
11+
motisProcess.stderr.on('data', (data) => {
12+
console.log(`stderr[${port}]: ${data}`);
13+
});
14+
motisInstances.set(port, motisProcess);
15+
}

src/routes/+page.server.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ import path from 'path';
66
import { exec, spawn } from 'child_process';
77
import util from 'util';
88
import { env } from '$env/dynamic/private';
9+
import { motisInstances, startInstance } from '$lib/instances';
910

1011
const execPromise = util.promisify(exec);
1112

12-
const motisInstances = new Map<number, ReturnType<typeof spawn>>();
13-
1413
export const load: PageServerLoad = async () => {
1514
return { ports: motisInstances.keys().toArray() };
1615
}
@@ -96,14 +95,7 @@ timetable:
9695

9796
await execPromise(`${env.MOTIS_FOLDER}/motis/motis import -c ${instanceFolder}/config.yml -d ${instanceFolder}/data`);
9897

99-
const motisProcess = spawn(`${env.MOTIS_FOLDER}/motis/motis`, ['server', '-d', `${instanceFolder}/data`]);
100-
motisProcess.stdout.on('data', (data) => {
101-
console.log(`stdout[${port}]: ${data}`);
102-
});
103-
motisProcess.stderr.on('data', (data) => {
104-
console.log(`stderr[${port}]: ${data}`);
105-
});
106-
motisInstances.set(port, motisProcess);
98+
startInstance(port);
10799

108100
return { success: true, port }
109101
}

src/routes/+page.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<Card.Description>These instances are already running.</Card.Description>
2525
</Card.Header>
2626
<Card.Content>
27-
<ul class="list-inside list-disc">
27+
<ul class="flex list-inside list-disc flex-col gap-2">
2828
{#each data.ports as port}
2929
<form method="post" action="?/stop">
3030
<li>

0 commit comments

Comments
 (0)