11import { forwardRef , Inject , Injectable , Logger } from "@nestjs/common" ;
22import { Queue } from "bullmq" ;
33import { InjectQueue } from "@nestjs/bullmq" ;
4- import { Interval } from "@nestjs/schedule" ;
4+ import { Cron } from "@nestjs/schedule" ;
55import { ConnectionsService } from "../../connections/connections.service" ;
6- import { hours } from "@nestjs/throttler" ;
76import {
87 PLAYTIME_WATCH_QUEUE_JOB_NAME ,
98 PLAYTIME_WATCH_QUEUE_NAME ,
@@ -23,11 +22,10 @@ export class PlaytimeWatchService {
2322 private readonly librariesService : LibrariesService ,
2423 @Inject ( forwardRef ( ( ) => ConnectionsService ) )
2524 private readonly connectionsService : ConnectionsService ,
26- ) {
27- this . registerWatchJobs ( ) ;
28- }
25+ ) { }
2926
30- @Interval ( hours ( 6 ) )
27+ // “At minute 0 past hour 6, 12, 18, and 0.”
28+ @Cron ( "0 6,12,18,0 * * *" )
3129 async registerWatchJobs ( ) {
3230 const userLibraries = await this . librariesService . findAllLibraries ( ) ;
3331 const userIds = userLibraries . map ( ( library ) => library . userId ) ;
@@ -47,25 +45,14 @@ export class PlaytimeWatchService {
4745 }
4846
4947 for ( const viableConnection of viableConnections ) {
50- const source = connectionToPlaytimeSource ( viableConnection . type ) ;
51- this . playtimeWatchQueue
52- . add (
53- PLAYTIME_WATCH_QUEUE_JOB_NAME ,
54- {
55- userId : viableConnection . profileUserId ,
56- source : source ,
57- } ,
58- {
59- jobId : `playtime-watch-${ viableConnection . profileUserId } -${ source } ` ,
60- } ,
61- )
62- . catch ( ( err ) => {
63- this . logger . error ( err ) ;
64- } ) ;
48+ await this . registerJob (
49+ viableConnection . profileUserId ,
50+ viableConnection . type ,
51+ ) ;
6552 }
6653 }
6754
68- async registerManualJob ( userId : string , source : EConnectionType ) {
55+ async registerJob ( userId : string , source : EConnectionType ) {
6956 const playtimeSource = connectionToPlaytimeSource ( source ) ;
7057
7158 this . playtimeWatchQueue
0 commit comments