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 @@
执行任务1 - 进度:{{ progress1 }} + 进度:{{ progress1 }} , 进程pid:{{ progress1_pid }}

执行任务2 - 进度:{{ progress2 }} + 进度:{{ progress2 }} , 进程pid:{{ progress2_pid }}
@@ -45,9 +45,13 @@ export default { data() { return { progress1: 0, + progress1_pid: 0, progress2: 0, + progress2_pid: 0, progress3: 0, + progress3_pid: 0, progress4: 0, + progress4_pid: 0, processPids: '', } }, @@ -59,18 +63,17 @@ export default { // 避免重复监听,或者将 on 功能写到一个统一的地方,只加载一次 this.$ipc.removeAllListeners(ipcApiRoute.timerJobProgress); this.$ipc.removeAllListeners(ipcApiRoute.createPoolNotice); - //this.$ipc.removeAllListeners(ipcApiRoute.timerJobProgressByPool); // 监听任务进度 this.$ipc.on(ipcApiRoute.timerJobProgress, (event, result) => { - console.log('[ipcRenderer] [someJob] result:', result); - switch (result.jobId) { case 1: this.progress1 = result.number; + this.progress1_pid = result.pid == 0 ? result.pid : this.progress1_pid; break; case 2: this.progress2 = result.number; + this.progress2_pid = result.pid == 0 ? result.pid : this.progress2_pid; break; case 3: this.progress3 = result.number; @@ -83,31 +86,25 @@ export default { // 监听pool this.$ipc.on(ipcApiRoute.createPoolNotice, (event, result) => { - console.log('[ipcRenderer] [createPoolNotice] result:', result); - let pidsStr = JSON.stringify(result); this.processPids = pidsStr; - }) - // 监听任务进度 pool - // this.$ipc.on(ipcApiRoute.timerJobProgressByPool, (event, result) => { - // console.log('[ipcRenderer] [someJobByPool] result:', result); - - // switch (result.jobId) { - // case 1: - // this.progress3 = result.number; - // break; - // case 2: - // this.progress4 = result.number; - // break; - // } - // }) + }) }, runJob(jobId) { let params = { id: jobId, type: 'timer' } - this.$ipc.send(ipcApiRoute.someJob, params) + this.$ipc.invoke(ipcApiRoute.someJob, params).then(data => { + switch (data.jobId) { + case 1: + this.progress1_pid = data.pid; + break; + case 2: + this.progress2_pid = data.pid; + break; + } + }) }, createPool() { let params = {