Create a Queue/Worker for NestJS application in Cloud Run.
I am wondering how to implement Queue when running an application with Cloud Run. While @nestjs/bull is a very good library, Cloud Run, which is serverless, cannot be used because the server is not always running. You can use "CPU always allocated" for resolve this issue, but it doesn't make sense to use Cloud Run.
Therefore, I used Cloud Pub/Sub or Cloud Tasks so that I could implement Queue via HTTP requests. This package supports both, so you can choose whichever you prefer.
Of course, these packages can work without using Cloud Run on workers since they handle tasks via HTTP requests.
See https://github.com/anchan828/nest-cloud-run-queue/tree/master/packages/demo
This demo uses an emulator, which runs PubSub and Tasks locally. Please see [docker compose.yml](https://github.com/anchan828/nest-cloud-run-queue/blob/master/docker compose.yml) if you are interested.
There are two types of packages.
Package | Description |
---|---|
@anchan828/nest-cloud-run-queue-pubsub-publisher | Library for sending messages using Cloud Pub/Sub. |
@anchan828/nest-cloud-run-queue-tasks-publisher | Library for sending messages using Cloud Tasks. |
Package | Description |
---|---|
@anchan828/nest-cloud-run-queue-worker | Library for creating applications that receive and process messages. |
See: @anchan828/nest-cloud-run-queue-pubsub-publisher - README.md
See: @anchan828/nest-cloud-run-queue-tasks-publisher - README.md
@Module({
imports: [QueueWorkerModule.register()],
})
export class WorkerAppModule {}
@QueueWorker("Worker name")
// @QueueWorker({ name: "Worker name" })
class Worker {
@QueueWorkerProcess()
public async process(message: string | object, raw: QueueWorkerRawMessage): Promise<void> {
console.log("Message:", message);
console.log("Raw message:", raw);
}
}
@Module({
imports: [QueueWorkerModule.register()],
providers: [Worker],
})
export class WorkerAppModule {}
The Controller who receives the message is automatically defined. You can customize it.
@Module({
imports: [
QueueWorkerModule.register({
workerController: {
method: RequestMethod.GET,
path: "/worker",
},
// Default
// workerController: {
// method: RequestMethod.POST,
// path: "/",
// },
}),
],
providers: [Worker],
})
export class WorkerAppModule {}
You can also define your own Controller. In that case, set workerController to null.
@Controller("/worker")
class WorkerController {
constructor(private readonly service: QueueWorkerService) {}
@Post()
public async execute(@Body() body: QueueWorkerReceivedMessage): Promise<void> {
const results = await this.service.execute(body.message);
for (const result of results) {
console.log(result.success);
}
// or
// const decodedMessage = decodeMessage(body.message);
// const results = await this.service.execute(decodedMessage);
}
}
@Module({
controllers: [WorkerController],
imports: [
QueueWorkerModule.register({
workerController: null,
}),
],
providers: [Worker],
})
export class WorkerAppModule {}
async function bootstrap(): Promise<void> {
const app = await NestFactory.create(WorkerAppModule);
await app.listen(process.env.PORT || 8080);
}
bootstrap();
You can disable worker/provider by setting enabled
to false.
For example, if you are reusing the same application, you can disable process on Cloud Run Service and enable it to run on Cloud Run Job.
@QueueWorker({ name: "Worker name", enabled: config.isEnabledWorker })
class Worker {
@QueueWorkerProcess({ enabled: config.isEnabledProcess })
public async process(message: string | object, raw: QueueWorkerRawMessage): Promise<void> {}
@QueueWorkerProcess()
public async process2(message: string | object, raw: QueueWorkerRawMessage): Promise<void> {
console.log("Message:", message);
console.log("Raw message:", raw);
}
}
You can execute worker/processor manually.
@Controller("/worker")
class WorkerController {
constructor(private readonly service: QueueWorkerService) {}
@Post()
public async execute(@Body() body: QueueWorkerReceivedMessage): Promise<void> {
const workers = await this.service.getWorkers(body.message);
for (const worker of workers) {
const processors = worker.getProcessors();
for (const processor of processors) {
const result = await processor.execute();
if (result.success) {
console.log("Success");
} else {
console.log("Failed:" + result.error.message);
}
}
}
}
}
If you want to set something up using worker metadata, you can retrieve all workers and process them.
async function bootstrap(): Promise<void> {
const app = await NestFactory.create(WorkerAppModule);
const service = app.get(QueueWorkerService);
const allWorkers = service.getAllWorkers();
const allProcessors = allWorkers.flatMap((w) => w.processors);
// Do somethings...
}
You can use Cloud Scheduler as trigger.
Payload is JSON string {"name": "worker name", "data": "str"}
There may be times when you want to use it for a one-time call, such as Cloud Run jobs.
import { QueueWorkerService } from "@anchan828/nest-cloud-run-queue-worker";
async function bootstrap(): Promise<void> {
const app = await NestFactory.createApplicationContext(WorkerAppModule);
await app.get(QueueWorkerService).execute({
name: "worker name",
data: "str",
});
}
bootstrap();