Skip to content

Commit 8d4b6ac

Browse files
authored
Merge pull request #183 from game-node-app/dev
refactored playtime watch job scheduling logic
2 parents 49e1ec3 + 2fcfc1a commit 8d4b6ac

File tree

5 files changed

+20
-35
lines changed

5 files changed

+20
-35
lines changed

src/connections/connections.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ export class ConnectionsService {
205205

206206
private onConnectionCreate(createdConnection: UserConnection) {
207207
this.playtimeWatchService
208-
.registerManualJob(
208+
.registerJob(
209209
createdConnection.profileUserId,
210210
createdConnection.type,
211211
)

src/game/external-game/external-game-mappings.service.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@ export class ExternalGameMappingsService {
1616
"id" | "createdAt" | "deletedAt" | "updatedAt" | "externalGame"
1717
>,
1818
) {
19-
await this.psnExtraMappingsRepository.upsert(
19+
const existingMapping = await this.psnExtraMappingsRepository.findOneBy(
2020
{
21-
...mappings,
22-
id: null as never,
21+
externalGameId: mappings.externalGameId,
22+
npServiceName: mappings.npServiceName,
23+
npCommunicationId: mappings.npCommunicationId,
2324
},
24-
["externalGameId", "npServiceName", "npCommunicationId"],
2525
);
26+
27+
if (!existingMapping) {
28+
await this.psnExtraMappingsRepository.insert(mappings);
29+
}
2630
}
2731
}

src/playtime/watch/playtime-watch.processor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import * as process from "process";
2323
import { seconds } from "@nestjs/throttler";
2424

2525
@Processor(PLAYTIME_WATCH_QUEUE_NAME, {
26-
concurrency: 4,
26+
concurrency: 1,
2727
limiter:
2828
process.env.NODE_ENV === "development"
2929
? {

src/playtime/watch/playtime-watch.service.ts

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { forwardRef, Inject, Injectable, Logger } from "@nestjs/common";
22
import { Queue } from "bullmq";
33
import { InjectQueue } from "@nestjs/bullmq";
4-
import { Interval } from "@nestjs/schedule";
4+
import { Cron } from "@nestjs/schedule";
55
import { ConnectionsService } from "../../connections/connections.service";
6-
import { hours } from "@nestjs/throttler";
76
import {
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

src/sync/psn/psn-sync.service.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,7 @@ import { Cacheable } from "../../utils/cacheable";
2121
export class PsnSyncService {
2222
private readonly logger = new Logger(PsnSyncService.name);
2323

24-
constructor(private readonly authService: PsnSyncAuthService) {
25-
// this.getGames("5847504196784127951", 0, 1);
26-
// this.getAllGames("5847504196784127951");
27-
// this.getUserTrophyTitles("1588575164307436368");
28-
// this.getGameAchievements("NPWR00951_00", "trophy");
29-
// this.getGameAchievementGroups("NPWR36088_00", "trophy2");
30-
}
24+
constructor(private readonly authService: PsnSyncAuthService) {}
3125

3226
public async resolveUserInfo(
3327
userIdentifier: string,

0 commit comments

Comments
 (0)