Skip to content

Commit

Permalink
feat: save-token 또한 worker에 job으로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
JeongSH1 committed Dec 12, 2023
1 parent 9f4ed8a commit 9815069
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 19 deletions.
10 changes: 4 additions & 6 deletions api/save-token-api.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
const { TokenManager } = require("../manager/token-manager.js");
const { taskQueue } = require("../bull/task-queue.js");


async function saveTokenAPI(req, res) {
const { userId, token } = req.body;
const tokenManager = new TokenManager()
const { token, userId } = req.body;
try {
await tokenManager.saveToken(token, userId);
console.log(`[${new Date().toUTCString()}] [COMPLETED] ${userId} saved token`);
await taskQueue.add('save-token', { token, userId });
res.status(200).send('Token saved successfully');
} catch (error) {
console.log(`[${new Date().toUTCString()}] [FAILED] ${userId} saved token`)
res.status(500).send('Internal Server Error');
}
}
Expand Down
13 changes: 0 additions & 13 deletions api/send-notification-api.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
const { QueueEvents } = require('bullmq');
const { taskQueue } = require('../bull/task-queue.js');
const { redisOptions } = require('../bull/redis-option.js');


const queueEvents = new QueueEvents('task-queue', redisOptions);

queueEvents.on('completed', (job) => {
if (job.returnvalue.jobName === 'resolve-token') {
for (const token of job.returnvalue.tokens) {
taskQueue.add('send-notification', { token: token.token, title: job.returnvalue.title, body: job.returnvalue.body })
}
}

})

async function sendNotificationAPI(req, res) {
const { userId, title = '무제', body = 'blank' } = req.body;
try {
Expand Down
11 changes: 11 additions & 0 deletions noti.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const { BullMQAdapter } = require('@bull-board/api/bullMQAdapter');
const { taskQueue } = require ('./bull/task-queue.js');
const { saveTokenAPI } = require('./api/save-token-api.js');
const { sendNotificationAPI } = require('./api/send-notification-api.js');
const { redisOptions } = require('./bull/redis-option.js');
const { QueueEvents } = require('bullmq');

const app = express();
const port = process.env.PORT || 3000;
Expand All @@ -16,6 +18,15 @@ createBullBoard({
serverAdapter,
});

const queueEvents = new QueueEvents('task-queue', redisOptions);

queueEvents.on('completed', (job) => {
if (job.returnvalue.jobName === 'resolve-token') {
for (const token of job.returnvalue.tokens) {
taskQueue.add('send-notification', { token: token.token, title: job.returnvalue.title, body: job.returnvalue.body })
}
}
})


app.use(express.json());
Expand Down
12 changes: 12 additions & 0 deletions worker/process/save-token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { TokenManager } = require("../../manager/token-manager.js");

const saveTokenProcess = async (job) => {
const tokenManager = new TokenManager();
const { token, userId } = job.data;
await tokenManager.saveToken(token, userId);
return { jobName: job.name };
}

module.exports = {
saveTokenProcess,
}
7 changes: 7 additions & 0 deletions worker/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ const { Worker } = require('bullmq');
const { resolveTokenProcess } = require('./process/resolve-token.js');
const { sendNotificationProcess } = require('./process/send-notification.js');
const { redisOptions } = require('../bull/redis-option.js');
const { saveTokenProcess } = require('./process/save-token.js');

function setupWorker() {
const worker = new Worker(
'task-queue',
async (job) => {

if (job.name == 'save-token') {
return await saveTokenProcess(job);
}

if (job.name === 'resolve-token') {
return await resolveTokenProcess(job);
Expand All @@ -15,6 +20,8 @@ function setupWorker() {
if (job.name == 'send-notification') {
return await sendNotificationProcess(job);
}


},
{
connection: redisOptions,
Expand Down

0 comments on commit 9815069

Please sign in to comment.