-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from J-Hoplin/feat/queue-strategy
Feat/queue strategy
- Loading branch information
Showing
40 changed files
with
583 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,28 @@ | ||
# Environment variables declared in this file are automatically made available to Prisma. | ||
# See the documentation for more detail: https://pris.ly/d/prisma-schema#accessing-environment-variables-from-the-schema | ||
# .env file for docker webserver | ||
# Application | ||
|
||
# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB. | ||
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings | ||
TYPE="webserver" # 'webserver' or 'worker' | ||
|
||
# Base | ||
DATABASE_URL="mysql://root:hoplin1234!@db:3306/judge?schema=public" | ||
|
||
ADMIN_EMAIL="hoplin.dev@gmail.com" | ||
ADMIN_PW = "admin" | ||
|
||
JWT_SECRET="SECRET" | ||
JUDGE_SERVER_ENDPOINT="a" | ||
ENV="a" # dev or production | ||
PORT="3000" | ||
|
||
JUDGE_SERVER_ENDPOINT="" | ||
|
||
ENV="" | ||
PORT="" | ||
# Queue | ||
QUEUE_TYPE="RMQ" # AWS or RMQ | ||
RMQ_URL="amqp://root:password@rmq:5672" | ||
RMQ_WORKER_QUEUE_NAME="JUDGE_QUEUE" | ||
|
||
# AWS | ||
AWS_REGION="" | ||
AWS_ACCESS_ID="" | ||
AWS_ACCESS_SECRET="" | ||
AWS_SQS_QUEUE="" | ||
AWS_S3_BUCKET="" | ||
AWS_REGION="a" | ||
AWS_ACCESS_ID="a" | ||
AWS_ACCESS_SECRET="a" | ||
AWS_SQS_QUEUE="a" | ||
AWS_S3_BUCKET="a" | ||
|
||
# Sentry | ||
SENTRY_DSN="" | ||
SENTRY_DSN="a" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# .env file for docker worker | ||
# Application | ||
|
||
TYPE="worker" # 'webserver' or 'worker' | ||
|
||
# Base | ||
DATABASE_URL="mysql://root:hoplin1234!@db:3306/judge?schema=public" | ||
ADMIN_EMAIL="hoplin.dev@gmail.com" | ||
ADMIN_PW = "admin" | ||
JWT_SECRET="SECRET" | ||
JUDGE_SERVER_ENDPOINT="a" | ||
ENV="a" # dev or production | ||
PORT="3000" | ||
|
||
# Queue | ||
QUEUE_TYPE="RMQ" # AWS or RMQ | ||
RMQ_URL="amqp://root:password@rmq:5672" | ||
RMQ_WORKER_QUEUE_NAME="JUDGE_QUEUE" | ||
|
||
# AWS | ||
AWS_REGION="a" | ||
AWS_ACCESS_ID="a" | ||
AWS_ACCESS_SECRET="a" | ||
AWS_SQS_QUEUE="a" | ||
AWS_S3_BUCKET="a" | ||
|
||
# Sentry | ||
SENTRY_DSN="a" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules/ | ||
node_modules/ | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import * as amqp from 'amqplib'; | ||
|
||
/** | ||
* | ||
* Method decorator of Rabbit MQ Strategy | ||
* | ||
*/ | ||
export function RabbitMQConenction(): MethodDecorator { | ||
return function ( | ||
target: any, | ||
propertyKey: string, | ||
descriptor: PropertyDescriptor, | ||
) { | ||
// Preserve original function | ||
const fn: (...args: any[]) => any = descriptor.value; | ||
descriptor.value = async function (...args) { | ||
// Connect to RMQ | ||
const connection = await amqp.connect(process.env.RMQ_URL); | ||
// Assert Channel | ||
const channel = await connection.createChannel(); | ||
// Assert Queue | ||
await channel.assertQueue(process.env.RMQ_WORKER_QUEUE_NAME, { | ||
durable: true, | ||
}); | ||
const task: string = await fn.apply(this, args); | ||
channel.sendToQueue(process.env.RMQ_WORKER_QUEUE_NAME, Buffer.from(task)); | ||
setTimeout(() => { | ||
connection.close(); | ||
}, 500); | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './queue.module'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Module, Provider } from '@nestjs/common'; | ||
import { QueueService } from './strategy/queue-strategy.abstract.service'; | ||
import { RabbitMQService } from './strategy/rmq.service'; | ||
import { AwsSqsQueueService } from './strategy'; | ||
import { ClientsModule, Transport } from '@nestjs/microservices'; | ||
import { RMQ_TOKEN } from './type'; | ||
|
||
@Module({ | ||
imports: | ||
process.env.QUEUE_TYPE === 'RMQ' | ||
? [ | ||
ClientsModule.register([ | ||
{ | ||
name: RMQ_TOKEN, | ||
transport: Transport.RMQ, | ||
options: { | ||
urls: [process.env.RMQ_URL], | ||
queue: process.env.RMQ_WORKER_QUEUE_NAME, | ||
queueOptions: { | ||
durable: true, | ||
}, | ||
}, | ||
}, | ||
]), | ||
] | ||
: [], | ||
providers: [ | ||
AwsSqsQueueService, | ||
RabbitMQService, | ||
{ | ||
useClass: | ||
process.env.QUEUE_TYPE === 'RMQ' ? RabbitMQService : AwsSqsQueueService, | ||
provide: QueueService, | ||
}, | ||
], | ||
exports: [QueueService, AwsSqsQueueService, RabbitMQService], | ||
}) | ||
export class QueueModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { QueueService } from './queue.service'; | ||
|
||
describe('QueueService', () => { | ||
let service: QueueService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [QueueService], | ||
}).compile(); | ||
|
||
service = module.get<QueueService>(QueueService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './aws-sqs.service'; | ||
export * from './queue-strategy.abstract.service'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { QueueTask } from './type'; | ||
|
||
export abstract class QueueService { | ||
abstract sendTask(task: QueueTask): Promise<void>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { ClientProxy } from '@nestjs/microservices'; | ||
import { RMQ_TOKEN } from '../type'; | ||
import { QueueService } from './queue-strategy.abstract.service'; | ||
import { QueueTask } from './type'; | ||
|
||
@Injectable() | ||
export class RabbitMQService extends QueueService { | ||
// Basic name of workerQueueName | ||
|
||
constructor(@Inject(RMQ_TOKEN) private client: ClientProxy) { | ||
super(); | ||
// If Rabbit MQ URL not found | ||
if (!process.env.RMQ_URL) { | ||
throw new Error('Rabbit MQ URL not found'); | ||
} | ||
} | ||
|
||
async sendTask(task: QueueTask) { | ||
// Send returns cold Observable. Requires to explicit subscribe before sent | ||
// https://docs.nestjs.com/microservices/basics#sending-messages | ||
this.client.send(task.message, task).subscribe(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
export enum SQSMessageTypeEnum { | ||
RE_CORRECTION, | ||
CODE_SUBMIT, | ||
RE_CORRECTION = 'RE_CORRECTION', | ||
CODE_SUBMIT = 'CODE_SUBMIT', | ||
} | ||
|
||
export type SQSMessageType = keyof typeof SQSMessageTypeEnum; | ||
|
||
export type SQSTask = { | ||
export type QueueTask = { | ||
message: SQSMessageType; | ||
id: string | number; | ||
id: number; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const RMQ_TOKEN = 'RMQ_CLIENT'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.