diff --git a/electron/controller/example.js b/electron/controller/example.js index eb177ce..2178db0 100644 --- a/electron/controller/example.js +++ b/electron/controller/example.js @@ -31,7 +31,7 @@ class ExampleController extends Controller { /** * 所有方法接收两个参数 * @param args 前端传的参数 - * @param event - ipc通信时才有值。invoke()方法时,event == IpcMainInvokeEvent; send()/sendSync()方法时,event == IpcMainEvent + * @param event - ipc通信时才有值。详情见:控制器文档 */ /** @@ -684,9 +684,13 @@ class ExampleController extends Controller { someJob (args, event) { let jobId = args.id; let type = args.type; - this.service.example.doJob(jobId, type, event); + let pid = this.service.example.doJob(jobId, type, event); - return; + let data = { + jobId, + pid + } + return data; } /** diff --git a/electron/jobs/example/timer.js b/electron/jobs/example/timer.js index d24e1f8..7091bd9 100644 --- a/electron/jobs/example/timer.js +++ b/electron/jobs/example/timer.js @@ -26,7 +26,7 @@ class TimerJob extends Job { let eventName = 'job-timer-progress'; let number = 0; let jobId = this.params.jobId; - setInterval(function() { + let timer = setInterval(function() { Hello.welcome(); childMessage.send(eventName, {jobId, number}); @@ -34,10 +34,18 @@ class TimerJob extends Job { }, 1000); // 用 setTimeout 模拟任务运行时长 - // 任务完成后,必须调用 Ps.exit() 方法,让进程退出,否则会常驻内存 setTimeout(() => { - Ps.exitChildJob(1); - }, 10 * 1000) + // 关闭定时器 + clearInterval(timer); + + // 如果是childJob任务,必须调用 Ps.exit() 方法,让进程退出,否则会常驻内存 + // 如果是childPoolJob任务,常驻内存,等待下一个任务 + if (Ps.isChildJob()) { + childMessage.send(eventName, {jobId, number:0, pid:0}); + Ps.exit(); + } + + }, 20 * 1000) } } diff --git a/electron/service/example.js b/electron/service/example.js index 9cc8bee..0fbfdf4 100644 --- a/electron/service/example.js +++ b/electron/service/example.js @@ -16,7 +16,6 @@ class ExampleService extends Service { // 在构造函数中初始化一些变量 this.myJob = new ChildJob(); this.myJobPool = new ChildPoolJob(); - console.log('ddddddddddddddd'); } /** @@ -35,6 +34,7 @@ class ExampleService extends Service { * 执行任务 */ doJob(jobId, type, event) { + let pid = 0; if (type == 'timer') { // 执行任务及监听进度 @@ -44,7 +44,7 @@ class ExampleService extends Service { Log.info('[main-process] timerTask, from TimerJob data:', data); // 发送数据到渲染进程 - event.reply(`${channel}`, data) + event.sender.send(`${channel}`, data) }) // 执行任务及监听进度 异步 @@ -53,10 +53,14 @@ class ExampleService extends Service { // Log.info('[main-process] timerTask, from TimerJob data:', data); // // 发送数据到渲染进程 - // event.reply(`${channel}`, data) + // event.sender.send(`${channel}`, data) // }) // }); + + pid = timerTask.pid; } + + return pid; } /** @@ -94,7 +98,7 @@ class ExampleService extends Service { */ monitorJob() { setInterval(() => { - let jobPids = this.myJobPool.getPids(); + let jobPids = this.myJob.getPids(); let jobPoolPids = this.myJobPool.getPids(); Log.info(`[main-process] [monitorJob] jobPids: ${jobPids}, jobPoolPids: ${jobPoolPids}`); }, 5000) diff --git a/frontend/src/views/base/jobs/Index.vue b/frontend/src/views/base/jobs/Index.vue index 0c4998e..74c116d 100644 --- a/frontend/src/views/base/jobs/Index.vue +++ b/frontend/src/views/base/jobs/Index.vue @@ -8,12 +8,12 @@