From 2088e1b7aa6419dec58800bc1d0cb24f5808affe Mon Sep 17 00:00:00 2001
From: Ferenc Szontágh <szf@fsociety.hu>
Date: Sat, 03 Feb 2024 21:19:43 +0000
Subject: [PATCH] added queue handler against simple start

---
 ui/QueueManager.cpp |   93 ++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 80 insertions(+), 13 deletions(-)

diff --git a/ui/QueueManager.cpp b/ui/QueueManager.cpp
index 126c7b5..bbe73cd 100644
--- a/ui/QueueManager.cpp
+++ b/ui/QueueManager.cpp
@@ -6,6 +6,7 @@
     this->eventHandler = eventHandler;
     this->eventHandler->Bind(wxEVT_THREAD, &QueueManager::OnThreadMessage, this);
     this->jobsDir = jobsdir;
+    this->QueueList = std::map<int, QM::QueueItem>();
 }
 
 int QM::QueueManager::AddItem(QM::QueueItem item)
@@ -16,6 +17,11 @@
     }
     this->QueueList[item.id] = item;
     this->SendEventToMainWindow(QM::QueueEvents::ITEM_ADDED, item);
+    if (this->isRunning == false)
+    {
+        this->SendEventToMainWindow(QM::QueueEvents::ITEM_START, item);
+        this->isRunning = true;
+    }
     return item.id;
 }
 
@@ -24,8 +30,7 @@
     QM::QueueItem item;
     item.params = *params;
     item.created_at = item.id = this->GetCurrentUnixTimestamp();
-    this->AddItem(item);
-    return item.id;
+    return this->AddItem(item);
 }
 
 int QM::QueueManager::AddItem(sd_gui_utils::SDParams params)
@@ -33,8 +38,7 @@
     QM::QueueItem item;
     item.params = params;
     item.created_at = item.id = this->GetCurrentUnixTimestamp();
-    this->AddItem(item);
-    return item.id;
+    return this->AddItem(item);
 }
 
 QM::QueueItem QM::QueueManager::GetItem(int id)
@@ -45,7 +49,7 @@
     }
     else
     {
-        return this->QueueList.at(id);
+        return this->QueueList[id];
     }
 }
 
@@ -78,15 +82,16 @@
 
 void QM::QueueManager::SetStatus(QM::QueueStatus status, int id)
 {
-    // todo, start - stop - pause
-    auto item = this->GetItem(id);
-    item.status = status;
-    this->SendEventToMainWindow(QM::QueueEvents::ITEM_STATUS_CHANGED, item);
+    if (this->QueueList.find(id) != this->QueueList.end())
+    {
+        this->QueueList[id].status = status;
+        this->SendEventToMainWindow(QM::QueueEvents::ITEM_STATUS_CHANGED, this->QueueList[id]);
+    }
 }
 
 void QM::QueueManager::PauseAll()
 {
-    for (auto &[key, value] : this->QueueList)
+    for (auto [key, value] : this->QueueList)
     {
         if (value.status == QM::QueueStatus::PENDING)
         {
@@ -99,21 +104,83 @@
 void QM::QueueManager::SendEventToMainWindow(QM::QueueEvents eventType, QM::QueueItem item)
 {
     wxThreadEvent *e = new wxThreadEvent();
-    // e->SetString(wxString::Format("MODEL_LOAD_START:%s", this->sd_params->model_path));
-    e->SetString(wxString("QUEUE:" + (int)eventType));
+    e->SetString(wxString::Format("QUEUE:%d", (int)eventType));
     e->SetPayload(item);
     wxQueueEvent(this->eventHandler, e);
 }
 
 void QM::QueueManager::OnThreadMessage(wxThreadEvent &e)
 {
+
+    e.Skip();
     auto msg = e.GetString().ToStdString();
 
     std::string token = msg.substr(0, msg.find(":"));
     std::string content = msg.substr(msg.find(":") + 1);
     // only numbers here...
-    QM::QueueEvents event = (QM::QueueEvents)std::stoi(content);
+
     // only handle the QUEUE messages, what this class generate
+    if (token == "QUEUE")
+    {
+        QM::QueueEvents event = (QM::QueueEvents)std::stoi(content);
+        auto payload = e.GetPayload<QM::QueueItem>();
+        if (event == QM::QueueEvents::ITEM_START)
+        {
+            this->SetStatus(QM::QueueStatus::RUNNING, payload.id);
+            this->isRunning = true;
+        }
+        if (event == QM::QueueEvents::ITEM_FINISHED)
+        {
+            this->SetStatus(QM::QueueStatus::DONE, payload.id);
+            this->isRunning = false;
+            // jump to the next item in queue
+            // find waiting jobs
+            for (auto job : this->QueueList)
+            {
+                if (job.second.status == QM::QueueStatus::PENDING)
+                {
+                    if (this->isRunning == false)
+                    {
+                        this->SendEventToMainWindow(QM::QueueEvents::ITEM_START, job.second);
+                        this->isRunning = true;
+                    }
+                    break;
+                }
+            }
+            // move finished job into another list - ??
+        }
+    }
+
+    if (token == "MODEL_LOAD_START")
+    {
+        auto payload = e.GetPayload<QM::QueueItem>();
+        this->SetStatus(QM::QueueStatus::MODEL_LOADING, payload.id);
+    }
+
+    // this state can not usable at here, because the payload is the sd_ctx* pointer here..
+    // we can't identify the current running job here... (we can maybe guess it, but not needed)
+    // see  GENERATION_START
+    if (token == "MODEL_LOAD_DONE")
+    {
+        // auto payload = e.GetPayload<QM::QueueItem>();
+        // this->SetStatus(QM::QueueStatus::RUNNING, payload.id);
+    }
+    if (token == "MODEL_LOAD_ERROR" || token == "GENERATION_ERROR")
+    {
+        auto payload = e.GetPayload<QM::QueueItem>();
+        this->SetStatus(QM::QueueStatus::FAILED, payload.id);
+        this->isRunning = false;
+    }
+    if (token == "GENERATION_START")
+    {
+        auto payload = e.GetPayload<QM::QueueItem>();
+        this->SetStatus(QM::QueueStatus::RUNNING, payload.id);
+        this->isRunning = true;
+    }
+    // nothing to todo here, the payload is the generated image list
+    if (token == "GENERATION_DONE")
+    {
+    }
 }
 
 int QM::QueueManager::GetCurrentUnixTimestamp()

--
Gitblit v1.9.3