哆啦好梦 2 년 전
부모
커밋
433cbfb0ff
5개의 변경된 파일66개의 추가작업 그리고 8개의 파일을 삭제
  1. 17 2
      electron/controller/example.js
  2. 3 1
      electron/jobs/example/timer.js
  3. 19 1
      electron/service/example.js
  4. 1 0
      frontend/src/api/main.js
  5. 26 4
      frontend/src/views/base/jobs/Index.vue

+ 17 - 2
electron/controller/example.js

@@ -694,6 +694,17 @@ class ExampleController extends Controller {
   }
 
   /**
+   * 关闭任务
+   */ 
+  closeJob (args, event) {
+    let jobId = args.id;
+    let type = args.type;
+    this.service.example.closeJob(jobId, type, event);
+    
+    return;
+  }  
+
+  /**
    * 创建任务池
    */ 
   async createPool (args, event) {
@@ -712,9 +723,13 @@ class ExampleController extends Controller {
   someJobByPool (args, event) {
     let jobId = args.id;
     let type = args.type;
-    this.service.example.doJobByPool(jobId, type, event);
+    let pid = this.service.example.doJobByPool(jobId, type, event);
     
-    return;
+    let data = {
+      jobId,
+      pid
+    }
+    return data;
   }
 
   /**

+ 3 - 1
electron/jobs/example/timer.js

@@ -38,10 +38,12 @@ class TimerJob extends Job {
       // 关闭定时器
       clearInterval(timer);
 
+      // 任务结束,重置前端显示
+      childMessage.send(eventName, {jobId, number:0, pid:0});
+
       // 如果是childJob任务,必须调用 Ps.exit() 方法,让进程退出,否则会常驻内存
       // 如果是childPoolJob任务,常驻内存,等待下一个任务
       if (Ps.isChildJob()) {
-        childMessage.send(eventName, {jobId, number:0, pid:0});
         Ps.exit();
       }
 

+ 19 - 1
electron/service/example.js

@@ -16,6 +16,8 @@ class ExampleService extends Service {
     // 在构造函数中初始化一些变量
     this.myJob = new ChildJob();
     this.myJobPool = new ChildPoolJob();
+    this.taskForJob = {};
+    this.taskForJobPool = {};
   }
 
   /**
@@ -58,12 +60,25 @@ class ExampleService extends Service {
       // });
 
       pid = timerTask.pid; 
+      this.taskForJob[jobId] = timerTask;
     }
 
     return pid;
   }
 
   /**
+   * 关闭任务
+   */ 
+  closeJob(jobId, type, event) {
+    let oneTask = this.taskForJob[jobId];
+    if (type == 'timer') {
+      oneTask.kill();
+      const channel = 'controller.example.timerJobProgress';
+      event.reply(`${channel}`, {jobId, number:0, pid:0})
+    }
+  }
+
+  /**
    * 执行任务
    */ 
   doCreatePool(num, event) {
@@ -79,6 +94,7 @@ class ExampleService extends Service {
    * 通过进程池执行任务
    */ 
   doJobByPool(jobId, type, event) {
+    let pid = 0;
     if (type == 'timer') {
 
       // 执行任务及监听进度
@@ -88,9 +104,11 @@ class ExampleService extends Service {
         Log.info('[main-process] [ChildPoolJob] timerTask, from TimerJob data:', data);
 
         // 发送数据到渲染进程
-        event.reply(`${channel}`, data)
+        event.sender.send(`${channel}`, data)
       })
+      pid = timerTask.pid; 
     }
+    return pid;
   }
 
   /**

+ 1 - 0
frontend/src/api/main.js

@@ -36,6 +36,7 @@ const ipcApiRoute = {
   closeJavaServer: 'controller.example.closeJavaServer',
   someJob: 'controller.example.someJob',
   timerJobProgress: 'controller.example.timerJobProgress',
+  closeJob: 'controller.example.closeJob',
   createPool: 'controller.example.createPool',
   createPoolNotice: 'controller.example.createPoolNotice',
   someJobByPool: 'controller.example.someJobByPool',

+ 26 - 4
frontend/src/views/base/jobs/Index.vue

@@ -9,11 +9,13 @@
       <a-space>
         <a-button @click="runJob(1)">执行任务1</a-button>
         进度:{{ progress1 }} , 进程pid:{{ progress1_pid }}
+        <a-button @click="closeJob(1)">关闭任务1</a-button>
       </a-space>
       <p></p>
       <a-space>
         <a-button @click="runJob(2)">执行任务2</a-button>
         进度:{{ progress2 }} , 进程pid:{{ progress2_pid }}
+        <a-button @click="closeJob(2)">关闭任务2</a-button>
       </a-space>            
     </div>
     <div class="one-block-1">
@@ -29,12 +31,14 @@
       <p></p>      
       <a-space>
         <a-button @click="runJobByPool(3)">执行任务3</a-button>
-        进度:{{ progress3 }}
+        进度:{{ progress3 }} , 进程pid:{{ progress3_pid }}
+        <a-button @click="closeJob(3)">关闭任务3</a-button>
       </a-space>
       <p></p>
       <a-space>
         <a-button @click="runJobByPool(4)">执行任务4</a-button>
-        进度:{{ progress4 }}
+        进度:{{ progress4 }} , 进程pid:{{ progress4_pid }}
+        <a-button @click="closeJob(4)">关闭任务4</a-button>
       </a-space>            
     </div>            
   </div>
@@ -77,9 +81,11 @@ export default {
             break;
           case 3:
             this.progress3 = result.number;
+            this.progress3_pid = result.pid == 0 ? result.pid : this.progress3_pid;
             break;
           case 4:
-            this.progress4 = result.number;            
+            this.progress4 = result.number;  
+            this.progress4_pid = result.pid == 0 ? result.pid : this.progress4_pid;          
             break;
         }
       })
@@ -106,6 +112,13 @@ export default {
         }
       })
     },
+    closeJob(jobId) {
+      let params = {
+        id: jobId,
+        type: 'timer'
+      }
+      this.$ipc.send(ipcApiRoute.closeJob, params);
+    },
     createPool() {
       let params = {
         number: 3,
@@ -117,7 +130,16 @@ export default {
         id: jobId,
         type: 'timer'
       }
-      this.$ipc.send(ipcApiRoute.someJobByPool, params)
+      this.$ipc.invoke(ipcApiRoute.someJobByPool, params).then(data => {
+        switch (data.jobId) {
+          case 3:
+            this.progress3_pid = data.pid;
+            break;
+          case 4:
+            this.progress4_pid = data.pid;
+            break;
+        }
+      })
     },
   }
 }