-
-
Notifications
You must be signed in to change notification settings - Fork 23
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 #803 from jorenn92/fix-stop-tasks-running-simultan…
…eous fix(tasks): Improved task management by limiting the simultaneous exe…
- Loading branch information
Showing
21 changed files
with
272 additions
and
147 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
server/src/database/migrations/1706275100801-Tasks_add_taskRunning_table.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,24 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
export class TasksAddTaskRunningTable1706275100801 | ||
implements MigrationInterface | ||
{ | ||
name = 'TasksAddTaskRunningTable1706275100801'; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(` | ||
CREATE TABLE "task_running" ( | ||
"id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, | ||
"name" varchar NOT NULL, | ||
"runningSince" datetime DEFAULT NULL, | ||
"running" boolean NOT NULL DEFAULT (0) | ||
) | ||
`); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(` | ||
DROP TABLE "task_running" | ||
`); | ||
} | ||
} |
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
2 changes: 1 addition & 1 deletion
2
server/src/modules/collections/entities/collection_log.entities.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
2 changes: 1 addition & 1 deletion
2
server/src/modules/collections/interfaces/collection-media.interface.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
47 changes: 19 additions & 28 deletions
47
server/src/modules/collections/tasks/collection-log-cleaner.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 |
---|---|---|
@@ -1,49 +1,40 @@ | ||
import { Injectable, Logger, OnApplicationBootstrap } from '@nestjs/common'; | ||
import { CollectionsService } from 'src/modules/collections/collections.service'; | ||
import { TasksService } from 'src/modules/tasks/tasks.service'; | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
import { CollectionsService } from '../../collections/collections.service'; | ||
import { TaskBase } from '../../tasks/task.base'; | ||
import { TasksService } from '../..//tasks/tasks.service'; | ||
|
||
@Injectable() | ||
export class CollectionLogCleanerService implements OnApplicationBootstrap { | ||
private readonly logger = new Logger(CollectionLogCleanerService.name); | ||
private jobCreationAttempts = 0; | ||
export class CollectionLogCleanerService extends TaskBase { | ||
protected logger = new Logger(CollectionLogCleanerService.name); | ||
|
||
protected name = 'Collection Log Cleaner'; | ||
protected cronSchedule = '45 5 * * *'; | ||
|
||
constructor( | ||
private readonly collectionService: CollectionsService, | ||
private readonly taskService: TasksService, | ||
) {} | ||
|
||
onApplicationBootstrap() { | ||
this.jobCreationAttempts++; | ||
const state = this.taskService.createJob( | ||
'Collection Log Cleaner', | ||
'45 5 * * *', | ||
this.execute.bind(this), | ||
); | ||
if (state.code === 0) { | ||
if (this.jobCreationAttempts <= 3) { | ||
this.logger.log( | ||
'Creation of job Collection Log Cleaner failed. Retrying in 10s..', | ||
); | ||
setTimeout(() => { | ||
this.onApplicationBootstrap(); | ||
}, 10000); | ||
} else { | ||
this.logger.error(`Creation of job Collection Log Cleaner failed.`); | ||
} | ||
} | ||
protected readonly taskService: TasksService, | ||
) { | ||
super(taskService); | ||
} | ||
|
||
public async execute() { | ||
try { | ||
await super.execute(); | ||
|
||
// start execution | ||
// get all collections | ||
const collections = await this.collectionService.getAllCollections(); | ||
|
||
// for each collection | ||
for (const collection of collections) { | ||
this.collectionService.removeOldCollectionLogs(collection); | ||
} | ||
|
||
// clean up | ||
this.finish(); | ||
} catch (e) { | ||
this.logger.debug(e); | ||
this.finish(); | ||
} | ||
} | ||
} |
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
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.