fix(server): refine error handling and PM2 connection logic
Deploy to Raspberry Pi / deploy (push) Canceled after 1m16s

- Reduced the maximum restarts for PM2 processes from 20 to 8 and added a restart delay of 2000ms for better stability.
- Improved error handling during server startup and PM2 connection, ensuring clearer logging and graceful exits on failure.
- Introduced a utility function to filter out PM2 metadata from environment variables, preventing conflicts during process management.
This commit is contained in:
2026-08-23 08:45:26 +07:00
parent 69312c9080
commit dd98421b08
3 changed files with 68 additions and 20 deletions
+21 -18
View File
@@ -64,13 +64,6 @@ try {
process.exit(1);
}
try {
await connectPm2();
} catch (err) {
log.error({ err }, "failed to connect to PM2 — is pm2 installed?");
process.exit(1);
}
async function applyDesiredState() {
const state = readControlState();
await ensureHttp({
@@ -98,8 +91,6 @@ async function applyDesiredState() {
);
}
await applyDesiredState();
const server = fastify({ loggerInstance: log });
await server.register(cookie);
await server.register(jwt, {
@@ -484,12 +475,24 @@ process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);
const port = Number(process.env.JFLOW_CONTROL_PORT ?? process.env.PORT ?? 8600);
server
.listen({ host: "0.0.0.0", port })
.then(() => {
log.info(`Control is running on port ${port}`);
})
.catch((err) => {
log.error({ err }, "failed to start control");
process.exit(1);
});
try {
await server.listen({ host: "0.0.0.0", port });
log.info(`Control is running on port ${port}`);
} catch (err) {
log.error({ err }, "failed to start control");
process.exit(1);
}
try {
await connectPm2();
} catch (err) {
log.error({ err }, "failed to connect to PM2 — is pm2 installed?");
process.exit(1);
}
try {
await applyDesiredState();
} catch (err) {
log.error({ err }, "failed to apply desired state");
process.exit(1);
}