From 097415f6b8dbbe5eb13645942554e673dafad8f4 Mon Sep 17 00:00:00 2001 From: Eli Bosley Date: Wed, 29 Jan 2025 11:39:50 -0500 Subject: [PATCH] feat: add logrotate cron again --- api/src/unraid-api/cron/cron.module.ts | 6 +++-- api/src/unraid-api/cron/log-rotate.service.ts | 24 +++++++++++++++++++ .../unraid-file-modifier.spec.ts | 2 +- 3 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 api/src/unraid-api/cron/log-rotate.service.ts diff --git a/api/src/unraid-api/cron/cron.module.ts b/api/src/unraid-api/cron/cron.module.ts index e1b2ed984..39a61d18f 100644 --- a/api/src/unraid-api/cron/cron.module.ts +++ b/api/src/unraid-api/cron/cron.module.ts @@ -1,9 +1,11 @@ -import { WriteFlashFileService } from '@app/unraid-api/cron/write-flash-file.service'; import { Module } from '@nestjs/common'; import { ScheduleModule } from '@nestjs/schedule'; +import { WriteFlashFileService } from '@app/unraid-api/cron/write-flash-file.service'; +import { LogRotateService } from '@app/unraid-api/cron/log-rotate.service'; + @Module({ imports: [ScheduleModule.forRoot()], - providers: [WriteFlashFileService], + providers: [WriteFlashFileService, LogRotateService], }) export class CronModule {} diff --git a/api/src/unraid-api/cron/log-rotate.service.ts b/api/src/unraid-api/cron/log-rotate.service.ts new file mode 100644 index 000000000..19c7da9c9 --- /dev/null +++ b/api/src/unraid-api/cron/log-rotate.service.ts @@ -0,0 +1,24 @@ +import { Injectable, Logger } from '@nestjs/common'; +import { Cron } from '@nestjs/schedule'; +import { existsSync } from 'fs'; + +import { execa } from 'execa'; + +@Injectable() +export class LogRotateService { + private readonly logger = new Logger(LogRotateService.name); + + logRotatePath: string = '/usr/sbin/logrotate'; + configPath: string = '/etc/logrotate.conf'; + @Cron('0 * * * *') + async handleCron() { + try { + if (existsSync(this.logRotatePath)) { + this.logger.debug('Running logrotate'); + await execa(`/usr/sbin/logrotate`, ['/etc/logrotate.conf']); + } + } catch (error) { + this.logger.error(error); + } + } +} diff --git a/api/src/unraid-api/unraid-file-modifier/unraid-file-modifier.spec.ts b/api/src/unraid-api/unraid-file-modifier/unraid-file-modifier.spec.ts index 8a6366e83..538b3d4b8 100644 --- a/api/src/unraid-api/unraid-file-modifier/unraid-file-modifier.spec.ts +++ b/api/src/unraid-api/unraid-file-modifier/unraid-file-modifier.spec.ts @@ -67,7 +67,7 @@ describe('FileModificationService', () => { it('should load modifications', async () => { const mods = await service.loadModifications(); - expect(mods).toHaveLength(2); + expect(mods).toHaveLength(3); }); it('should apply modifications', async () => {