From eb5cc3943dc0036b7b514b6ea1f49d0faf43b554 Mon Sep 17 00:00:00 2001 From: whyour Date: Sun, 21 Jul 2024 01:15:16 +0800 Subject: [PATCH] =?UTF-8?q?Javascript=20=E5=92=8C=20Python=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=86=85=E7=BD=AE=E5=87=BD=E6=95=B0=20QLAPI.notify?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + back/config/index.ts | 4 ++++ back/loaders/initFile.ts | 5 +++++ sample/ql_sample.js | 6 ++---- sample/ql_sample.py | 5 ++--- shell/preload/sitecustomize.js | 21 +++++++++++++++------ shell/preload/sitecustomize.py | 13 +++++++++++++ shell/share.sh | 2 -- 8 files changed, 42 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 584181f55be..09d1e8f881b 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,4 @@ /.tmp __pycache__ /shell/preload/env.* +/shell/preload/notify.* diff --git a/back/config/index.ts b/back/config/index.ts index 536a5eb22ae..1f28febfdf4 100644 --- a/back/config/index.ts +++ b/back/config/index.ts @@ -41,6 +41,8 @@ const systemLogPath = path.join(dataPath, 'syslog/'); const envFile = path.join(preloadPath, 'env.sh'); const jsEnvFile = path.join(preloadPath, 'env.js'); const pyEnvFile = path.join(preloadPath, 'env.py'); +const jsNotifyFile = path.join(preloadPath, 'notify.js'); +const pyNotifyFile = path.join(preloadPath, 'notify.py'); const confFile = path.join(configPath, 'config.sh'); const crontabFile = path.join(configPath, 'crontab.list'); const authConfigFile = path.join(configPath, 'auth.json'); @@ -92,6 +94,8 @@ export default { envFile, jsEnvFile, pyEnvFile, + jsNotifyFile, + pyNotifyFile, dbPath, uploadPath, configPath, diff --git a/back/loaders/initFile.ts b/back/loaders/initFile.ts index 9aa057e5ae1..3750da2224c 100644 --- a/back/loaders/initFile.ts +++ b/back/loaders/initFile.ts @@ -28,6 +28,8 @@ const sampleNotifyJsFile = path.join(samplePath, 'notify.js'); const sampleNotifyPyFile = path.join(samplePath, 'notify.py'); const scriptNotifyJsFile = path.join(scriptPath, 'sendNotify.js'); const scriptNotifyPyFile = path.join(scriptPath, 'notify.py'); +const jsNotifyFile = path.join(preloadPath, 'notify.js'); +const pyNotifyFile = path.join(preloadPath, 'notify.py'); const TaskBeforeFile = path.join(configPath, 'task_before.sh'); const TaskAfterFile = path.join(configPath, 'task_after.sh'); const homedir = os.homedir(); @@ -102,6 +104,9 @@ export default async () => { await fs.writeFile(confFile, await fs.readFile(sampleConfigFile)); } + await fs.writeFile(jsNotifyFile, await fs.readFile(sampleNotifyJsFile)); + await fs.writeFile(pyNotifyFile, await fs.readFile(sampleNotifyPyFile)); + if (!scriptNotifyJsFileExist) { await fs.writeFile( scriptNotifyJsFile, diff --git a/sample/ql_sample.js b/sample/ql_sample.js index 2e08ba01e21..8a0a6c28b3e 100644 --- a/sample/ql_sample.js +++ b/sample/ql_sample.js @@ -4,8 +4,6 @@ * 定时规则 * cron: 1 9 * * * */ -const { sendNotify } = require('./sendNotify.js'); // commonjs -// import { sendNotify } from './sendNotify'; // es6 - console.log('test scripts'); -sendNotify('test scripts', 'test desc'); +QLAPI.notify('test scripts', 'test desc'); +console.log('test desc'); diff --git a/sample/ql_sample.py b/sample/ql_sample.py index 1595467baff..0fbd1a24385 100644 --- a/sample/ql_sample.py +++ b/sample/ql_sample.py @@ -4,7 +4,6 @@ 定时规则 cron: 1 9 * * * """ -import notify - print("test script") -notify.send('test script', 'test desc') +QLAPI.notify('test script', 'test desc') +print("test desc") diff --git a/shell/preload/sitecustomize.js b/shell/preload/sitecustomize.js index 6f6e1b30efd..74ccf72984a 100644 --- a/shell/preload/sitecustomize.js +++ b/shell/preload/sitecustomize.js @@ -1,6 +1,13 @@ const { execSync } = require('child_process'); +const { sendNotify } = require('./notify.js'); require(`./env.js`); +function initGlobal() { + global.QLAPI = { + notify: sendNotify, + }; +} + function expandRange(rangeStr, max) { const tempRangeStr = rangeStr .trim() @@ -19,6 +26,7 @@ function expandRange(rangeStr, max) { function run() { try { + // TODO: big size const splitStr = '__sitecustomize__'; let command = `bash -c "source ${process.env.taskBefore} ${process.env.fileName}`; if (process.env.task_before) { @@ -31,17 +39,17 @@ function run() { }, ); const [output, envStr] = res.split(splitStr); - const json = JSON.parse(envStr.trim()); - for (const key in json) { - process.env[key] = json[key]; + const newEnvObject = JSON.parse(envStr.trim()); + for (const key in newEnvObject) { + process.env[key] = newEnvObject[key]; } console.log(output); } catch (error) { - console.log(`run task before error `, error); + console.log(`run task before error: `, error.message); } - if (process.env.envParam && process.env.numParam) { - const { envParam, numParam } = process.env; + const { envParam, numParam } = process.env; + if (envParam && numParam) { const array = (process.env[envParam] || '').split('&'); const runArr = expandRange(numParam, array.length); const arrayRun = runArr.map((i) => array[i - 1]); @@ -50,4 +58,5 @@ function run() { } } +initGlobal(); run(); diff --git a/shell/preload/sitecustomize.py b/shell/preload/sitecustomize.py index 3275a417d38..38434eaddaf 100644 --- a/shell/preload/sitecustomize.py +++ b/shell/preload/sitecustomize.py @@ -3,6 +3,18 @@ import env import subprocess import json +import builtins +from notify import send + + +class BaseApi: + def notify(self, *args, **kwargs): + return send(*args, **kwargs) + + +def init_global(): + QLAPI = BaseApi() + builtins.QLAPI = QLAPI def try_parse_int(value): @@ -66,4 +78,5 @@ def run(): os.environ[env_param] = env_str +init_global() run() diff --git a/shell/share.sh b/shell/share.sh index 7afa627260c..bd9823904a7 100755 --- a/shell/share.sh +++ b/shell/share.sh @@ -27,8 +27,6 @@ ql_static_repo=$dir_repo/static ## 文件 file_config_sample=$dir_sample/config.sample.sh file_env=$dir_preload/env.sh -js_file_env=$dir_preload/env.js -py_file_env=$dir_preload/env.py preload_js_file=$dir_preload/sitecustomize.js file_sharecode=$dir_config/sharecode.sh file_config_user=$dir_config/config.sh