From 65cb30100b3c7de11c346658c4d424bc0a48060b 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: Tue, 4 Apr 2023 16:36:18 +0800 Subject: [PATCH] pool --- electron/controller/example.js | 41 ++++++--------- electron/service/example.js | 68 ++++++++++++++++++++++++- frontend/src/api/main.js | 4 ++ frontend/src/views/base/jobs/Index.vue | 70 +++++++++++++++++++++++++- 4 files changed, 153 insertions(+), 30 deletions(-) diff --git a/electron/controller/example.js b/electron/controller/example.js index 0a4463f..a1ca112 100644 --- a/electron/controller/example.js +++ b/electron/controller/example.js @@ -11,7 +11,6 @@ const { powerMonitor, screen, nativeTheme } = require('electron'); const dayjs = require('dayjs'); -const { ChildJob } = require('ee-core/jobs'); const Ps = require('ee-core/ps'); const Log = require('ee-core/log'); @@ -684,29 +683,8 @@ class ExampleController extends Controller { */ someJob (args, event) { let jobId = args.id; - if (args.type == 'timer') { - let myjob = new ChildJob(); - - // 执行任务及监听进度 - 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) - // }) - // }); - } + let type = args.type; + this.service.example.doJob(jobId, type, event); 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; - } + } /** * 测试接口 diff --git a/electron/service/example.js b/electron/service/example.js index eaa406e..466fbbb 100644 --- a/electron/service/example.js +++ b/electron/service/example.js @@ -2,21 +2,27 @@ const { Service } = require('ee-core'); const Log = require('ee-core/log'); +const { ChildJob, ChildPoolJob } = require('ee-core/jobs'); /** - * 示例服务 + * 示例服务(service层为单例) * @class */ class ExampleService extends Service { constructor(ctx) { super(ctx); + + // 在构造函数中初始化一些变量 + this.myJob = new ChildJob(); + this.myJobPool = new ChildPoolJob(); + console.log('ddddddddddddddd'); } /** * test */ - async test (args) { + async test(args) { let obj = { status:'ok', params: args @@ -25,6 +31,64 @@ class ExampleService extends Service { 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 */ diff --git a/frontend/src/api/main.js b/frontend/src/api/main.js index 790225c..923cb82 100644 --- a/frontend/src/api/main.js +++ b/frontend/src/api/main.js @@ -36,6 +36,10 @@ const ipcApiRoute = { closeJavaServer: 'controller.example.closeJavaServer', someJob: 'controller.example.someJob', timerJobProgress: 'controller.example.timerJobProgress', + createPool: 'controller.example.createPool', + createPoolNotice: 'controller.example.createPoolNotice', + someJobByPool: 'controller.example.someJobByPool', + timerJobProgressByPool: 'controller.example.timerJobProgressByPool', hello: 'controller.example.hello', } diff --git a/frontend/src/views/base/jobs/Index.vue b/frontend/src/views/base/jobs/Index.vue index bb8c9bc..0c4998e 100644 --- a/frontend/src/views/base/jobs/Index.vue +++ b/frontend/src/views/base/jobs/Index.vue @@ -2,7 +2,7 @@