增加主进程调用 job 子进程函数的demo

This commit is contained in:
gaoshuaixing
2024-10-15 18:59:21 +08:00
parent da44fd900d
commit 261bc27152
5 changed files with 68 additions and 20 deletions

View File

@@ -264,6 +264,7 @@ class FrameworkController extends Controller {
}
/**
* 废弃,请使用 cross 模块
* 启动java项目
*/
async startJavaServer() {
@@ -287,6 +288,7 @@ class FrameworkController extends Controller {
}
/**
* 废弃,请使用 cross 模块
* 关闭java项目
*/
async closeJavaServer() {
@@ -307,6 +309,7 @@ class FrameworkController extends Controller {
}
/**
* 废弃,请使用 cross 模块
* java运行状态
*/
async runStatus() {
@@ -337,6 +340,12 @@ class FrameworkController extends Controller {
case 'close':
Services.get('framework').doJob(jobId, action, event);
break;
case 'pause':
Services.get('framework').doJob(jobId, action, event);
break;
case 'resume':
Services.get('framework').doJob(jobId, action, event);
break;
default:
}

View File

@@ -3,8 +3,8 @@ const Loader = require('ee-core/loader');
const Log = require('ee-core/log');
const Ps = require('ee-core/ps');
const { childMessage } = require('ee-core/message');
const Hello = Loader.requireJobsModule('./example/hello');
const EffectService = require('../../service/effect');
const Hello = Loader.requireModule('./jobs/example/hello');
const EffectService = Loader.requireModule('./service/effect');
/**
* example - TimerJob
@@ -15,6 +15,10 @@ class TimerJob extends Job {
constructor(params) {
super();
this.params = params;
this.timer = undefined;
this.timeoutTimer = undefined;
this.number = 0;
this.countdown = 10; // 倒计时
}
/**
@@ -22,6 +26,7 @@ class TimerJob extends Job {
*/
async handle () {
Log.info("[child-process] TimerJob params: ", this.params);
const { jobId } = this.params;
// 子进程中使用service
// 1. 需要重新实例化因为子进程中没有ee的上下文
@@ -29,21 +34,45 @@ class TimerJob extends Job {
const effectService = new EffectService();
effectService.hello('job');
// 计时器任务
let number = 0;
let jobId = this.params.jobId;
// 执行任务
this.doTimer(jobId);
}
/**
* 暂停任务运行
*/
async pause(jobId) {
Log.info("[child-process] Pause timerJob, jobId: ", jobId);
clearInterval(this.timer);
clearInterval(this.timeoutTimer);
}
/**
* 恢复任务运行
*/
async resume(jobId, pid) {
Log.info("[child-process] Resume timerJob, jobId: ", jobId, ", pid: ", pid);
this.doTimer(jobId);
}
/**
* 运行任务
*/
async doTimer(jobId) {
// 计时器模拟任务
let eventName = 'job-timer-progress-' + jobId;
let timer = setInterval(function() {
this.timer = setInterval(() => {
Hello.welcome();
childMessage.send(eventName, {jobId, number, end: false});
number++;
childMessage.send(eventName, {jobId, number: this.number, end: false});
this.number++;
this.countdown--;
}, 1000);
// 用 setTimeout 模拟任务运行时长
setTimeout(() => {
// 关闭时器
clearInterval(timer);
this.timeoutTimer = setTimeout(() => {
// 关闭时器模拟任务
clearInterval(this.timer);
// 任务结束,重置前端显示
childMessage.send(eventName, {jobId, number:0, pid:0, end: true});
@@ -53,8 +82,8 @@ class TimerJob extends Job {
if (Ps.isChildJob()) {
Ps.exit();
}
}, 10 * 1000)
}
}, this.countdown * 1000)
}
}
TimerJob.toString = () => '[class TimerJob]';

View File

@@ -9,10 +9,6 @@ const Log = require('ee-core/log');
*/
class EffectService extends Service {
constructor(ctx) {
super(ctx);
}
/**
* hello
*/

View File

@@ -92,11 +92,21 @@ class FrameworkService extends Service {
oneTask = this.taskForJob[jobId];
oneTask.kill();
event.sender.send(`${channel}`, {jobId, number:0, pid:0});
}
}
if (action == 'pause') {
oneTask = this.taskForJob[jobId];
oneTask.callFunc('./jobs/example/timer', 'pause', jobId);
}
if (action == 'resume') {
oneTask = this.taskForJob[jobId];
oneTask.callFunc('./jobs/example/timer', 'resume', jobId, oneTask.pid);
}
return res;
}
/**
* 创建pool
*/
@@ -139,7 +149,7 @@ class FrameworkService extends Service {
}
/**
* test
* 获取正在运行的 job 进程
*/
monitorJob() {
setInterval(() => {

View File

@@ -9,12 +9,16 @@
<a-space>
<a-button @click="runJob(1, 'create')">执行任务1</a-button>
进度{{ progress1 }} 进程pid{{ progress1_pid }}
<a-button @click="runJob(1, 'pause')">暂停</a-button>
<a-button @click="runJob(1, 'resume')">恢复</a-button>
<a-button @click="runJob(1, 'close')">关闭</a-button>
</a-space>
<p></p>
<a-space>
<a-button @click="runJob(2, 'create')">执行任务2</a-button>
进度{{ progress2 }} 进程pid{{ progress2_pid }}
<a-button @click="runJob(2, 'pause')">暂停</a-button>
<a-button @click="runJob(2, 'resume')">恢复</a-button>
<a-button @click="runJob(2, 'close')">关闭</a-button>
</a-space>
</div>
@@ -125,7 +129,7 @@ export default {
action: operation
}
ipc.invoke(ipcApiRoute.someJob, params).then(data => {
if (operation == 'close') return;
if (operation != 'create') return;
switch (data.jobId) {
case 1:
this.progress1_pid = data.result.pid;