This commit is contained in:
gsx
2023-04-08 14:06:38 +08:00
parent b15db4f4c2
commit baeec344f9
3 changed files with 15 additions and 21 deletions

View File

@@ -711,7 +711,7 @@ class ExampleController extends Controller {
let num = args.number;
this.service.example.doCreatePool(num, event);
// monitor
// test monitor
this.service.example.monitorJob();
return;

View File

@@ -42,12 +42,11 @@ class TimerJob extends Job {
childMessage.send(eventName, {jobId, number:0, pid:0});
// 如果是childJob任务必须调用 Ps.exit() 方法,让进程退出,否则会常驻内存
// 如果是childPoolJob任务常驻内存等待下一个
// 如果是childPoolJob任务常驻内存等待下一个
if (Ps.isChildJob()) {
Ps.exit();
}
}, 20 * 1000)
}, 10 * 1000)
}
}

View File

@@ -17,7 +17,6 @@ class ExampleService extends Service {
this.myJob = new ChildJob();
this.myJobPool = new ChildPoolJob();
this.taskForJob = {};
this.taskForJobPool = {};
}
/**
@@ -70,13 +69,13 @@ class ExampleService extends Service {
}
/**
* 执行任务
* 创建pool
*/
doCreatePool(num, event) {
const channel = 'controller.example.createPoolNotice';
this.myJobPool.create(num).then(pids => {
event.reply(`${channel}`, pids);
});
});
}
/**
@@ -84,22 +83,18 @@ class ExampleService extends Service {
*/
doJobByPool(jobId, action, event) {
let res = {};
let timerTask;
const channel = 'controller.example.timerJobProgress';
if (action == 'run') {
// 执行任务及监听进度
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.sender.send(`${channel}`, data)
})
res.pid = timerTask.pid;
// ???
//this.taskForJobPool[jobId] = timerTask;
// 异步-执行任务及监听进度
this.myJobPool.runPromise('./jobs/example/timer', {jobId}).then(task => {
task.emitter.on('job-timer-progress', (data) => {
Log.info('[main-process] [ChildPoolJob] timerTask, from TimerJob data:', data);
// 发送数据到渲染进程
event.sender.send(`${channel}`, data)
})
res.pid = task.pid;
});
}
return res;
}