-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #626 from VitNode/admin/corn_jobs
feat: Add cron jobs page in AdminCP
- Loading branch information
Showing
22 changed files
with
691 additions
and
373 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
import { Module } from '@nestjs/common'; | ||
|
||
import { CronAdvancedAdminModule } from './cron/cron.module'; | ||
import { FilesAdvancedAdminModule } from './files/files.module'; | ||
|
||
@Module({ | ||
imports: [FilesAdvancedAdminModule], | ||
imports: [FilesAdvancedAdminModule, CronAdvancedAdminModule], | ||
}) | ||
export class AdvancedAdminModule {} |
25 changes: 25 additions & 0 deletions
25
packages/backend/src/core/admin/advanced/cron/cron.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Controllers } from '@/helpers/controller.decorator'; | ||
import { Get } from '@nestjs/common'; | ||
import { ApiOkResponse } from '@nestjs/swagger'; | ||
import { ShowCronAdvancedAdminObj } from 'vitnode-shared/admin/advanced/cron.dto'; | ||
|
||
import { ShowCronAdvancedAdminService } from './services/show.service'; | ||
|
||
@Controllers({ | ||
plugin_name: 'Core', | ||
plugin_code: 'advanced', | ||
isAdmin: true, | ||
route: 'cron', | ||
}) | ||
export class CoreAdvancedAdminController { | ||
constructor(private readonly showService: ShowCronAdvancedAdminService) {} | ||
|
||
@ApiOkResponse({ | ||
type: ShowCronAdvancedAdminObj, | ||
description: 'Show cron jobs', | ||
}) | ||
@Get() | ||
show(): ShowCronAdvancedAdminObj { | ||
return this.showService.show(); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/backend/src/core/admin/advanced/cron/cron.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Module } from '@nestjs/common'; | ||
|
||
import { CoreAdvancedAdminController } from './cron.controller'; | ||
import { ShowCronAdvancedAdminService } from './services/show.service'; | ||
|
||
@Module({ | ||
providers: [ShowCronAdvancedAdminService], | ||
controllers: [CoreAdvancedAdminController], | ||
}) | ||
export class CronAdvancedAdminModule {} |
31 changes: 31 additions & 0 deletions
31
packages/backend/src/core/admin/advanced/cron/services/show.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { SchedulerRegistry } from '@nestjs/schedule'; | ||
import { ShowCronAdvancedAdminObj } from 'vitnode-shared/admin/advanced/cron.dto'; | ||
|
||
@Injectable() | ||
export class ShowCronAdvancedAdminService { | ||
constructor(private readonly schedulerRegistry: SchedulerRegistry) {} | ||
|
||
show(): ShowCronAdvancedAdminObj { | ||
const jobs = this.schedulerRegistry.getCronJobs(); | ||
|
||
const edges: ShowCronAdvancedAdminObj['edges'] = Array.from(jobs).map( | ||
([key, value]) => { | ||
return { | ||
name: key, | ||
running: value.running, | ||
next_date: value.nextDate().toJSDate(), | ||
last_execution: value.lastExecution, | ||
schedule: value.cronTime.source.toString(), | ||
}; | ||
}, | ||
); | ||
|
||
// Sort by next_date descending | ||
const sortedEdges = edges.sort( | ||
(a, b) => b.next_date.getTime() - a.next_date.getTime(), | ||
); | ||
|
||
return { edges: sortedEdges }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { core_files, core_files_using } from '@/database/schema/files'; | ||
import { FilesHelperService } from '@/helpers/files/files-helper.service'; | ||
import { InternalDatabaseService } from '@/utils/database/internal_database.service'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { Cron, CronExpression } from '@nestjs/schedule'; | ||
import { and, eq, isNull, lte } from 'drizzle-orm'; | ||
|
||
@Injectable() | ||
export class FilesCron { | ||
constructor( | ||
private readonly databaseService: InternalDatabaseService, | ||
private readonly filesService: FilesHelperService, | ||
) {} | ||
|
||
@Cron(CronExpression.EVERY_6_HOURS, { | ||
name: 'core_clear_unused_files', | ||
}) | ||
async clearUnusedFiles() { | ||
const twelveHoursAgo = new Date(Date.now() - 12 * 60 * 60 * 1000); | ||
const findFiles = await this.databaseService.db | ||
.select() | ||
.from(core_files) | ||
.leftJoin(core_files_using, eq(core_files.id, core_files_using.file_id)) | ||
.where( | ||
and( | ||
isNull(core_files_using.file_id), | ||
lte(core_files.created_at, twelveHoursAgo), | ||
), | ||
); | ||
|
||
await Promise.all( | ||
findFiles.map(async file => { | ||
await Promise.all([ | ||
this.filesService.delete({ | ||
dir_folder: file.core_files.dir_folder, | ||
file_name: file.core_files.file_name, | ||
}), | ||
this.databaseService.db | ||
.delete(core_files) | ||
.where(eq(core_files.id, file.core_files.id)), | ||
]); | ||
}), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import { Module } from '@nestjs/common'; | ||
|
||
import { FilesCron } from './clean.core'; | ||
import { FilesController } from './files.controller'; | ||
import { DeleteFilesService } from './services/delete.service'; | ||
import { UploadFilesService } from './services/upload.service'; | ||
|
||
@Module({ | ||
providers: [UploadFilesService, DeleteFilesService], | ||
providers: [UploadFilesService, DeleteFilesService, FilesCron], | ||
controllers: [FilesController], | ||
}) | ||
export class FilesModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.