Skip to content

Commit

Permalink
feat: manually start job endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikburmester committed Oct 5, 2024
1 parent cd5fb35 commit bcd758e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
Post,
Query,
Res,
HttpException,
HttpStatus,
} from '@nestjs/common';
import { Response } from 'express';
import * as fs from 'fs';
Expand All @@ -18,7 +20,7 @@ import { AppService, Job } from './app.service';
export class AppController {
constructor(
private readonly appService: AppService,
private logger: Logger, // Inject Logger
private logger: Logger,
) {}

@Get('statistics')
Expand Down Expand Up @@ -68,6 +70,25 @@ export class AppController {
return this.appService.getJobStatus(id);
}

@Post('start-job/:id')
async startJob(@Param('id') id: string): Promise<{ message: string }> {
this.logger.log(`Manual start request for job: ${id}`);

try {
const result = await this.appService.manuallyStartJob(id);
if (result) {
return { message: 'Job started successfully' };
} else {
throw new HttpException(
'Job not found or already started',
HttpStatus.BAD_REQUEST,
);
}
} catch (error) {
throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR);
}
}

@Delete('cancel-job/:id')
async cancelJob(@Param('id') id: string) {
this.logger.log(`Cancellation request for job: ${id}`);
Expand Down
15 changes: 15 additions & 0 deletions src/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ export class AppService {
this.videoDurations.delete(jobId);
}

getMaxConcurrentJobs(): number {
return this.maxConcurrentJobs;
}

async getStatistics() {
const cacheSize = await this.getCacheSize();
const totalTranscodes = this.getTotalTranscodes();
Expand All @@ -159,6 +163,17 @@ export class AppService {
};
}

async manuallyStartJob(jobId: string): Promise<boolean> {
const job = this.activeJobs.find((job) => job.id === jobId);

if (!job || job.status !== 'queued') {
return false;
}

this.startJob(jobId);
return true;
}

private async getCacheSize(): Promise<string> {
const cacheSize = await this.getDirectorySize(this.cacheDir);
return this.formatSize(cacheSize);
Expand Down

0 comments on commit bcd758e

Please sign in to comment.