From 1923ceb5c2856b801de67d4fb4a6c09c8c53e368 Mon Sep 17 00:00:00 2001 From: hanhh <18330117883@163.com> Date: Sun, 13 Jun 2021 18:18:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=87=AA=E5=8A=A8=E8=BF=90?= =?UTF-8?q?=E8=A1=8C=E4=BB=BB=E5=8A=A1=E6=97=B6=E4=BB=BB=E5=8A=A1=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E5=B1=95=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 添加bot远程地址 2. 增加文件后缀配置 3. 增加定时任务页数缓存 --- back/api/cron.ts | 21 +++++++++++++++++++++ back/services/cron.ts | 10 ++++++++-- sample/config.sample.sh | 3 +++ shell/api.sh | 26 ++++++++++++++++++++++++++ shell/bot.sh | 8 ++++++++ shell/share.sh | 1 + shell/task.sh | 4 ++++ shell/update.sh | 9 +++++++-- src/pages/crontab/index.tsx | 24 ++++++++++++++++++------ 9 files changed, 96 insertions(+), 10 deletions(-) diff --git a/back/api/cron.ts b/back/api/cron.ts index ee3f6e4e..63436808 100644 --- a/back/api/cron.ts +++ b/back/api/cron.ts @@ -225,4 +225,25 @@ export default (app: Router) => { } }, ); + + route.put( + '/crons/status', + celebrate({ + body: Joi.object({ + ids: Joi.array().items(Joi.string().required()), + status: Joi.string().required(), + }), + }), + async (req: Request, res: Response, next: NextFunction) => { + const logger: Logger = Container.get('logger'); + try { + const cronService = Container.get(CronService); + const data = await cronService.status(req.body); + return res.send({ code: 200, data }); + } catch (e) { + logger.error('🔥 error: %o', e); + return next(e); + } + }, + ); }; diff --git a/back/services/cron.ts b/back/services/cron.ts index c5c718ec..88f94032 100644 --- a/back/services/cron.ts +++ b/back/services/cron.ts @@ -83,8 +83,14 @@ export default class CronService { }); } - public async status(_id: string, stopped: boolean) { - this.cronDb.update({ _id }, { $set: { stopped, saved: false } }); + public async status({ + ids, + status, + }: { + ids: string[]; + status: CrontabStatus; + }) { + this.cronDb.update({ _id: { $in: ids } }, { $set: { status } }); } public async remove(ids: string[]) { diff --git a/sample/config.sample.sh b/sample/config.sample.sh index 44efdf7b..924ed3d2 100644 --- a/sample/config.sample.sh +++ b/sample/config.sample.sh @@ -10,6 +10,9 @@ AutoDelCron="true" ## 在运行 ql repo 命令时,是否自动增加新的本地定时任务 AutoAddCron="true" +## ql repo命令拉取脚本时需要拉取的文件后缀,直接写文件后缀名即可 +RepoFileExtensions="js py" + ## 由于github仓库拉取较慢,所以会默认添加代理前缀,如不需要请移除 GithubProxyUrl="https://ghproxy.com/" diff --git a/shell/api.sh b/shell/api.sh index 2e3b7d81..b5bec373 100755 --- a/shell/api.sh +++ b/shell/api.sh @@ -147,3 +147,29 @@ get_user_info() { exit 0 fi } + +update_cron_status() { + local ids=$1 + local status=$2 + local currentTimeStamp=$(date +%s) + local api=$( + curl -s "http://localhost:5700/api/crons/status?t=$currentTimeStamp" \ + -X 'PUT' \ + -H "Accept: application/json" \ + -H "Authorization: Bearer $token" \ + -H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36" \ + -H "Content-Type: application/json;charset=UTF-8" \ + -H "Origin: http://localhost:5700" \ + -H "Referer: http://localhost:5700/crontab" \ + -H "Accept-Language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7" \ + --data-raw "{\"ids\":\"[$ids]\",\"status\":\"$status\"}" \ + --compressed + ) + code=$(echo $api | jq -r .code) + message=$(echo $api | jq -r .message) + if [[ $code == 200 ]]; then + echo -e "成功" + else + echo -e "失败(${message})" + fi +} diff --git a/shell/bot.sh b/shell/bot.sh index e047721f..c6b71d05 100644 --- a/shell/bot.sh +++ b/shell/bot.sh @@ -3,6 +3,7 @@ ## 导入通用变量与函数 dir_shell=/ql/shell . $dir_shell/share.sh +url="${github_proxy_url}https://github.com/SuMaiKaDe/bot.git" repo_path="${dir_repo}/dockerbot" echo -e "\n1、安装bot依赖...\n" @@ -10,6 +11,13 @@ apk --no-cache add -f zlib-dev gcc jpeg-dev python3-dev musl-dev freetype-dev echo -e "\nbot依赖安装成功...\n" echo -e "2、下载bot所需文件...\n" +if [ -d ${repo_path}/.git ]; then + git_pull_scripts ${repo_path} +else + rm -rf ${repo_path} + git_clone_scripts ${url} ${repo_path} "main" +fi + cp -rf "$repo_path/jbot" $dir_root if [[ ! -f "$dir_root/config/bot.json" ]]; then cp -f "$repo_path/config/bot.json" "$dir_root/config" diff --git a/shell/share.sh b/shell/share.sh index 464be17f..e4d9e0f2 100755 --- a/shell/share.sh +++ b/shell/share.sh @@ -62,6 +62,7 @@ import_config() { command_timeout_time=${CommandTimeoutTime:-"1h"} github_proxy_url=${GithubProxyUrl:-""} block_cookie=${TempBlockCookie:-""} + file_extensions=${RepoFileExtensions:-"js py"} } ## 创建目录,$1:目录的绝对路径 diff --git a/shell/task.sh b/shell/task.sh index b6208786..35e3ae6c 100755 --- a/shell/task.sh +++ b/shell/task.sh @@ -120,6 +120,10 @@ run_normal() { log_dir="$dir_log/${log_dir_tmp%%.*}" log_path="$log_dir/$log_time.log" make_dir "$log_dir" + + local id=$(cat $list_crontab_user | grep -E "$cmd_task $p1$" | perl -pe "s|.*ID=(.*) $cmd_task $p1$|\1|" | xargs | sed 's/ /","/g') + local status="0" ## 0 任务运行中 + update_cron_status "\"$id\"" $status timeout $command_timeout_time $which_program $p1 2>&1 | tee $log_path } diff --git a/shell/update.sh b/shell/update.sh index bc5cb976..21fac8b9 100755 --- a/shell/update.sh +++ b/shell/update.sh @@ -357,7 +357,12 @@ gen_list_repo() { rm -f $dir_list_tmp/${repo}*.list >/dev/null 2>&1 cd ${repo_path} - files=$(find . -name "*.js" | sed 's/^..//') + + local cmd="find ." + for extension in $file_extensions; do + cmd="${cmd} -o -name \"*.${extension}\"" + done + files=$($cmd | sed 's/^..//') if [[ $path ]]; then files=$(echo "$files" | egrep $path) fi @@ -365,7 +370,7 @@ gen_list_repo() { files=$(echo "$files" | egrep -v $blackword) fi if [[ $dependence ]]; then - find . -name "*.js" | sed 's/^..//' | egrep $dependence | xargs -i cp {} $dir_scripts + $cmd | sed 's/^..//' | egrep $dependence | xargs -i cp {} $dir_scripts fi for file in ${files}; do filename=$(basename $file) diff --git a/src/pages/crontab/index.tsx b/src/pages/crontab/index.tsx index 8d57ae2f..18da79dc 100644 --- a/src/pages/crontab/index.tsx +++ b/src/pages/crontab/index.tsx @@ -187,6 +187,8 @@ const Crontab = () => { const [isLogModalVisible, setIsLogModalVisible] = useState(false); const [logCron, setLogCron] = useState(); const [selectedRowIds, setSelectedRowIds] = useState([]); + const [currentPage, setCurrentPage] = useState(1); + const [pageSize, setPageSize] = useState(20); const getCrons = () => { setLoading(true); @@ -233,7 +235,7 @@ const Crontab = () => { if (data.code === 200) { message.success('删除成功'); const result = [...value]; - result.splice(index, 1); + result.splice(index + pageSize * (currentPage - 1), 1); setValue(result); } else { message.error(data); @@ -264,7 +266,7 @@ const Crontab = () => { .then((data: any) => { if (data.code === 200) { const result = [...value]; - result.splice(index, 1, { + result.splice(index + pageSize * (currentPage - 1), 1, { ...record, status: CrontabStatus.running, }); @@ -298,7 +300,7 @@ const Crontab = () => { .then((data: any) => { if (data.code === 200) { const result = [...value]; - result.splice(index, 1, { + result.splice(index + pageSize * (currentPage - 1), 1, { ...record, pid: null, status: CrontabStatus.idle, @@ -342,7 +344,7 @@ const Crontab = () => { if (data.code === 200) { const newStatus = record.isDisabled === 1 ? 0 : 1; const result = [...value]; - result.splice(index, 1, { + result.splice(index + pageSize * (currentPage - 1), 1, { ...record, isDisabled: newStatus, }); @@ -429,7 +431,7 @@ const Crontab = () => { if (index === -1) { result.push(cron); } else { - result.splice(index, 1, { + result.splice(index + pageSize * (currentPage - 1), 1, { ...cron, }); } @@ -442,7 +444,7 @@ const Crontab = () => { .then((data: any) => { const index = value.findIndex((x) => x._id === cron._id); const result = [...value]; - result.splice(index, 1, { + result.splice(index + pageSize * (currentPage - 1), 1, { ...cron, ...data.data, }); @@ -511,6 +513,12 @@ const Crontab = () => { }); }; + const onPageChange = (page: number, pageSize: number | undefined) => { + setCurrentPage(page); + setPageSize(pageSize as number); + localStorage.setItem('pageSize', pageSize + ''); + }; + useEffect(() => { if (logCron) { localStorage.setItem('logCron', logCron._id); @@ -532,6 +540,7 @@ const Crontab = () => { setMarginLeft(0); setMarginTop(-72); } + setPageSize(parseInt(localStorage.getItem('pageSize') || '20')); }, []); return ( @@ -602,6 +611,9 @@ const Crontab = () => { columns={columns} pagination={{ hideOnSinglePage: true, + current: currentPage, + onChange: onPageChange, + pageSize: pageSize, showSizeChanger: true, defaultPageSize: 20, showTotal: (total: number, range: number[]) =>