Skip to content
This repository has been archived by the owner on Jul 21, 2022. It is now read-only.

Commit

Permalink
Merge pull request #8 from Zibbp/cron
Browse files Browse the repository at this point in the history
feat: Allow users set live cron schedules
  • Loading branch information
Zibbp committed May 11, 2022
2 parents c2d7f44 + 9cf2d6a commit f6ceb11
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/config.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ export const configValidationSchema = Joi.object({
CLIENT_ID: Joi.string().required(),
CLIENT_SECRET: Joi.string().required(),
API_URL: Joi.string().required(),
LIVE_CRON_SCHEDULE: Joi.string(),
});
39 changes: 32 additions & 7 deletions src/live/live.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable, InternalServerErrorException, Logger } from '@nestjs/common';
import { Cron, CronExpression } from '@nestjs/schedule';
import { Cron, CronExpression, SchedulerRegistry } from '@nestjs/schedule';
import { InjectRepository } from '@nestjs/typeorm';
import { ChannelsService } from 'src/channels/channels.service';
import { Channel } from 'src/channels/entities/channel.entity';
Expand All @@ -10,23 +10,48 @@ import { CreateLiveDto } from './dto/create-live.dto';
import { UpdateLiveDto } from './dto/update-live.dto';
import { Live } from './entities/live.entity';
import { LiveRepository } from './live.repository';
import { ConfigService } from '@nestjs/config';
import { CronJob } from 'cron';

@Injectable()
export class LiveService {
private logger = new Logger('LiveService');
private cronSchedule = this.configService.get('LIVE_CRON_SCHEDULE')
constructor(
@InjectRepository(LiveRepository)
private liveRepository: LiveRepository,
private channelService: ChannelsService,
private twitchService: TwitchService,
private vodService: VodsService
private vodService: VodsService,
private configService: ConfigService,
private schedulerRegistry: SchedulerRegistry
) { }
async onModuleInit() {
// Check if cron is set via env var - if not default to 5 minutes
if (!this.cronSchedule) {
this.logger.log('No user set cron schedule found, cron will run every 5 minutes');
this.cronSchedule = CronExpression.EVERY_5_MINUTES
}

// If user cron is valid via
if (this.cronSchedule in CronExpression) {
this.logger.log('Cron schedule found, cron will run every', this.cronSchedule);
this.cronSchedule = CronExpression[this.configService.get('LIVE_CRON_SCHEDULE')]
} else {
// Cron schedule is not valid
// Default to 5 minutes
this.logger.log('Invalid cron schedule, defaulting to 5 minutes');
this.cronSchedule = CronExpression.EVERY_5_MINUTES
}

// Create cron job
const checkLiveJob = new CronJob(this.cronSchedule, async () => {
this.logger.verbose('Checking if channels are live');
await this.cronChannelLiveCheck();
})

// Check if channels are live (5 minutes)
@Cron(CronExpression.EVERY_5_MINUTES)
async handleCron() {
this.logger.verbose('Checking if channels are live');
await this.cronChannelLiveCheck();
this.schedulerRegistry.addCronJob('liveCronCheck', checkLiveJob);
checkLiveJob.start();
}

async create(createLiveDto: CreateLiveDto, user: User) {
Expand Down

0 comments on commit f6ceb11

Please sign in to comment.