Skip to content

Commit ebcd347

Browse files
authored
fix(api): pm2 start script & limit auto restarts (#1040)
* fix(api): limit auto restarts to 10 so persistent errors (e.g. during server boot) don't cause an infinite loop that's difficult for users to see * fix(api): invoke js directly from pm2 instead of npm script npm script wraps it in a child process, so we lose ipc. * fix(api): update api key service test for ensureDir change * chore: increase max_restart threshold to 10s per cycle
1 parent 5455116 commit ebcd347

File tree

3 files changed

+7
-9
lines changed

3 files changed

+7
-9
lines changed

api/ecosystem.config.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
"apps": [
33
{
44
"name": "unraid-api",
5-
"script": "npm",
6-
"args": "start",
5+
"script": "./dist/main.js",
76
"cwd": "/usr/local/unraid-api",
87
"log": "/var/log/unraid-api/unraid-api.log",
98
"exec_mode": "fork",
109
"wait_ready": true,
1110
"listen_timeout": 30000,
11+
"max_restarts": 10,
12+
"min_uptime": 10000,
1213
"ignore_watch": [
1314
"node_modules",
1415
"src",
@@ -17,4 +18,4 @@
1718
]
1819
}
1920
]
20-
}
21+
}

api/src/unraid-api/auth/api-key.service.spec.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ describe('ApiKeyService', () => {
9191
vi.mocked(ensureDir).mockResolvedValue();
9292

9393
apiKeyService = new ApiKeyService();
94-
await apiKeyService.initialize();
9594

9695
vi.spyOn(apiKeyService as any, 'generateApiKey').mockReturnValue('test-api-key');
9796
vi.mock('uuid', () => ({
@@ -113,12 +112,8 @@ describe('ApiKeyService', () => {
113112

114113
describe('initialization', () => {
115114
it('should ensure directory exists', async () => {
116-
vi.mocked(ensureDir).mockResolvedValue();
117115
const service = new ApiKeyService();
118-
119-
await service.initialize();
120-
121-
expect(ensureDir).toHaveBeenCalledWith(mockBasePath);
116+
expect(ensureDirSync).toHaveBeenCalledWith(mockBasePath);
122117
});
123118

124119
it('should return correct paths', async () => {

api/src/unraid-api/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ export async function bootstrapNestServer(): Promise<NestFastifyApplication> {
5454
// See https://pm2.keymetrics.io/docs/usage/signals-clean-restart/
5555
if (process.send) {
5656
process.send('ready');
57+
} else {
58+
console.log('Warning: process.send is unavailable. This will affect IPC communication with PM2.');
5759
}
5860
apiLogger.info('Nest Server is now listening');
5961

0 commit comments

Comments
 (0)