2023-07-21 21:32:24 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <mutex>
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <thread>
|
|
|
|
#include <functional>
|
2023-10-07 22:36:20 +00:00
|
|
|
#include <filesystem>
|
2023-07-21 21:32:24 +00:00
|
|
|
#include <vector>
|
2023-07-23 12:04:26 +00:00
|
|
|
#include <list>
|
2023-07-21 21:32:24 +00:00
|
|
|
#include <queue>
|
|
|
|
#include <string>
|
|
|
|
#include <atomic>
|
2023-07-23 12:04:26 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <array>
|
2023-10-07 22:36:20 +00:00
|
|
|
#include <memory>
|
2023-07-23 12:04:26 +00:00
|
|
|
|
2023-10-07 22:36:20 +00:00
|
|
|
#include "settings.h"
|
2023-10-08 23:29:40 +00:00
|
|
|
#include "logger/printer.h"
|
2023-07-21 21:32:24 +00:00
|
|
|
|
|
|
|
class TaskManager {
|
2023-10-13 01:00:16 +00:00
|
|
|
using JobResult = std::pair<bool, std::list<Logger::Message>>;
|
|
|
|
struct Job;
|
2023-07-21 21:32:24 +00:00
|
|
|
public:
|
2023-10-08 23:29:40 +00:00
|
|
|
TaskManager(const std::shared_ptr<Settings>& settings, const std::shared_ptr<Printer>& logger);
|
2023-07-21 21:32:24 +00:00
|
|
|
~TaskManager();
|
|
|
|
|
|
|
|
void start();
|
2023-10-13 01:00:16 +00:00
|
|
|
void queueConvert(const std::filesystem::path& source, const std::filesystem::path& destination);
|
|
|
|
void queueCopy(const std::filesystem::path& source, const std::filesystem::path& destination);
|
2023-07-21 21:32:24 +00:00
|
|
|
void stop();
|
|
|
|
bool busy() const;
|
|
|
|
void wait();
|
2023-10-08 23:29:40 +00:00
|
|
|
|
|
|
|
unsigned int getCompleteTasks() const;
|
2023-07-21 21:32:24 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void loop();
|
2023-10-13 01:00:16 +00:00
|
|
|
JobResult execute(const Job& job);
|
|
|
|
void printResilt(const Job& job, const JobResult& result);
|
|
|
|
static JobResult mp3Job(const Job& job, Logger::Severity logLevel);
|
|
|
|
static JobResult copyJob(const Job& job, Logger::Severity logLevel);
|
|
|
|
|
2023-07-21 21:32:24 +00:00
|
|
|
private:
|
2023-10-07 22:36:20 +00:00
|
|
|
std::shared_ptr<Settings> settings;
|
2023-10-08 23:29:40 +00:00
|
|
|
std::shared_ptr<Printer> logger;
|
|
|
|
unsigned int busyThreads;
|
|
|
|
unsigned int maxTasks;
|
|
|
|
unsigned int completeTasks;
|
2023-07-21 21:32:24 +00:00
|
|
|
bool terminate;
|
2023-10-08 23:29:40 +00:00
|
|
|
bool running;
|
2023-07-21 21:32:24 +00:00
|
|
|
mutable std::mutex queueMutex;
|
|
|
|
std::condition_variable loopConditional;
|
|
|
|
std::condition_variable waitConditional;
|
|
|
|
std::vector<std::thread> threads;
|
2023-10-13 01:00:16 +00:00
|
|
|
std::queue<Job> jobs;
|
2023-07-21 21:32:24 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2023-10-13 01:00:16 +00:00
|
|
|
struct TaskManager::Job {
|
|
|
|
enum Type {
|
|
|
|
copy,
|
|
|
|
convert
|
|
|
|
};
|
|
|
|
Job(Type type, const std::filesystem::path& source, std::filesystem::path destination);
|
|
|
|
Type type;
|
|
|
|
std::filesystem::path source;
|
|
|
|
std::filesystem::path destination;
|
|
|
|
};
|