Skip to content

Commit

Permalink
Fix multitasking in Worker
Browse files Browse the repository at this point in the history
  • Loading branch information
potat-dev committed May 24, 2024
1 parent 9f6a123 commit 295b842
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/rabbitCore/worker/RabbitWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,8 @@ void RabbitWorker::startPolling() {
std::cout << "RabbitWorker::startPolling - Received task request" << std::endl;
struct TaskRequest task = messageData.get<struct TaskRequest>();
json data = json::parse(task.data);

if (mapping.find(task.func) != mapping.end()) {
std::cout << "RabbitWorker::startPolling - Executing handler for func: " << task.func << ", id: "
<< task.id << std::endl;
(this->*mapping[task.func])(task.id, data, task.cores);
} else {
std::cout << "RabbitWorker::startPolling - Function not found: " << task.func << std::endl;
}
// Call a separate function to handle the task asynchronously
handleTaskRequest(task.id, task.func, data, task.cores);
break;
}
default:
Expand All @@ -108,6 +102,24 @@ void RabbitWorker::startPolling() {
}
}

void RabbitWorker::handleTaskRequest(const std::string &request_id, const std::string &func, const json &data,
int taskCores) {
// Check if the function exists in the mapping
if (mapping.find(func) == mapping.end()) {
std::cout << "RabbitWorker::handleTaskRequest - Function not found: " << func << std::endl;
return; // Exit early if the function doesn't exist
}

// Create a new thread for each task request
std::thread taskThread([this, request_id, func, data, taskCores]() {
// Directly call the corresponding handler
(this->*mapping[func])(request_id, data, taskCores);
});

// Detach the thread to run asynchronously without waiting
taskThread.detach();
}

// worker function implementations

void RabbitWorker::doWait(int seconds) {
Expand Down
2 changes: 2 additions & 0 deletions src/rabbitCore/worker/RabbitWorker.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class RabbitWorker {
void matrixMultiplicationHandler(const std::string& id, json data, int taskCores);

func_map_type mapping;

void handleTaskRequest(const std::string &request_id, const std::string &func, const json &data, int taskCores);
};


Expand Down

0 comments on commit 295b842

Please sign in to comment.