mirror of
https://github.com/apricote/Listory.git
synced 2026-02-06 17:57:03 +00:00
41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
|
|
import {
|
||
|
|
Injectable,
|
||
|
|
OnApplicationBootstrap,
|
||
|
|
OnModuleInit,
|
||
|
|
} from "@nestjs/common";
|
||
|
|
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();
|
||
|
|
}
|
||
|
|
|
||
|
|
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);
|
||
|
|
}
|
||
|
|
}
|