| 1 | import { execFile } from 'node:child_process' |
| 2 | import { Injectable, Logger } from '@nestjs/common' |
| 3 | import { AppException, ResponseCode } from '@yikart/common' |
| 4 | |
| 5 | @Injectable() |
| 6 | export class ConfigRestartService { |
| 7 | private readonly logger = new Logger(ConfigRestartService.name) |
| 8 | |
| 9 | restart(): void { |
| 10 | const pm2ProcessId = process.env['pm_id'] |
| 11 | if (!pm2ProcessId) { |
| 12 | throw new AppException(ResponseCode.ConfigEditorPm2Unavailable) |
| 13 | } |
| 14 | |
| 15 | try { |
| 16 | const child = execFile('pm2', ['restart', pm2ProcessId], (error) => { |
| 17 | if (error) |
| 18 | this.logger.error(error) |
| 19 | }) |
| 20 | child.unref() |
| 21 | } |
| 22 | catch (error) { |
| 23 | throw new AppException( |
| 24 | ResponseCode.ConfigEditorRestartFailed, |
| 25 | error instanceof Error ? error.message : String(error), |
| 26 | ) |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 |