From 92b299f512f93963fa20cd268974a28039760861 Mon Sep 17 00:00:00 2001
From: Julian Matschinske <julian.matschinske@wzw.tum.de>
Date: Thu, 9 Apr 2020 15:48:13 +0200
Subject: [PATCH] Limit number of tasks that can be run at once to 3

---
 src/app/analysis.service.ts                   | 41 ++++++++++++++++++-
 .../launch-analysis.component.html            |  8 +++-
 2 files changed, 46 insertions(+), 3 deletions(-)

diff --git a/src/app/analysis.service.ts b/src/app/analysis.service.ts
index 1cf9ff85..cd137b45 100644
--- a/src/app/analysis.service.ts
+++ b/src/app/analysis.service.ts
@@ -41,6 +41,7 @@ export class AnalysisService {
   public tasks: Task[] = [];
 
   private intervalId: any;
+  private canLaunchNewTask = false;
 
   constructor(private http: HttpClient) {
     const tokens = localStorage.getItem('tokens');
@@ -147,6 +148,19 @@ export class AnalysisService {
   }
 
   async startQuickAnalysis() {
+    if (!this.canLaunchTask()) {
+      toast({
+        message: 'You can only run 3 tasks at once. Please wait for one of them to finish or delete it from the task list.',
+        duration: 5000,
+        dismissible: true,
+        pauseOnHover: true,
+        type: 'is-danger',
+        position: 'top-center',
+        animate: {in: 'fadeIn', out: 'fadeOut'}
+      });
+      return;
+    }
+
     const resp = await this.http.post<any>(`${environment.backend}task/`, {
       algorithm: 'quick',
       target: 'drug',
@@ -171,6 +185,19 @@ export class AnalysisService {
   }
 
   async startAnalysis(algorithm, target: 'drug' | 'drug-target', parameters) {
+    if (!this.canLaunchTask()) {
+      toast({
+        message: 'You can only run 3 tasks at once. Please wait for one of them to finish or delete it from the task list.',
+        duration: 5000,
+        dismissible: true,
+        pauseOnHover: true,
+        type: 'is-danger',
+        position: 'top-center',
+        animate: {in: 'fadeIn', out: 'fadeOut'}
+      });
+      return;
+    }
+
     const resp = await this.http.post<any>(`${environment.backend}task/`, {
       algorithm,
       target,
@@ -203,13 +230,20 @@ export class AnalysisService {
     });
   }
 
+  public canLaunchTask(): boolean {
+    return this.canLaunchNewTask;
+  }
+
   startWatching() {
     const watch = async () => {
       if (this.tokens.length > 0) {
         this.tasks = await this.getTasks();
+        let queuedOrRunningTasks = 0;
         this.tasks.forEach((task) => {
-          if (this.finishedTokens.find((finishedToken) => finishedToken === task.token)) {
-          } else {
+          if (!task.info.done && !task.info.failed) {
+            queuedOrRunningTasks++;
+          }
+          if (!this.finishedTokens.find((finishedToken) => finishedToken === task.token)) {
             if (task.info.done) {
               this.finishedTokens.push(task.token);
               this.showToast(task, 'DONE');
@@ -222,6 +256,9 @@ export class AnalysisService {
             }
           }
         });
+        this.canLaunchNewTask = queuedOrRunningTasks < 3;
+      } else {
+        this.canLaunchNewTask = true;
       }
     };
     watch();
diff --git a/src/app/components/launch-analysis/launch-analysis.component.html b/src/app/components/launch-analysis/launch-analysis.component.html
index 95560e6e..fa30c15b 100644
--- a/src/app/components/launch-analysis/launch-analysis.component.html
+++ b/src/app/components/launch-analysis/launch-analysis.component.html
@@ -28,6 +28,12 @@
         </div>
       </div>
 
+      <div *ngIf="!analysis.canLaunchTask()">
+        <div class="notification is-warning warning">
+          You can only run 3 tasks at once. Please wait for one of them to finish or delete it from the task list.
+        </div>
+      </div>
+
       <div *ngIf="algorithm==='trustrank'">
         <div class="field" *ngIf="target === 'drug'">
           <label class="label">Indirect Drugs</label>
@@ -136,7 +142,7 @@
     </section>
 
     <footer class="modal-card-foot">
-      <button (click)="startTask(); close()" class="button is-success is-rounded" [disabled]="target === 'drug' && hasBaits">Launch</button>
+      <button (click)="startTask(); close()" class="button is-success is-rounded" [disabled]="(target === 'drug' && hasBaits) || !analysis.canLaunchTask()">Launch</button>
       <button (click)="close()" class="button is-rounded">Close</button>
     </footer>
   </div>
-- 
GitLab