refactor(server): worker env (#13160)

This commit is contained in:
Jason Rasmussen 2024-10-03 15:28:36 -04:00 committed by GitHub
parent 892a35acb5
commit db1623f43f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 63 additions and 58 deletions

View file

@ -1,33 +0,0 @@
#!/usr/bin/env node
const port = Number(process.env.IMMICH_PORT) || 3001;
const controller = new AbortController();
const main = async () => {
if (!process.env.IMMICH_WORKERS_INCLUDE?.includes('api')) {
process.exit();
}
const timeout = setTimeout(() => controller.abort(), 2000);
try {
const response = await fetch(`http://localhost:${port}/api/server-info/ping`, {
signal: controller.signal,
});
if (response.ok) {
const body = await response.json();
if (body.res === 'pong') {
process.exit();
}
}
} catch (error) {
if (error instanceof DOMException === false) {
console.error(error);
}
} finally {
clearTimeout(timeout);
}
process.exit(1);
};
void main();

View file

@ -1,49 +0,0 @@
import { getWorkers } from 'src/utils/workers';
describe('getWorkers', () => {
beforeEach(() => {
process.env.IMMICH_WORKERS_INCLUDE = '';
process.env.IMMICH_WORKERS_EXCLUDE = '';
});
it('should return default workers', () => {
expect(getWorkers()).toEqual(['api', 'microservices']);
});
it('should return included workers', () => {
process.env.IMMICH_WORKERS_INCLUDE = 'api';
expect(getWorkers()).toEqual(['api']);
});
it('should excluded workers from defaults', () => {
process.env.IMMICH_WORKERS_EXCLUDE = 'api';
expect(getWorkers()).toEqual(['microservices']);
});
it('should exclude workers from include list', () => {
process.env.IMMICH_WORKERS_INCLUDE = 'api,microservices,randomservice';
process.env.IMMICH_WORKERS_EXCLUDE = 'randomservice,microservices';
expect(getWorkers()).toEqual(['api']);
});
it('should remove whitespace from included workers before parsing', () => {
process.env.IMMICH_WORKERS_INCLUDE = 'api, microservices';
expect(getWorkers()).toEqual(['api', 'microservices']);
});
it('should remove whitespace from excluded workers before parsing', () => {
process.env.IMMICH_WORKERS_EXCLUDE = 'api, microservices';
expect(getWorkers()).toEqual([]);
});
it('should remove whitespace from included and excluded workers before parsing', () => {
process.env.IMMICH_WORKERS_INCLUDE = 'api, microservices, randomservice,randomservice2';
process.env.IMMICH_WORKERS_EXCLUDE = 'randomservice,microservices, randomservice2';
expect(getWorkers()).toEqual(['api']);
});
it('should throw error for invalid workers', () => {
process.env.IMMICH_WORKERS_INCLUDE = 'api,microservices,randomservice';
expect(getWorkers).toThrowError('Invalid worker(s) found: api,microservices,randomservice');
});
});

View file

@ -1,21 +0,0 @@
const WORKER_TYPES = new Set(['api', 'microservices']);
export const getWorkers = () => {
let workers = ['api', 'microservices'];
const includedWorkers = process.env.IMMICH_WORKERS_INCLUDE?.replaceAll(/\s/g, '');
const excludedWorkers = process.env.IMMICH_WORKERS_EXCLUDE?.replaceAll(/\s/g, '');
if (includedWorkers) {
workers = includedWorkers.split(',');
}
if (excludedWorkers) {
workers = workers.filter((worker) => !excludedWorkers.split(',').includes(worker));
}
if (workers.some((worker) => !WORKER_TYPES.has(worker))) {
throw new Error(`Invalid worker(s) found: ${workers}`);
}
return workers;
};