This commit is contained in:
哆啦好梦
2023-04-07 19:01:23 +08:00
parent 433cbfb0ff
commit 36d164ae52
4 changed files with 59 additions and 52 deletions

View File

@@ -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;
}
/**
* 创建任务池
*/

View File

@@ -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;
}
/**

View File

@@ -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',

View File

@@ -7,15 +7,19 @@
</div>
<div class="one-block-2">
<a-space>
<a-button @click="runJob(1)">执行任务1</a-button>
<a-button @click="runJob(1, 'create')">执行任务1</a-button>
进度{{ progress1 }} 进程pid{{ progress1_pid }}
<a-button @click="closeJob(1)">关闭任务1</a-button>
<a-button @click="runJob(1, 'pause')">暂停</a-button>
<a-button @click="runJob(1, 'continue')">继续</a-button>
<a-button @click="runJob(1, 'close')">关闭</a-button>
</a-space>
<p></p>
<a-space>
<a-button @click="runJob(2)">执行任务2</a-button>
<a-button @click="runJob(2, 'create')">执行任务2</a-button>
进度{{ progress2 }} 进程pid{{ progress2_pid }}
<a-button @click="closeJob(2)">关闭任务2</a-button>
<a-button @click="runJob(2, 'pause')">暂停</a-button>
<a-button @click="runJob(2, 'continue')">继续</a-button>
<a-button @click="runJob(2, 'close')">关闭</a-button>
</a-space>
</div>
<div class="one-block-1">
@@ -32,13 +36,11 @@
<a-space>
<a-button @click="runJobByPool(3)">执行任务3</a-button>
进度{{ 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 }} 进程pid{{ progress4_pid }}
<a-button @click="closeJob(4)">关闭任务4</a-button>
</a-space>
</div>
</div>
@@ -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,