Skip to content

Commit

Permalink
fix(maintenance): Extended the maintenance task with an action to rem…
Browse files Browse the repository at this point in the history
…ove orphaned collection objects
  • Loading branch information
jorenn92 committed Jan 5, 2024
1 parent 1c4accd commit f5826cc
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion server/src/modules/rules/rule-maintenance.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { TasksService } from '../tasks/tasks.service';
import { SettingsService } from '../settings/settings.service';
import { RulesService } from './rules.service';
import { PlexApiService } from '../api/plex-api/plex-api.service';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Collection } from '../collections/entities/collection.entities';

@Injectable()
export class RuleMaintenanceService implements OnApplicationBootstrap {
Expand All @@ -13,14 +16,16 @@ export class RuleMaintenanceService implements OnApplicationBootstrap {
private readonly taskService: TasksService,
private readonly settings: SettingsService,
private readonly rulesService: RulesService,
@InjectRepository(Collection)
private readonly collectionRepo: Repository<Collection>,
private readonly plexApi: PlexApiService,
) {}

onApplicationBootstrap() {
this.jobCreationAttempts++;
const state = this.taskService.createJob(
'RuleMaintenance',
'30 3 * * 2',
'20 4 * * */1',
this.execute.bind(this),
);
if (state.code === 0) {
Expand All @@ -45,6 +50,12 @@ export class RuleMaintenanceService implements OnApplicationBootstrap {
if (appStatus) {
// remove media exclusions that are no longer available
this.removeLeftoverExclusions();
this.removeCollectionsWithoutRule();
this.logger.log('Maintenance done');
} else {
this.logger.error(
`Maintenance skipped, not all applications were reachable.`,
);
}
} catch (e) {
this.logger.error(`RuleMaintenance failed : ${e.message}`);
Expand All @@ -64,4 +75,23 @@ export class RuleMaintenanceService implements OnApplicationBootstrap {
}
}
}

private async removeCollectionsWithoutRule() {
try {
const collections = await this.collectionRepo.find(); // get all collections
const rulegroups = await this.rulesService.getRuleGroups();

for (const collection of collections) {
if (
!rulegroups.find(
(rulegroup) => rulegroup.collection?.id === collection.id,
)
) {
await this.collectionRepo.delete({ id: collection.id });
}
}
} catch (err) {
this.logger.warn("Couldn't remove collection without rule: " + err);
}
}
}

0 comments on commit f5826cc

Please sign in to comment.