add jobs demo for ts

This commit is contained in:
gaoshuaixing
2025-01-08 19:45:19 +08:00
parent 2325f0b32d
commit 47ba701c61
3 changed files with 48 additions and 49 deletions

View File

@@ -1,11 +1,10 @@
'use strict';
import { logger } from 'ee-core/log';
const { logger } = require('ee-core/log');
function welcome() {
logger.info('[child-process] [jobs/example/hello] welcome ! ');
/**
* Welcome function
*/
function welcome(): void {
logger.info('[child-process] [jobs/example/hello] welcome !');
}
module.exports = {
welcome
};
export { welcome };

View File

@@ -1,18 +1,21 @@
'use strict';
const { logger } = require('ee-core/log');
const { isChildJob, exit } = require('ee-core/ps');
const { childMessage } = require('ee-core/message');
const { welcome } = require('./hello');
const { UserService } = require('../../service/job/user');
import { logger } from 'ee-core/log';
import { isChildJob, exit } from 'ee-core/ps';
import { childMessage } from 'ee-core/message';
import { welcome } from './hello';
import { UserService } from '../../service/job/user';
/**
* example - TimerJob
* @class
*/
class TimerJob {
params: any;
timer: NodeJS.Timeout | undefined;
timeoutTimer: NodeJS.Timeout | undefined;
number: number;
countdown: number;
constructor(params) {
constructor(params: any) {
this.params = params;
this.timer = undefined;
this.timeoutTimer = undefined;
@@ -21,43 +24,43 @@ class TimerJob {
}
/**
* handle()方法是必要的,且会被自动调用
* handle() method is necessary and will be automatically called
*/
async handle () {
async handle(): Promise<void> {
logger.info("[child-process] TimerJob params: ", this.params);
const { jobId } = this.params;
// 子进程中使用service
// 1. 确保引入的 service 中不能有electron 的 api或依赖 electron 不支持
// Use service in child process
// 1. Ensure that the service does not have Electron's API or dependencies, as Electron does not support them
const userService = new UserService();
userService.hello('job');
// 执行任务
// Execute the task
this.doTimer(jobId);
}
/**
* 暂停任务运行
* Pause the job
*/
async pause(jobId) {
async pause(jobId: string): Promise<void> {
logger.info("[child-process] Pause timerJob, jobId: ", jobId);
clearInterval(this.timer);
clearInterval(this.timeoutTimer);
}
/**
* 恢复任务运行
* Resume the job
*/
async resume(jobId, pid) {
async resume(jobId: string, pid: number): Promise<void> {
logger.info("[child-process] Resume timerJob, jobId: ", jobId, ", pid: ", pid);
this.doTimer(jobId);
}
/**
* 运行任务
* Run the task
*/
async doTimer(jobId) {
// 计时器模拟任务
// Timer to simulate the task
const eventName = 'job-timer-progress-' + jobId;
this.timer = setInterval(() => {
welcome();
@@ -67,22 +70,22 @@ class TimerJob {
this.countdown--;
}, 1000);
// setTimeout 模拟任务运行时长
// Use setTimeout to simulate the task duration
this.timeoutTimer = setTimeout(() => {
// 关闭计时器模拟任务
// Stop the timer to simulate the task
clearInterval(this.timer);
// 任务结束,重置前端显示
// Task completed, reset the front-end display
childMessage.send(eventName, {jobId, number:0, pid:0, end: true});
// 如果是childJob任务必须调用 exit() 方法,让进程退出,否则会常驻内存
// 如果是childPoolJob任务,常驻内存,等待下一个业务
// If it is a childJob task, call exit() to exit the process, otherwise it will stay in memory
// If it is a childPoolJob task, stay in memory and wait for the next business
if (isChildJob()) {
exit();
}
}, this.countdown * 1000)
}
}
TimerJob.toString = () => '[class TimerJob]';
module.exports = TimerJob;
export default TimerJob;

View File

@@ -32,16 +32,12 @@ class WindowService {
let addr = 'http://localhost:8080'
if (isProd()) {
const { mainServer } = getConfig();
if (isFileProtocol(mainServer.protocol)) {
if (mainServer.protocol && isFileProtocol(mainServer.protocol)) {
addr = mainServer.protocol + path.join(getBaseDir(), mainServer.indexPath);
} else {
addr = mainServer.protocol + mainServer.host + ':' + mainServer.port;
}
}
contentUrl = addr + content;
} else {
// some
}
console.log('contentUrl: ', contentUrl);
@@ -68,9 +64,9 @@ class WindowService {
/**
* Get window contents id
*/
getWCid(args) {
getWCid(args: { windowName: string }): number {
const { windowName } = args;
let win;
let win: BrowserWindow;
if (windowName == 'main') {
win = getMainWindow();
} else {
@@ -83,7 +79,7 @@ class WindowService {
/**
* Realize communication between two windows through the transfer of the main process
*/
communicate(args) {
communicate(args: { receiver: string; content: any }): void {
const { receiver, content } = args;
if (receiver == 'main') {
const win = getMainWindow();
@@ -97,12 +93,12 @@ class WindowService {
/**
* createNotification
*/
createNotification(options, event) {
createNotification(options: any, event: any): void {
const channel = 'controller.os.sendNotification';
this.myNotification = new Notification(options);
if (options.clickEvent) {
this.myNotification.on('click', (e) => {
this.myNotification.on('click', () => {
let data = {
type: 'click',
msg: '您点击了通知消息'
@@ -112,7 +108,7 @@ class WindowService {
}
if (options.closeEvent) {
this.myNotification.on('close', (e) => {
this.myNotification.on('close', () => {
let data = {
type: 'close',
msg: '您关闭了通知消息'
@@ -125,9 +121,10 @@ class WindowService {
}
}
WindowService.toString = () => '[class WindowService]';
module.exports = {
const windowService = new WindowService();
export {
WindowService,
windowService: new WindowService()
};
windowService
}