gsx 2 years ago
parent
commit
baeec344f9
3 changed files with 15 additions and 21 deletions
  1. 1 1
      electron/controller/example.js
  2. 2 3
      electron/jobs/example/timer.js
  3. 12 17
      electron/service/example.js

+ 1 - 1
electron/controller/example.js

@@ -711,7 +711,7 @@ class ExampleController extends Controller {
     let num = args.number;
     this.service.example.doCreatePool(num, event);
 
-    // monitor
+    // test monitor
     this.service.example.monitorJob();
 
     return;

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

@@ -42,12 +42,11 @@ class TimerJob extends Job {
       childMessage.send(eventName, {jobId, number:0, pid:0});
 
       // 如果是childJob任务,必须调用 Ps.exit() 方法,让进程退出,否则会常驻内存
-      // 如果是childPoolJob任务,常驻内存,等待下一个
+      // 如果是childPoolJob任务,常驻内存,等待下一个
       if (Ps.isChildJob()) {
         Ps.exit();
       }
-
-    }, 20 * 1000)
+    }, 10 * 1000)
   }   
 }
 

+ 12 - 17
electron/service/example.js

@@ -17,7 +17,6 @@ class ExampleService extends Service {
     this.myJob = new ChildJob();
     this.myJobPool = new ChildPoolJob();
     this.taskForJob = {};
-    this.taskForJobPool = {};
   }
 
   /**
@@ -70,13 +69,13 @@ class ExampleService extends Service {
   }
 
   /**
-   * 执行任务
+   * 创建pool
    */ 
   doCreatePool(num, event) {
     const channel = 'controller.example.createPoolNotice';
     this.myJobPool.create(num).then(pids => {
       event.reply(`${channel}`, pids);
-    });    
+    });
   }
 
   /**
@@ -84,22 +83,18 @@ class ExampleService extends Service {
    */ 
   doJobByPool(jobId, action, event) {
     let res = {};
-    let timerTask;
     const channel = 'controller.example.timerJobProgress';
     if (action == 'run') {
-
-      // 执行任务及监听进度
-      timerTask = this.myJobPool.run('./jobs/example/timer', {jobId});
-      timerTask.emitter.on('job-timer-progress', (data) => {
-        Log.info('[main-process] [ChildPoolJob] timerTask, from TimerJob data:', data);
-
-        // 发送数据到渲染进程
-        event.sender.send(`${channel}`, data)
-      })
-      res.pid = timerTask.pid; 
-
-      // ???
-      //this.taskForJobPool[jobId] = timerTask;
+      // 异步-执行任务及监听进度
+      this.myJobPool.runPromise('./jobs/example/timer', {jobId}).then(task => {
+        task.emitter.on('job-timer-progress', (data) => {
+          Log.info('[main-process] [ChildPoolJob] timerTask, from TimerJob data:', data);
+  
+          // 发送数据到渲染进程
+          event.sender.send(`${channel}`, data)
+        })
+        res.pid = task.pid; 
+      });
     }
     return res;
   }