Last active
July 30, 2021 10:47
-
-
Save witalobenicio/4a62273fa457b64ccb555672b6faca64 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import QueueManager from '@queue/QueueManager'; | |
import { MessagesManager } from '@managers/messages/MessagesManager'; | |
import TaskHandlerBuilder from '@workers/handlers/task/TaskHandlerBuilder'; | |
import { IQueue } from '@queue/types/Queue'; | |
import QueueToDo from '@queue/types/QueueToDo'; | |
import Processor from '@workers/processor/Processor'; | |
import ToDoProcessorQueue from '@workers/processor/ToDoProcessorQueue'; | |
export default () => { | |
checkForMessages(); | |
}; | |
async function checkForMessages(): Promise<void> { | |
return checkMessages(QueueToDo, ToDoProcessorQueue.getInstance()); | |
} | |
async function checkMessages(queue: IQueue, processor: Processor): Promise<void> { | |
const messagesController = new MessagesManager(TaskHandlerBuilder.generate()); | |
const queueController = new QueueManager(); | |
const quantity = processor.getAvailableBatch(); | |
if (quantity && quantity > 0) { | |
try { | |
const messages = await queueController.receive(queue, 1); | |
await messagesController.distribute(messages, queue); | |
} catch (e) { | |
console.log('ERROR GETTING MESSAGES AND DISTRIBUTING', e); | |
} finally { | |
checkMessages(queue, processor); | |
} | |
} else { | |
setTimeout(() => { | |
checkMessages(queue, processor); | |
}, 2 * 1000); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment