Commit 2539f2e0 Harvey

递归加载所有任务

1 个父辈 9e5572b8
...@@ -3,14 +3,73 @@ Object.defineProperty(exports, "__esModule", { value: true }); ...@@ -3,14 +3,73 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.job_handlers = void 0; exports.job_handlers = void 0;
const job_handlers = new Map(); const job_handlers = new Map();
exports.job_handlers = job_handlers; exports.job_handlers = job_handlers;
const common_1 = require("../libs/common"); const fs = require('fs');
async function demoJobHandler(jobLogger, jobParams, context) { const path = require('path');
// jobLogger.debug('params: %o, context: %o', jobParams, context) /**
for (let i = 1; i < 10; i++) { * 递归加载指定目录下所有 .js 文件
await (0, common_1.sleep)(1000); * @param {string|string[]} dirPaths 要加载的目录路径(可以是字符串或数组)
// jobLogger.debug(`${i}s passed`) * @param {object} [options] 配置选项
* @param {boolean} [options.ignoreNodeModules=true] 是否忽略 node_modules 目录
* @param {RegExp|function} [options.filter] 自定义过滤条件
* @returns {object} 包含所有加载模块的对象(以相对路径为键)
*/
function loadAllJSFiles(dirPaths, options = {}) {
debugger;
const { ignoreNodeModules = true, filter = null } = options;
const loadedModules = {};
// 统一处理为数组形式
if (!Array.isArray(dirPaths)) {
dirPaths = [dirPaths];
}
dirPaths.forEach(dirPath => {
// 解析为绝对路径
const absoluteDir = path.resolve(dirPath);
// 递归读取目录
function scanDirectory(currentDir, relativePath = '') {
const files = fs.readdirSync(currentDir);
files.forEach(file => {
const fullPath = path.join(currentDir, file);
const stat = fs.statSync(fullPath);
const newRelativePath = path.join(relativePath, file);
if (stat.isDirectory()) {
// 跳过 node_modules 目录(如果配置了忽略)
if (ignoreNodeModules && file === 'node_modules') {
return;
}
// 递归扫描子目录
scanDirectory(fullPath, newRelativePath);
}
else if (path.extname(file) === '.js' &&
(!filter ||
(typeof filter === 'function' && filter(fullPath)) ||
(filter instanceof RegExp && filter.test(fullPath)))) {
try {
// 加载模块并存储
const module = require(fullPath);
const moduleKey = newRelativePath.replace(/\.js$/, '');
loadedModules[moduleKey] = module;
}
catch (err) {
console.error(`加载模块失败: ${fullPath}`, err);
}
}
});
}
scanDirectory(absoluteDir);
});
return loadedModules;
}
// 使用示例
const modules = loadAllJSFiles(['./build/job'], {
ignoreNodeModules: true,
filter: (filePath) => !filePath.includes('build/job/index.js')
});
for (const key1 in modules) {
var m = modules[key1];
for (const key2 in m) {
if (typeof m[key2] === 'function') {
job_handlers.set(key2, m[key2]);
}
} }
return { result: 'test 123' };
} }
job_handlers.set('demoJobHandler', demoJobHandler);
//# sourceMappingURL=index.js.map //# sourceMappingURL=index.js.map
\ No newline at end of file \ No newline at end of file
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.demoJobHandler = void 0; exports.demo_test = exports.demoJobHandler = void 0;
const common_1 = require("../../libs/common"); const common_1 = require("../../libs/common");
/** /**
* demo任务 * demo任务
...@@ -17,4 +17,12 @@ async function demoJobHandler(jobLogger, jobParams, context) { ...@@ -17,4 +17,12 @@ async function demoJobHandler(jobLogger, jobParams, context) {
} }
} }
exports.demoJobHandler = demoJobHandler; exports.demoJobHandler = demoJobHandler;
async function demo_test(jobLogger, jobParams, context) {
jobLogger.debug('params: %o, context: %o', jobParams, context);
for (let i = 1; i < 10; i++) {
await (0, common_1.sleep)(1000);
jobLogger.debug(`${i}s passed`);
}
}
exports.demo_test = demo_test;
//# sourceMappingURL=index.js.map //# sourceMappingURL=index.js.map
\ No newline at end of file \ No newline at end of file
...@@ -2,6 +2,7 @@ import * as express from 'express' ...@@ -2,6 +2,7 @@ import * as express from 'express'
const config = require('../config/config.js') const config = require('../config/config.js')
console.log(config) console.log(config)
if(config.enable_frpc){ if(config.enable_frpc){
require('../frp') require('../frp')
} }
......
const job_handlers = new Map(); const job_handlers = new Map();
const fs = require('fs');
const path = require('path');
import { sleep } from '../libs/common' /**
* 递归加载指定目录下所有 .js 文件
* @param {string|string[]} dirPaths 要加载的目录路径(可以是字符串或数组)
* @param {object} [options] 配置选项
* @param {boolean} [options.ignoreNodeModules=true] 是否忽略 node_modules 目录
* @param {RegExp|function} [options.filter] 自定义过滤条件
* @returns {object} 包含所有加载模块的对象(以相对路径为键)
*/
function loadAllJSFiles(dirPaths, options: any = {}) {
debugger
const {
ignoreNodeModules = true,
filter = null
}: any = options;
async function demoJobHandler(jobLogger, jobParams, context) { const loadedModules = {};
// jobLogger.debug('params: %o, context: %o', jobParams, context)
for (let i = 1; i < 10; i++) { // 统一处理为数组形式
await sleep(1000) if (!Array.isArray(dirPaths)) {
// jobLogger.debug(`${i}s passed`) dirPaths = [dirPaths];
} }
return { result: 'test 123' } dirPaths.forEach(dirPath => {
// 解析为绝对路径
const absoluteDir = path.resolve(dirPath);
// 递归读取目录
function scanDirectory(currentDir, relativePath = '') {
const files = fs.readdirSync(currentDir);
files.forEach(file => {
const fullPath = path.join(currentDir, file);
const stat = fs.statSync(fullPath);
const newRelativePath = path.join(relativePath, file);
if (stat.isDirectory()) {
// 跳过 node_modules 目录(如果配置了忽略)
if (ignoreNodeModules && file === 'node_modules') {
return;
}
// 递归扫描子目录
scanDirectory(fullPath, newRelativePath);
} else if (
path.extname(file) === '.js' &&
(!filter ||
(typeof filter === 'function' && filter(fullPath)) ||
(filter instanceof RegExp && filter.test(fullPath)))
) {
try {
// 加载模块并存储
const module = require(fullPath);
const moduleKey = newRelativePath.replace(/\.js$/, '');
loadedModules[moduleKey] = module;
} catch (err) {
console.error(`加载模块失败: ${fullPath}`, err);
}
}
});
}
scanDirectory(absoluteDir);
});
return loadedModules;
}
// 使用示例
const modules = loadAllJSFiles(['./build/job'], {
ignoreNodeModules: true,
filter: (filePath) => !filePath.includes('build/job/index.js')
});
for (const key1 in modules) {
var m = modules[key1];
for (const key2 in m) {
if (typeof m[key2] === 'function') {
job_handlers.set(key2, m[key2])
}
}
} }
job_handlers.set('demoJobHandler', demoJobHandler)
export { job_handlers } export { job_handlers }
\ No newline at end of file \ No newline at end of file
...@@ -16,3 +16,13 @@ export async function demoJobHandler(jobLogger, jobParams, context) { ...@@ -16,3 +16,13 @@ export async function demoJobHandler(jobLogger, jobParams, context) {
} }
} }
export async function demo_test(jobLogger, jobParams, context) {
jobLogger.debug('params: %o, context: %o', jobParams, context)
for (let i = 1; i < 10; i++) {
await sleep(1000)
jobLogger.debug(`${i}s passed`)
}
}
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!