Skip to content

Commit

Permalink
image processing job test
Browse files Browse the repository at this point in the history
  • Loading branch information
cyri113 committed May 21, 2023
1 parent 3314030 commit f03809e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 33 deletions.
17 changes: 17 additions & 0 deletions __test__/jobs/imageProcessing.job.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { type Job } from 'bullmq';
import imageProcessingJob from '../../src/jobs/imageProcessing.job';

describe('imageProcessingJob', () => {
it('should process the image successfully', async () => {
const imageUrl = 'https://example.com/image.jpg';
const jobData = { imageUrl };
const job: Partial<Job> = {
id: 'job-id',
name: 'image-processing',
data: jobData,
// Mock other attributes as needed
};

await expect(imageProcessingJob(job as Job)).resolves.toBeUndefined();
});
});
22 changes: 22 additions & 0 deletions src/jobs/imageProcessing.job.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { type Job } from "bullmq";

/* eslint-disable @typescript-eslint/restrict-template-expressions */
const imageProcessingJob = async function (job: Job): Promise<void> {
// Define the processing logic for image processing jobs
const { imageUrl } = job.data; // Assuming the job data contains the URL of the image to be processed

try {
// Perform image processing logic here
console.log(`Processing image: ${imageUrl}`);

// Simulating image processing time
await new Promise((resolve) => setTimeout(resolve, 3000));

console.log('Image processing completed');
} catch (error) {
console.error('Error processing image:', error);
throw new Error('Failed to process image'); // Throw an error if image processing fails
}
}

export default imageProcessingJob
35 changes: 2 additions & 33 deletions src/workers/imageProcessing.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,9 @@
import { Worker } from 'bullmq';
import { imageProcessingQueue } from '../queues'; // Assuming you have already defined the imageProcessingQueue
import connection from '../queues/connection';
import imageProcessingJob from '../jobs/imageProcessing.job';

// Create a new BullMQ worker instance for imageProcessingQueue
const imageProcessingWorker = new Worker(imageProcessingQueue.name, async (job) => {
// Define the processing logic for image processing jobs
const { imageUrl } = job.data; // Assuming the job data contains the URL of the image to be processed

try {
// Perform image processing logic here
console.log(`Processing image: ${imageUrl}`);

// Simulating image processing time
await new Promise((resolve) => setTimeout(resolve, 3000));

console.log('Image processing completed');
} catch (error) {
console.error('Error processing image:', error);
throw new Error('Failed to process image'); // Throw an error if image processing fails
}
}, { connection });

imageProcessingWorker.on('failed', (job, err) => {
if (job !== undefined) {
console.error(`Job ${job.id} failed with error:`, err);
} else {
console.error('The job was undefined.')
}
});

imageProcessingWorker.on('completed', (job) => {
console.log(`Job ${job.id} completed successfully`);
});

imageProcessingWorker.on('error', (error) => {
console.error('Worker error:', error);
});
const imageProcessingWorker = new Worker(imageProcessingQueue.name, imageProcessingJob, { connection });

export default imageProcessingWorker

0 comments on commit f03809e

Please sign in to comment.