Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated: queue init at module level #48

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions src/services/jobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ const httpResponse = require('@constants/httpResponse')
const { redisConfiguration } = require('@configs/redis')
const { Queue } = require('bullmq')

// Initialize Queue once at the module level
const myQueue = new Queue(process.env.DEFAULT_QUEUE, redisConfiguration)

exports.create = async (requestBody) => {
try {
const myQueue = new Queue(process.env.DEFAULT_QUEUE, redisConfiguration)

const jobExists = await myQueue.getJob(requestBody.jobOptions.jobId)
const allRepeatableJobs = await myQueue.getRepeatableJobs()
const repeatableJobExists = allRepeatableJobs.find((job) => job.id === requestBody.jobOptions.jobId)
Expand All @@ -27,9 +28,7 @@ exports.create = async (requestBody) => {
})
}

const jobDetails = Object.assign({}, requestBody)
delete jobDetails.jobName
delete jobDetails.jobOptions
const { jobName, jobOptions, ...jobDetails } = requestBody

const newJob = await myQueue.add(requestBody.jobName, jobDetails, requestBody.jobOptions)

Expand All @@ -55,9 +54,9 @@ exports.create = async (requestBody) => {
})
}
}

exports.updateDelay = async (requestBody) => {
try {
const myQueue = new Queue(process.env.DEFAULT_QUEUE, redisConfiguration)
const jobExists = await myQueue.getJob(requestBody.id)

if (!jobExists) {
Expand Down Expand Up @@ -86,10 +85,9 @@ exports.updateDelay = async (requestBody) => {
})
}
}

exports.remove = async (requestBody) => {
try {
const myQueue = new Queue(process.env.DEFAULT_QUEUE, redisConfiguration)

const jobExists = await myQueue.getJob(requestBody.jobId)

const allRepeatableJobs = await myQueue.getRepeatableJobs()
Expand Down Expand Up @@ -141,8 +139,6 @@ exports.list = async (query) => {
try {
const filters = query.filter ? query.filter.split(',') : [] // Split the filter string if it exists, otherwise use an empty array

const myQueue = new Queue(process.env.DEFAULT_QUEUE, redisConfiguration)

let allJobs = await myQueue.getJobs(filters, 0, -1, true)
allJobs.forEach((pendingJob) => {
delete pendingJob['data']
Expand Down Expand Up @@ -172,7 +168,6 @@ exports.list = async (query) => {

exports.purge = async (requestBody) => {
try {
const myQueue = new Queue(process.env.DEFAULT_QUEUE, redisConfiguration)
let isDeleted, deletedJobIds, responseMessageKey
switch (requestBody.method) {
case 'clean':
Expand Down