From 36d164ae529640b112db26ecf8a6f2a0a2b0c4c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=93=86=E5=95=A6=E5=A5=BD=E6=A2=A6?= <530353222@qq.com> Date: Fri, 7 Apr 2023 19:01:23 +0800 Subject: [PATCH] job --- electron/controller/example.js | 34 ++++++++++++--------- electron/service/example.js | 42 ++++++++++++++------------ frontend/src/api/main.js | 1 - frontend/src/views/base/jobs/Index.vue | 34 ++++++++++----------- 4 files changed, 59 insertions(+), 52 deletions(-) diff --git a/electron/controller/example.js b/electron/controller/example.js index e543868..ac76b14 100644 --- a/electron/controller/example.js +++ b/electron/controller/example.js @@ -683,27 +683,33 @@ class ExampleController extends Controller { */ someJob (args, event) { let jobId = args.id; - let type = args.type; - let pid = this.service.example.doJob(jobId, type, event); + let action = args.action; + + let result; + switch (action) { + case 'create': + result = this.service.example.doJob(jobId, action, event); + break; + case 'pause': + this.service.example.doJob(jobId, action, event); + break; + case 'continue': + this.service.example.doJob(jobId, action, event); + break; + case 'close': + this.service.example.doJob(jobId, action, event); + break; + default: + } let data = { jobId, - pid + action, + result } return data; } - /** - * 关闭任务 - */ - closeJob (args, event) { - let jobId = args.id; - let type = args.type; - this.service.example.closeJob(jobId, type, event); - - return; - } - /** * 创建任务池 */ diff --git a/electron/service/example.js b/electron/service/example.js index a4c63e7..953e99f 100644 --- a/electron/service/example.js +++ b/electron/service/example.js @@ -35,16 +35,15 @@ class ExampleService extends Service { /** * 执行任务 */ - doJob(jobId, type, event) { - let pid = 0; - if (type == 'timer') { - + doJob(jobId, action, event) { + let res = {}; + let oneTask; + const channel = 'controller.example.timerJobProgress'; + if (action == 'create') { // 执行任务及监听进度 - const channel = 'controller.example.timerJobProgress'; const timerTask = this.myJob.exec('./jobs/example/timer', {jobId}); timerTask.emitter.on('job-timer-progress', (data) => { Log.info('[main-process] timerTask, from TimerJob data:', data); - // 发送数据到渲染进程 event.sender.send(`${channel}`, data) }) @@ -53,29 +52,32 @@ class ExampleService extends Service { // myjob.execPromise('./jobs/example/timer', {jobId}).then(task => { // task.emitter.on('job-timer-progress', (data) => { // Log.info('[main-process] timerTask, from TimerJob data:', data); - // // 发送数据到渲染进程 // event.sender.send(`${channel}`, data) // }) // }); - pid = timerTask.pid; + res.pid = timerTask.pid; this.taskForJob[jobId] = timerTask; } - - return pid; - } - - /** - * 关闭任务 - */ - closeJob(jobId, type, event) { - let oneTask = this.taskForJob[jobId]; - if (type == 'timer') { + if (action == 'pause') { + oneTask = this.taskForJob[jobId]; + // 不支持window平台 + oneTask.sleep(); + } + if (action == 'continue') { + oneTask = this.taskForJob[jobId]; + // 不支持window平台 + oneTask.wakeup(); + } + if (action == 'close') { + oneTask = this.taskForJob[jobId]; oneTask.kill(); const channel = 'controller.example.timerJobProgress'; - event.reply(`${channel}`, {jobId, number:0, pid:0}) - } + event.reply(`${channel}`, {jobId, number:0, pid:0}); + } + + return res; } /** diff --git a/frontend/src/api/main.js b/frontend/src/api/main.js index b0fc062..923cb82 100644 --- a/frontend/src/api/main.js +++ b/frontend/src/api/main.js @@ -36,7 +36,6 @@ 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', diff --git a/frontend/src/views/base/jobs/Index.vue b/frontend/src/views/base/jobs/Index.vue index 1751939..c28ef0e 100644 --- a/frontend/src/views/base/jobs/Index.vue +++ b/frontend/src/views/base/jobs/Index.vue @@ -7,15 +7,19 @@
- 执行任务1 + 执行任务1 进度:{{ progress1 }} , 进程pid:{{ progress1_pid }} - 关闭任务1 + 暂停 + 继续 + 关闭

- 执行任务2 + 执行任务2 进度:{{ progress2 }} , 进程pid:{{ progress2_pid }} - 关闭任务2 + 暂停 + 继续 + 关闭
@@ -32,13 +36,11 @@ 执行任务3 进度:{{ progress3 }} , 进程pid:{{ progress3_pid }} - 关闭任务3

执行任务4 进度:{{ progress4 }} , 进程pid:{{ progress4_pid }} - 关闭任务4
@@ -96,29 +98,27 @@ export default { this.processPids = pidsStr; }) }, - runJob(jobId) { + runJob(jobId, operation) { let params = { id: jobId, - type: 'timer' + type: 'timer', + action: operation } this.$ipc.invoke(ipcApiRoute.someJob, params).then(data => { switch (data.jobId) { case 1: - this.progress1_pid = data.pid; + if (data.action == 'create') { + this.progress1_pid = data.result.pid; + } break; case 2: - this.progress2_pid = data.pid; + if (data.action == 'create') { + this.progress2_pid = data.result.pid; + } break; } }) }, - closeJob(jobId) { - let params = { - id: jobId, - type: 'timer' - } - this.$ipc.send(ipcApiRoute.closeJob, params); - }, createPool() { let params = { number: 3,