2023-07-21 21:32:24 +00:00
|
|
|
#include "taskmanager.h"
|
|
|
|
|
|
|
|
#include "flactomp3.h"
|
|
|
|
|
2023-10-08 23:29:40 +00:00
|
|
|
TaskManager::TaskManager(const std::shared_ptr<Settings>& settings, const std::shared_ptr<Printer>& logger):
|
2023-10-07 22:36:20 +00:00
|
|
|
settings(settings),
|
2023-10-08 23:29:40 +00:00
|
|
|
logger(logger),
|
2023-07-21 21:32:24 +00:00
|
|
|
busyThreads(0),
|
|
|
|
maxTasks(0),
|
|
|
|
completeTasks(0),
|
|
|
|
terminate(false),
|
2023-10-08 23:29:40 +00:00
|
|
|
running(false),
|
2023-07-21 21:32:24 +00:00
|
|
|
queueMutex(),
|
|
|
|
loopConditional(),
|
|
|
|
waitConditional(),
|
|
|
|
threads(),
|
2023-10-08 23:29:40 +00:00
|
|
|
jobs()
|
2023-07-21 21:32:24 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TaskManager::~TaskManager() {
|
|
|
|
}
|
|
|
|
|
2023-10-07 22:36:20 +00:00
|
|
|
void TaskManager::queueJob(const std::filesystem::path& source, const std::filesystem::path& destination) {
|
2023-10-08 23:29:40 +00:00
|
|
|
std::unique_lock<std::mutex> lock(queueMutex);
|
|
|
|
jobs.emplace(source, destination);
|
|
|
|
|
2023-07-21 21:32:24 +00:00
|
|
|
++maxTasks;
|
2023-10-08 23:29:40 +00:00
|
|
|
logger->setStatusMessage(std::to_string(completeTasks) + "/" + std::to_string(maxTasks));
|
|
|
|
|
|
|
|
lock.unlock();
|
2023-07-21 21:32:24 +00:00
|
|
|
loopConditional.notify_one();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TaskManager::busy() const {
|
2023-10-08 23:29:40 +00:00
|
|
|
std::lock_guard lock(queueMutex);
|
|
|
|
return !jobs.empty();
|
2023-07-21 21:32:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TaskManager::start() {
|
2023-10-08 23:29:40 +00:00
|
|
|
std::lock_guard lock(queueMutex);
|
|
|
|
if (running)
|
|
|
|
return;
|
|
|
|
|
2023-10-09 21:47:00 +00:00
|
|
|
unsigned int amount = settings->getThreads();
|
|
|
|
if (amount == 0)
|
|
|
|
amount = std::thread::hardware_concurrency();
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < amount; ++i)
|
2023-07-21 21:32:24 +00:00
|
|
|
threads.emplace_back(std::thread(&TaskManager::loop, this));
|
2023-10-08 23:29:40 +00:00
|
|
|
|
|
|
|
running = true;
|
2023-07-21 21:32:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TaskManager::loop() {
|
|
|
|
while (true) {
|
2023-10-08 23:29:40 +00:00
|
|
|
std::unique_lock<std::mutex> lock(queueMutex);
|
|
|
|
while (!terminate && jobs.empty())
|
|
|
|
loopConditional.wait(lock);
|
|
|
|
|
|
|
|
if (terminate)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::pair<std::string, std::string> pair = jobs.front();
|
|
|
|
++busyThreads;
|
|
|
|
jobs.pop();
|
|
|
|
lock.unlock();
|
2023-07-21 21:32:24 +00:00
|
|
|
|
2023-10-07 22:36:20 +00:00
|
|
|
JobResult result;
|
|
|
|
switch (settings->getType()) {
|
|
|
|
case Settings::mp3:
|
2023-10-08 23:29:40 +00:00
|
|
|
result = mp3Job(pair.first, pair.second + ".mp3", settings->getLogLevel());
|
|
|
|
break;
|
|
|
|
default:
|
2023-10-07 22:36:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-10-08 23:29:40 +00:00
|
|
|
lock.lock();
|
2023-07-21 21:32:24 +00:00
|
|
|
++completeTasks;
|
2023-10-08 23:29:40 +00:00
|
|
|
logger->printNested(
|
|
|
|
result.first ? "Encoding complete but there are messages about it" : "Encoding failed!",
|
|
|
|
{"Source: \t" + pair.first, "Destination: \t" + pair.second},
|
|
|
|
result.second,
|
|
|
|
std::to_string(completeTasks) + "/" + std::to_string(maxTasks)
|
|
|
|
);
|
2023-07-21 21:32:24 +00:00
|
|
|
--busyThreads;
|
2023-10-08 23:29:40 +00:00
|
|
|
lock.unlock();
|
2023-07-21 21:32:24 +00:00
|
|
|
waitConditional.notify_all();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TaskManager::stop() {
|
2023-10-08 23:29:40 +00:00
|
|
|
std::unique_lock lock(queueMutex);
|
|
|
|
if (!running)
|
|
|
|
return;
|
|
|
|
|
|
|
|
terminate = true;
|
2023-07-21 21:32:24 +00:00
|
|
|
|
2023-10-08 23:29:40 +00:00
|
|
|
lock.unlock();
|
2023-07-21 21:32:24 +00:00
|
|
|
loopConditional.notify_all();
|
|
|
|
for (std::thread& thread : threads)
|
|
|
|
thread.join();
|
|
|
|
|
2023-10-08 23:29:40 +00:00
|
|
|
lock.lock();
|
2023-07-21 21:32:24 +00:00
|
|
|
threads.clear();
|
2023-10-08 23:29:40 +00:00
|
|
|
running = false;
|
|
|
|
logger->clearStatusMessage();
|
2023-07-21 21:32:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TaskManager::wait() {
|
2023-10-08 23:29:40 +00:00
|
|
|
std::unique_lock lock(queueMutex);
|
|
|
|
while (busyThreads != 0 || !jobs.empty())
|
|
|
|
waitConditional.wait(lock);
|
2023-07-21 21:32:24 +00:00
|
|
|
}
|
|
|
|
|
2023-10-08 23:29:40 +00:00
|
|
|
unsigned int TaskManager::getCompleteTasks() const {
|
|
|
|
std::lock_guard lock(queueMutex);
|
|
|
|
return completeTasks;
|
2023-07-21 21:32:24 +00:00
|
|
|
}
|
2023-07-23 12:04:26 +00:00
|
|
|
|
2023-10-08 23:29:40 +00:00
|
|
|
TaskManager::JobResult TaskManager::mp3Job(
|
2023-10-07 22:36:20 +00:00
|
|
|
const std::filesystem::path& source,
|
2023-10-08 23:29:40 +00:00
|
|
|
const std::filesystem::path& destination,
|
|
|
|
Logger::Severity logLevel)
|
2023-10-07 22:36:20 +00:00
|
|
|
{
|
2023-10-08 23:29:40 +00:00
|
|
|
FLACtoMP3 convertor(logLevel);
|
|
|
|
convertor.setInputFile(source);
|
|
|
|
convertor.setOutputFile(destination);
|
|
|
|
bool result = convertor.run();
|
|
|
|
return {result, convertor.getHistory()};
|
2023-07-23 12:04:26 +00:00
|
|
|
}
|