feat(api): custom spotify crawler interval

This commit is contained in:
Julian Tölle 2020-11-28 18:28:38 +01:00
parent 97f5ef70ac
commit 66fd6ce1b4
6 changed files with 46 additions and 4 deletions

View file

@ -29,7 +29,7 @@ All configuration must be set as environment variables. Default values are added
- `SPOTIFY_CLIENT_ID`: _Required_, Spotify App Client ID
- `SPOTIFY_CLIENT_SECRET`: _Required_, Spotify App Client Secret
- `SPOTIFY_FETCH_INTERVAL_MIN`: **5**: Interval for fetching recently listened tracks from Spotify.
- `SPOTIFY_FETCH_INTERVAL_SEC`: **60**: Interval for fetching recently listened tracks from Spotify.
- `SPOTIFY_WEB_API_URL`: **https://api.spotify.com/**: Spotify WEB API Endpoint.
- `SPOTIFY_AUTH_API_URL`: **https://accounts.spotify.com/**: Spotify Authentication API Endpoint.
- `SPOTIFY_USER_FILTER`: **""**: If set, only allow Spotify users with these ids to access the app. If empty, allow all users to access the app. Seperate ids with `,` eg.: `231421323123,other_id`.

View file

@ -23,7 +23,7 @@ import { ConfigModule as NestConfigModule } from "@nestjs/config";
// Spotify
SPOTIFY_CLIENT_ID: Joi.string().required(),
SPOTIFY_CLIENT_SECRET: Joi.string().required(),
SPOTIFY_FETCH_INTERVAL_MS: Joi.number().default(5 * 60 * 1000),
SPOTIFY_FETCH_INTERVAL_SEC: Joi.number().default(60),
SPOTIFY_WEB_API_URL: Joi.string().default("https://api.spotify.com/"),
SPOTIFY_AUTH_API_URL: Joi.string().default(
"https://accounts.spotify.com/"

View file

@ -0,0 +1,40 @@
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);
}
}

View file

@ -1,7 +1,10 @@
import { Module } from "@nestjs/common";
import { ConfigModule } from "../config/config.module";
import { SchedulerService } from "./scheduler.service";
import { SpotifyModule } from "./spotify/spotify.module";
@Module({
imports: [SpotifyModule],
providers: [SchedulerService],
})
export class SourcesModule {}

View file

@ -15,5 +15,6 @@ import { SpotifyService } from "./spotify.service";
SpotifyAuthModule,
],
providers: [SpotifyService],
exports: [SpotifyService],
})
export class SpotifyModule {}

View file

@ -1,5 +1,4 @@
import { Injectable } from "@nestjs/common";
import { Interval } from "@nestjs/schedule";
import { ListensService } from "../../listens/listens.service";
import { Logger } from "../../logger/logger.service";
import { Album } from "../../music-library/album.entity";
@ -31,7 +30,6 @@ export class SpotifyService {
this.logger.setContext(this.constructor.name);
}
@Interval(20 * 1000)
async runCrawlerForAllUsers(): Promise<void> {
this.logger.debug("Starting Spotify crawler loop");
const users = await this.usersService.findAll();