From 1683c8a090c3efc51c43107d5ede0dcd5d506e3b Mon Sep 17 00:00:00 2001
From: Ferenc Szontágh <szf@fsociety.hu>
Date: Sun, 04 Feb 2024 20:45:08 +0000
Subject: [PATCH] added better queue manager, some clean-up and new feature: model management (wip), progressbar

---
 ui/QueueManager.cpp |  108 ++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 100 insertions(+), 8 deletions(-)

diff --git a/ui/QueueManager.cpp b/ui/QueueManager.cpp
index bbe73cd..b8efdbc 100644
--- a/ui/QueueManager.cpp
+++ b/ui/QueueManager.cpp
@@ -7,21 +7,33 @@
     this->eventHandler->Bind(wxEVT_THREAD, &QueueManager::OnThreadMessage, this);
     this->jobsDir = jobsdir;
     this->QueueList = std::map<int, QM::QueueItem>();
+    this->LoadJobListFromDir();
+}
+
+QM::QueueManager::~QueueManager()
+{
 }
 
 int QM::QueueManager::AddItem(QM::QueueItem item)
 {
-    if (!item.id)
+    if (item.id == 0)
     {
-        item.id = this->GetCurrentUnixTimestamp();
+        item.id = this->GetAnId();
     }
+    if (item.created_at == 0)
+    {
+        item.created_at = this->GetCurrentUnixTimestamp();
+    }
+
     this->QueueList[item.id] = item;
+
     this->SendEventToMainWindow(QM::QueueEvents::ITEM_ADDED, item);
-    if (this->isRunning == false)
+    if (this->isRunning == false && item.status == QM::QueueStatus::PENDING)
     {
         this->SendEventToMainWindow(QM::QueueEvents::ITEM_START, item);
         this->isRunning = true;
     }
+    this->SaveJobToFile(item);
     return item.id;
 }
 
@@ -29,7 +41,6 @@
 {
     QM::QueueItem item;
     item.params = *params;
-    item.created_at = item.id = this->GetCurrentUnixTimestamp();
     return this->AddItem(item);
 }
 
@@ -37,7 +48,6 @@
 {
     QM::QueueItem item;
     item.params = params;
-    item.created_at = item.id = this->GetCurrentUnixTimestamp();
     return this->AddItem(item);
 }
 
@@ -85,6 +95,12 @@
     if (this->QueueList.find(id) != this->QueueList.end())
     {
         this->QueueList[id].status = status;
+        this->QueueList[id].updated_at = this->GetCurrentUnixTimestamp();
+        if (status == QM::QueueStatus::DONE)
+        {
+            this->QueueList[id].finished_at = this->GetCurrentUnixTimestamp();
+        }
+        this->SaveJobToFile(this->QueueList[id]);
         this->SendEventToMainWindow(QM::QueueEvents::ITEM_STATUS_CHANGED, this->QueueList[id]);
     }
 }
@@ -112,7 +128,10 @@
 void QM::QueueManager::OnThreadMessage(wxThreadEvent &e)
 {
 
-    e.Skip();
+    if (e.GetSkipped() == false)
+    {
+        e.Skip();
+    }
     auto msg = e.GetString().ToStdString();
 
     std::string token = msg.substr(0, msg.find(":"));
@@ -128,6 +147,7 @@
         {
             this->SetStatus(QM::QueueStatus::RUNNING, payload.id);
             this->isRunning = true;
+            return;
         }
         if (event == QM::QueueEvents::ITEM_FINISHED)
         {
@@ -147,7 +167,7 @@
                     break;
                 }
             }
-            // move finished job into another list - ??
+            return;
         }
     }
 
@@ -177,10 +197,26 @@
         this->SetStatus(QM::QueueStatus::RUNNING, payload.id);
         this->isRunning = true;
     }
-    // nothing to todo here, the payload is the generated image list
+    // nothing to todo here, the payload is the generated image list, we can't find whitch item was it...
+    // TODO: use struct to payload, and store multiple items in it...
     if (token == "GENERATION_DONE")
     {
     }
+}
+
+void QM::QueueManager::SaveJobToFile(int id)
+{
+    auto item = this->GetItem(id);
+    this->SaveJobToFile(item);
+}
+
+void QM::QueueManager::SaveJobToFile(QM::QueueItem item)
+{
+    nlohmann::json jsonfile(item);
+    std::string filename = this->jobsDir + "/" + std::to_string(item.id) + ".json";
+    std::ofstream file(filename);
+    file << jsonfile;
+    file.close();
 }
 
 int QM::QueueManager::GetCurrentUnixTimestamp()
@@ -189,6 +225,62 @@
     return static_cast<int>(std::chrono::duration_cast<std::chrono::seconds>(p1.time_since_epoch()).count());
 }
 
+void QM::QueueManager::LoadJobListFromDir()
+{
+
+    if (!std::filesystem::exists(this->jobsDir))
+    {
+        std::filesystem::create_directories(this->jobsDir);
+    }
+
+    int i = 0;
+    for (auto const &dir_entry : std::filesystem::recursive_directory_iterator(this->jobsDir))
+    {
+        if (!dir_entry.exists() || !dir_entry.is_regular_file() || !dir_entry.path().has_extension())
+        {
+            continue;
+        }
+
+        std::filesystem::path path = dir_entry.path();
+
+        std::string ext = path.extension().string();
+
+        if (ext != ".json")
+        {
+            continue;
+        }
+
+        std::string name = path.filename().replace_extension("").string();
+
+        std::ifstream f(path.string());
+        try
+        {
+            nlohmann::json data = nlohmann::json::parse(f);
+            auto item = data.get<QM::QueueItem>();
+            if (item.status == QM::QueueStatus::RUNNING)
+            {
+                item.status = QM::QueueStatus::FAILED;
+            }
+            this->AddItem(item);
+        }
+        catch (const std::exception &e)
+        {
+            std::cerr << e.what() << '\n';
+        }
+    }
+}
+
+int QM::QueueManager::GetAnId()
+{
+    int id = this->GetCurrentUnixTimestamp();
+    while (id <= this->lastId)
+    {
+        id++;
+    }
+    this->lastId = id;
+    return id;
+}
+
 void QM::QueueManager::onItemAdded(QM::QueueItem item)
 {
     // this->parent->m_joblist

--
Gitblit v1.9.3