mirror of
https://gitee.com/dromara/electron-egg.git
synced 2026-05-14 11:52:07 +08:00
pool
This commit is contained in:
@@ -11,7 +11,6 @@ const {
|
|||||||
powerMonitor, screen, nativeTheme
|
powerMonitor, screen, nativeTheme
|
||||||
} = require('electron');
|
} = require('electron');
|
||||||
const dayjs = require('dayjs');
|
const dayjs = require('dayjs');
|
||||||
const { ChildJob } = require('ee-core/jobs');
|
|
||||||
const Ps = require('ee-core/ps');
|
const Ps = require('ee-core/ps');
|
||||||
const Log = require('ee-core/log');
|
const Log = require('ee-core/log');
|
||||||
|
|
||||||
@@ -684,29 +683,8 @@ class ExampleController extends Controller {
|
|||||||
*/
|
*/
|
||||||
someJob (args, event) {
|
someJob (args, event) {
|
||||||
let jobId = args.id;
|
let jobId = args.id;
|
||||||
if (args.type == 'timer') {
|
let type = args.type;
|
||||||
let myjob = new ChildJob();
|
this.service.example.doJob(jobId, type, event);
|
||||||
|
|
||||||
// 执行任务及监听进度
|
|
||||||
const channel = 'controller.example.timerJobProgress';
|
|
||||||
const timerTask = myjob.exec('./jobs/example/timer', {jobId});
|
|
||||||
timerTask.emitter.on('job-timer-progress', (data) => {
|
|
||||||
Log.info('[main-process] timerTask, from TimerJob data:', data);
|
|
||||||
|
|
||||||
// 发送数据到渲染进程
|
|
||||||
event.reply(`${channel}`, data)
|
|
||||||
})
|
|
||||||
|
|
||||||
// 执行任务及监听进度 异步
|
|
||||||
// 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.reply(`${channel}`, data)
|
|
||||||
// })
|
|
||||||
// });
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -714,12 +692,23 @@ class ExampleController extends Controller {
|
|||||||
/**
|
/**
|
||||||
* 创建任务池
|
* 创建任务池
|
||||||
*/
|
*/
|
||||||
createJobPool (args, event) {
|
async createPool (args, event) {
|
||||||
|
let num = args.number;
|
||||||
|
this.service.example.doCreatePool(num, event);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过进程池执行任务
|
||||||
|
*/
|
||||||
|
someJobByPool (args, event) {
|
||||||
|
let jobId = args.id;
|
||||||
|
let type = args.type;
|
||||||
|
this.service.example.doJobByPool(jobId, type, event);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 测试接口
|
* 测试接口
|
||||||
|
|||||||
@@ -2,21 +2,27 @@
|
|||||||
|
|
||||||
const { Service } = require('ee-core');
|
const { Service } = require('ee-core');
|
||||||
const Log = require('ee-core/log');
|
const Log = require('ee-core/log');
|
||||||
|
const { ChildJob, ChildPoolJob } = require('ee-core/jobs');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 示例服务
|
* 示例服务(service层为单例)
|
||||||
* @class
|
* @class
|
||||||
*/
|
*/
|
||||||
class ExampleService extends Service {
|
class ExampleService extends Service {
|
||||||
|
|
||||||
constructor(ctx) {
|
constructor(ctx) {
|
||||||
super(ctx);
|
super(ctx);
|
||||||
|
|
||||||
|
// 在构造函数中初始化一些变量
|
||||||
|
this.myJob = new ChildJob();
|
||||||
|
this.myJobPool = new ChildPoolJob();
|
||||||
|
console.log('ddddddddddddddd');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* test
|
* test
|
||||||
*/
|
*/
|
||||||
async test (args) {
|
async test(args) {
|
||||||
let obj = {
|
let obj = {
|
||||||
status:'ok',
|
status:'ok',
|
||||||
params: args
|
params: args
|
||||||
@@ -25,6 +31,64 @@ class ExampleService extends Service {
|
|||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行任务
|
||||||
|
*/
|
||||||
|
doJob(jobId, type, event) {
|
||||||
|
if (type == 'timer') {
|
||||||
|
|
||||||
|
// 执行任务及监听进度
|
||||||
|
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.reply(`${channel}`, data)
|
||||||
|
})
|
||||||
|
|
||||||
|
// 执行任务及监听进度 异步
|
||||||
|
// 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.reply(`${channel}`, data)
|
||||||
|
// })
|
||||||
|
// });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行任务
|
||||||
|
*/
|
||||||
|
doCreatePool(num, event) {
|
||||||
|
const channel = 'controller.example.createPoolNotice';
|
||||||
|
|
||||||
|
// let pids = await myjobPool.create(num);
|
||||||
|
this.myJobPool.create(num).then(pids => {
|
||||||
|
event.reply(`${channel}`, pids);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过进程池执行任务
|
||||||
|
*/
|
||||||
|
doJobByPool(jobId, type, event) {
|
||||||
|
if (type == 'timer') {
|
||||||
|
|
||||||
|
// 执行任务及监听进度
|
||||||
|
const channel = 'controller.example.timerJobProgress';
|
||||||
|
const 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.reply(`${channel}`, data)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 上传到smms
|
* 上传到smms
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -36,6 +36,10 @@ const ipcApiRoute = {
|
|||||||
closeJavaServer: 'controller.example.closeJavaServer',
|
closeJavaServer: 'controller.example.closeJavaServer',
|
||||||
someJob: 'controller.example.someJob',
|
someJob: 'controller.example.someJob',
|
||||||
timerJobProgress: 'controller.example.timerJobProgress',
|
timerJobProgress: 'controller.example.timerJobProgress',
|
||||||
|
createPool: 'controller.example.createPool',
|
||||||
|
createPoolNotice: 'controller.example.createPoolNotice',
|
||||||
|
someJobByPool: 'controller.example.someJobByPool',
|
||||||
|
timerJobProgressByPool: 'controller.example.timerJobProgressByPool',
|
||||||
hello: 'controller.example.hello',
|
hello: 'controller.example.hello',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
<div id="app-base-jobs">
|
<div id="app-base-jobs">
|
||||||
<div class="one-block-1">
|
<div class="one-block-1">
|
||||||
<span>
|
<span>
|
||||||
1. 任务/并发任务
|
1. 任务 / 并发任务
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="one-block-2">
|
<div class="one-block-2">
|
||||||
@@ -15,7 +15,28 @@
|
|||||||
<a-button @click="runJob(2)">执行任务2</a-button>
|
<a-button @click="runJob(2)">执行任务2</a-button>
|
||||||
进度:{{ progress2 }}
|
进度:{{ progress2 }}
|
||||||
</a-space>
|
</a-space>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="one-block-1">
|
||||||
|
<span>
|
||||||
|
2. 任务池 / 并发任务
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div class="one-block-2">
|
||||||
|
<a-space>
|
||||||
|
<a-button @click="createPool()">创建进程池</a-button>
|
||||||
|
进程pids:{{ processPids }}
|
||||||
|
</a-space>
|
||||||
|
<p></p>
|
||||||
|
<a-space>
|
||||||
|
<a-button @click="runJobByPool(3)">执行任务3</a-button>
|
||||||
|
进度:{{ progress3 }}
|
||||||
|
</a-space>
|
||||||
|
<p></p>
|
||||||
|
<a-space>
|
||||||
|
<a-button @click="runJobByPool(4)">执行任务4</a-button>
|
||||||
|
进度:{{ progress4 }}
|
||||||
|
</a-space>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
<script>
|
<script>
|
||||||
@@ -25,6 +46,9 @@ export default {
|
|||||||
return {
|
return {
|
||||||
progress1: 0,
|
progress1: 0,
|
||||||
progress2: 0,
|
progress2: 0,
|
||||||
|
progress3: 0,
|
||||||
|
progress4: 0,
|
||||||
|
processPids: '',
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
mounted () {
|
mounted () {
|
||||||
@@ -34,6 +58,8 @@ export default {
|
|||||||
init () {
|
init () {
|
||||||
// 避免重复监听,或者将 on 功能写到一个统一的地方,只加载一次
|
// 避免重复监听,或者将 on 功能写到一个统一的地方,只加载一次
|
||||||
this.$ipc.removeAllListeners(ipcApiRoute.timerJobProgress);
|
this.$ipc.removeAllListeners(ipcApiRoute.timerJobProgress);
|
||||||
|
this.$ipc.removeAllListeners(ipcApiRoute.createPoolNotice);
|
||||||
|
//this.$ipc.removeAllListeners(ipcApiRoute.timerJobProgressByPool);
|
||||||
|
|
||||||
// 监听任务进度
|
// 监听任务进度
|
||||||
this.$ipc.on(ipcApiRoute.timerJobProgress, (event, result) => {
|
this.$ipc.on(ipcApiRoute.timerJobProgress, (event, result) => {
|
||||||
@@ -46,8 +72,35 @@ export default {
|
|||||||
case 2:
|
case 2:
|
||||||
this.progress2 = result.number;
|
this.progress2 = result.number;
|
||||||
break;
|
break;
|
||||||
|
case 3:
|
||||||
|
this.progress3 = result.number;
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
this.progress4 = result.number;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// 监听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) {
|
runJob(jobId) {
|
||||||
let params = {
|
let params = {
|
||||||
@@ -56,6 +109,19 @@ export default {
|
|||||||
}
|
}
|
||||||
this.$ipc.send(ipcApiRoute.someJob, params)
|
this.$ipc.send(ipcApiRoute.someJob, params)
|
||||||
},
|
},
|
||||||
|
createPool() {
|
||||||
|
let params = {
|
||||||
|
number: 3,
|
||||||
|
}
|
||||||
|
this.$ipc.send(ipcApiRoute.createPool, params);
|
||||||
|
},
|
||||||
|
runJobByPool(jobId) {
|
||||||
|
let params = {
|
||||||
|
id: jobId,
|
||||||
|
type: 'timer'
|
||||||
|
}
|
||||||
|
this.$ipc.send(ipcApiRoute.someJobByPool, params)
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user