0
0
NestJSframework~20 mins

Why background processing handles heavy tasks in NestJS - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Background Processing Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use background processing in NestJS?

In NestJS, why is it beneficial to handle heavy tasks using background processing?

AIt allows the main application to stay responsive by offloading long tasks to separate workers.
BIt automatically fixes bugs in the heavy tasks without developer intervention.
CIt prevents any tasks from running concurrently, ensuring sequential execution.
DIt makes the application run slower because tasks are queued and delayed.
Attempts:
2 left
💡 Hint

Think about how the app behaves when it has to do many things at once.

component_behavior
intermediate
2:00remaining
What happens when a heavy task runs on the main thread?

Consider a NestJS app that processes a large file upload directly on the main thread. What is the likely effect on the app's behavior?

AThe app crashes immediately due to memory overflow.
BThe app processes the file instantly without any delay.
CThe app becomes unresponsive until the file processing finishes.
DThe app automatically delegates the task to a background worker.
Attempts:
2 left
💡 Hint

Think about what happens when one big job blocks everything else.

📝 Syntax
advanced
2:30remaining
Identify the correct way to define a background job in NestJS using Bull

Which code snippet correctly defines a background job processor using the Bull module in NestJS?

NestJS
import { Processor, Process } from '@nestjs/bull';
import { Job } from 'bull';

@Processor('email')
export class EmailProcessor {
  @Process('send')
  async handleSendEmail(job: Job) {
    // send email logic
  }
}
AUses @Processor decorator with queue name and @Process with job name to handle jobs.
BUses @Injectable decorator only without @Process to handle jobs.
CDefines a method without decorators and calls it manually for jobs.
DUses @Controller decorator to define background jobs.
Attempts:
2 left
💡 Hint

Look for decorators that connect the class and method to the queue and job.

🔧 Debug
advanced
2:30remaining
Why does the background job never run in this NestJS code?

Given this NestJS Bull processor code, why might the background job never execute?

NestJS
import { Processor, Process } from '@nestjs/bull';
import { Job } from 'bull';

@Processor('notifications')
export class NotificationProcessor {
  async handleNotification(job: Job) {
    console.log('Processing notification');
  }
}
AThe @Processor decorator is missing the job name parameter.
BThe method handleNotification lacks the @Process decorator, so it is not registered as a job handler.
CThe Job import from 'bull' is incorrect and causes runtime errors.
DThe class is missing the @Injectable decorator, so it won't be instantiated.
Attempts:
2 left
💡 Hint

Check if the method is properly marked to handle jobs.

state_output
expert
3:00remaining
What is the output when a heavy task is offloaded to a background queue in NestJS?

Consider this NestJS controller code that adds a heavy task to a Bull queue. What will the client receive immediately after calling the endpoint?

NestJS
import { Controller, Post } from '@nestjs/common';
import { InjectQueue } from '@nestjs/bull';
import { Queue } from 'bull';

@Controller('tasks')
export class TasksController {
  constructor(@InjectQueue('heavy') private heavyQueue: Queue) {}

  @Post('start')
  async startHeavyTask() {
    await this.heavyQueue.add('process', { data: 'big job' });
    return { status: 'Task queued' };
  }
}
AThe endpoint returns no response because the task is asynchronous.
BThe client waits until the heavy task finishes before receiving any response.
CThe client receives an error because the queue is not processed synchronously.
D{ status: 'Task queued' } is returned immediately, while the heavy task runs in the background.
Attempts:
2 left
💡 Hint

Think about what happens when you add a job to a queue instead of running it directly.