2021-05-25 18:12:42 +02:00
|
|
|
import { Injectable, OnApplicationBootstrap } from "@nestjs/common";
|
2020-11-28 18:28:38 +01:00
|
|
|
import { ConfigService } from "@nestjs/config";
|
|
|
|
|
import { SchedulerRegistry } from "@nestjs/schedule";
|
|
|
|
|
import { captureException } from "@sentry/node";
|
|
|
|
|
import { Logger } from "../logger/logger.service";
|
|
|
|
|
import { SpotifyService } from "./spotify/spotify.service";
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class SchedulerService implements OnApplicationBootstrap {
|
|
|
|
|
constructor(
|
|
|
|
|
private readonly config: ConfigService,
|
|
|
|
|
private readonly registry: SchedulerRegistry,
|
|
|
|
|
private readonly spotifyService: SpotifyService,
|
|
|
|
|
private readonly logger: Logger
|
|
|
|
|
) {
|
|
|
|
|
this.logger.setContext(this.constructor.name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onApplicationBootstrap() {
|
|
|
|
|
this.setupSpotifyCrawler();
|
2021-11-21 15:53:27 +01:00
|
|
|
this.setupSpotifyMusicLibraryUpdater();
|
2020-11-28 18:28:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private setupSpotifyCrawler() {
|
|
|
|
|
const callback = () =>
|
|
|
|
|
this.spotifyService.runCrawlerForAllUsers().catch((err) => {
|
|
|
|
|
captureException(err);
|
|
|
|
|
this.logger.error(`Spotify crawler loop crashed! ${err.stack}`);
|
|
|
|
|
});
|
|
|
|
|
const timeoutMs =
|
|
|
|
|
this.config.get<number>("SPOTIFY_FETCH_INTERVAL_SEC") * 1000;
|
|
|
|
|
|
|
|
|
|
const interval = setInterval(callback, timeoutMs);
|
|
|
|
|
|
|
|
|
|
this.registry.addInterval("crawler_spotify", interval);
|
|
|
|
|
}
|
2021-11-21 15:53:27 +01:00
|
|
|
|
|
|
|
|
private setupSpotifyMusicLibraryUpdater() {
|
|
|
|
|
const callback = () => {
|
|
|
|
|
this.spotifyService.runUpdaterForAllEntities();
|
|
|
|
|
};
|
|
|
|
|
const timeoutMs =
|
|
|
|
|
this.config.get<number>("SPOTIFY_UPDATE_INTERVAL_SEC") * 1000;
|
|
|
|
|
|
|
|
|
|
const interval = setInterval(callback, timeoutMs);
|
|
|
|
|
|
|
|
|
|
this.registry.addInterval("updater_spotify", interval);
|
|
|
|
|
}
|
2020-11-28 18:28:38 +01:00
|
|
|
}
|