Skip to content

Commit

Permalink
Javascript 和 Python 增加内置函数 QLAPI.notify
Browse files Browse the repository at this point in the history
  • Loading branch information
whyour committed Jul 20, 2024
1 parent 1b39d3a commit eb5cc39
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@
/.tmp
__pycache__
/shell/preload/env.*
/shell/preload/notify.*
4 changes: 4 additions & 0 deletions back/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -92,6 +94,8 @@ export default {
envFile,
jsEnvFile,
pyEnvFile,
jsNotifyFile,
pyNotifyFile,
dbPath,
uploadPath,
configPath,
Expand Down
5 changes: 5 additions & 0 deletions back/loaders/initFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 2 additions & 4 deletions sample/ql_sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
5 changes: 2 additions & 3 deletions sample/ql_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
21 changes: 15 additions & 6 deletions shell/preload/sitecustomize.js
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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) {
Expand All @@ -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]);
Expand All @@ -50,4 +58,5 @@ function run() {
}
}

initGlobal();
run();
13 changes: 13 additions & 0 deletions shell/preload/sitecustomize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -66,4 +78,5 @@ def run():
os.environ[env_param] = env_str


init_global()
run()
2 changes: 0 additions & 2 deletions shell/share.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit eb5cc39

Please sign in to comment.