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

View File

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

View File

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

View File

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

View File

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