2022-02-27 17:57:33 +01:00
|
|
|
import { Injectable, Logger, OnApplicationBootstrap } from "@nestjs/common";
|
2020-11-28 18:28:38 +01:00
|
|
|
import { ConfigService } from "@nestjs/config";
|
|
|
|
|
import { SpotifyService } from "./spotify/spotify.service";
|
2022-06-29 21:29:00 +02:00
|
|
|
import {
|
|
|
|
|
CrawlerSupervisorJob,
|
|
|
|
|
ICrawlerSupervisorJob,
|
|
|
|
|
IImportSpotifyJob,
|
|
|
|
|
ImportSpotifyJob,
|
|
|
|
|
IUpdateSpotifyLibraryJob,
|
|
|
|
|
UpdateSpotifyLibraryJob,
|
|
|
|
|
} from "./jobs";
|
|
|
|
|
import { JobService } from "@apricote/nest-pg-boss";
|
2020-11-28 18:28:38 +01:00
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class SchedulerService implements OnApplicationBootstrap {
|
2022-02-27 17:57:33 +01:00
|
|
|
private readonly logger = new Logger(this.constructor.name);
|
|
|
|
|
|
2020-11-28 18:28:38 +01:00
|
|
|
constructor(
|
|
|
|
|
private readonly config: ConfigService,
|
2022-06-29 21:29:00 +02:00
|
|
|
private readonly spotifyService: SpotifyService,
|
|
|
|
|
@CrawlerSupervisorJob.Inject()
|
|
|
|
|
private readonly superviseImportJobsJobService: JobService<ICrawlerSupervisorJob>,
|
|
|
|
|
@ImportSpotifyJob.Inject()
|
|
|
|
|
private readonly importSpotifyJobService: JobService<IImportSpotifyJob>,
|
|
|
|
|
@UpdateSpotifyLibraryJob.Inject()
|
|
|
|
|
private readonly updateSpotifyLibraryJobService: JobService<IUpdateSpotifyLibraryJob>
|
2022-02-27 17:57:33 +01:00
|
|
|
) {}
|
2020-11-28 18:28:38 +01:00
|
|
|
|
2022-06-29 21:29:00 +02:00
|
|
|
async onApplicationBootstrap() {
|
|
|
|
|
await this.setupSpotifyCrawlerSupervisor();
|
|
|
|
|
await this.setupSpotifyMusicLibraryUpdater();
|
2020-11-28 18:28:38 +01:00
|
|
|
}
|
|
|
|
|
|
2022-06-29 21:29:00 +02:00
|
|
|
private async setupSpotifyCrawlerSupervisor(): Promise<void> {
|
|
|
|
|
await this.superviseImportJobsJobService.schedule("*/1 * * * *", {}, {});
|
|
|
|
|
}
|
2020-11-28 18:28:38 +01:00
|
|
|
|
2022-06-29 21:29:00 +02:00
|
|
|
@CrawlerSupervisorJob.Handle()
|
|
|
|
|
async superviseImportJobs(): Promise<void> {
|
|
|
|
|
this.logger.log("Starting crawler jobs");
|
|
|
|
|
const users = await this.spotifyService.getCrawlableUserInfo();
|
2020-11-28 18:28:38 +01:00
|
|
|
|
2022-06-29 21:29:00 +02:00
|
|
|
await Promise.all(
|
|
|
|
|
users.map((user) =>
|
|
|
|
|
this.importSpotifyJobService.sendOnce({ userID: user.id }, {}, user.id)
|
|
|
|
|
)
|
|
|
|
|
);
|
2020-11-28 18:28:38 +01:00
|
|
|
}
|
2021-11-21 15:53:27 +01:00
|
|
|
|
2022-06-29 21:29:00 +02:00
|
|
|
private async setupSpotifyMusicLibraryUpdater() {
|
|
|
|
|
await this.updateSpotifyLibraryJobService.schedule("*/1 * * * *", {}, {});
|
|
|
|
|
}
|
2021-11-21 15:53:27 +01:00
|
|
|
|
2022-06-29 21:29:00 +02:00
|
|
|
@UpdateSpotifyLibraryJob.Handle()
|
|
|
|
|
async updateSpotifyLibrary() {
|
|
|
|
|
this.spotifyService.runUpdaterForAllEntities();
|
2021-11-21 15:53:27 +01:00
|
|
|
}
|
2020-11-28 18:28:38 +01:00
|
|
|
}
|