Skip to content

Commit

Permalink
βœ” Fix πŸŽ‰ Cron-Job ✨ Scheduler ~
Browse files Browse the repository at this point in the history
  • Loading branch information
bifeldy committed Apr 30, 2023
1 parent 5c9a308 commit 2c51693
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 80 deletions.
2 changes: 1 addition & 1 deletion dist/fansubid/browser/ngsw.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"configVersion": 1,
"timestamp": 1682258514450,
"timestamp": 1682840128531,
"index": "/index.html",
"assetGroups": [
{
Expand Down
2 changes: 1 addition & 1 deletion dist/fansubid/server/main.js

Large diffs are not rendered by default.

6 changes: 1 addition & 5 deletions src/api/controllers/task-cron-job.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@ import { FilterApiKeyAccess } from '../decorators/filter-api-key-access.decorato
import { Roles } from '../decorators/roles.decorator';
import { VerifiedOnly } from '../decorators/verified.decorator';

import { ConfigService } from '../services/config.service';

@ApiExcludeController()
@Controller('/task-cron-job')
export class TaskCronJobController {

constructor(
private sr: SchedulerRegistry,
private cfg: ConfigService,
private sr: SchedulerRegistry
) {
//
}
Expand Down Expand Up @@ -55,7 +52,6 @@ export class TaskCronJobController {
if (cronJob) {
if (cronJob.running) {
cronJob.stop();
this.cfg.CRON[req.params['id']] = false;
} else {
cronJob.start();
}
Expand Down
86 changes: 40 additions & 46 deletions src/api/scheduler/rss-feed-tasks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { FansubModel } from '../../models/req-res.model';

import { FansubService } from '../repository/fansub.service';

import { ConfigService } from '../services/config.service';
import { GlobalService } from '../services/global.service';

@Injectable()
Expand All @@ -26,7 +25,6 @@ export class RssFeedTasksService {
constructor(
@Inject(CACHE_MANAGER) private cm: Cache,
private fansubRepo: FansubService,
private cfg: ConfigService,
private gs: GlobalService
) {
//
Expand Down Expand Up @@ -112,55 +110,51 @@ export class RssFeedTasksService {
}
)
async fansubRssFeedAll(): Promise<void> {
if (!this.cfg.CRON[CONSTANTS.cronFansubRssFeed]) {
const startTime = new Date();
this.gs.log('[CRON_TASK_FANSUB_RSS_FEED-START] 🐾', `${startTime}`);
this.cfg.CRON[CONSTANTS.cronFansubRssFeed] = true;
try {
const rssFeedAll = [];
const rssFeedActive = [];
const fansubs = await this.fansubRepo.find({
where: [
{ rss_feed: Not(IsNull()) }
],
order: {
updated_at: 'DESC'
}
});
const rgx = new RegExp(CONSTANTS.regexUrl);
for (const fs of fansubs) {
if (fs.rss_feed.match(rgx)) {
try {
const feed = await this.getFeedByUrl(fs.rss_feed);
this.sortRssFeedWhileAdding(fs, rssFeedAll, feed);
if (fs.active) {
this.sortRssFeedWhileAdding(fs, rssFeedActive, feed, 1);
}
} catch (e) {
this.gs.log('[CRON_TASK_FANSUB_RSS_FEED-ERROR_PARSE] 🐾', e, 'error');
const startTime = new Date();
this.gs.log('[CRON_TASK_FANSUB_RSS_FEED-START] 🐾', `${startTime}`);
try {
const rssFeedAll = [];
const rssFeedActive = [];
const fansubs = await this.fansubRepo.find({
where: [
{ rss_feed: Not(IsNull()) }
],
order: {
updated_at: 'DESC'
}
});
const rgx = new RegExp(CONSTANTS.regexUrl);
for (const fs of fansubs) {
if (fs.rss_feed.match(rgx)) {
try {
const feed = await this.getFeedByUrl(fs.rss_feed);
this.sortRssFeedWhileAdding(fs, rssFeedAll, feed);
if (fs.active) {
this.sortRssFeedWhileAdding(fs, rssFeedActive, feed, 1);
}
} catch (e) {
this.gs.log('[CRON_TASK_FANSUB_RSS_FEED-ERROR_PARSE] 🐾', e, 'error');
}
}
this.saveFeedToFileAndCache('fansub-rss-feed-all', {
info: `πŸ˜… 200 - Fansub API :: RSS Feed All Full Fansubs 🀣`,
count: rssFeedAll.length,
pages: 1,
results: rssFeedAll
});
this.saveFeedToFileAndCache('fansub-rss-feed-active', {
info: `πŸ˜… 200 - Fansub API :: RSS Feed All Active Fansubs 🀣`,
count: rssFeedActive.length,
pages: 1,
results: rssFeedActive
});
} catch (error) {
this.gs.log('[CRON_TASK_FANSUB_RSS_FEED-ERROR] 🐾', error, 'error');
}
this.cfg.CRON[CONSTANTS.cronFansubRssFeed] = false;
const endTime = new Date();
const elapsedTime = endTime.getTime() - startTime.getTime();
this.gs.log('[CRON_TASK_FANSUB_RSS_FEED-END] 🐾', `${endTime} @ ${elapsedTime} ms`);
this.saveFeedToFileAndCache('fansub-rss-feed-all', {
info: `πŸ˜… 200 - Fansub API :: RSS Feed All Full Fansubs 🀣`,
count: rssFeedAll.length,
pages: 1,
results: rssFeedAll
});
this.saveFeedToFileAndCache('fansub-rss-feed-active', {
info: `πŸ˜… 200 - Fansub API :: RSS Feed All Active Fansubs 🀣`,
count: rssFeedActive.length,
pages: 1,
results: rssFeedActive
});
} catch (error) {
this.gs.log('[CRON_TASK_FANSUB_RSS_FEED-ERROR] 🐾', error, 'error');
}
const endTime = new Date();
const elapsedTime = endTime.getTime() - startTime.getTime();
this.gs.log('[CRON_TASK_FANSUB_RSS_FEED-END] 🐾', `${endTime} @ ${elapsedTime} ms`);
}

}
34 changes: 14 additions & 20 deletions src/api/scheduler/tracker-statistics-tasks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { CONSTANTS } from '../../constants';
import { environment } from '../../environments/api/environment';

import { ApiService } from '../services/api.service';
import { ConfigService } from '../services/config.service';
import { GlobalService } from '../services/global.service';
import { SocketIoService } from '../services/socket-io.service';
import { DiscordService } from '../services/discord.service';
Expand All @@ -30,7 +29,6 @@ export class TrackerStatisticsService {
};

constructor(
private cfg: ConfigService,
private gs: GlobalService,
private sis: SocketIoService,
private api: ApiService,
Expand Down Expand Up @@ -61,25 +59,21 @@ export class TrackerStatisticsService {
}
)
async statistics(): Promise<void> {
if (!this.cfg.CRON[CONSTANTS.cronTrackerStatistics] && environment.production) {
const startTime = new Date();
this.gs.log('[CRON_TASK_TRACKER_STATISTICS-START] 🐾', `${startTime}`);
this.cfg.CRON[CONSTANTS.cronTrackerStatistics] = true;
try {
const url = new URL(`http://tracker.${environment.domain}/stats.json`);
const res_raw = await this.api.getData(url, {
...environment.nodeJsXhrHeader
});
this.torrentTracker = await res_raw.json();
} catch (error) {
this.gs.log('[CRON_TASK_TRACKER_STATISTICS-ERROR] 🐾', error, 'error');
}
this.updateVisitor();
this.cfg.CRON[CONSTANTS.cronTrackerStatistics] = false;
const endTime = new Date();
const elapsedTime = endTime.getTime() - startTime.getTime();
this.gs.log('[CRON_TASK_TRACKER_STATISTICS-END] 🐾', `${endTime} @ ${elapsedTime} ms`);
const startTime = new Date();
this.gs.log('[CRON_TASK_TRACKER_STATISTICS-START] 🐾', `${startTime}`);
try {
const url = new URL(`http://tracker.${environment.domain}/stats.json`);
const res_raw = await this.api.getData(url, {
...environment.nodeJsXhrHeader
});
this.torrentTracker = await res_raw.json();
} catch (error) {
this.gs.log('[CRON_TASK_TRACKER_STATISTICS-ERROR] 🐾', error, 'error');
}
this.updateVisitor();
const endTime = new Date();
const elapsedTime = endTime.getTime() - startTime.getTime();
this.gs.log('[CRON_TASK_TRACKER_STATISTICS-END] 🐾', `${endTime} @ ${elapsedTime} ms`);
}

}
7 changes: 0 additions & 7 deletions src/api/services/config.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Injectable } from '@nestjs/common';

import { CONSTANTS } from '../../constants';

import { environment } from '../../environments/api/environment';

import { ServerInfoModel } from '../../models/socket-io.model';
Expand All @@ -20,11 +18,6 @@ export class ConfigService {
'localhost'
];

CRON = {
[CONSTANTS.cronFansubRssFeed]: false,
[CONSTANTS.cronTrackerStatistics]: false
};

github = null;

settings: ServerInfoModel = {
Expand Down

0 comments on commit 2c51693

Please sign in to comment.