diff --git a/api/save-token-api.js b/api/save-token-api.js index dbba90c..579dfa2 100644 --- a/api/save-token-api.js +++ b/api/save-token-api.js @@ -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'); } } diff --git a/api/send-notification-api.js b/api/send-notification-api.js index fdf5202..df7bef1 100644 --- a/api/send-notification-api.js +++ b/api/send-notification-api.js @@ -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 { diff --git a/noti.js b/noti.js index 156346c..5217890 100644 --- a/noti.js +++ b/noti.js @@ -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; @@ -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()); diff --git a/worker/process/save-token.js b/worker/process/save-token.js new file mode 100644 index 0000000..33a6dad --- /dev/null +++ b/worker/process/save-token.js @@ -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, +} diff --git a/worker/worker.js b/worker/worker.js index d55c747..d402afe 100644 --- a/worker/worker.js +++ b/worker/worker.js @@ -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); @@ -15,6 +20,8 @@ function setupWorker() { if (job.name == 'send-notification') { return await sendNotificationProcess(job); } + + }, { connection: redisOptions,