Skip to content
切换导航条
切换导航条
当前项目
正在载入...
登录
Harvey
/
job-executor
转到一个项目
切换导航栏
切换导航栏固定状态
项目
群组
代码片段
帮助
项目
活动
版本库
流水线
图表
问题
0
合并请求
0
维基
网络
创建新的问题
作业
提交
问题看板
文件
提交
网络
比较
分支
标签
Commit 2539f2e0
由
Harvey
编写于
2025-05-12 11:40:12 +0800
浏览文件
选项
浏览文件
标签
下载
电子邮件补丁
差异文件
递归加载所有任务
1 个父辈
9e5572b8
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
165 行增加
和
17 行删除
build/job/index.js
build/job/test/index.js
src/index.ts
src/job/index.ts
src/job/test/index.ts
build/job/index.js
查看文件 @
2539f2e
...
@@ -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
build/job/test/index.js
查看文件 @
2539f2e
"use strict"
;
"use strict"
;
Object
.
defineProperty
(
exports
,
"__esModule"
,
{
value
:
true
});
Object
.
defineProperty
(
exports
,
"__esModule"
,
{
value
:
true
});
exports
.
demoJobHandler
=
void
0
;
exports
.
demo
_test
=
exports
.
demo
JobHandler
=
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
src/index.ts
查看文件 @
2539f2e
...
@@ -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'
)
}
}
...
...
src/job/index.ts
查看文件 @
2539f2e
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
src/job/test/index.ts
查看文件 @
2539f2e
...
@@ -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!
Cancel
请
注册
或
登录
后发表评论