This commit is contained in:
哆啦好梦
2023-04-04 16:36:18 +08:00
parent 91bf5a1a84
commit 65cb30100b
4 changed files with 153 additions and 30 deletions

View File

@@ -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
*/