Parcourir la source

Merge pull request #9 from StronglogicSolutions/queueTasks

Queue tasks
Emmanuel Buckshi il y a 5 ans
Parent
commit
e924a8a18f

+ 1 - 1
.circleci/config.yml

@@ -15,7 +15,7 @@ jobs:
       - run:
           name: notify_production
           command: |
-            curl http://artifact.stronglogicsolutions.com/?artifact=ky_gui
+            curl http://artifact.stronglogicsolutions.com/?artifact=ky_gui&build_id=<< pipeline.number >>
 
 workflows:
  version: 2

+ 2 - 2
.circleci/production/Dockerfile

@@ -1,9 +1,9 @@
 FROM jonathonf/manjaro:latest
-RUN pacman -Syu flatbuffers base-devel qt5-base noto-fonts ffmpeg --noconfirm
+RUN pacman -Syu flatbuffers base-devel qt5-base noto-fonts ffmpeg adobe-source-han-sans-kr-fonts --noconfirm
 RUN ln -sf /usr/share/zoneinfo/America/Toronto /etc/localtime
 ARG REBUILD_KY_GUI=false
 WORKDIR ky_gui
 RUN curl -o ky_gui http://artifact.stronglogicsolutions.com/download?name=ky_gui
 RUN chmod +x ky_gui
-RUN mkdir config && echo '{"defaultApp":"instagram"}' >> config/config.json
+RUN mkdir config && echo '{"defaultApp":"instagram","fileDirectory":"/data/kstyleyo/instagram", "schedulerMode": "true"}' >> config/config.json
 RUN mkdir -p assets/previews

+ 19 - 11
headers/util.hpp

@@ -1,20 +1,20 @@
 #ifndef UTIL_HPP
 #define UTIL_HPP
 #pragma GCC system_header
-#include <string>
+#include <QDebug>
+#include <QQueue>
+#include <QString>
+#include <QVector>
 #include <charconv>
+#include <string>
 #include <utility>
 #include <vector>
-#include <QDebug>
-#include <QVector>
-#include <QString>
-#include "rapidjson/writer.h"
-#include "rapidjson/prettywriter.h"
+#include "json.hpp"
+#include "rapidjson/document.h"
 #include "rapidjson/pointer.h"
+#include "rapidjson/prettywriter.h"
 #include "rapidjson/stringbuffer.h"
-#include "rapidjson/document.h"
-#include "json.hpp"
-
+#include "rapidjson/writer.h"
 
 namespace Kontainer {
 /** Reverse Iterator */
@@ -26,7 +26,7 @@ public:
     auto begin() {return _obj.rbegin();}
     auto end() {return _obj.rend();}
 };
-}
+}  // namespace Kontainer
 
 enum FileType {
     VIDEO = 1,
@@ -39,6 +39,14 @@ struct KFileData {
     QByteArray bytes;
 };
 
+struct Task {
+  QVector<KFileData> files;
+  std::vector<std::string> args;
+  int mask;
+};
+
+typedef QQueue<Task> TaskQueue;
+
 namespace {
 using namespace rapidjson;
 using json = nlohmann::json;
@@ -397,5 +405,5 @@ QString generatePreview(QString video_path, QString video_name) {
   return preview_name;
 }
 }; // namespace FileUtils
-}
+}  // namespace
 #endif  // UTIL_HPP

+ 0 - 5
include/argdialog.h

@@ -23,11 +23,6 @@ const QString REQUESTED_BY_TYPE = "requested by";
 
 typedef std::string Str;
 
-typedef struct Task {
-    int mask;
-    std::vector<std::string> args;
-} Task;
-
 typedef struct KFile {
   QString name;
   QString path;

+ 1 - 0
include/client.hpp

@@ -104,5 +104,6 @@ private:
     std::vector<int> selected_commands;
     QQueue<KFileData> outgoing_files;
     std::vector<SentFile> sent_files;
+    TaskQueue m_task_queue;
 };
 #endif // CLIENT_HPP

+ 2 - 2
src/argdialog.cpp

@@ -203,8 +203,8 @@ void ArgDialog::clearPost() {
 }
 
 void ArgDialog::clearTask() {
-    m_task.args = {};
-    m_task.mask = -1;
+  m_task.args.clear();
+  m_task.mask = -1;
 }
 
 void ArgDialog::addRequestedBy(QString value) {

+ 25 - 2
src/client.cpp

@@ -279,6 +279,18 @@ void Client::sendTaskEncoded(TaskType type, std::vector<std::string> args) {
         builder.Clear();
         sent_files.clear();
         m_task.clear();
+        if (!m_task_queue.isEmpty()) {
+          auto task = m_task_queue.dequeue();
+          if (!task.files.empty() && !outgoing_files.empty()) {
+            qDebug() << "There are still outgoing files left over from last "
+                        "task which were never sent. They are being deleted";
+            outgoing_files.clear();
+          }
+          // We simply need to send files. Once the last file is sent, Client
+          // will check the value of m_task and send it to Server.
+          m_task = task.args;
+          sendFiles(task.files);
+        }
     }
 }
 
@@ -441,7 +453,15 @@ void Client::execute() {
  */
 void Client::scheduleTask(std::vector<std::string> task_args, bool file_pending) {
     if (file_pending) {
+      if (m_task.empty()) {
         m_task = task_args;
+      } else {
+        if (!m_task_queue.empty() && m_task_queue.front().args.empty()) {
+          m_task_queue.front().args.assign(task_args.begin(), task_args.end());
+        } else {
+          qDebug() << "Could not identify the queued task for updating";
+        }
+      }
     } else {
         qDebug() << "Requesting a task to be scheduled";
         sendTaskEncoded(TaskType::INSTAGRAM, task_args);
@@ -456,11 +476,14 @@ void Client::sendFiles(QVector<KFileData> files) {
     if (outgoing_files.isEmpty()) {
       file_was_sent = false;
       for (const auto& file : files) {
-        outgoing_files.enqueue(file);
+        outgoing_files.enqueue(std::move(file));
         }
         std::string send_file_operation = createOperation("FileUpload", {});
         sendEncoded(send_file_operation);
     } else {
-        qDebug() << "Still attempting to send a different file";
+      // TODO: place in queue and check queue after we finish scheduling the
+      // task associated with the outgoing files
+      m_task_queue.enqueue(Task{.files = files});
+      qDebug() << "Still attempting to send a different file";
     }
 }

+ 0 - 7
src/mainwindow.cpp

@@ -139,19 +139,12 @@ void MainWindow::connectClient() {
   QObject::connect(
       arg_ui, &ArgDialog::taskRequestReady, this,
       [this](Task task, bool file_pending) {
-        // TODO: Maybe this should be handled by the Client class directly
         auto mask = q_client->getSelectedApp();
         if (mask > -1) {
           if (q_client->getAppName(mask) == "Instagram") {
-            auto datetime = task.args.at(0);
-            auto current_datetime = QDateTime::currentDateTime().toTime_t();
-            auto seconds_diff = std::stoi(datetime) - current_datetime;
-            //                qDebug() << "Time difference: " << seconds_diff;
-            //                if (seconds_diff > 3600) {
             qDebug() << "Scheduling a task";
             task.args.push_back(std::to_string(mask));
             q_client->scheduleTask(task.args, file_pending);
-            //                }
           }
         }
       });