mirror of
https://github.com/whyour/qinglong.git
synced 2026-11-18 00:57:53 +08:00
Compare commits
14
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d9e9e083ab | ||
|
|
8a9e70e90b | ||
|
|
3b6a873520 | ||
|
|
2ddb592800 | ||
|
|
b838d4bb42 | ||
|
|
73ad00ba32 | ||
|
|
27b322a10d | ||
|
|
1cfc0c8e2e | ||
|
|
cae580e02f | ||
|
|
76b285c3db | ||
|
|
13e0ff55d4 | ||
|
|
2bf814e562 | ||
|
|
41910b5236 | ||
|
|
442ab2c616 |
@@ -33,6 +33,10 @@ on:
|
|||||||
type: choice
|
type: choice
|
||||||
options: [branch, tag]
|
options: [branch, tag]
|
||||||
default: branch
|
default: branch
|
||||||
|
npm_rc_version:
|
||||||
|
description: Publish the verified main npm RC from develop, for example 2.25.0-rc.0
|
||||||
|
type: string
|
||||||
|
default: ''
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: read
|
||||||
@@ -470,7 +474,7 @@ jobs:
|
|||||||
retention-days: 14
|
retention-days: 14
|
||||||
|
|
||||||
publish:
|
publish:
|
||||||
if: ${{ github.repository == 'whyour/qinglong' && github.ref == 'refs/heads/master' }}
|
if: ${{ github.repository == 'whyour/qinglong' && (github.ref == 'refs/heads/master' || (github.event_name == 'workflow_dispatch' && github.ref == 'refs/heads/develop')) }}
|
||||||
needs: [build-alpine, build-debian, verify-npm-package]
|
needs: [build-alpine, build-debian, verify-npm-package]
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
permissions:
|
permissions:
|
||||||
@@ -491,6 +495,8 @@ jobs:
|
|||||||
|
|
||||||
- name: Publish npm package with OIDC
|
- name: Publish npm package with OIDC
|
||||||
shell: bash
|
shell: bash
|
||||||
|
env:
|
||||||
|
NPM_RC_VERSION: ${{ inputs.npm_rc_version }}
|
||||||
run: |
|
run: |
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
shopt -s nullglob
|
shopt -s nullglob
|
||||||
@@ -503,11 +509,21 @@ jobs:
|
|||||||
metadata=$(tar -xOf "$archive" package/package.json)
|
metadata=$(tar -xOf "$archive" package/package.json)
|
||||||
name=$(jq -er '.name | select(. == "@whyour/qinglong")' <<< "$metadata")
|
name=$(jq -er '.name | select(. == "@whyour/qinglong")' <<< "$metadata")
|
||||||
version=$(jq -er '.version | select(type == "string")' <<< "$metadata")
|
version=$(jq -er '.version | select(type == "string")' <<< "$metadata")
|
||||||
|
if [[ "${GITHUB_EVENT_NAME:-}" == workflow_dispatch && "${GITHUB_REF:-}" == refs/heads/develop ]]; then
|
||||||
|
if [[ ! "${NPM_RC_VERSION:-}" =~ ^[0-9]+\.[0-9]+\.[0-9]+-rc\.[0-9]+$ || "$version" != "${NPM_RC_VERSION:-}" ]]; then
|
||||||
|
echo '::error::Develop npm publication requires the exact requested RC archive version.'
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
if npm view "$name@$version" version --json --registry=https://registry.npmjs.org > "$RUNNER_TEMP/npm-version.json" 2> "$RUNNER_TEMP/npm-version-error.log"; then
|
if npm view "$name@$version" version --json --registry=https://registry.npmjs.org > "$RUNNER_TEMP/npm-version.json" 2> "$RUNNER_TEMP/npm-version-error.log"; then
|
||||||
jq -e --arg version "$version" '. == $version' "$RUNNER_TEMP/npm-version.json" > /dev/null
|
jq -e --arg version "$version" '. == $version' "$RUNNER_TEMP/npm-version.json" > /dev/null
|
||||||
echo "::notice::$name@$version is already published; skipping npm publication for this image rebuild."
|
echo "::notice::$name@$version is already published; skipping npm publication for this image rebuild."
|
||||||
elif jq -e '.error.code == "E404"' "$RUNNER_TEMP/npm-version.json" > /dev/null; then
|
elif jq -e '.error.code == "E404"' "$RUNNER_TEMP/npm-version.json" > /dev/null; then
|
||||||
|
if [[ "${GITHUB_EVENT_NAME:-}" == workflow_dispatch && "${GITHUB_REF:-}" == refs/heads/develop ]]; then
|
||||||
|
npm publish "./$archive" --ignore-scripts --access public --tag rc --registry=https://registry.npmjs.org
|
||||||
|
else
|
||||||
npm publish "./$archive" --ignore-scripts --access public --registry=https://registry.npmjs.org
|
npm publish "./$archive" --ignore-scripts --access public --registry=https://registry.npmjs.org
|
||||||
|
fi
|
||||||
else
|
else
|
||||||
echo '::error::Could not check the published npm version; refusing to publish.'
|
echo '::error::Could not check the published npm version; refusing to publish.'
|
||||||
exit 1
|
exit 1
|
||||||
|
|||||||
+122
-11
@@ -1,4 +1,10 @@
|
|||||||
|
import { executionIdPattern, executionTriggerId, receiptFinishTime } from '../shared/executionIdentity';
|
||||||
|
import { readDailyResults } from '../shared/legacyDailyStatistics';
|
||||||
|
import { PendingCronInstall } from '../shared/errors';
|
||||||
|
import { triggerHistory } from '../shared/triggerHistory';
|
||||||
|
import { previewSchedule, withNextRun } from '../shared/cronPreview';
|
||||||
import { Router, Request, Response, NextFunction } from 'express';
|
import { Router, Request, Response, NextFunction } from 'express';
|
||||||
|
import { literal, Op } from 'sequelize';
|
||||||
import { Container } from 'typedi';
|
import { Container } from 'typedi';
|
||||||
import { Logger } from 'winston';
|
import { Logger } from 'winston';
|
||||||
import CronService from '../services/cron';
|
import CronService from '../services/cron';
|
||||||
@@ -164,14 +170,25 @@ export default (app: Router) => {
|
|||||||
const logger: Logger = Container.get('logger');
|
const logger: Logger = Container.get('logger');
|
||||||
try {
|
try {
|
||||||
const cronService = Container.get(CronService);
|
const cronService = Container.get(CronService);
|
||||||
const data = await cronService.crontabs(req.query as any);
|
const result = await cronService.crontabs(req.query as any, true);
|
||||||
return res.send({ code: 200, data });
|
return res.send({ code: 200, data: { ...result, data: result.data.map(withNextRun) } });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.error('🔥 error: %o', e);
|
logger.error('🔥 error: %o', e);
|
||||||
return next(e);
|
return next(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
route.get('/preview', celebrate({ query: Joi.object({
|
||||||
|
schedule: Joi.string().required(),
|
||||||
|
}).unknown(true) }), (req, res) => {
|
||||||
|
try {
|
||||||
|
const times = previewSchedule(String(req.query.schedule), 3, Date.now());
|
||||||
|
return res.send({ code: 200, data: { times, timezone: Intl.DateTimeFormat().resolvedOptions().timeZone } });
|
||||||
|
} catch {
|
||||||
|
return res.status(400).send({ code: 400, message: t('Cron表达式格式有误') });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
route.get(
|
route.get(
|
||||||
'/detail',
|
'/detail',
|
||||||
async (req: Request, res: Response, next: NextFunction) => {
|
async (req: Request, res: Response, next: NextFunction) => {
|
||||||
@@ -199,6 +216,11 @@ export default (app: Router) => {
|
|||||||
const data = await cronService.create(req.body);
|
const data = await cronService.create(req.body);
|
||||||
return res.send({ code: 200, data });
|
return res.send({ code: 200, data });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
if (e instanceof PendingCronInstall) {
|
||||||
|
return res.status(e.status).send({
|
||||||
|
code: e.status, message: e.message, data: { id: e.cronId, saved: false },
|
||||||
|
});
|
||||||
|
}
|
||||||
return next(e);
|
return next(e);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -224,13 +246,14 @@ export default (app: Router) => {
|
|||||||
route.put(
|
route.put(
|
||||||
'/stop',
|
'/stop',
|
||||||
celebrate({
|
celebrate({
|
||||||
|
headers: Joi.object({ 'idempotency-key': Joi.string().guid().optional() }).unknown(true),
|
||||||
body: Joi.array().items(Joi.number().required()),
|
body: Joi.array().items(Joi.number().required()),
|
||||||
}),
|
}),
|
||||||
async (req: Request, res: Response, next: NextFunction) => {
|
async (req: Request, res: Response, next: NextFunction) => {
|
||||||
const logger: Logger = Container.get('logger');
|
const logger: Logger = Container.get('logger');
|
||||||
try {
|
try {
|
||||||
const cronService = Container.get(CronService);
|
const cronService = Container.get(CronService);
|
||||||
const data = await cronService.stop(req.body);
|
const data = await cronService.stop(req.body, req.get('Idempotency-Key'));
|
||||||
return res.send({ code: 200, data });
|
return res.send({ code: 200, data });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return next(e);
|
return next(e);
|
||||||
@@ -322,6 +345,11 @@ export default (app: Router) => {
|
|||||||
id: Joi.number().required(),
|
id: Joi.number().required(),
|
||||||
}),
|
}),
|
||||||
query: Joi.object({
|
query: Joi.object({
|
||||||
|
triggerId: Joi.string().max(256).optional(),
|
||||||
|
instanceId: Joi.number().integer().positive().optional(),
|
||||||
|
previousPath: Joi.string().optional(),
|
||||||
|
follow: Joi.boolean().optional(),
|
||||||
|
cursor: Joi.string().pattern(/^\d{1,16}\.[a-f0-9]{64}$/).optional(),
|
||||||
offset: Joi.number().integer().min(0).optional(),
|
offset: Joi.number().integer().min(0).optional(),
|
||||||
limit: Joi.number()
|
limit: Joi.number()
|
||||||
.integer()
|
.integer()
|
||||||
@@ -337,6 +365,11 @@ export default (app: Router) => {
|
|||||||
try {
|
try {
|
||||||
const cronService = Container.get(CronService);
|
const cronService = Container.get(CronService);
|
||||||
const result = await cronService.log(req.params.id, {
|
const result = await cronService.log(req.params.id, {
|
||||||
|
triggerId: req.query.triggerId as string | undefined,
|
||||||
|
instanceId: req.query.instanceId === undefined ? undefined : Number(req.query.instanceId),
|
||||||
|
previousPath: req.query.previousPath as string | undefined,
|
||||||
|
follow: req.query.follow as unknown as boolean,
|
||||||
|
cursor: req.query.cursor as string | undefined,
|
||||||
offset: req.query.offset as unknown as number,
|
offset: req.query.offset as unknown as number,
|
||||||
limit: req.query.limit as unknown as number,
|
limit: req.query.limit as unknown as number,
|
||||||
tail: req.query.tail as unknown as boolean,
|
tail: req.query.tail as unknown as boolean,
|
||||||
@@ -345,6 +378,10 @@ export default (app: Router) => {
|
|||||||
code: 200,
|
code: 200,
|
||||||
data: result.content,
|
data: result.content,
|
||||||
logStatus: result.status,
|
logStatus: result.status,
|
||||||
|
logPath: result.logPath,
|
||||||
|
triggerId: result.triggerId,
|
||||||
|
cursor: result.cursor,
|
||||||
|
reset: result.reset,
|
||||||
offset: result.offset,
|
offset: result.offset,
|
||||||
nextOffset: result.nextOffset,
|
nextOffset: result.nextOffset,
|
||||||
total: result.total,
|
total: result.total,
|
||||||
@@ -379,7 +416,10 @@ export default (app: Router) => {
|
|||||||
route.delete(
|
route.delete(
|
||||||
'/',
|
'/',
|
||||||
celebrate({
|
celebrate({
|
||||||
body: Joi.array().items(Joi.number().required()),
|
body: Joi.array().min(1).items(Joi.number().integer().positive(), Joi.object({
|
||||||
|
id: Joi.number().integer().positive().required(), script: Joi.string().required(),
|
||||||
|
revision: Joi.number().integer(), command: Joi.string(), sub_id: Joi.number().integer().allow(null),
|
||||||
|
})),
|
||||||
}),
|
}),
|
||||||
async (req: Request, res: Response, next: NextFunction) => {
|
async (req: Request, res: Response, next: NextFunction) => {
|
||||||
const logger: Logger = Container.get('logger');
|
const logger: Logger = Container.get('logger');
|
||||||
@@ -452,8 +492,8 @@ export default (app: Router) => {
|
|||||||
const logger: Logger = Container.get('logger');
|
const logger: Logger = Container.get('logger');
|
||||||
try {
|
try {
|
||||||
const cronService = Container.get(CronService);
|
const cronService = Container.get(CronService);
|
||||||
const data = await cronService.getDb({ id: req.params.id });
|
const data = await cronService.getDb({ id: req.params.id }, true);
|
||||||
return res.send({ code: 200, data });
|
return res.send({ code: 200, data: withNextRun(data) });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return next(e);
|
return next(e);
|
||||||
}
|
}
|
||||||
@@ -471,13 +511,19 @@ export default (app: Router) => {
|
|||||||
last_running_time: Joi.number().optional().allow(null),
|
last_running_time: Joi.number().optional().allow(null),
|
||||||
last_execution_time: Joi.number().optional().allow(null),
|
last_execution_time: Joi.number().optional().allow(null),
|
||||||
exit_code: Joi.number().optional().allow(null),
|
exit_code: Joi.number().optional().allow(null),
|
||||||
}),
|
trigger_stopped: Joi.boolean().optional(),
|
||||||
|
execution_id: Joi.string().pattern(executionIdPattern).optional(),
|
||||||
|
trigger_id: Joi.string().pattern(/^[a-z0-9-]{1,120}$/).optional(),
|
||||||
|
}).oxor('execution_id', 'trigger_id'),
|
||||||
}),
|
}),
|
||||||
async (req: Request, res: Response, next: NextFunction) => {
|
async (req: Request, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
const cronService = Container.get(CronService);
|
const cronService = Container.get(CronService);
|
||||||
|
const { execution_id, ...body } = req.body;
|
||||||
const data = await cronService.status({
|
const data = await cronService.status({
|
||||||
...req.body,
|
...body,
|
||||||
|
finished_at: receiptFinishTime(req.get('X-QL-Finished-At')),
|
||||||
|
...(execution_id && { trigger_id: executionTriggerId(execution_id) }),
|
||||||
status: req.body.status ? parseInt(req.body.status) : undefined,
|
status: req.body.status ? parseInt(req.body.status) : undefined,
|
||||||
pid: req.body.pid ? parseInt(req.body.pid) : undefined,
|
pid: req.body.pid ? parseInt(req.body.pid) : undefined,
|
||||||
});
|
});
|
||||||
@@ -488,22 +534,87 @@ export default (app: Router) => {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
route.get('/:id/daily-results', celebrate({
|
||||||
|
params: Joi.object({ id: Joi.number().integer().positive().required() }),
|
||||||
|
}), async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
const data = await readDailyResults(RunningInstanceModel.sequelize!, Number(req.params.id));
|
||||||
|
return res.send({ code: 200, data });
|
||||||
|
} catch (error) {
|
||||||
|
return next(error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
route.get('/:id/triggers', celebrate({
|
||||||
|
params: Joi.object({ id: Joi.number().integer().positive().required() }),
|
||||||
|
query: Joi.object({
|
||||||
|
offset: Joi.number().integer().min(0),
|
||||||
|
limit: Joi.number().integer().min(1).max(26).default(25),
|
||||||
|
ids: Joi.array().items(Joi.string().max(256)).min(1).max(25).single(),
|
||||||
|
beforeTime: Joi.number().integer().min(0),
|
||||||
|
beforeId: Joi.string().max(256),
|
||||||
|
}).and('beforeTime', 'beforeId').oxor('offset', 'beforeTime', 'ids').unknown(true),
|
||||||
|
}), async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
const { offset, limit, beforeTime, beforeId, ids } = req.query;
|
||||||
|
const history = triggerHistory();
|
||||||
|
const before = beforeId ? { time: Number(beforeTime), id: String(beforeId) } : undefined;
|
||||||
|
const data = ids
|
||||||
|
? await history.visibleTriggers(Number(req.params.id), ids as string[])
|
||||||
|
: await history.recent(Number(req.params.id), Number(offset || 0), Number(limit), before);
|
||||||
|
if (req.query.withQueueWait === 'true') {
|
||||||
|
const now = Date.now();
|
||||||
|
return res.send({ code: 200, data: data.map(row => row.state === 'queued' && row.queued_at != null
|
||||||
|
? { ...row, queue_wait_ms: Math.max(0, (row.dequeued_at ?? now) - row.queued_at) } : row) });
|
||||||
|
}
|
||||||
|
return res.send({ code: 200, data });
|
||||||
|
} catch (err) { next(err); }
|
||||||
|
});
|
||||||
|
|
||||||
route.get(
|
route.get(
|
||||||
'/:id/instances',
|
'/:id/instances',
|
||||||
celebrate({
|
celebrate({
|
||||||
|
// This route historically ignored query parameters. Only validate and
|
||||||
|
// apply cursor options when the caller explicitly requests pagination.
|
||||||
|
query: Joi.alternatives().conditional(Joi.object({ paged: Joi.valid('true').required() }).unknown(), {
|
||||||
|
then: Joi.object({
|
||||||
|
limit: Joi.number().integer().min(1).max(100),
|
||||||
|
before: Joi.number().integer().positive(),
|
||||||
|
ids: Joi.string().pattern(/^\d+(,\d+){0,99}$/),
|
||||||
|
}).unknown(true).oxor('ids', 'limit').oxor('ids', 'before'),
|
||||||
|
otherwise: Joi.object().unknown(true),
|
||||||
|
}),
|
||||||
params: Joi.object({
|
params: Joi.object({
|
||||||
id: Joi.number().required(),
|
id: Joi.number().required(),
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
async (req: Request<{ id: number }>, res: Response, next: NextFunction) => {
|
async (req: Request<{ id: number }>, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
|
// The instance row may retain a provisional end for cleanup after a restart.
|
||||||
|
const pending = `EXISTS (SELECT 1 FROM CronTriggers t WHERE t.instance_id=RunningInstance.id
|
||||||
|
AND (t.exit_pending=1 OR (t.state='unknown' AND t.reason='restart')))`;
|
||||||
|
const paged = req.query.paged === 'true';
|
||||||
|
const ids = paged && req.query.ids ? String(req.query.ids).split(',').map(Number) : undefined;
|
||||||
|
const limit = paged && !ids ? Number(req.query.limit ?? 25) : undefined;
|
||||||
const instances = await RunningInstanceModel.findAll({
|
const instances = await RunningInstanceModel.findAll({
|
||||||
|
attributes: {
|
||||||
|
exclude: ['finished_at', 'exit_code'],
|
||||||
|
include: [
|
||||||
|
[literal(`CASE WHEN ${pending} THEN NULL ELSE finished_at END`), 'finished_at'],
|
||||||
|
[literal(`CASE WHEN ${pending} THEN NULL ELSE exit_code END`), 'exit_code'],
|
||||||
|
],
|
||||||
|
},
|
||||||
where: {
|
where: {
|
||||||
cron_id: req.params.id,
|
cron_id: req.params.id,
|
||||||
|
...(ids && { id: { [Op.in]: ids } }),
|
||||||
|
...(paged && req.query.before && { id: { [Op.lt]: Number(req.query.before) } }),
|
||||||
},
|
},
|
||||||
order: [['started_at', 'DESC']],
|
order: paged ? [['id', 'DESC']] : [['started_at', 'DESC'], ['id', 'DESC']],
|
||||||
|
...(limit && { limit: limit + 1 }),
|
||||||
raw: true,
|
raw: true,
|
||||||
});
|
});
|
||||||
|
if (limit) return res.send({ code: 200, data: instances.slice(0, limit),
|
||||||
|
next: instances.length > limit ? instances[limit - 1].id : null });
|
||||||
return res.send({ code: 200, data: instances });
|
return res.send({ code: 200, data: instances });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return next(e);
|
return next(e);
|
||||||
@@ -522,8 +633,8 @@ export default (app: Router) => {
|
|||||||
async (req: Request<{ id: number; instanceId: number }>, res: Response, next: NextFunction) => {
|
async (req: Request<{ id: number; instanceId: number }>, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
const cronService = Container.get(CronService);
|
const cronService = Container.get(CronService);
|
||||||
const data = await cronService.stopInstance(req.params.instanceId);
|
const data = await cronService.stopInstance(Number(req.params.id), Number(req.params.instanceId));
|
||||||
return res.send(data);
|
return res.status(data.code).send(data);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return next(e);
|
return next(e);
|
||||||
}
|
}
|
||||||
|
|||||||
+43
-51
@@ -1,3 +1,7 @@
|
|||||||
|
import { executionIdPattern, executionTriggerId, receiptFinishTime } from '../shared/executionIdentity';
|
||||||
|
import { oldestQueued } from '../shared/queueDiagnostics';
|
||||||
|
import { containerResources } from '../shared/containerResources';
|
||||||
|
import { TriggerHistory } from '../shared/triggerHistory';
|
||||||
import { Router, Request, Response, NextFunction } from 'express';
|
import { Router, Request, Response, NextFunction } from 'express';
|
||||||
import { Container } from 'typedi';
|
import { Container } from 'typedi';
|
||||||
import { fn, col, where, Op } from 'sequelize';
|
import { fn, col, where, Op } from 'sequelize';
|
||||||
@@ -12,6 +16,11 @@ import os from 'os';
|
|||||||
import { isEmpty } from 'lodash';
|
import { isEmpty } from 'lodash';
|
||||||
import { t, tf } from '../shared/i18n';
|
import { t, tf } from '../shared/i18n';
|
||||||
|
|
||||||
|
function averageElapsed(totalTime: number, runs: number, untimed: number) {
|
||||||
|
const samples = Number(runs) - (Number(untimed) || 0);
|
||||||
|
return samples > 0 ? Math.round(Number(totalTime) / samples) : 0;
|
||||||
|
}
|
||||||
|
|
||||||
const route = Router();
|
const route = Router();
|
||||||
|
|
||||||
export default (app: Router) => {
|
export default (app: Router) => {
|
||||||
@@ -19,41 +28,24 @@ export default (app: Router) => {
|
|||||||
|
|
||||||
route.post('/record', async (req: Request, res: Response) => {
|
route.post('/record', async (req: Request, res: Response) => {
|
||||||
try {
|
try {
|
||||||
const { ref_id, code, elapsed } = req.body;
|
const { ref_id, code, elapsed, trigger_id, execution_id } = req.body;
|
||||||
if (!ref_id) return res.send({ code: 400, message: 'ref_id required' });
|
if (!ref_id) return res.send({ code: 400, message: 'ref_id required' });
|
||||||
|
|
||||||
const today = dayjs().format('YYYY-MM-DD');
|
if (trigger_id != null && (typeof trigger_id !== 'string' || !/^[a-z0-9-]{1,120}$/.test(trigger_id))) {
|
||||||
const isSuccess = code === 0 ? 1 : 0;
|
return res.send({ code: 400 });
|
||||||
const isFail = code !== 0 ? 1 : 0;
|
|
||||||
const elapsedMs = (Number(elapsed) || 0) * 1000;
|
|
||||||
|
|
||||||
const existing = await CrontabStatModel.findOne({
|
|
||||||
where: { ref_id: Number(ref_id), date: today },
|
|
||||||
});
|
|
||||||
|
|
||||||
if (existing) {
|
|
||||||
await CrontabStatModel.update(
|
|
||||||
{
|
|
||||||
run_count: (existing.run_count || 0) + 1,
|
|
||||||
success_count: (existing.success_count || 0) + isSuccess,
|
|
||||||
fail_count: (existing.fail_count || 0) + isFail,
|
|
||||||
total_time: (existing.total_time || 0) + elapsedMs,
|
|
||||||
max_time: Math.max(existing.max_time || 0, elapsedMs),
|
|
||||||
},
|
|
||||||
{ where: { id: existing.id } },
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
await CrontabStatModel.create({
|
|
||||||
ref_id: Number(ref_id),
|
|
||||||
date: today,
|
|
||||||
run_count: 1,
|
|
||||||
success_count: isSuccess,
|
|
||||||
fail_count: isFail,
|
|
||||||
total_time: elapsedMs,
|
|
||||||
max_time: elapsedMs,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
if (execution_id !== undefined && (trigger_id !== undefined ||
|
||||||
|
typeof execution_id !== 'string' || !executionIdPattern.test(execution_id))) return res.send({ code: 400 });
|
||||||
|
const id = execution_id ? executionTriggerId(execution_id) : trigger_id;
|
||||||
|
// Old wrappers also send a status receipt with the process identity.
|
||||||
|
// A stats-only retry cannot be distinguished from another identical run.
|
||||||
|
if (!id) return res.send({ code: 200, data: { counted: false, reason: 'missing_execution_identity' } });
|
||||||
|
const exitCode = typeof code === 'string' && /^-?\d+$/.test(code) ? Number(code) : code;
|
||||||
|
const elapsedMs = Number(elapsed ?? 0) * 1000;
|
||||||
|
if (!Number.isInteger(exitCode) || !Number.isSafeInteger(elapsedMs) || elapsedMs < 0) {
|
||||||
|
return res.send({ code: 400, message: 'invalid exit code or elapsed time' });
|
||||||
|
}
|
||||||
|
await new TriggerHistory(CrontabStatModel.sequelize!).recordShellResult(id, Number(ref_id), exitCode, elapsedMs, Date.now(), receiptFinishTime(req.get('X-QL-Finished-At')));
|
||||||
res.send({ code: 200 });
|
res.send({ code: 200 });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
res.send({ code: 500 });
|
res.send({ code: 500 });
|
||||||
@@ -66,7 +58,7 @@ export default (app: Router) => {
|
|||||||
try {
|
try {
|
||||||
const today = dayjs().format('YYYY-MM-DD');
|
const today = dayjs().format('YYYY-MM-DD');
|
||||||
|
|
||||||
const [total, enabled, disabled, stats] = await Promise.all([
|
const [total, enabled, disabled, stats, triggers] = await Promise.all([
|
||||||
CrontabModel.count(),
|
CrontabModel.count(),
|
||||||
CrontabModel.count({ where: { isDisabled: 0 } }),
|
CrontabModel.count({ where: { isDisabled: 0 } }),
|
||||||
CrontabModel.count({ where: { isDisabled: 1 } }),
|
CrontabModel.count({ where: { isDisabled: 1 } }),
|
||||||
@@ -75,11 +67,13 @@ export default (app: Router) => {
|
|||||||
[fn('SUM', col('run_count')), 'total_runs'],
|
[fn('SUM', col('run_count')), 'total_runs'],
|
||||||
[fn('SUM', col('success_count')), 'total_success'],
|
[fn('SUM', col('success_count')), 'total_success'],
|
||||||
[fn('SUM', col('fail_count')), 'total_fail'],
|
[fn('SUM', col('fail_count')), 'total_fail'],
|
||||||
|
[fn('SUM', col('untimed_count')), 'untimed_count'],
|
||||||
[fn('SUM', col('total_time')), 'total_time'],
|
[fn('SUM', col('total_time')), 'total_time'],
|
||||||
],
|
],
|
||||||
where: { date: today },
|
where: { date: today },
|
||||||
raw: true,
|
raw: true,
|
||||||
}),
|
}),
|
||||||
|
new TriggerHistory(CrontabStatModel.sequelize!).metrics(dayjs().startOf('day').valueOf()),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const row = stats as any;
|
const row = stats as any;
|
||||||
@@ -94,11 +88,13 @@ export default (app: Router) => {
|
|||||||
total,
|
total,
|
||||||
enabled,
|
enabled,
|
||||||
disabled,
|
disabled,
|
||||||
|
triggers,
|
||||||
todayRuns: totalRuns,
|
todayRuns: totalRuns,
|
||||||
todaySuccess: totalSuccess,
|
todaySuccess: totalSuccess,
|
||||||
todayFail: totalFail,
|
todayFail: totalFail,
|
||||||
|
todayStopped: totalRuns - totalSuccess - totalFail,
|
||||||
successRate: totalRuns > 0 ? ((totalSuccess / totalRuns) * 100).toFixed(1) : '0',
|
successRate: totalRuns > 0 ? ((totalSuccess / totalRuns) * 100).toFixed(1) : '0',
|
||||||
avgTime: totalRuns > 0 ? Math.round(totalTime / totalRuns) : 0,
|
avgTime: averageElapsed(totalTime,totalRuns,row?.untimed_count),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -202,6 +198,7 @@ export default (app: Router) => {
|
|||||||
const rows = (await CrontabStatModel.findAll({
|
const rows = (await CrontabStatModel.findAll({
|
||||||
attributes: [
|
attributes: [
|
||||||
'ref_id',
|
'ref_id',
|
||||||
|
[fn('SUM', col('untimed_count')), 'untimed_count'],
|
||||||
[fn('SUM', col('total_time')), 'total_time'],
|
[fn('SUM', col('total_time')), 'total_time'],
|
||||||
[fn('SUM', col('run_count')), 'run_count'],
|
[fn('SUM', col('run_count')), 'run_count'],
|
||||||
[fn('MAX', col('max_time')), 'max_time'],
|
[fn('MAX', col('max_time')), 'max_time'],
|
||||||
@@ -224,7 +221,7 @@ export default (app: Router) => {
|
|||||||
const data = rows.map((r: any, i) => ({
|
const data = rows.map((r: any, i) => ({
|
||||||
rank: i + 1,
|
rank: i + 1,
|
||||||
name: nameMap[Number(r.ref_id)] || tf('任务#%s', r.ref_id),
|
name: nameMap[Number(r.ref_id)] || tf('任务#%s', r.ref_id),
|
||||||
avgTime: Math.round(Number(r.total_time) / Number(r.run_count)),
|
avgTime: averageElapsed(r.total_time,r.run_count,r.untimed_count),
|
||||||
maxTime: Number(r.max_time),
|
maxTime: Number(r.max_time),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -245,6 +242,7 @@ export default (app: Router) => {
|
|||||||
attributes: [
|
attributes: [
|
||||||
'ref_id',
|
'ref_id',
|
||||||
[fn('SUM', col('run_count')), 'run_count'],
|
[fn('SUM', col('run_count')), 'run_count'],
|
||||||
|
[fn('SUM', col('untimed_count')), 'untimed_count'],
|
||||||
[fn('SUM', col('total_time')), 'total_time'],
|
[fn('SUM', col('total_time')), 'total_time'],
|
||||||
[fn('SUM', col('success_count')), 'success_count'],
|
[fn('SUM', col('success_count')), 'success_count'],
|
||||||
],
|
],
|
||||||
@@ -267,7 +265,7 @@ export default (app: Router) => {
|
|||||||
rank: i + 1,
|
rank: i + 1,
|
||||||
name: nameMap[Number(r.ref_id)] || tf('任务#%s', r.ref_id),
|
name: nameMap[Number(r.ref_id)] || tf('任务#%s', r.ref_id),
|
||||||
runCount: Number(r.run_count),
|
runCount: Number(r.run_count),
|
||||||
avgTime: Math.round(Number(r.total_time) / Number(r.run_count)),
|
avgTime: averageElapsed(r.total_time,r.run_count,r.untimed_count),
|
||||||
successRate:
|
successRate:
|
||||||
Number(r.run_count) > 0
|
Number(r.run_count) > 0
|
||||||
? ((Number(r.success_count) / Number(r.run_count)) * 100).toFixed(1)
|
? ((Number(r.success_count) / Number(r.run_count)) * 100).toFixed(1)
|
||||||
@@ -285,6 +283,7 @@ export default (app: Router) => {
|
|||||||
'/runtime',
|
'/runtime',
|
||||||
async (req: Request, res: Response, next: NextFunction) => {
|
async (req: Request, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
|
const queued = await new TriggerHistory(CrontabModel.sequelize!).queueCounts();
|
||||||
const runningInstances = await RunningInstanceModel.findAll({
|
const runningInstances = await RunningInstanceModel.findAll({
|
||||||
where: {
|
where: {
|
||||||
status: InstanceStatus.running,
|
status: InstanceStatus.running,
|
||||||
@@ -292,13 +291,6 @@ export default (app: Router) => {
|
|||||||
raw: true,
|
raw: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
const queuedCrons = await CrontabModel.findAll({
|
|
||||||
where: {
|
|
||||||
status: 3, // queued
|
|
||||||
},
|
|
||||||
raw: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Fetch cron names for running instances
|
// Fetch cron names for running instances
|
||||||
const cronIds = [
|
const cronIds = [
|
||||||
...new Set(runningInstances.map((i: any) => i.cron_id)),
|
...new Set(runningInstances.map((i: any) => i.cron_id)),
|
||||||
@@ -341,7 +333,9 @@ export default (app: Router) => {
|
|||||||
code: 200,
|
code: 200,
|
||||||
data: {
|
data: {
|
||||||
runningCount: running.length,
|
runningCount: running.length,
|
||||||
queuedCount: queuedCrons.length,
|
queuedCount: queued.triggers,
|
||||||
|
queuedTasks: queued.tasks,
|
||||||
|
oldestQueued: queued.triggers ? await oldestQueued(CrontabModel.sequelize!) : [],
|
||||||
running,
|
running,
|
||||||
idleTasks: idleTasks.map((c: any) => ({
|
idleTasks: idleTasks.map((c: any) => ({
|
||||||
id: c.id,
|
id: c.id,
|
||||||
@@ -371,7 +365,7 @@ export default (app: Router) => {
|
|||||||
const statMap: Record<number, any> = {};
|
const statMap: Record<number, any> = {};
|
||||||
stats.forEach((s: any) => { statMap[s.ref_id] = s; });
|
stats.forEach((s: any) => { statMap[s.ref_id] = s; });
|
||||||
|
|
||||||
const labelMap: Record<string, { count: number; runs: number; success: number; totalTime: number }> = {};
|
const labelMap: Record<string, { count: number; runs: number; success: number; untimed: number; totalTime: number }> = {};
|
||||||
crons.forEach((c) => {
|
crons.forEach((c) => {
|
||||||
let rawLabels = c.labels;
|
let rawLabels = c.labels;
|
||||||
if (typeof rawLabels === 'string') rawLabels = JSON.parse(rawLabels);
|
if (typeof rawLabels === 'string') rawLabels = JSON.parse(rawLabels);
|
||||||
@@ -383,11 +377,12 @@ export default (app: Router) => {
|
|||||||
}
|
}
|
||||||
const st = statMap[c.id!];
|
const st = statMap[c.id!];
|
||||||
labels.forEach((label: string) => {
|
labels.forEach((label: string) => {
|
||||||
if (!labelMap[label]) labelMap[label] = { count: 0, runs: 0, success: 0, totalTime: 0 };
|
if (!labelMap[label]) labelMap[label] = { count: 0, runs: 0, success: 0, untimed: 0, totalTime: 0 };
|
||||||
labelMap[label].count += 1;
|
labelMap[label].count += 1;
|
||||||
if (st) {
|
if (st) {
|
||||||
labelMap[label].runs += Number(st.run_count) || 0;
|
labelMap[label].runs += Number(st.run_count) || 0;
|
||||||
labelMap[label].success += Number(st.success_count) || 0;
|
labelMap[label].success += Number(st.success_count) || 0;
|
||||||
|
labelMap[label].untimed += Number(st.untimed_count) || 0;
|
||||||
labelMap[label].totalTime += Number(st.total_time) || 0;
|
labelMap[label].totalTime += Number(st.total_time) || 0;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -399,7 +394,7 @@ export default (app: Router) => {
|
|||||||
count: v.count,
|
count: v.count,
|
||||||
todayRuns: v.runs,
|
todayRuns: v.runs,
|
||||||
successRate: v.runs > 0 ? ((v.success / v.runs) * 100).toFixed(1) : '0',
|
successRate: v.runs > 0 ? ((v.success / v.runs) * 100).toFixed(1) : '0',
|
||||||
avgTime: v.runs > 0 ? Math.round(v.totalTime / v.runs) : 0,
|
avgTime: averageElapsed(v.totalTime,v.runs,v.untimed),
|
||||||
}))
|
}))
|
||||||
.sort((a, b) => b.todayRuns - a.todayRuns);
|
.sort((a, b) => b.todayRuns - a.todayRuns);
|
||||||
|
|
||||||
@@ -420,13 +415,10 @@ export default (app: Router) => {
|
|||||||
data: {
|
data: {
|
||||||
platform: os.platform(),
|
platform: os.platform(),
|
||||||
uptime: Math.floor(process.uptime()),
|
uptime: Math.floor(process.uptime()),
|
||||||
memTotal: os.totalmem(),
|
...containerResources(),
|
||||||
memFree: os.freemem(),
|
|
||||||
memUsagePercent: ((1 - os.freemem() / os.totalmem()) * 100).toFixed(1),
|
|
||||||
heapUsed: Math.round(memUsage.heapUsed / 1024 / 1024),
|
heapUsed: Math.round(memUsage.heapUsed / 1024 / 1024),
|
||||||
heapTotal: Math.round(memUsage.heapTotal / 1024 / 1024),
|
heapTotal: Math.round(memUsage.heapTotal / 1024 / 1024),
|
||||||
loadAvg: os.loadavg().map((v) => Number(v.toFixed(2))),
|
loadAvg: os.loadavg().map((v) => Number(v.toFixed(2))),
|
||||||
cpus: os.cpus().length,
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
+64
-2
@@ -11,8 +11,7 @@ export default (app: Router) => {
|
|||||||
route.get(
|
route.get(
|
||||||
'/',
|
'/',
|
||||||
celebrate({
|
celebrate({
|
||||||
query:
|
query: Joi.object({
|
||||||
Joi.object({
|
|
||||||
searchValue: Joi.string().optional().allow(''),
|
searchValue: Joi.string().optional().allow(''),
|
||||||
type: Joi.string().optional().allow(''),
|
type: Joi.string().optional().allow(''),
|
||||||
status: Joi.string().optional().allow(''),
|
status: Joi.string().optional().allow(''),
|
||||||
@@ -31,6 +30,69 @@ export default (app: Router) => {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
route.post(
|
||||||
|
'/refresh',
|
||||||
|
celebrate({
|
||||||
|
body: Joi.object({
|
||||||
|
type: Joi.string().valid('nodejs', 'python3').required(),
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
async (req: Request, res: Response, next: NextFunction) => {
|
||||||
|
try {
|
||||||
|
const dependenceService = Container.get(DependenceService);
|
||||||
|
const { job, accepted } = dependenceService.startRefresh(req.body.type);
|
||||||
|
if (!accepted) {
|
||||||
|
return res.status(409).send({
|
||||||
|
code: 409,
|
||||||
|
message: 'Another dependency refresh is already running',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return res.send({ code: 200, data: job });
|
||||||
|
} catch (e) {
|
||||||
|
return next(e);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
route.get(
|
||||||
|
'/refresh/active',
|
||||||
|
async (_req: Request, res: Response, next: NextFunction) => {
|
||||||
|
try {
|
||||||
|
const dependenceService = Container.get(DependenceService);
|
||||||
|
res.set('Cache-Control', 'no-store');
|
||||||
|
return res.send({
|
||||||
|
code: 200,
|
||||||
|
data: dependenceService.getActiveRefreshJob() || null,
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
return next(e);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
route.get(
|
||||||
|
'/refresh/:id',
|
||||||
|
celebrate({
|
||||||
|
params: Joi.object({ id: Joi.string().uuid().required() }),
|
||||||
|
}),
|
||||||
|
async (req: Request, res: Response, next: NextFunction) => {
|
||||||
|
try {
|
||||||
|
const dependenceService = Container.get(DependenceService);
|
||||||
|
const job = dependenceService.getRefreshJob(req.params.id);
|
||||||
|
if (!job) {
|
||||||
|
return res.status(404).send({
|
||||||
|
code: 404,
|
||||||
|
message: 'Dependency refresh job not found',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
res.set('Cache-Control', 'no-store');
|
||||||
|
return res.send({ code: 200, data: job });
|
||||||
|
} catch (e) {
|
||||||
|
return next(e);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
route.post(
|
route.post(
|
||||||
'/',
|
'/',
|
||||||
celebrate({
|
celebrate({
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ export default (app: Router) => {
|
|||||||
res.status(500).send({
|
res.status(500).send({
|
||||||
code: 500,
|
code: 500,
|
||||||
message: 'Health check failed',
|
message: 'Health check failed',
|
||||||
error: err.message,
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
+10
-1
@@ -1,3 +1,4 @@
|
|||||||
|
import { directoryCheckHandler, directoryPageHandler } from '../shared/directoryPageRoute';
|
||||||
import { celebrate, Joi } from 'celebrate';
|
import { celebrate, Joi } from 'celebrate';
|
||||||
import { NextFunction, Request, Response, Router } from 'express';
|
import { NextFunction, Request, Response, Router } from 'express';
|
||||||
import { Container } from 'typedi';
|
import { Container } from 'typedi';
|
||||||
@@ -17,6 +18,8 @@ const blacklist = ['.tmp'];
|
|||||||
|
|
||||||
export default (app: Router) => {
|
export default (app: Router) => {
|
||||||
app.use('/logs', route);
|
app.use('/logs', route);
|
||||||
|
route.get('/entries', ...directoryPageHandler(() => config.logPath, blacklist, true));
|
||||||
|
route.post('/entries/check', ...directoryCheckHandler(() => config.logPath, blacklist));
|
||||||
|
|
||||||
route.get('/', async (req: Request, res: Response, next: NextFunction) => {
|
route.get('/', async (req: Request, res: Response, next: NextFunction) => {
|
||||||
const logger: Logger = Container.get('logger');
|
const logger: Logger = Container.get('logger');
|
||||||
@@ -38,6 +41,8 @@ export default (app: Router) => {
|
|||||||
query: Joi.object({
|
query: Joi.object({
|
||||||
path: Joi.string().allow('').optional(),
|
path: Joi.string().allow('').optional(),
|
||||||
file: Joi.string().required(),
|
file: Joi.string().required(),
|
||||||
|
follow: Joi.boolean().optional(),
|
||||||
|
cursor: Joi.string().pattern(/^\d{1,16}\.[a-f0-9]{64}$/).optional(),
|
||||||
offset: Joi.number().integer().min(0).optional(),
|
offset: Joi.number().integer().min(0).optional(),
|
||||||
limit: Joi.number()
|
limit: Joi.number()
|
||||||
.integer()
|
.integer()
|
||||||
@@ -67,14 +72,18 @@ export default (app: Router) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const chunk = await readLogChunk(finalPath, {
|
const chunk = await readLogChunk(finalPath, {
|
||||||
|
follow: req.query.follow as unknown as boolean,
|
||||||
|
cursor: req.query.cursor as string | undefined,
|
||||||
offset: req.query.offset as unknown as number,
|
offset: req.query.offset as unknown as number,
|
||||||
limit: req.query.limit as unknown as number,
|
limit: req.query.limit as unknown as number,
|
||||||
tail: req.query.tail as unknown as boolean,
|
tail: req.query.tail as unknown as boolean,
|
||||||
});
|
});
|
||||||
res.send({
|
res.send({
|
||||||
code: 200,
|
code: 200,
|
||||||
data: removeAnsi(chunk.content),
|
data: req.query.follow ? chunk.content : removeAnsi(chunk.content),
|
||||||
logStatus: runningInstance ? 'running' : undefined,
|
logStatus: runningInstance ? 'running' : undefined,
|
||||||
|
cursor: chunk.cursor,
|
||||||
|
reset: chunk.reset,
|
||||||
offset: chunk.offset,
|
offset: chunk.offset,
|
||||||
nextOffset: chunk.nextOffset,
|
nextOffset: chunk.nextOffset,
|
||||||
total: chunk.total,
|
total: chunk.total,
|
||||||
|
|||||||
+2
-2
@@ -27,7 +27,7 @@ export default (app: Router) => {
|
|||||||
celebrate({
|
celebrate({
|
||||||
body: Joi.object({
|
body: Joi.object({
|
||||||
name: Joi.string().optional().allow('').disallow('system'),
|
name: Joi.string().optional().allow('').disallow('system'),
|
||||||
scopes: Joi.array().items(Joi.string().required()),
|
scopes: Joi.array().items(Joi.string().required().invalid('apps')),
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
async (req: Request, res: Response, next: NextFunction) => {
|
async (req: Request, res: Response, next: NextFunction) => {
|
||||||
@@ -47,7 +47,7 @@ export default (app: Router) => {
|
|||||||
celebrate({
|
celebrate({
|
||||||
body: Joi.object({
|
body: Joi.object({
|
||||||
name: Joi.string().optional().allow(''),
|
name: Joi.string().optional().allow(''),
|
||||||
scopes: Joi.array().items(Joi.string()),
|
scopes: Joi.array().items(Joi.string().invalid('apps')),
|
||||||
id: Joi.number().required(),
|
id: Joi.number().required(),
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
|
|||||||
+12
-8
@@ -1,3 +1,4 @@
|
|||||||
|
import { directoryCheckHandler, directoryPageHandler } from '../shared/directoryPageRoute';
|
||||||
import { randomUUID } from 'crypto';
|
import { randomUUID } from 'crypto';
|
||||||
import { resolveFileAccess } from '../shared/fileAccess';
|
import { resolveFileAccess } from '../shared/fileAccess';
|
||||||
import { fileExist, readDirs, readDir, rmPath, IFile } from '../config/util';
|
import { fileExist, readDirs, readDir, rmPath, IFile } from '../config/util';
|
||||||
@@ -18,6 +19,14 @@ import {
|
|||||||
HistoryUnavailableError,
|
HistoryUnavailableError,
|
||||||
} from '../shared/scriptHistory';
|
} from '../shared/scriptHistory';
|
||||||
const route = Router();
|
const route = Router();
|
||||||
|
const blacklist = [
|
||||||
|
'node_modules',
|
||||||
|
'.git',
|
||||||
|
'.pnpm',
|
||||||
|
'pnpm-lock.yaml',
|
||||||
|
'yarn.lock',
|
||||||
|
'package-lock.json',
|
||||||
|
];
|
||||||
|
|
||||||
function isPathAllowed(targetPath: string): boolean {
|
function isPathAllowed(targetPath: string): boolean {
|
||||||
const resolved = path.resolve(targetPath);
|
const resolved = path.resolve(targetPath);
|
||||||
@@ -75,6 +84,8 @@ const upload = multer({ storage: storage });
|
|||||||
|
|
||||||
export default (app: Router) => {
|
export default (app: Router) => {
|
||||||
app.use('/scripts', route);
|
app.use('/scripts', route);
|
||||||
|
route.get('/entries', ...directoryPageHandler(() => config.scriptPath, blacklist, false));
|
||||||
|
route.post('/entries/check', ...directoryCheckHandler(() => config.scriptPath, blacklist));
|
||||||
|
|
||||||
route.get(
|
route.get(
|
||||||
'/',
|
'/',
|
||||||
@@ -87,14 +98,7 @@ export default (app: Router) => {
|
|||||||
const logger: Logger = Container.get('logger');
|
const logger: Logger = Container.get('logger');
|
||||||
try {
|
try {
|
||||||
let result: IFile[] = [];
|
let result: IFile[] = [];
|
||||||
const blacklist = [
|
|
||||||
'node_modules',
|
|
||||||
'.git',
|
|
||||||
'.pnpm',
|
|
||||||
'pnpm-lock.yaml',
|
|
||||||
'yarn.lock',
|
|
||||||
'package-lock.json',
|
|
||||||
];
|
|
||||||
if (req.query.path) {
|
if (req.query.path) {
|
||||||
if (
|
if (
|
||||||
!resolveFileAccess(config.scriptPath, [req.query.path as string])
|
!resolveFileAccess(config.scriptPath, [req.query.path as string])
|
||||||
|
|||||||
+3
-1
@@ -4,6 +4,7 @@ import { Logger } from 'winston';
|
|||||||
import * as fs from 'fs/promises';
|
import * as fs from 'fs/promises';
|
||||||
import config from '../config';
|
import config from '../config';
|
||||||
import SystemService from '../services/system';
|
import SystemService from '../services/system';
|
||||||
|
import { containerResources } from '../shared/containerResources';
|
||||||
import { celebrate, Joi } from 'celebrate';
|
import { celebrate, Joi } from 'celebrate';
|
||||||
import UserService from '../services/user';
|
import UserService from '../services/user';
|
||||||
import { t } from '../shared/i18n';
|
import { t } from '../shared/i18n';
|
||||||
@@ -62,7 +63,8 @@ export default (app: Router) => {
|
|||||||
try {
|
try {
|
||||||
const systemService = Container.get(SystemService);
|
const systemService = Container.get(SystemService);
|
||||||
const data = await systemService.getSystemConfig();
|
const data = await systemService.getSystemConfig();
|
||||||
res.send({ code: 200, data });
|
res.send({ code: 200, data: { ...data, effectiveCronConcurrency: systemService.cronConcurrency,
|
||||||
|
availableCpus: containerResources().cpus } });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return next(e);
|
return next(e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -310,20 +310,6 @@ export default (app: Router) => {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
route.put(
|
|
||||||
'/notification/init',
|
|
||||||
async (req: Request, res: Response, next: NextFunction) => {
|
|
||||||
const logger: Logger = Container.get('logger');
|
|
||||||
try {
|
|
||||||
const userService = Container.get(UserService);
|
|
||||||
const result = await userService.updateNotificationMode(req.body);
|
|
||||||
res.send(result);
|
|
||||||
} catch (e) {
|
|
||||||
return next(e);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
route.put(
|
route.put(
|
||||||
'/avatar',
|
'/avatar',
|
||||||
upload.single('avatar'),
|
upload.single('avatar'),
|
||||||
|
|||||||
+31
@@ -138,7 +138,19 @@ class Application {
|
|||||||
workerEnv.pmx = 'false';
|
workerEnv.pmx = 'false';
|
||||||
}
|
}
|
||||||
const worker = cluster.fork(workerEnv);
|
const worker = cluster.fork(workerEnv);
|
||||||
|
if (serviceType === 'grpc') worker.on('message', (msg) => {
|
||||||
|
if (msg === 'crontab-install-failed' && this.httpWorker?.isConnected()) {
|
||||||
|
this.httpWorker.send('reregister-crons');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (serviceType === 'http') worker.on('message', (msg) => {
|
||||||
|
if (msg?.type !== 'cancel-queued-triggers') return;
|
||||||
|
for (const [id, meta] of this.workerMetadataMap) {
|
||||||
|
const target = cluster.workers?.[id];
|
||||||
|
if (meta.serviceType === 'grpc' && target?.isConnected()) target.send(msg);
|
||||||
|
}
|
||||||
|
});
|
||||||
this.workerMetadataMap.set(worker.id, {
|
this.workerMetadataMap.set(worker.id, {
|
||||||
id: worker.id,
|
id: worker.id,
|
||||||
pid: worker.process.pid!,
|
pid: worker.process.pid!,
|
||||||
@@ -217,8 +229,21 @@ class Application {
|
|||||||
Logger.info(`[boot] ${serviceType} worker started (PID: ${process.pid})`);
|
Logger.info(`[boot] ${serviceType} worker started (PID: ${process.pid})`);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
const { triggerHistory } = await import('./shared/triggerHistory');
|
||||||
|
const history = triggerHistory();
|
||||||
|
await history.interrupt(serviceType);
|
||||||
if (serviceType === 'http') {
|
if (serviceType === 'http') {
|
||||||
await this.startHttpService();
|
await this.startHttpService();
|
||||||
|
const retryExits = async () => {
|
||||||
|
try {
|
||||||
|
await history.retryPendingExits();
|
||||||
|
} catch (error) {
|
||||||
|
Logger.error(`[scheduler] Exit receipt retry failed: ${errStack(error)}`);
|
||||||
|
} finally {
|
||||||
|
if (!this.isShuttingDown) setTimeout(retryExits, 15000).unref();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
setTimeout(retryExits, 15000).unref();
|
||||||
} else {
|
} else {
|
||||||
await this.startGrpcService();
|
await this.startGrpcService();
|
||||||
}
|
}
|
||||||
@@ -265,6 +290,10 @@ class Application {
|
|||||||
process.on('message', async (msg) => {
|
process.on('message', async (msg) => {
|
||||||
if (msg === 'shutdown') {
|
if (msg === 'shutdown') {
|
||||||
this.gracefulShutdown(serviceType);
|
this.gracefulShutdown(serviceType);
|
||||||
|
} else if (serviceType === 'grpc' && typeof msg === 'object' && msg &&
|
||||||
|
'type' in msg && msg.type === 'cancel-queued-triggers' && 'ids' in msg && Array.isArray(msg.ids)) {
|
||||||
|
const { cancelWaitingTriggers } = await import('./shared/runCron');
|
||||||
|
cancelWaitingTriggers(msg.ids);
|
||||||
} else if (serviceType === 'http' &&
|
} else if (serviceType === 'http' &&
|
||||||
(msg === 'reregister-crons' || msg === 'scheduler-unavailable')) {
|
(msg === 'reregister-crons' || msg === 'scheduler-unavailable')) {
|
||||||
const { default: cronClient } = await import('./schedule/client');
|
const { default: cronClient } = await import('./schedule/client');
|
||||||
@@ -282,6 +311,8 @@ class Application {
|
|||||||
this.isShuttingDown = true;
|
this.isShuttingDown = true;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
const { triggerHistory } = await import('./shared/triggerHistory');
|
||||||
|
await triggerHistory().interrupt(serviceType);
|
||||||
if (serviceType === 'http') {
|
if (serviceType === 'http') {
|
||||||
await this.httpServerService?.shutdown();
|
await this.httpServerService?.shutdown();
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -0,0 +1,61 @@
|
|||||||
|
import { storageFull, writeExit } from '../shared/managedExit.cjs';
|
||||||
|
import { QueryTypes } from 'sequelize';
|
||||||
|
import { randomUUID } from 'crypto';
|
||||||
|
import { sequelize } from '../data';
|
||||||
|
import { systemOwner, TriggerHistory } from '../shared/triggerHistory';
|
||||||
|
|
||||||
|
async function receipt() {
|
||||||
|
const [action, task, pid, id, status, end] = process.argv.slice(2);
|
||||||
|
const history = new TriggerHistory(sequelize);
|
||||||
|
if (action === 'start') {
|
||||||
|
await history.recoverSystemOwners(Number(task));
|
||||||
|
const triggerId = id || `system-${task}-${randomUUID()}`;
|
||||||
|
const owner = systemOwner(Number(pid));
|
||||||
|
const command = status ? systemOwner(Number(status)) : owner;
|
||||||
|
const at = Number(process.env.QL_SYSTEM_ENTERED);
|
||||||
|
const rule = process.env.QL_SYSTEM_RULE;
|
||||||
|
const dispatched = Number(process.env.QL_SYSTEM_DISPATCHED) * 1000;
|
||||||
|
const dispatch = Number.isSafeInteger(dispatched) && dispatched > 0 && dispatched <= at ? dispatched : undefined;
|
||||||
|
const wrapper = rule && Number.isSafeInteger(at) && at > 0 && at <= Date.now() ? { rule, at, dispatch } : undefined;
|
||||||
|
const acquired = await history.startSystem(triggerId, Number(task), owner, command, wrapper);
|
||||||
|
if (!acquired) process.exitCode = 75;
|
||||||
|
else if (!id) process.stdout.write(triggerId);
|
||||||
|
} else if (action === 'finish') {
|
||||||
|
// The live wrapper inherited both IDs from the start it just claimed. A
|
||||||
|
// standalone retry must match the registered system task before writing.
|
||||||
|
const fromWrapper = process.env.QL_TRIGGER_ID === id && process.env.ID === task;
|
||||||
|
if (!fromWrapper || !id?.startsWith(`system-${Number(task)}-`)) {
|
||||||
|
const [registered] = await sequelize.query(
|
||||||
|
`SELECT 1 FROM CronTriggers WHERE id=$id AND cron_id=$task AND source='system' AND owner LIKE 'crond:%'
|
||||||
|
UNION ALL SELECT 1 FROM CronLegacyExitBookings WHERE trigger_id=$id AND cron_id=$task AND source='system' LIMIT 1`,
|
||||||
|
{ bind: { id, task: Number(task) }, type: QueryTypes.SELECT });
|
||||||
|
if (!registered) return;
|
||||||
|
}
|
||||||
|
const code = Number(status);
|
||||||
|
const finishedAt = end && /^\d{13}$/.test(end) && Number(end) <= Date.now() ? Number(end) : undefined;
|
||||||
|
const bound = fromWrapper && finishedAt !== undefined;
|
||||||
|
let journaled = false;
|
||||||
|
if (bound) {
|
||||||
|
try { writeExit(id, code, null, finishedAt); journaled = true; }
|
||||||
|
catch (error) {
|
||||||
|
if (!storageFull(error)) throw error;
|
||||||
|
console.error('[crond] Receipt storage full; settling the observed exit in SQLite:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
await history.complete(id, code === 0 ? 'finished' : 'failed', null, code, finishedAt,
|
||||||
|
bound ? { version: 1, code, signal: null, time: finishedAt } : finishedAt === undefined ? undefined : 'legacy');
|
||||||
|
} catch (error) {
|
||||||
|
if (!journaled || !storageFull(error)) throw error;
|
||||||
|
// The regular recovery pass will settle the receipt after space returns.
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new Error('Unknown crond receipt action');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
receipt().catch((error) => {
|
||||||
|
console.error('[crond] Failed to persist execution receipt:', error);
|
||||||
|
// At start, 75 means that another execution already owns the slot.
|
||||||
|
process.exitCode = process.argv[2] === 'finish' && storageFull(error) ? 75 : 1;
|
||||||
|
}).finally(() => sequelize.close());
|
||||||
@@ -180,12 +180,10 @@ export default {
|
|||||||
'/api/user/two-factor/login',
|
'/api/user/two-factor/login',
|
||||||
'/api/system',
|
'/api/system',
|
||||||
'/api/user/init',
|
'/api/user/init',
|
||||||
'/api/user/notification/init',
|
|
||||||
'/open/user/login',
|
'/open/user/login',
|
||||||
'/open/user/two-factor/login',
|
'/open/user/two-factor/login',
|
||||||
'/open/system',
|
'/open/system',
|
||||||
'/open/user/init',
|
'/open/user/init',
|
||||||
'/open/user/notification/init',
|
|
||||||
],
|
],
|
||||||
versionFile,
|
versionFile,
|
||||||
lastVersionFile,
|
lastVersionFile,
|
||||||
|
|||||||
+60
-16
@@ -1,3 +1,5 @@
|
|||||||
|
import { cronOperand } from '../shared/cronOperand';
|
||||||
|
import { finishStoppedTree, isProcessDescendant, systemOwner } from '../shared/cronProcess';
|
||||||
import * as fs from 'fs/promises';
|
import * as fs from 'fs/promises';
|
||||||
import { statSync } from 'fs';
|
import { statSync } from 'fs';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
@@ -6,7 +8,7 @@ import psTreeFun from 'ps-tree';
|
|||||||
import { promisify } from 'util';
|
import { promisify } from 'util';
|
||||||
import { load } from 'js-yaml';
|
import { load } from 'js-yaml';
|
||||||
import config from './index';
|
import config from './index';
|
||||||
import { PYTHON_INSTALL_DIR, PYTHON_VENV_DIR, TASK_COMMAND } from './const';
|
import { PYTHON_INSTALL_DIR, PYTHON_VENV_DIR } from './const';
|
||||||
import Logger from '../loaders/logger';
|
import Logger from '../loaders/logger';
|
||||||
import { writeFileWithLock } from '../shared/utils';
|
import { writeFileWithLock } from '../shared/utils';
|
||||||
import { DependenceTypes } from '../data/dependence';
|
import { DependenceTypes } from '../data/dependence';
|
||||||
@@ -496,13 +498,17 @@ export function psTree(pid: number): Promise<number[]> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function killTask(pid: number, waitForExit = false) {
|
export async function killTask(pid: number, waitForExit = false, verifyOwner?: () => void,
|
||||||
|
withStopRequest?: (signalTree: () => Error | void, targets: string[]) => Promise<void | { targets: string[]; signalledAt: number | null }>) {
|
||||||
const descendants = await psTree(pid);
|
const descendants = await psTree(pid);
|
||||||
|
verifyOwner?.();
|
||||||
const signal = (target: number, sig: NodeJS.Signals) => {
|
const signal = (target: number, sig: NodeJS.Signals) => {
|
||||||
try {
|
try {
|
||||||
process.kill(target, sig);
|
process.kill(target, sig);
|
||||||
|
return true;
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
if (error.code !== 'ESRCH') throw error;
|
if (error.code !== 'ESRCH') throw error;
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if (!waitForExit) {
|
if (!waitForExit) {
|
||||||
@@ -515,8 +521,53 @@ export async function killTask(pid: number, waitForExit = false) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const pids = [...descendants.reverse(), pid];
|
const pids = [...descendants.reverse(), pid];
|
||||||
for (const target of pids) signal(target, 'SIGTERM');
|
const births = new Map<number, string>();
|
||||||
|
if (verifyOwner) {
|
||||||
|
for (const target of pids) {
|
||||||
|
try {
|
||||||
|
const birth = systemOwner(target);
|
||||||
|
if (target === pid || isProcessDescendant(target, pid)) births.set(target, birth);
|
||||||
|
} catch (error) {
|
||||||
|
if (!['ENOENT', 'ESRCH'].includes((error as NodeJS.ErrnoException).code!)) throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
verifyOwner();
|
||||||
|
}
|
||||||
|
const sameProcess = (target: number) => {
|
||||||
|
if (!verifyOwner) return true;
|
||||||
|
if (!births.has(target)) return false;
|
||||||
|
try { return births.get(target) === systemOwner(target); }
|
||||||
|
catch (error) {
|
||||||
|
if (['ENOENT', 'ESRCH'].includes((error as NodeJS.ErrnoException).code!)) return false;
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const signalTree = () => {
|
||||||
|
verifyOwner?.();
|
||||||
|
// The root owns the command's result. An ESRCH here must roll back the
|
||||||
|
// stop claim even if a descendant is still draining the output pipe.
|
||||||
|
if (verifyOwner && (!sameProcess(pid) || !signal(pid, 'SIGTERM'))) {
|
||||||
|
throw Object.assign(new Error('Command already exited'), { code: 'ESRCH' });
|
||||||
|
}
|
||||||
|
let deliveryError: Error | undefined;
|
||||||
|
for (const target of pids) {
|
||||||
|
if (verifyOwner && target === pid) continue;
|
||||||
|
try {
|
||||||
|
if (sameProcess(target)) signal(target, 'SIGTERM');
|
||||||
|
} catch (error) {
|
||||||
|
if (!withStopRequest || !verifyOwner) throw error;
|
||||||
|
// Root TERM succeeded. Commit that fact even if a child needs a retry.
|
||||||
|
deliveryError ??= error as Error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return deliveryError;
|
||||||
|
};
|
||||||
|
if (withStopRequest) {
|
||||||
|
const claim = await withStopRequest(signalTree, [...births.values()]);
|
||||||
|
if (claim) return finishStoppedTree(claim.targets, claim.signalledAt);
|
||||||
|
} else signalTree();
|
||||||
const alive = async (target: number) => {
|
const alive = async (target: number) => {
|
||||||
|
if (!sameProcess(target)) return false;
|
||||||
try {
|
try {
|
||||||
process.kill(target, 0);
|
process.kill(target, 0);
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
@@ -555,7 +606,9 @@ export async function killTask(pid: number, waitForExit = false) {
|
|||||||
};
|
};
|
||||||
let remaining = await wait();
|
let remaining = await wait();
|
||||||
if (!remaining.length) return;
|
if (!remaining.length) return;
|
||||||
for (const target of remaining) signal(target, 'SIGKILL');
|
for (const target of remaining) {
|
||||||
|
if (sameProcess(target)) signal(target, 'SIGKILL');
|
||||||
|
}
|
||||||
remaining = await wait();
|
remaining = await wait();
|
||||||
if (remaining.length)
|
if (remaining.length)
|
||||||
throw new Error(`Task processes did not exit: ${remaining.join(', ')}`);
|
throw new Error(`Task processes did not exit: ${remaining.join(', ')}`);
|
||||||
@@ -612,17 +665,7 @@ export async function getUniqPath(
|
|||||||
suffix = `_${id}`;
|
suffix = `_${id}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
let items = command.split(/ +/);
|
let str = cronOperand(command) || 'task';
|
||||||
|
|
||||||
const maxTimeCommandIndex = items.findIndex((x) => x === '-m');
|
|
||||||
if (maxTimeCommandIndex !== -1) {
|
|
||||||
items = items.slice(maxTimeCommandIndex + 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
let str = items[0];
|
|
||||||
if (items[0] === TASK_COMMAND) {
|
|
||||||
str = items[1];
|
|
||||||
}
|
|
||||||
|
|
||||||
const dotIndex = str.lastIndexOf('.');
|
const dotIndex = str.lastIndexOf('.');
|
||||||
|
|
||||||
@@ -642,7 +685,8 @@ export async function getUniqPath(
|
|||||||
str = `${tempStr}_${str.slice(slashIndex + 1)}`;
|
str = `${tempStr}_${str.slice(slashIndex + 1)}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return `${str}${suffix}`;
|
const base = str.replace(/[^a-zA-Z0-9_\u4e00-\u9fa5.-]+/g, '_').slice(0, 100 - suffix.length);
|
||||||
|
return `${base && !/^\.+$/.test(base) ? base : 'task'}${suffix}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function safeJSONParse(value?: string) {
|
export function safeJSONParse(value?: string) {
|
||||||
|
|||||||
+15
-1
@@ -16,7 +16,8 @@ export class Crontab {
|
|||||||
queued_token?: string | null;
|
queued_token?: string | null;
|
||||||
isPinned?: 1 | 0;
|
isPinned?: 1 | 0;
|
||||||
labels?: string[];
|
labels?: string[];
|
||||||
last_running_time?: number;
|
last_instance_id?: number;
|
||||||
|
last_running_time?: number | null;
|
||||||
last_execution_time?: number;
|
last_execution_time?: number;
|
||||||
sub_id?: number;
|
sub_id?: number;
|
||||||
extra_schedules?: Array<{ schedule: string }>;
|
extra_schedules?: Array<{ schedule: string }>;
|
||||||
@@ -24,6 +25,10 @@ export class Crontab {
|
|||||||
task_after?: string;
|
task_after?: string;
|
||||||
log_name?: string;
|
log_name?: string;
|
||||||
allow_multiple_instances?: 1 | 0;
|
allow_multiple_instances?: 1 | 0;
|
||||||
|
skip_if_pending?: boolean;
|
||||||
|
max_queue_wait?: number;
|
||||||
|
catchup_seconds?: number;
|
||||||
|
recovery_from?: number;
|
||||||
work_dir?: string;
|
work_dir?: string;
|
||||||
|
|
||||||
constructor(options: Crontab) {
|
constructor(options: Crontab) {
|
||||||
@@ -51,6 +56,10 @@ export class Crontab {
|
|||||||
this.task_after = options.task_after;
|
this.task_after = options.task_after;
|
||||||
this.log_name = options.log_name;
|
this.log_name = options.log_name;
|
||||||
this.allow_multiple_instances = options.allow_multiple_instances || 0;
|
this.allow_multiple_instances = options.allow_multiple_instances || 0;
|
||||||
|
this.skip_if_pending = Boolean(options.skip_if_pending);
|
||||||
|
this.max_queue_wait = options.max_queue_wait || 0;
|
||||||
|
this.catchup_seconds = options.catchup_seconds || 0;
|
||||||
|
this.recovery_from = options.recovery_from;
|
||||||
this.work_dir = options.work_dir;
|
this.work_dir = options.work_dir;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -86,6 +95,7 @@ export const CrontabModel = sequelize.define<CronInstance>('Crontab', {
|
|||||||
log_path: DataTypes.STRING,
|
log_path: DataTypes.STRING,
|
||||||
queued_token: DataTypes.STRING,
|
queued_token: DataTypes.STRING,
|
||||||
labels: DataTypes.JSON,
|
labels: DataTypes.JSON,
|
||||||
|
last_instance_id: DataTypes.INTEGER,
|
||||||
last_running_time: DataTypes.NUMBER,
|
last_running_time: DataTypes.NUMBER,
|
||||||
last_execution_time: DataTypes.NUMBER,
|
last_execution_time: DataTypes.NUMBER,
|
||||||
sub_id: { type: DataTypes.NUMBER, allowNull: true },
|
sub_id: { type: DataTypes.NUMBER, allowNull: true },
|
||||||
@@ -94,5 +104,9 @@ export const CrontabModel = sequelize.define<CronInstance>('Crontab', {
|
|||||||
task_after: DataTypes.STRING,
|
task_after: DataTypes.STRING,
|
||||||
log_name: DataTypes.STRING,
|
log_name: DataTypes.STRING,
|
||||||
allow_multiple_instances: DataTypes.NUMBER,
|
allow_multiple_instances: DataTypes.NUMBER,
|
||||||
|
skip_if_pending: DataTypes.BOOLEAN,
|
||||||
|
max_queue_wait: DataTypes.INTEGER,
|
||||||
|
catchup_seconds: DataTypes.INTEGER,
|
||||||
|
recovery_from: DataTypes.BIGINT,
|
||||||
work_dir: DataTypes.STRING,
|
work_dir: DataTypes.STRING,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -8,8 +8,10 @@ export class CrontabStat {
|
|||||||
run_count?: number;
|
run_count?: number;
|
||||||
success_count?: number;
|
success_count?: number;
|
||||||
fail_count?: number;
|
fail_count?: number;
|
||||||
|
untimed_count?: number;
|
||||||
total_time?: number;
|
total_time?: number;
|
||||||
max_time?: number;
|
max_time?: number;
|
||||||
|
fixed_max_time?: number | null;
|
||||||
|
|
||||||
constructor(options: CrontabStat) {
|
constructor(options: CrontabStat) {
|
||||||
this.id = options.id;
|
this.id = options.id;
|
||||||
@@ -18,6 +20,7 @@ export class CrontabStat {
|
|||||||
this.run_count = options.run_count || 0;
|
this.run_count = options.run_count || 0;
|
||||||
this.success_count = options.success_count || 0;
|
this.success_count = options.success_count || 0;
|
||||||
this.fail_count = options.fail_count || 0;
|
this.fail_count = options.fail_count || 0;
|
||||||
|
this.untimed_count = options.untimed_count || 0;
|
||||||
this.total_time = options.total_time || 0;
|
this.total_time = options.total_time || 0;
|
||||||
this.max_time = options.max_time || 0;
|
this.max_time = options.max_time || 0;
|
||||||
}
|
}
|
||||||
@@ -53,10 +56,16 @@ export const CrontabStatModel = sequelize.define<CrontabStatInstance>(
|
|||||||
type: DataTypes.NUMBER,
|
type: DataTypes.NUMBER,
|
||||||
defaultValue: 0,
|
defaultValue: 0,
|
||||||
},
|
},
|
||||||
|
untimed_count: {
|
||||||
|
type: DataTypes.INTEGER,
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: 0,
|
||||||
|
},
|
||||||
total_time: {
|
total_time: {
|
||||||
type: DataTypes.NUMBER,
|
type: DataTypes.NUMBER,
|
||||||
defaultValue: 0,
|
defaultValue: 0,
|
||||||
},
|
},
|
||||||
|
fixed_max_time: { type: DataTypes.NUMBER, allowNull: true },
|
||||||
max_time: {
|
max_time: {
|
||||||
type: DataTypes.NUMBER,
|
type: DataTypes.NUMBER,
|
||||||
defaultValue: 0,
|
defaultValue: 0,
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { Sequelize, Transaction } from 'sequelize';
|
import { Sequelize, Transaction } from 'sequelize';
|
||||||
import config from '../config/index';
|
import config from '../config/index';
|
||||||
|
import type { Database } from 'sqlite3';
|
||||||
import { join } from 'path';
|
import { join } from 'path';
|
||||||
|
|
||||||
export const sequelize = new Sequelize({
|
export const sequelize = new Sequelize({
|
||||||
@@ -20,4 +21,29 @@ export const sequelize = new Sequelize({
|
|||||||
transactionType: Transaction.TYPES.IMMEDIATE,
|
transactionType: Transaction.TYPES.IMMEDIATE,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// In DELETE journal mode, EXTRA flushes the unlink that makes a transaction
|
||||||
|
// final. Sequelize opens a separate SQLite handle for each transaction.
|
||||||
|
const initialized = new WeakMap<Database, Promise<void>>();
|
||||||
|
sequelize.addHook('beforeQuery', async (_options, query) => {
|
||||||
|
const connection = query.connection as Database;
|
||||||
|
let ready = initialized.get(connection);
|
||||||
|
if (!ready) {
|
||||||
|
ready = new Promise<void>((resolve, reject) => {
|
||||||
|
connection.run('PRAGMA synchronous=EXTRA', error => error ? reject(error) : resolve());
|
||||||
|
}).catch(error => {
|
||||||
|
initialized.delete(connection);
|
||||||
|
throw error;
|
||||||
|
});
|
||||||
|
initialized.set(connection, ready);
|
||||||
|
}
|
||||||
|
await ready;
|
||||||
|
});
|
||||||
|
|
||||||
|
// SQLite transactions use dedicated file handles outside Sequelize 6's pool.
|
||||||
|
// Its default destroyConnection targets the pool on COMMIT/ROLLBACK errors,
|
||||||
|
// leaving the native writer open. The SQLite release path closes that handle.
|
||||||
|
sequelize.connectionManager.destroyConnection = async (connection) => {
|
||||||
|
sequelize.connectionManager.releaseConnection(connection);
|
||||||
|
};
|
||||||
|
|
||||||
export type ResponseType<T> = { code: number; data?: T; message?: string };
|
export type ResponseType<T> = { code: number; data?: T; message?: string };
|
||||||
|
|||||||
+19
-1
@@ -24,7 +24,25 @@ export interface AppToken {
|
|||||||
expiration: number;
|
expiration: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type AppScope = 'envs' | 'crons' | 'configs' | 'scripts' | 'logs' | 'system' | 'dashboard';
|
export type AppScope =
|
||||||
|
| 'crons'
|
||||||
|
| 'crons:read'
|
||||||
|
| 'subscriptions'
|
||||||
|
| 'subscriptions:read'
|
||||||
|
| 'envs'
|
||||||
|
| 'envs:read'
|
||||||
|
| 'configs'
|
||||||
|
| 'configs:read'
|
||||||
|
| 'scripts'
|
||||||
|
| 'scripts:read'
|
||||||
|
| 'logs'
|
||||||
|
| 'logs:read'
|
||||||
|
| 'dependencies'
|
||||||
|
| 'dependencies:read'
|
||||||
|
| 'dashboard'
|
||||||
|
| 'dashboard:read'
|
||||||
|
| 'system'
|
||||||
|
| 'apps';
|
||||||
|
|
||||||
export interface AppInstance extends Model<App, App>, App {}
|
export interface AppInstance extends Model<App, App>, App {}
|
||||||
export const AppModel = sequelize.define<AppInstance>('App', {
|
export const AppModel = sequelize.define<AppInstance>('App', {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ export enum InstanceStatus {
|
|||||||
'finished' = 1,
|
'finished' = 1,
|
||||||
'stopped' = 2,
|
'stopped' = 2,
|
||||||
'error' = 3,
|
'error' = 3,
|
||||||
|
'unknown' = 4,
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface RunningInstanceAttributes {
|
export interface RunningInstanceAttributes {
|
||||||
@@ -78,4 +79,5 @@ export const RunningInstanceModel = sequelize.define<RunningInstanceModel>(
|
|||||||
allowNull: true,
|
allowNull: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{ indexes: [{ fields: ['cron_id', 'pid', 'started_at'] }, { fields: ['cron_id', 'id'] }] },
|
||||||
);
|
);
|
||||||
|
|||||||
+72
-37
@@ -16,6 +16,7 @@ import { t } from '../shared/i18n';
|
|||||||
import { AppScope } from '../data/open';
|
import { AppScope } from '../data/open';
|
||||||
import uniqueConstraintError from '../middlewares/uniqueConstraintError';
|
import uniqueConstraintError from '../middlewares/uniqueConstraintError';
|
||||||
import protectedPathCase from '../middlewares/protectedPathCase';
|
import protectedPathCase from '../middlewares/protectedPathCase';
|
||||||
|
import Logger from './logger';
|
||||||
|
|
||||||
function resolveTrustProxy(value = process.env.QL_TRUST_PROXY) {
|
function resolveTrustProxy(value = process.env.QL_TRUST_PROXY) {
|
||||||
const setting = value?.trim();
|
const setting = value?.trim();
|
||||||
@@ -112,7 +113,43 @@ export default ({ app }: { app: Application }) => {
|
|||||||
const keyMatch = pathLower.match(/\/open\/([a-z]+)\/*/);
|
const keyMatch = pathLower.match(/\/open\/([a-z]+)\/*/);
|
||||||
const key = keyMatch && keyMatch[1];
|
const key = keyMatch && keyMatch[1];
|
||||||
|
|
||||||
if (!doc.scopes.includes(key as AppScope)) {
|
const isReadRequest =
|
||||||
|
(['GET', 'HEAD'].includes(req.method) &&
|
||||||
|
!(
|
||||||
|
key === 'crons' && /^\/open\/crons\/import\/?$/.test(pathLower)
|
||||||
|
)) ||
|
||||||
|
(['scripts', 'logs'].includes(key || '') &&
|
||||||
|
req.method === 'POST' &&
|
||||||
|
/^\/open\/(?:scripts|logs)\/(?:download|entries\/check)\/?$/.test(pathLower));
|
||||||
|
const hasNamespaceScope =
|
||||||
|
doc.scopes.includes(key as AppScope) ||
|
||||||
|
([
|
||||||
|
'crons',
|
||||||
|
'subscriptions',
|
||||||
|
'envs',
|
||||||
|
'configs',
|
||||||
|
'scripts',
|
||||||
|
'logs',
|
||||||
|
'dependencies',
|
||||||
|
'dashboard',
|
||||||
|
].includes(key || '') &&
|
||||||
|
doc.scopes.includes(`${key}:read` as AppScope) &&
|
||||||
|
isReadRequest);
|
||||||
|
if (!hasNamespaceScope) {
|
||||||
|
const err = new UnauthorizedError('credentials_bad_scheme', {
|
||||||
|
message: t('暂无权限'),
|
||||||
|
});
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
// The built-in system app still needs its local CLI maintenance API.
|
||||||
|
const appRouteAllowed =
|
||||||
|
key !== 'system' ||
|
||||||
|
doc.name === 'system' ||
|
||||||
|
((req.method === 'GET' || req.method === 'HEAD') &&
|
||||||
|
(pathLower === '/open/system' || pathLower === '/open/system/')) ||
|
||||||
|
(req.method === 'PUT' && pathLower === '/open/system/notify');
|
||||||
|
if (!appRouteAllowed) {
|
||||||
const err = new UnauthorizedError('credentials_bad_scheme', {
|
const err = new UnauthorizedError('credentials_bad_scheme', {
|
||||||
message: t('暂无权限'),
|
message: t('暂无权限'),
|
||||||
});
|
});
|
||||||
@@ -155,14 +192,7 @@ export default ({ app }: { app: Application }) => {
|
|||||||
|
|
||||||
app.use(async (req, res, next) => {
|
app.use(async (req, res, next) => {
|
||||||
const pathLower = req.path.toLowerCase();
|
const pathLower = req.path.toLowerCase();
|
||||||
if (
|
if (!['/api/user/init', '/open/user/init'].includes(pathLower)) {
|
||||||
![
|
|
||||||
'/api/user/init',
|
|
||||||
'/api/user/notification/init',
|
|
||||||
'/open/user/init',
|
|
||||||
'/open/user/notification/init',
|
|
||||||
].includes(pathLower)
|
|
||||||
) {
|
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
const authInfo = (await shareStore.getAuthInfo()) || ({} as AuthInfo);
|
const authInfo = (await shareStore.getAuthInfo()) || ({} as AuthInfo);
|
||||||
@@ -202,37 +232,23 @@ export default ({ app }: { app: Application }) => {
|
|||||||
|
|
||||||
app.use(
|
app.use(
|
||||||
(
|
(
|
||||||
err: Error & { status: number },
|
err: Error & { status: number; code?: string },
|
||||||
req: Request,
|
req: Request,
|
||||||
res: Response,
|
res: Response,
|
||||||
next: NextFunction,
|
next: NextFunction,
|
||||||
) => {
|
) => {
|
||||||
if (err.name === 'UnauthorizedError') {
|
if (err.name === 'UnauthorizedError') {
|
||||||
return res
|
let message = err.message;
|
||||||
.status(err.status)
|
if (!config.isDevelopment) {
|
||||||
.send({ code: 401, message: err.message })
|
if (err.code === 'credentials_bad_scheme') {
|
||||||
.end();
|
message = t('暂无权限');
|
||||||
|
} else if (err.code === 'credentials_required') {
|
||||||
|
message = t('请先登录');
|
||||||
|
} else {
|
||||||
|
message = t('Token 已失效');
|
||||||
}
|
}
|
||||||
return next(err);
|
}
|
||||||
},
|
return res.status(401).send({ code: 401, message }).end();
|
||||||
);
|
|
||||||
|
|
||||||
app.use(
|
|
||||||
(
|
|
||||||
err: Error & { errors: any[] },
|
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
next: NextFunction,
|
|
||||||
) => {
|
|
||||||
if (err.name.includes('Sequelize')) {
|
|
||||||
return res
|
|
||||||
.status(500)
|
|
||||||
.send({
|
|
||||||
code: 400,
|
|
||||||
message: `${err.message}`,
|
|
||||||
errors: err.errors,
|
|
||||||
})
|
|
||||||
.end();
|
|
||||||
}
|
}
|
||||||
return next(err);
|
return next(err);
|
||||||
},
|
},
|
||||||
@@ -245,10 +261,29 @@ export default ({ app }: { app: Application }) => {
|
|||||||
res: Response,
|
res: Response,
|
||||||
next: NextFunction,
|
next: NextFunction,
|
||||||
) => {
|
) => {
|
||||||
res.status(err.status || 500);
|
const status =
|
||||||
|
Number.isInteger(err.status) && err.status >= 400 && err.status < 600
|
||||||
|
? err.status
|
||||||
|
: 500;
|
||||||
|
if (status >= 500) {
|
||||||
|
const route =
|
||||||
|
typeof req.route?.path === 'string' ? req.route.path : '?';
|
||||||
|
const errorName = /^[A-Za-z][A-Za-z0-9]{0,63}$/.test(err.name)
|
||||||
|
? err.name
|
||||||
|
: 'Error';
|
||||||
|
Logger.error(
|
||||||
|
'HTTP request failed: status=%d method=%s route=%s type=%s',
|
||||||
|
status,
|
||||||
|
req.method,
|
||||||
|
route,
|
||||||
|
errorName,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
res.status(status);
|
||||||
res.json({
|
res.json({
|
||||||
code: err.status || 500,
|
code: status,
|
||||||
message: err.message,
|
message:
|
||||||
|
!config.isDevelopment && status >= 500 ? t('未知错误') : err.message,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
+10
-16
@@ -1,7 +1,7 @@
|
|||||||
import DependenceService from '../services/dependence';
|
import DependenceService from '../services/dependence';
|
||||||
import { exec } from 'child_process';
|
import { exec } from 'child_process';
|
||||||
import { Container } from 'typedi';
|
import { Container } from 'typedi';
|
||||||
import { Crontab, CrontabModel, CrontabStatus } from '../data/cron';
|
import { Crontab, CrontabModel } from '../data/cron';
|
||||||
import CronService from '../services/cron';
|
import CronService from '../services/cron';
|
||||||
import EnvService from '../services/env';
|
import EnvService from '../services/env';
|
||||||
import { DependenceModel, DependenceStatus } from '../data/dependence';
|
import { DependenceModel, DependenceStatus } from '../data/dependence';
|
||||||
@@ -18,7 +18,7 @@ import OpenService from '../services/open';
|
|||||||
import { shareStore } from '../shared/store';
|
import { shareStore } from '../shared/store';
|
||||||
import Logger from './logger';
|
import Logger from './logger';
|
||||||
import cronClient from '../schedule/client';
|
import cronClient from '../schedule/client';
|
||||||
import { InstanceStatus, RunningInstanceModel } from '../data/runningInstance';
|
import { TriggerHistory } from '../shared/triggerHistory';
|
||||||
import { setLang, systemLang } from '../shared/i18n';
|
import { setLang, systemLang } from '../shared/i18n';
|
||||||
|
|
||||||
export default async () => {
|
export default async () => {
|
||||||
@@ -121,18 +121,11 @@ export default async () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// 初始化更新所有任务状态为空闲
|
const history = new TriggerHistory(CrontabModel.sequelize!);
|
||||||
await CrontabModel.update({ status: CrontabStatus.idle }, { where: {} });
|
const pendingStops = await history.pendingStops();
|
||||||
|
const pendingBatches = await history.pendingStopBatches();
|
||||||
// 清空所有运行中的实例记录(服务重启后进程已不存在)
|
if (pendingStops.length || pendingBatches.length) await cronService.resumePendingStops(history, pendingStops, pendingBatches);
|
||||||
await RunningInstanceModel.update(
|
await history.reconcileInstances();
|
||||||
{
|
|
||||||
status: InstanceStatus.stopped,
|
|
||||||
finished_at: Math.floor(Date.now() / 1000),
|
|
||||||
// The previous process's exit code cannot be recovered after a restart.
|
|
||||||
},
|
|
||||||
{ where: { status: InstanceStatus.running } },
|
|
||||||
);
|
|
||||||
|
|
||||||
// 初始化时执行一次所有的 ql repo 任务
|
// 初始化时执行一次所有的 ql repo 任务
|
||||||
CrontabModel.findAll({
|
CrontabModel.findAll({
|
||||||
@@ -222,6 +215,9 @@ export default async () => {
|
|||||||
}
|
}
|
||||||
} catch { }
|
} catch { }
|
||||||
|
|
||||||
|
// Replace legacy task environment files before recovered tasks can run.
|
||||||
|
await envService.set_envs();
|
||||||
|
|
||||||
// 初始化保存一次ck和定时任务数据
|
// 初始化保存一次ck和定时任务数据
|
||||||
cronClient.readiness.configure(async () => {
|
cronClient.readiness.configure(async () => {
|
||||||
await cronService.autosave_crontab(true);
|
await cronService.autosave_crontab(true);
|
||||||
@@ -229,8 +225,6 @@ export default async () => {
|
|||||||
});
|
});
|
||||||
await cronClient.readiness.recover();
|
await cronClient.readiness.recover();
|
||||||
|
|
||||||
await envService.set_envs();
|
|
||||||
|
|
||||||
const authInfo = await userService.getAuthInfo();
|
const authInfo = await userService.getAuthInfo();
|
||||||
await shareStore.updateAuthInfo(authInfo);
|
await shareStore.updateAuthInfo(authInfo);
|
||||||
await openService.refreshApps();
|
await openService.refreshApps();
|
||||||
|
|||||||
@@ -16,6 +16,10 @@ message ICron {
|
|||||||
string command = 3;
|
string command = 3;
|
||||||
repeated ISchedule extra_schedules = 4;
|
repeated ISchedule extra_schedules = 4;
|
||||||
string name = 5;
|
string name = 5;
|
||||||
|
optional uint32 max_queue_wait = 6;
|
||||||
|
optional uint32 catchup_seconds = 7;
|
||||||
|
optional int64 recovery_from = 8;
|
||||||
|
optional bool skip_if_pending = 9;
|
||||||
}
|
}
|
||||||
|
|
||||||
message AddCronRequest {
|
message AddCronRequest {
|
||||||
|
|||||||
+91
-2
@@ -1,7 +1,7 @@
|
|||||||
// Code generated by protoc-gen-ts_proto. DO NOT EDIT.
|
// Code generated by protoc-gen-ts_proto. DO NOT EDIT.
|
||||||
// versions:
|
// versions:
|
||||||
// protoc-gen-ts_proto v2.6.1
|
// protoc-gen-ts_proto v2.6.1
|
||||||
// protoc v5.28.3
|
// protoc v5.29.0
|
||||||
// source: back/protos/cron.proto
|
// source: back/protos/cron.proto
|
||||||
|
|
||||||
/* eslint-disable */
|
/* eslint-disable */
|
||||||
@@ -31,6 +31,10 @@ export interface ICron {
|
|||||||
command: string;
|
command: string;
|
||||||
extra_schedules: ISchedule[];
|
extra_schedules: ISchedule[];
|
||||||
name: string;
|
name: string;
|
||||||
|
max_queue_wait?: number | undefined;
|
||||||
|
catchup_seconds?: number | undefined;
|
||||||
|
recovery_from?: number | undefined;
|
||||||
|
skip_if_pending?: boolean | undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AddCronRequest {
|
export interface AddCronRequest {
|
||||||
@@ -112,7 +116,17 @@ export const ISchedule: MessageFns<ISchedule> = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
function createBaseICron(): ICron {
|
function createBaseICron(): ICron {
|
||||||
return { id: "", schedule: "", command: "", extra_schedules: [], name: "" };
|
return {
|
||||||
|
id: "",
|
||||||
|
schedule: "",
|
||||||
|
command: "",
|
||||||
|
extra_schedules: [],
|
||||||
|
name: "",
|
||||||
|
max_queue_wait: undefined,
|
||||||
|
catchup_seconds: undefined,
|
||||||
|
recovery_from: undefined,
|
||||||
|
skip_if_pending: undefined,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ICron: MessageFns<ICron> = {
|
export const ICron: MessageFns<ICron> = {
|
||||||
@@ -132,6 +146,18 @@ export const ICron: MessageFns<ICron> = {
|
|||||||
if (message.name !== "") {
|
if (message.name !== "") {
|
||||||
writer.uint32(42).string(message.name);
|
writer.uint32(42).string(message.name);
|
||||||
}
|
}
|
||||||
|
if (message.max_queue_wait !== undefined) {
|
||||||
|
writer.uint32(48).uint32(message.max_queue_wait);
|
||||||
|
}
|
||||||
|
if (message.catchup_seconds !== undefined) {
|
||||||
|
writer.uint32(56).uint32(message.catchup_seconds);
|
||||||
|
}
|
||||||
|
if (message.recovery_from !== undefined) {
|
||||||
|
writer.uint32(64).int64(message.recovery_from);
|
||||||
|
}
|
||||||
|
if (message.skip_if_pending !== undefined) {
|
||||||
|
writer.uint32(72).bool(message.skip_if_pending);
|
||||||
|
}
|
||||||
return writer;
|
return writer;
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -182,6 +208,38 @@ export const ICron: MessageFns<ICron> = {
|
|||||||
message.name = reader.string();
|
message.name = reader.string();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
case 6: {
|
||||||
|
if (tag !== 48) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
message.max_queue_wait = reader.uint32();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
case 7: {
|
||||||
|
if (tag !== 56) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
message.catchup_seconds = reader.uint32();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
case 8: {
|
||||||
|
if (tag !== 64) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
message.recovery_from = longToNumber(reader.int64());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
case 9: {
|
||||||
|
if (tag !== 72) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
message.skip_if_pending = reader.bool();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if ((tag & 7) === 4 || tag === 0) {
|
if ((tag & 7) === 4 || tag === 0) {
|
||||||
break;
|
break;
|
||||||
@@ -200,6 +258,10 @@ export const ICron: MessageFns<ICron> = {
|
|||||||
? object.extra_schedules.map((e: any) => ISchedule.fromJSON(e))
|
? object.extra_schedules.map((e: any) => ISchedule.fromJSON(e))
|
||||||
: [],
|
: [],
|
||||||
name: isSet(object.name) ? globalThis.String(object.name) : "",
|
name: isSet(object.name) ? globalThis.String(object.name) : "",
|
||||||
|
max_queue_wait: isSet(object.max_queue_wait) ? globalThis.Number(object.max_queue_wait) : undefined,
|
||||||
|
catchup_seconds: isSet(object.catchup_seconds) ? globalThis.Number(object.catchup_seconds) : undefined,
|
||||||
|
recovery_from: isSet(object.recovery_from) ? globalThis.Number(object.recovery_from) : undefined,
|
||||||
|
skip_if_pending: isSet(object.skip_if_pending) ? globalThis.Boolean(object.skip_if_pending) : undefined,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -220,6 +282,18 @@ export const ICron: MessageFns<ICron> = {
|
|||||||
if (message.name !== "") {
|
if (message.name !== "") {
|
||||||
obj.name = message.name;
|
obj.name = message.name;
|
||||||
}
|
}
|
||||||
|
if (message.max_queue_wait !== undefined) {
|
||||||
|
obj.max_queue_wait = Math.round(message.max_queue_wait);
|
||||||
|
}
|
||||||
|
if (message.catchup_seconds !== undefined) {
|
||||||
|
obj.catchup_seconds = Math.round(message.catchup_seconds);
|
||||||
|
}
|
||||||
|
if (message.recovery_from !== undefined) {
|
||||||
|
obj.recovery_from = Math.round(message.recovery_from);
|
||||||
|
}
|
||||||
|
if (message.skip_if_pending !== undefined) {
|
||||||
|
obj.skip_if_pending = message.skip_if_pending;
|
||||||
|
}
|
||||||
return obj;
|
return obj;
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -233,6 +307,10 @@ export const ICron: MessageFns<ICron> = {
|
|||||||
message.command = object.command ?? "";
|
message.command = object.command ?? "";
|
||||||
message.extra_schedules = object.extra_schedules?.map((e) => ISchedule.fromPartial(e)) || [];
|
message.extra_schedules = object.extra_schedules?.map((e) => ISchedule.fromPartial(e)) || [];
|
||||||
message.name = object.name ?? "";
|
message.name = object.name ?? "";
|
||||||
|
message.max_queue_wait = object.max_queue_wait ?? undefined;
|
||||||
|
message.catchup_seconds = object.catchup_seconds ?? undefined;
|
||||||
|
message.recovery_from = object.recovery_from ?? undefined;
|
||||||
|
message.skip_if_pending = object.skip_if_pending ?? undefined;
|
||||||
return message;
|
return message;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@@ -618,6 +696,17 @@ type KeysOfUnion<T> = T extends T ? keyof T : never;
|
|||||||
export type Exact<P, I extends P> = P extends Builtin ? P
|
export type Exact<P, I extends P> = P extends Builtin ? P
|
||||||
: P & { [K in keyof P]: Exact<P[K], I[K]> } & { [K in Exclude<keyof I, KeysOfUnion<P>>]: never };
|
: P & { [K in keyof P]: Exact<P[K], I[K]> } & { [K in Exclude<keyof I, KeysOfUnion<P>>]: never };
|
||||||
|
|
||||||
|
function longToNumber(int64: { toString(): string }): number {
|
||||||
|
const num = globalThis.Number(int64.toString());
|
||||||
|
if (num > globalThis.Number.MAX_SAFE_INTEGER) {
|
||||||
|
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
|
||||||
|
}
|
||||||
|
if (num < globalThis.Number.MIN_SAFE_INTEGER) {
|
||||||
|
throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
|
||||||
|
}
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
|
||||||
function isSet(value: any): boolean {
|
function isSet(value: any): boolean {
|
||||||
return value !== null && value !== undefined;
|
return value !== null && value !== undefined;
|
||||||
}
|
}
|
||||||
|
|||||||
+45
-14
@@ -3,6 +3,8 @@ import { AddCronRequest, AddCronResponse } from '../protos/cron';
|
|||||||
import { createCronJob, CronJob } from '../shared/cronScheduler';
|
import { createCronJob, CronJob } from '../shared/cronScheduler';
|
||||||
import { isValidCronSchedule } from '../shared/cronSchedule';
|
import { isValidCronSchedule } from '../shared/cronSchedule';
|
||||||
import { scheduleStacks } from './data';
|
import { scheduleStacks } from './data';
|
||||||
|
import { latestMissedSlot } from '../shared/cronRecovery';
|
||||||
|
import { slotId } from '../shared/triggerHistory';
|
||||||
import { runCron } from '../shared/runCron';
|
import { runCron } from '../shared/runCron';
|
||||||
import Logger from '../loaders/logger';
|
import Logger from '../loaders/logger';
|
||||||
import { t, tf } from '../shared/i18n';
|
import { t, tf } from '../shared/i18n';
|
||||||
@@ -12,9 +14,12 @@ const isValidCronField = (cron: string): boolean => {
|
|||||||
return isValidCronSchedule(cron);
|
return isValidCronSchedule(cron);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const definitions = new WeakMap<CronJob[], string>();
|
||||||
|
|
||||||
const addCron = (
|
const addCron = (
|
||||||
call: ServerUnaryCall<AddCronRequest, AddCronResponse>,
|
call: ServerUnaryCall<AddCronRequest, AddCronResponse>,
|
||||||
callback: sendUnaryData<AddCronResponse>,
|
callback: sendUnaryData<AddCronResponse>,
|
||||||
|
savePlan?: () => Promise<void>,
|
||||||
) => {
|
) => {
|
||||||
// ===== 第一遍:预校验所有 cron 表达式 =====
|
// ===== 第一遍:预校验所有 cron 表达式 =====
|
||||||
const validationErrors: string[] = [];
|
const validationErrors: string[] = [];
|
||||||
@@ -54,26 +59,41 @@ const addCron = (
|
|||||||
|
|
||||||
// Prepare stopped jobs first: construction errors must preserve the old snapshot.
|
// Prepare stopped jobs first: construction errors must preserve the old snapshot.
|
||||||
const prepared = new Map<string, CronJob[]>();
|
const prepared = new Map<string, CronJob[]>();
|
||||||
|
const desired = new Map(call.request.crons.map((item) => [item.id, item]));
|
||||||
|
const recoveries: (() => Promise<void>)[] = [];
|
||||||
try {
|
try {
|
||||||
for (const item of call.request.crons) {
|
for (const item of desired.values()) {
|
||||||
|
const signature = JSON.stringify([item.schedule, item.command,
|
||||||
|
(item.extra_schedules || []).map((s) => s.schedule), item.max_queue_wait || 0,
|
||||||
|
!!item.skip_if_pending, item.catchup_seconds || 0, item.catchup_seconds || item.skip_if_pending ? item.recovery_from : 0]);
|
||||||
|
const current = scheduleStacks.get(item.id);
|
||||||
|
if (current && definitions.get(current) === signature) continue;
|
||||||
const jobs: CronJob[] = [];
|
const jobs: CronJob[] = [];
|
||||||
prepared.get(item.id)?.forEach((job) => job.cancel());
|
definitions.set(jobs, signature);
|
||||||
prepared.set(item.id, jobs);
|
prepared.set(item.id, jobs);
|
||||||
for (const schedule of [
|
for (const [ruleIndex, schedule] of [
|
||||||
item.schedule,
|
item.schedule,
|
||||||
...(item.extra_schedules || []).map((x) => x.schedule),
|
...(item.extra_schedules || []).map((x) => x.schedule),
|
||||||
]) {
|
].entries()) {
|
||||||
|
const fire = async (date: Date, source: 'node' | 'recovery' = 'node') => {
|
||||||
|
const isCurrent = () => scheduleStacks.get(item.id) === jobs;
|
||||||
|
if (!isCurrent()) return;
|
||||||
|
await runCron(item.command, item, isCurrent, date && {
|
||||||
|
id: slotId(item.id, ruleIndex, schedule, +date), time: +date, source,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
if (call.request.replace && item.catchup_seconds && item.recovery_from) {
|
||||||
|
recoveries.push(async () => {
|
||||||
|
const now = Date.now();
|
||||||
|
const since = Math.max(item.recovery_from!, now - item.catchup_seconds! * 1000);
|
||||||
|
const time = latestMissedSlot(schedule, since, now);
|
||||||
|
if (time !== null) await fire(new Date(time), 'recovery');
|
||||||
|
});
|
||||||
|
}
|
||||||
jobs.push(
|
jobs.push(
|
||||||
createCronJob(
|
createCronJob(
|
||||||
schedule,
|
schedule,
|
||||||
async () => {
|
fire,
|
||||||
Logger.info(t('[schedule][准备运行任务] 命令: %s'), item.command);
|
|
||||||
await runCron(
|
|
||||||
item.command,
|
|
||||||
item,
|
|
||||||
() => scheduleStacks.get(item.id) === jobs,
|
|
||||||
);
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
name: `${item.id}: ${item.name || ''}`,
|
name: `${item.id}: ${item.name || ''}`,
|
||||||
logger: Logger,
|
logger: Logger,
|
||||||
@@ -94,10 +114,13 @@ const addCron = (
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const activate = () => {
|
||||||
if (call.request.replace) {
|
if (call.request.replace) {
|
||||||
for (const jobs of scheduleStacks.values())
|
for (const [id, jobs] of scheduleStacks) {
|
||||||
|
if (desired.has(id)) continue;
|
||||||
jobs.forEach((job) => job?.cancel());
|
jobs.forEach((job) => job?.cancel());
|
||||||
scheduleStacks.clear();
|
scheduleStacks.delete(id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for (const [id, jobs] of prepared) {
|
for (const [id, jobs] of prepared) {
|
||||||
scheduleStacks.get(id)?.forEach((job) => job.cancel());
|
scheduleStacks.get(id)?.forEach((job) => job.cancel());
|
||||||
@@ -111,6 +134,14 @@ const addCron = (
|
|||||||
}
|
}
|
||||||
|
|
||||||
callback(null, null);
|
callback(null, null);
|
||||||
|
for (const recover of recoveries) setImmediate(() => recover().catch((err) => Logger.error(String(err))));
|
||||||
|
};
|
||||||
|
if (savePlan) {
|
||||||
|
savePlan().then(activate).catch(error => {
|
||||||
|
for (const jobs of prepared.values()) jobs.forEach(job => job.cancel());
|
||||||
|
callback(error);
|
||||||
|
});
|
||||||
|
} else activate();
|
||||||
};
|
};
|
||||||
|
|
||||||
export { addCron };
|
export { addCron };
|
||||||
|
|||||||
@@ -94,7 +94,7 @@ class Client {
|
|||||||
const registrationTimeoutMs = Math.min(120000, 5000 + ruleCount * 5);
|
const registrationTimeoutMs = Math.min(120000, 5000 + ruleCount * 5);
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this.client.addCron(
|
this.client.addCron(
|
||||||
{ crons: request, replace },
|
AddCronRequest.fromPartial({ crons: request, replace }),
|
||||||
new Metadata(),
|
new Metadata(),
|
||||||
{ deadline: Date.now() + registrationTimeoutMs },
|
{ deadline: Date.now() + registrationTimeoutMs },
|
||||||
(err, res) => {
|
(err, res) => {
|
||||||
|
|||||||
@@ -0,0 +1,59 @@
|
|||||||
|
import { ServerUnaryCall, sendUnaryData } from '@grpc/grpc-js';
|
||||||
|
import {
|
||||||
|
AddCronRequest,
|
||||||
|
AddCronResponse,
|
||||||
|
DeleteCronRequest,
|
||||||
|
DeleteCronResponse,
|
||||||
|
} from '../protos/cron';
|
||||||
|
import { addCron } from './addCron';
|
||||||
|
import { delCron } from './delCron';
|
||||||
|
import { randomUUID } from 'crypto';
|
||||||
|
import { saveSchedulePlan, confirmSchedulePlan } from '../shared/scheduleAudit';
|
||||||
|
|
||||||
|
// Keep acknowledgement, publication and timer replacement in request order,
|
||||||
|
// even if the client times out and sends a replacement snapshot.
|
||||||
|
let pending = Promise.resolve();
|
||||||
|
function serialize(action: () => Promise<void>) {
|
||||||
|
pending = pending.then(action, action);
|
||||||
|
return pending;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registeredAdd(
|
||||||
|
call: ServerUnaryCall<AddCronRequest, AddCronResponse>,
|
||||||
|
callback: sendUnaryData<AddCronResponse>,
|
||||||
|
) {
|
||||||
|
return serialize(async () => {
|
||||||
|
const db = require('../data').sequelize;
|
||||||
|
const token = randomUUID();
|
||||||
|
let first = 0;
|
||||||
|
const last = await new Promise<number>((resolve, reject) => {
|
||||||
|
addCron(
|
||||||
|
call,
|
||||||
|
(error) => (error ? reject(error) : resolve(Date.now())),
|
||||||
|
async () => {
|
||||||
|
await saveSchedulePlan(db, call.request, undefined, token);
|
||||||
|
first = Date.now();
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
await confirmSchedulePlan(db, token, first, last, call.request.replace);
|
||||||
|
callback(null, null);
|
||||||
|
}).catch((error) => callback(error));
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registeredDelete(
|
||||||
|
call: ServerUnaryCall<DeleteCronRequest, DeleteCronResponse>,
|
||||||
|
callback: sendUnaryData<DeleteCronResponse>,
|
||||||
|
) {
|
||||||
|
return serialize(async () => {
|
||||||
|
const db = require('../data').sequelize;
|
||||||
|
const token = randomUUID();
|
||||||
|
await saveSchedulePlan(db, call.request, undefined, token);
|
||||||
|
const first = Date.now();
|
||||||
|
delCron(call, (error) => {
|
||||||
|
if (error) throw error;
|
||||||
|
});
|
||||||
|
await confirmSchedulePlan(db, token, first, Date.now(), false);
|
||||||
|
callback(null, null);
|
||||||
|
}).catch((error) => callback(error));
|
||||||
|
}
|
||||||
+593
-267
File diff suppressed because it is too large
Load Diff
+74
-17
@@ -27,9 +27,20 @@ import taskLimit from '../shared/pLimit';
|
|||||||
import { detectOS } from '../config/util';
|
import { detectOS } from '../config/util';
|
||||||
import { LINUX_DEPENDENCE_COMMAND } from '../config/const';
|
import { LINUX_DEPENDENCE_COMMAND } from '../config/const';
|
||||||
import { t, tf } from '../shared/i18n';
|
import { t, tf } from '../shared/i18n';
|
||||||
|
import { randomUUID } from 'crypto';
|
||||||
|
|
||||||
|
type RefreshType = 'nodejs' | 'python3';
|
||||||
|
type RefreshJob = {
|
||||||
|
id: string;
|
||||||
|
type: RefreshType;
|
||||||
|
state: 'running' | 'completed' | 'failed';
|
||||||
|
};
|
||||||
|
|
||||||
@Service()
|
@Service()
|
||||||
export default class DependenceService {
|
export default class DependenceService {
|
||||||
|
private activeRefreshJob?: RefreshJob;
|
||||||
|
private refreshJobs = new Map<string, RefreshJob>();
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@Inject('logger') private logger: winston.Logger,
|
@Inject('logger') private logger: winston.Logger,
|
||||||
private sockService: SockService,
|
private sockService: SockService,
|
||||||
@@ -118,11 +129,6 @@ export default class DependenceService {
|
|||||||
query: any = {},
|
query: any = {},
|
||||||
): Promise<Dependence[]> {
|
): Promise<Dependence[]> {
|
||||||
let condition = query;
|
let condition = query;
|
||||||
const dependenceType =
|
|
||||||
type && DependenceTypes[type] !== undefined
|
|
||||||
? DependenceTypes[type]
|
|
||||||
: undefined;
|
|
||||||
await this.refreshInstalledStatuses(dependenceType);
|
|
||||||
if (type && DependenceTypes[type] !== undefined) {
|
if (type && DependenceTypes[type] !== undefined) {
|
||||||
condition.type = DependenceTypes[type];
|
condition.type = DependenceTypes[type];
|
||||||
}
|
}
|
||||||
@@ -145,6 +151,52 @@ export default class DependenceService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public startRefresh(type: RefreshType): {
|
||||||
|
job: RefreshJob;
|
||||||
|
accepted: boolean;
|
||||||
|
} {
|
||||||
|
if (this.activeRefreshJob) {
|
||||||
|
return {
|
||||||
|
job: this.activeRefreshJob,
|
||||||
|
accepted: this.activeRefreshJob.type === type,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const job: RefreshJob = { id: randomUUID(), type, state: 'running' };
|
||||||
|
this.activeRefreshJob = job;
|
||||||
|
this.refreshJobs.set(job.id, job);
|
||||||
|
while (this.refreshJobs.size > 20) {
|
||||||
|
const oldestId = this.refreshJobs.keys().next().value;
|
||||||
|
this.refreshJobs.delete(oldestId);
|
||||||
|
}
|
||||||
|
|
||||||
|
void this.refreshInstalledStatuses(DependenceTypes[type])
|
||||||
|
.then(() => {
|
||||||
|
job.state = 'completed';
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
this.logger.error('Dependency refresh failed: %o', error);
|
||||||
|
job.state = 'failed';
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
if (this.activeRefreshJob === job) {
|
||||||
|
this.activeRefreshJob = undefined;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return { job, accepted: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
public getRefreshJob(id: string): RefreshJob | undefined {
|
||||||
|
return this.refreshJobs.get(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public getActiveRefreshJob(): RefreshJob | undefined {
|
||||||
|
return this.activeRefreshJob?.state === 'running'
|
||||||
|
? this.activeRefreshJob
|
||||||
|
: undefined;
|
||||||
|
}
|
||||||
|
|
||||||
private async refreshInstalledStatuses(type?: DependenceTypes) {
|
private async refreshInstalledStatuses(type?: DependenceTypes) {
|
||||||
const cacheDependenceTypes = [
|
const cacheDependenceTypes = [
|
||||||
DependenceTypes.nodejs,
|
DependenceTypes.nodejs,
|
||||||
@@ -160,26 +212,31 @@ export default class DependenceService {
|
|||||||
type: type === undefined ? { [Op.in]: cacheDependenceTypes } : type,
|
type: type === undefined ? { [Op.in]: cacheDependenceTypes } : type,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
const checks = await concurrentRun(
|
const checkableDocs = docs.filter(
|
||||||
docs
|
|
||||||
.filter(
|
|
||||||
(doc) =>
|
(doc) =>
|
||||||
doc.type !== DependenceTypes.python3 ||
|
doc.type !== DependenceTypes.python3 ||
|
||||||
!isPythonSourceDependency(doc.name),
|
!isPythonSourceDependency(doc.name),
|
||||||
)
|
);
|
||||||
.map((doc) => async () => {
|
const checks = await concurrentRun(
|
||||||
return (await this.isDependenceInstalled(doc)) ? undefined : doc.id;
|
checkableDocs.map((doc) => async () => this.isDependenceInstalled(doc)),
|
||||||
}),
|
|
||||||
5,
|
5,
|
||||||
);
|
);
|
||||||
const missingIds = (checks || []).filter(
|
|
||||||
(id): id is number => id !== undefined,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (missingIds.length) {
|
for (const [index, installed] of (checks || []).entries()) {
|
||||||
|
if (installed) continue;
|
||||||
|
const doc = checkableDocs[index];
|
||||||
await DependenceModel.update(
|
await DependenceModel.update(
|
||||||
{ status: DependenceStatus.installFailed },
|
{ status: DependenceStatus.installFailed },
|
||||||
{ where: { id: missingIds } },
|
{
|
||||||
|
where: {
|
||||||
|
id: doc.id,
|
||||||
|
status: DependenceStatus.installed,
|
||||||
|
name: doc.name,
|
||||||
|
type: doc.type,
|
||||||
|
updatedAt: (doc as typeof doc & { updatedAt: Date }).updatedAt,
|
||||||
|
// Sequelize's automatic timestamp is absent from Dependence's attribute type.
|
||||||
|
} as any,
|
||||||
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+22
-3
@@ -14,12 +14,17 @@ import {
|
|||||||
} from '../data/env';
|
} from '../data/env';
|
||||||
import { writeFileWithLock } from '../shared/utils';
|
import { writeFileWithLock } from '../shared/utils';
|
||||||
import { sequelize } from '../data';
|
import { sequelize } from '../data';
|
||||||
|
import {
|
||||||
|
assertAllowedTaskEnvName,
|
||||||
|
isAllowedTaskEnvName,
|
||||||
|
} from '../shared/taskEnvName';
|
||||||
|
|
||||||
@Service()
|
@Service()
|
||||||
export default class EnvService {
|
export default class EnvService {
|
||||||
constructor(@Inject('logger') private logger: winston.Logger) { }
|
constructor(@Inject('logger') private logger: winston.Logger) { }
|
||||||
|
|
||||||
public async create(payloads: Env[]): Promise<Env[]> {
|
public async create(payloads: Env[]): Promise<Env[]> {
|
||||||
|
payloads.forEach((payload) => assertAllowedTaskEnvName(payload.name));
|
||||||
const envs = await this.envs();
|
const envs = await this.envs();
|
||||||
let position = initPosition;
|
let position = initPosition;
|
||||||
if (
|
if (
|
||||||
@@ -52,6 +57,7 @@ export default class EnvService {
|
|||||||
public async update(payload: Env): Promise<Env> {
|
public async update(payload: Env): Promise<Env> {
|
||||||
const doc = await this.getDb({ id: payload.id });
|
const doc = await this.getDb({ id: payload.id });
|
||||||
const tab = new Env({ ...doc, ...payload });
|
const tab = new Env({ ...doc, ...payload });
|
||||||
|
assertAllowedTaskEnvName(tab.name);
|
||||||
const newDoc = await this.updateDb(tab);
|
const newDoc = await this.updateDb(tab);
|
||||||
await this.set_envs();
|
await this.set_envs();
|
||||||
return newDoc;
|
return newDoc;
|
||||||
@@ -151,7 +157,10 @@ export default class EnvService {
|
|||||||
['position', 'DESC'],
|
['position', 'DESC'],
|
||||||
['createdAt', 'ASC'],
|
['createdAt', 'ASC'],
|
||||||
]);
|
]);
|
||||||
return result;
|
return result.map((env) => ({
|
||||||
|
...env,
|
||||||
|
securityBlocked: !isAllowedTaskEnvName(env.name),
|
||||||
|
}));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
@@ -182,11 +191,14 @@ export default class EnvService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async enabled(ids: number[]) {
|
public async enabled(ids: number[]) {
|
||||||
|
const docs = await EnvModel.findAll({ where: { id: ids } });
|
||||||
|
docs.forEach((doc) => assertAllowedTaskEnvName(doc.name));
|
||||||
await EnvModel.update({ status: EnvStatus.normal }, { where: { id: ids } });
|
await EnvModel.update({ status: EnvStatus.normal }, { where: { id: ids } });
|
||||||
await this.set_envs();
|
await this.set_envs();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async updateNames({ ids, name }: { ids: number[]; name: string }) {
|
public async updateNames({ ids, name }: { ids: number[]; name: string }) {
|
||||||
|
assertAllowedTaskEnvName(name);
|
||||||
await EnvModel.update({ name }, { where: { id: ids } });
|
await EnvModel.update({ name }, { where: { id: ids } });
|
||||||
await this.set_envs();
|
await this.set_envs();
|
||||||
}
|
}
|
||||||
@@ -242,6 +254,13 @@ export default class EnvService {
|
|||||||
name: { [Op.not]: null },
|
name: { [Op.not]: null },
|
||||||
status: EnvStatus.normal,
|
status: EnvStatus.normal,
|
||||||
});
|
});
|
||||||
|
const invalid = envs.filter((env) => !isAllowedTaskEnvName(env.name));
|
||||||
|
if (invalid.length > 0) {
|
||||||
|
this.logger.warn(
|
||||||
|
'Skipped reserved environment variables in task files: %s',
|
||||||
|
JSON.stringify(invalid.map((env) => ({ id: env.id, name: env.name }))),
|
||||||
|
);
|
||||||
|
}
|
||||||
const groups = groupBy(envs, 'name');
|
const groups = groupBy(envs, 'name');
|
||||||
let env_string = '';
|
let env_string = '';
|
||||||
let js_env_string = '';
|
let js_env_string = '';
|
||||||
@@ -250,8 +269,8 @@ export default class EnvService {
|
|||||||
if (Object.prototype.hasOwnProperty.call(groups, key)) {
|
if (Object.prototype.hasOwnProperty.call(groups, key)) {
|
||||||
const group = groups[key];
|
const group = groups[key];
|
||||||
|
|
||||||
// 忽略不符合bash要求的环境变量名称
|
// Ignore invalid and reserved names, including legacy database rows.
|
||||||
if (/^[a-zA-Z_][0-9a-zA-Z_]*$/.test(key)) {
|
if (isAllowedTaskEnvName(key)) {
|
||||||
let value = group
|
let value = group
|
||||||
.map((x) => x.value)
|
.map((x) => x.value)
|
||||||
.join('&')
|
.join('&')
|
||||||
|
|||||||
@@ -2,8 +2,7 @@ import { Server, ServerCredentials } from '@grpc/grpc-js';
|
|||||||
import { CronService } from '../protos/cron';
|
import { CronService } from '../protos/cron';
|
||||||
import { HealthService } from '../protos/health';
|
import { HealthService } from '../protos/health';
|
||||||
import { ApiService } from '../protos/api';
|
import { ApiService } from '../protos/api';
|
||||||
import { addCron } from '../schedule/addCron';
|
import { registeredAdd, registeredDelete } from '../schedule/registeredPlans';
|
||||||
import { delCron } from '../schedule/delCron';
|
|
||||||
import { setConcurrency } from '../schedule/setConcurrency';
|
import { setConcurrency } from '../schedule/setConcurrency';
|
||||||
import { check } from '../schedule/health';
|
import { check } from '../schedule/health';
|
||||||
import * as Api from '../schedule/api';
|
import * as Api from '../schedule/api';
|
||||||
@@ -28,7 +27,7 @@ export class GrpcServerService {
|
|||||||
async initialize() {
|
async initialize() {
|
||||||
try {
|
try {
|
||||||
this.server.addService(HealthService, { check });
|
this.server.addService(HealthService, { check });
|
||||||
this.server.addService(CronService, { addCron, delCron, setConcurrency });
|
this.server.addService(CronService, { addCron: registeredAdd, delCron: registeredDelete, setConcurrency });
|
||||||
this.server.addService(ApiService, Api);
|
this.server.addService(ApiService, Api);
|
||||||
|
|
||||||
const tlsConfig = await initGrpcCerts();
|
const tlsConfig = await initGrpcCerts();
|
||||||
|
|||||||
@@ -8,6 +8,6 @@ export default class LogService {
|
|||||||
constructor(@Inject('logger') private logger: winston.Logger) {}
|
constructor(@Inject('logger') private logger: winston.Logger) {}
|
||||||
|
|
||||||
public checkFilePath(filePath: string, fileName: string) {
|
public checkFilePath(filePath: string, fileName: string) {
|
||||||
return resolveFileAccess(config.logPath, [filePath || '', fileName]);
|
return resolveFileAccess(config.logPath, [filePath || '', fileName], ['.tmp']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,6 +92,10 @@ export default class OpenService {
|
|||||||
|
|
||||||
private async updateDb(payload: Partial<App>): Promise<App> {
|
private async updateDb(payload: Partial<App>): Promise<App> {
|
||||||
return this.withAppsLock(async () => {
|
return this.withAppsLock(async () => {
|
||||||
|
const target = await AppModel.findOne({ where: { id: payload.id } });
|
||||||
|
if (target?.name === 'system' || payload.name === 'system') {
|
||||||
|
throw Object.assign(new Error(t('暂无权限')), { status: 403 });
|
||||||
|
}
|
||||||
await AppModel.update(payload, { where: { id: payload.id } });
|
await AppModel.update(payload, { where: { id: payload.id } });
|
||||||
const apps = await this.find({});
|
const apps = await this.find({});
|
||||||
await shareStore.updateApps(apps);
|
await shareStore.updateApps(apps);
|
||||||
@@ -109,6 +113,12 @@ export default class OpenService {
|
|||||||
|
|
||||||
public async remove(ids: number[]) {
|
public async remove(ids: number[]) {
|
||||||
return this.withAppsLock(async () => {
|
return this.withAppsLock(async () => {
|
||||||
|
const systemApp = await AppModel.findOne({
|
||||||
|
where: { id: ids, name: 'system' },
|
||||||
|
});
|
||||||
|
if (systemApp) {
|
||||||
|
throw Object.assign(new Error(t('暂无权限')), { status: 403 });
|
||||||
|
}
|
||||||
await AppModel.destroy({ where: { id: ids } });
|
await AppModel.destroy({ where: { id: ids } });
|
||||||
const apps = await this.find({});
|
const apps = await this.find({});
|
||||||
await shareStore.updateApps(apps);
|
await shareStore.updateApps(apps);
|
||||||
|
|||||||
+53
-22
@@ -1,11 +1,13 @@
|
|||||||
import fs from 'fs/promises';
|
import fs from 'fs/promises';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import dayjs from 'dayjs';
|
import dayjs from 'dayjs';
|
||||||
import { Op } from 'sequelize';
|
import { literal, Op } from 'sequelize';
|
||||||
import { Service } from 'typedi';
|
import { Service } from 'typedi';
|
||||||
import config from '../config';
|
import config from '../config';
|
||||||
import { CrontabStatModel } from '../data/cronStats';
|
import { CrontabStatModel } from '../data/cronStats';
|
||||||
import { sequelize } from '../data';
|
import { sequelize } from '../data';
|
||||||
|
import { TriggerHistory } from '../shared/triggerHistory';
|
||||||
|
import { prunedExitMigrationId } from '../shared/schemaMigrations';
|
||||||
import { InstanceStatus, RunningInstanceModel } from '../data/runningInstance';
|
import { InstanceStatus, RunningInstanceModel } from '../data/runningInstance';
|
||||||
import { AuthDataType, SystemModel } from '../data/system';
|
import { AuthDataType, SystemModel } from '../data/system';
|
||||||
import {
|
import {
|
||||||
@@ -21,10 +23,12 @@ export interface StorageCleanupRequest extends RetentionPolicy {
|
|||||||
compactDatabase?: boolean;
|
compactDatabase?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
function runningInstanceWhere(days: number) {
|
function runningInstanceWhere(cutoffMs: number) {
|
||||||
const cutoff = dayjs().subtract(days, 'day').unix();
|
const cutoff = cutoffMs / 1000;
|
||||||
return {
|
return {
|
||||||
status: { [Op.ne]: InstanceStatus.running },
|
status: { [Op.ne]: InstanceStatus.running },
|
||||||
|
id: { [Op.notIn]: literal(`(SELECT instance_id FROM CronTriggers WHERE instance_id IS NOT NULL
|
||||||
|
AND (exit_pending=1 OR state IN ('queued','running') OR finished_at IS NULL OR observed_at >= ${cutoffMs} OR finished_at >= ${cutoffMs}))`) },
|
||||||
[Op.or]: [
|
[Op.or]: [
|
||||||
{ finished_at: { [Op.lt]: cutoff } },
|
{ finished_at: { [Op.lt]: cutoff } },
|
||||||
{
|
{
|
||||||
@@ -35,9 +39,11 @@ function runningInstanceWhere(days: number) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function cronStatWhere(days: number) {
|
function retentionCutoffs(policy: RetentionPolicy) {
|
||||||
|
const now = dayjs();
|
||||||
return {
|
return {
|
||||||
date: { [Op.lt]: dayjs().subtract(days, 'day').format('YYYY-MM-DD') },
|
history: now.subtract(policy.runningInstanceRetentionDays, 'day').startOf('second').valueOf(),
|
||||||
|
firstDay: now.subtract(policy.cronStatRetentionDays, 'day').format('YYYY-MM-DD'),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,20 +69,20 @@ export default class RetentionService {
|
|||||||
return normalized;
|
return normalized;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async preview(request: StorageCleanupRequest) {
|
public async preview(request: StorageCleanupRequest, cutoffs = retentionCutoffs(normalizeRetentionPolicy(request))) {
|
||||||
const policy = normalizeRetentionPolicy(request);
|
const policy = normalizeRetentionPolicy(request);
|
||||||
const dependenceCacheTypes = (request.dependenceCacheTypes || []).filter(
|
const dependenceCacheTypes = (request.dependenceCacheTypes || []).filter(
|
||||||
isDependenceCacheType,
|
isDependenceCacheType,
|
||||||
);
|
);
|
||||||
const [runningInstances, cronStats, dependenceCaches] = await Promise.all([
|
const [runningInstances, cronStats, dependenceCaches, triggers] = await Promise.all([
|
||||||
policy.runningInstanceRetentionDays > 0
|
policy.runningInstanceRetentionDays > 0
|
||||||
? RunningInstanceModel.count({
|
? RunningInstanceModel.count({
|
||||||
where: runningInstanceWhere(policy.runningInstanceRetentionDays),
|
where: runningInstanceWhere(cutoffs.history),
|
||||||
})
|
})
|
||||||
: 0,
|
: 0,
|
||||||
policy.cronStatRetentionDays > 0
|
policy.cronStatRetentionDays > 0
|
||||||
? CrontabStatModel.count({
|
? CrontabStatModel.count({
|
||||||
where: cronStatWhere(policy.cronStatRetentionDays),
|
where: { date: { [Op.lt]: cutoffs.firstDay } },
|
||||||
})
|
})
|
||||||
: 0,
|
: 0,
|
||||||
Promise.all(
|
Promise.all(
|
||||||
@@ -87,11 +93,15 @@ export default class RetentionService {
|
|||||||
),
|
),
|
||||||
})),
|
})),
|
||||||
),
|
),
|
||||||
|
policy.runningInstanceRetentionDays > 0
|
||||||
|
? new TriggerHistory(sequelize).countExpired(cutoffs.history)
|
||||||
|
: 0,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
policy,
|
policy,
|
||||||
runningInstances,
|
runningInstances,
|
||||||
|
triggers,
|
||||||
cronStats,
|
cronStats,
|
||||||
dependenceCaches,
|
dependenceCaches,
|
||||||
dependenceCacheBytes: dependenceCaches.reduce(
|
dependenceCacheBytes: dependenceCaches.reduce(
|
||||||
@@ -103,25 +113,46 @@ export default class RetentionService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async cleanup(request: StorageCleanupRequest) {
|
public async cleanup(request: StorageCleanupRequest) {
|
||||||
const preview = await this.preview(request);
|
const cutoffs = retentionCutoffs(normalizeRetentionPolicy(request));
|
||||||
|
const preview = await this.preview(request, cutoffs);
|
||||||
const deleted = await sequelize.transaction(async (transaction) => {
|
const deleted = await sequelize.transaction(async (transaction) => {
|
||||||
|
const history = new TriggerHistory(sequelize);
|
||||||
|
if (preview.policy.runningInstanceRetentionDays || preview.policy.cronStatRetentionDays) {
|
||||||
|
await history.replayPublishedExits(transaction);
|
||||||
|
}
|
||||||
const runningInstances =
|
const runningInstances =
|
||||||
preview.policy.runningInstanceRetentionDays > 0
|
preview.policy.runningInstanceRetentionDays > 0
|
||||||
? await RunningInstanceModel.destroy({
|
? await RunningInstanceModel.destroy({
|
||||||
where: runningInstanceWhere(
|
where: runningInstanceWhere(cutoffs.history),
|
||||||
preview.policy.runningInstanceRetentionDays,
|
|
||||||
),
|
|
||||||
transaction,
|
transaction,
|
||||||
})
|
})
|
||||||
: 0;
|
: 0;
|
||||||
const cronStats =
|
let cronStats = 0;
|
||||||
preview.policy.cronStatRetentionDays > 0
|
if (preview.policy.cronStatRetentionDays > 0) {
|
||||||
? await CrontabStatModel.destroy({
|
const firstDay = cutoffs.firstDay;
|
||||||
where: cronStatWhere(preview.policy.cronStatRetentionDays),
|
cronStats = await CrontabStatModel.destroy({ where: { date: { [Op.lt]: firstDay } }, transaction });
|
||||||
transaction,
|
await sequelize.query(`INSERT INTO CronStatRetention(id,first_day) VALUES (1,$firstDay)
|
||||||
})
|
ON CONFLICT(id) DO UPDATE SET first_day=MAX(first_day,excluded.first_day)`,
|
||||||
|
{ bind: { firstDay }, transaction });
|
||||||
|
await sequelize.query(`DELETE FROM CronPrunedUnknownRuns WHERE last_possible_day <
|
||||||
|
(SELECT first_day FROM CronStatRetention WHERE id=1)`, { transaction });
|
||||||
|
}
|
||||||
|
const triggers = preview.policy.runningInstanceRetentionDays > 0
|
||||||
|
? await history.deleteExpired(cutoffs.history, transaction)
|
||||||
: 0;
|
: 0;
|
||||||
return { runningInstances, cronStats };
|
if (preview.policy.runningInstanceRetentionDays || preview.policy.cronStatRetentionDays) {
|
||||||
|
// An unbound receipt's day can be wrong. A later bound receipt still
|
||||||
|
// needs the execution-to-task link even after both visible histories expire.
|
||||||
|
// A writer can also win the lock before an interrupted upgrade retries.
|
||||||
|
// Keep its bound bookings until the proof-copy transaction has committed.
|
||||||
|
await sequelize.query(`DELETE FROM CronLegacyExitBookings WHERE day <
|
||||||
|
(SELECT first_day FROM CronStatRetention WHERE id=1)
|
||||||
|
AND NOT EXISTS (SELECT 1 FROM CronTriggers WHERE id=CronLegacyExitBookings.trigger_id)
|
||||||
|
AND ((bound=1 AND EXISTS (SELECT 1 FROM SchemaMigrations WHERE id=$proofMigration))
|
||||||
|
OR (bound=0 AND NOT EXISTS (SELECT 1 FROM Crontabs WHERE id=CronLegacyExitBookings.cron_id)))`,
|
||||||
|
{ bind: { proofMigration: prunedExitMigrationId }, transaction });
|
||||||
|
}
|
||||||
|
return { runningInstances, cronStats, triggers };
|
||||||
});
|
});
|
||||||
|
|
||||||
const dependenceCaches = [];
|
const dependenceCaches = [];
|
||||||
@@ -135,7 +166,7 @@ export default class RetentionService {
|
|||||||
|
|
||||||
if (
|
if (
|
||||||
request.compactDatabase &&
|
request.compactDatabase &&
|
||||||
(deleted.runningInstances || deleted.cronStats)
|
(deleted.runningInstances || deleted.cronStats || deleted.triggers)
|
||||||
) {
|
) {
|
||||||
await sequelize.query('VACUUM');
|
await sequelize.query('VACUUM');
|
||||||
}
|
}
|
||||||
@@ -148,7 +179,7 @@ export default class RetentionService {
|
|||||||
},
|
},
|
||||||
compactedDatabase: Boolean(
|
compactedDatabase: Boolean(
|
||||||
request.compactDatabase &&
|
request.compactDatabase &&
|
||||||
(deleted.runningInstances || deleted.cronStats),
|
(deleted.runningInstances || deleted.cronStats || deleted.triggers),
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,6 +58,10 @@ export default class SystemService {
|
|||||||
private sockService: SockService,
|
private sockService: SockService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
public get cronConcurrency() {
|
||||||
|
return taskLimit.cronConcurrency;
|
||||||
|
}
|
||||||
|
|
||||||
public async getSystemConfig() {
|
public async getSystemConfig() {
|
||||||
const doc = await this.getDb({ type: AuthDataType.systemConfig });
|
const doc = await this.getDb({ type: AuthDataType.systemConfig });
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -0,0 +1,62 @@
|
|||||||
|
import os from 'os';
|
||||||
|
import { readFileSync } from 'fs';
|
||||||
|
import path from 'path';
|
||||||
|
|
||||||
|
function read(file: string): string {
|
||||||
|
try { return readFileSync(file, 'utf8').trim(); } catch { return ''; }
|
||||||
|
}
|
||||||
|
|
||||||
|
// Walk ancestors too: a nested cgroup can inherit a tighter parent quota.
|
||||||
|
export function containerResources(readValue = read, platform = process.platform) {
|
||||||
|
const hostCpus = os.cpus().length;
|
||||||
|
const hostMemory = os.totalmem();
|
||||||
|
let cpus = hostCpus;
|
||||||
|
let memTotal = hostMemory;
|
||||||
|
let memFree = os.freemem();
|
||||||
|
if (platform === 'linux') {
|
||||||
|
const groups = readValue('/proc/self/cgroup').split('\n');
|
||||||
|
for (const line of groups) {
|
||||||
|
const match = /^\d+:([^:]*):(\/.*)$/.exec(line);
|
||||||
|
if (!match) continue;
|
||||||
|
const controllers = match[1].split(',');
|
||||||
|
const unified = !match[1];
|
||||||
|
for (const kind of ['cpu', 'memory', 'cpuset']) {
|
||||||
|
if (!unified && !controllers.includes(kind)) continue;
|
||||||
|
const roots = unified ? ['/sys/fs/cgroup'] : [...new Set([kind, match[1]].map(k => '/sys/fs/cgroup/' + k))];
|
||||||
|
for (const root of roots) {
|
||||||
|
let dir = path.join(root, path.normalize(match[2]));
|
||||||
|
while (dir === root || dir.startsWith(root + '/')) {
|
||||||
|
const value = (file: string) => readValue(path.join(dir, file));
|
||||||
|
if (kind === 'cpu') {
|
||||||
|
const [quota, period] = (unified ? value('cpu.max').split(/\s+/) :
|
||||||
|
[value('cpu.cfs_quota_us'), value('cpu.cfs_period_us')]).map(Number);
|
||||||
|
if (quota > 0 && period > 0) cpus = Math.min(cpus, quota / period);
|
||||||
|
} else if (kind === 'cpuset') {
|
||||||
|
const set = value(unified ? 'cpuset.cpus.effective' : 'cpuset.cpus');
|
||||||
|
if (/^\d+(?:-\d+)?(?:,\d+(?:-\d+)?)*$/.test(set)) {
|
||||||
|
const count = set.split(',').reduce((n, range) => {
|
||||||
|
const [lo, hi] = range.split('-').map(Number);
|
||||||
|
return n + (hi ?? lo) - lo + 1;
|
||||||
|
}, 0);
|
||||||
|
if (count > 0) cpus = Math.min(cpus, count);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const limit = Number(value(unified ? 'memory.max' : 'memory.limit_in_bytes'));
|
||||||
|
if (limit > 0 && limit <= memTotal) {
|
||||||
|
const usage = value(unified ? 'memory.current' : 'memory.usage_in_bytes');
|
||||||
|
if (usage && Number.isFinite(Number(usage))) {
|
||||||
|
memTotal = limit;
|
||||||
|
memFree = Math.max(0, limit - Number(usage));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (dir === root) break;
|
||||||
|
dir = path.dirname(dir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return { cpus, memTotal, memFree, hostCpus, hostMemory,
|
||||||
|
memUsagePercent: ((1 - memFree / memTotal) * 100).toFixed(1) };
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
import { join } from 'path';
|
||||||
|
import { existsSync } from 'fs';
|
||||||
|
import { receiptPath } from './managedExit.cjs';
|
||||||
|
import { spawn } from 'cross-spawn';
|
||||||
|
import type { ChildProcessWithoutNullStreams } from 'child_process';
|
||||||
|
import type { Readable } from 'stream';
|
||||||
|
|
||||||
|
// A managed command waits on its parent's pipe. EOF before authorization exits
|
||||||
|
// without interpreting the command, even when the worker is killed by SIGKILL.
|
||||||
|
export function spawnCronCommand(command: string, triggerId?: string, onCommandStart?: (at: number) => void) {
|
||||||
|
if (!triggerId) return spawn(command, { shell: '/bin/bash' });
|
||||||
|
const sourceScript = join(__dirname, '../../shell/cron-command.py');
|
||||||
|
const supervisor = existsSync(sourceScript) ? sourceScript : join(process.env.QL_DIR!, 'shell/cron-command.py');
|
||||||
|
const args = ['-I', '-S', supervisor, command, String(process.pid),
|
||||||
|
receiptPath(triggerId) || '', process.execPath, join(__dirname, 'managedExit.cjs'),
|
||||||
|
];
|
||||||
|
if (onCommandStart) args.push('report-start');
|
||||||
|
const child = spawn('python3', args, { env: { ...process.env, QL_TRIGGER_ID: triggerId },
|
||||||
|
stdio: onCommandStart ? ['pipe', 'pipe', 'pipe', 'pipe'] : 'pipe' }) as ChildProcessWithoutNullStreams;
|
||||||
|
if (onCommandStart && child.stdio[3]) {
|
||||||
|
const timing = child.stdio[3] as Readable;
|
||||||
|
let stamp = '';
|
||||||
|
timing.setEncoding('ascii').on('data', chunk => { stamp += chunk; });
|
||||||
|
timing.once('end', () => { if (/^\d{13}$/.test(stamp)) onCommandStart(Number(stamp)); });
|
||||||
|
timing.on('error', () => {}); // Missing telemetry must not change the command result.
|
||||||
|
}
|
||||||
|
// A concurrent stop can close the pipe before the authorization write.
|
||||||
|
child.stdin.on('error', () => {});
|
||||||
|
return child;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function releaseCronCommand(child: ChildProcessWithoutNullStreams): Promise<void> {
|
||||||
|
return new Promise((resolve, reject) =>
|
||||||
|
child.stdin.end('run\n', (error?: Error) => error ? reject(error) : resolve()));
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
// Read literal shell words only. Never expand or execute a command to derive a
|
||||||
|
// path. Adjacent quoted/unquoted fragments belong to the same argument.
|
||||||
|
function words(command: string): string[] {
|
||||||
|
const result: string[] = [];
|
||||||
|
let value = '', quote = '', active = false;
|
||||||
|
for (let i = 0; i < command.length; i++) {
|
||||||
|
const c = command[i];
|
||||||
|
if (quote && c === quote) { quote = ''; continue; }
|
||||||
|
if (!quote && (c === "'" || c === '"')) { quote = c; active = true; continue; }
|
||||||
|
if (c === '\\' && quote !== "'") {
|
||||||
|
const next = command[++i];
|
||||||
|
if (next === undefined) return [];
|
||||||
|
if (quote === '"' && !/[$`"\\\n]/.test(next)) value += '\\';
|
||||||
|
if (next !== '\n') { value += next; active = true; }
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!quote && /[;&|<>()\n]/.test(c)) break;
|
||||||
|
if (!quote && /\s/.test(c)) {
|
||||||
|
if (active) result.push(value);
|
||||||
|
value = ''; active = false; continue;
|
||||||
|
}
|
||||||
|
if (!active && !quote && c === '#') break;
|
||||||
|
value += c; active = true;
|
||||||
|
}
|
||||||
|
if (quote) return [];
|
||||||
|
if (active) result.push(value);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function cronOperand(command: string, taskOnly = false): string | undefined {
|
||||||
|
const args = words(command.trim());
|
||||||
|
if (args[0] === 'task') args.shift();
|
||||||
|
else if (taskOnly && args[0] === 'ql') return;
|
||||||
|
while (args[0] === '-l' || args[0] === '-m') {
|
||||||
|
if (args.shift() === '-m') args.shift();
|
||||||
|
}
|
||||||
|
if (args.length > 1 && /^(?:.*\/)?(?:node|python3?|bash|sh|ts-node(?:-transpile-only)?)$/.test(args[0])) args.shift();
|
||||||
|
return args[0];
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
import { upcomingSlots } from './cronScheduler';
|
||||||
|
|
||||||
|
type Definition = {
|
||||||
|
schedule?: string;
|
||||||
|
extra_schedules?: { schedule: string }[];
|
||||||
|
isDisabled?: number;
|
||||||
|
saved?: boolean;
|
||||||
|
};
|
||||||
|
const cache = new Map<string, { after: number; times: number[] }>();
|
||||||
|
|
||||||
|
export function previewSchedule(
|
||||||
|
schedule: string,
|
||||||
|
count: number,
|
||||||
|
now: number,
|
||||||
|
): number[] {
|
||||||
|
const key = JSON.stringify([
|
||||||
|
Intl.DateTimeFormat().resolvedOptions().timeZone,
|
||||||
|
schedule,
|
||||||
|
]);
|
||||||
|
const cached = cache.get(key);
|
||||||
|
if (cached && now >= cached.after) {
|
||||||
|
const future = cached.times.filter((time) => time > now);
|
||||||
|
if (future.length >= count) return future.slice(0, count);
|
||||||
|
}
|
||||||
|
const times = upcomingSlots(schedule, now, count);
|
||||||
|
if (cache.size >= 512) cache.delete(cache.keys().next().value!);
|
||||||
|
cache.set(key, { after: now, times });
|
||||||
|
return times;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function withNextRun<T extends Definition>(cron: T) {
|
||||||
|
let nextRunTime: number | null = null;
|
||||||
|
if (
|
||||||
|
cron.schedule &&
|
||||||
|
!cron.isDisabled &&
|
||||||
|
cron.saved !== false &&
|
||||||
|
!cron.schedule.startsWith('@once') &&
|
||||||
|
!cron.schedule.startsWith('@boot')
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
const now = Date.now();
|
||||||
|
nextRunTime = Math.min(
|
||||||
|
...[
|
||||||
|
cron.schedule,
|
||||||
|
...(cron.extra_schedules || []).map((x) => x.schedule),
|
||||||
|
].map((rule) => previewSchedule(rule, 1, now)[0]),
|
||||||
|
);
|
||||||
|
} catch {
|
||||||
|
// Legacy invalid rows remain visible and editable.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return { ...cron, nextRunTime };
|
||||||
|
}
|
||||||
@@ -0,0 +1,172 @@
|
|||||||
|
import { readFileSync } from 'fs';
|
||||||
|
import { execFileSync, spawnSync } from 'child_process';
|
||||||
|
|
||||||
|
// Legacy runs can outlive a scheduler restart without an active trigger receipt.
|
||||||
|
export function hasLiveCronProcess(status?: number, pid?: number | null) {
|
||||||
|
if (status !== 0 || !pid || pid < 0) return false; // CrontabStatus.running
|
||||||
|
try {
|
||||||
|
process.kill(pid, 0);
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
const code = (error as NodeJS.ErrnoException).code;
|
||||||
|
if (code === 'ESRCH') return false;
|
||||||
|
if (code === 'EPERM') return true;
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function readBsdProcessField(pid: number, field: 'lstart' | 'ppid') {
|
||||||
|
const result = spawnSync('ps', ['-p', String(pid), '-o', field + '='], {
|
||||||
|
encoding: 'utf8', env: { ...process.env, LC_ALL: 'C', TZ: 'UTC' },
|
||||||
|
});
|
||||||
|
// ENOENT from spawning ps refers to the executable, not the target PID.
|
||||||
|
if (result.error) throw new Error('Cannot run ps: ' + result.error.message, { cause: result.error });
|
||||||
|
const value = result.stdout.trim();
|
||||||
|
if (result.status === 0 && value) return value;
|
||||||
|
// BSD ps exits 1 without output when the selected PID has already exited.
|
||||||
|
if (result.status === 1 && !value && !result.stderr.trim())
|
||||||
|
throw Object.assign(new Error('Process has exited'), { code: 'ESRCH' });
|
||||||
|
throw new Error(`Cannot read ${field} for PID ${pid}: ${result.stderr.trim() || result.signal || 'ps status ' + result.status}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function systemOwner(pid: number) {
|
||||||
|
if (process.platform !== 'linux') {
|
||||||
|
// BSD crontab installations have no procfs. ps exposes the process birth time.
|
||||||
|
process.kill(pid, 0);
|
||||||
|
const birth = readBsdProcessField(pid, 'lstart');
|
||||||
|
return `crond:${pid}:${birth}`;
|
||||||
|
}
|
||||||
|
const stat = readFileSync(`/proc/${pid}/stat`, 'utf8');
|
||||||
|
const fields = stat.slice(stat.lastIndexOf(')') + 2).split(' ');
|
||||||
|
if (['Z', 'X', 'x'].includes(fields[0])) throw Object.assign(new Error('Process has exited'), { code: 'ESRCH' });
|
||||||
|
const startTicks = fields[19];
|
||||||
|
const boot = readFileSync('/proc/sys/kernel/random/boot_id', 'utf8').trim();
|
||||||
|
return `crond:${pid}:${startTicks}:${boot}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
let linuxClockTicks: number | undefined;
|
||||||
|
|
||||||
|
// A start packet may arrive after its PID was recycled, before the replacement reports.
|
||||||
|
export function legacyProcessOwner(pid: number, started: number): string | undefined {
|
||||||
|
try {
|
||||||
|
const identity = systemOwner(pid);
|
||||||
|
const birth = identity.slice(`crond:${pid}:`.length);
|
||||||
|
let bornAt: number;
|
||||||
|
if (process.platform === 'linux') {
|
||||||
|
linuxClockTicks ??= Number(execFileSync('getconf', ['CLK_TCK'], { encoding: 'utf8' }));
|
||||||
|
const boot = /^btime (\d+)$/m.exec(readFileSync('/proc/stat', 'utf8'));
|
||||||
|
if (!boot || !(linuxClockTicks > 0)) throw new Error('Cannot read process birth time');
|
||||||
|
bornAt = Number(boot[1]) + Number(birth.split(':')[0]) / linuxClockTicks;
|
||||||
|
} else {
|
||||||
|
bornAt = Date.parse(birth + ' UTC') / 1000;
|
||||||
|
}
|
||||||
|
// Both the callback and Linux boot time have one-second precision.
|
||||||
|
return Math.floor(bornAt) <= started + 1 ? identity : undefined;
|
||||||
|
} catch (error) {
|
||||||
|
if (['ESRCH', 'ENOENT'].includes((error as NodeJS.ErrnoException).code!)) return undefined;
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// PID 1 survives worker restarts, but changes when a container or Linux host restarts.
|
||||||
|
export function pidNamespaceOwner() {
|
||||||
|
return process.platform === 'linux' ? systemOwner(1) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// psTree enumerates asynchronously. Reject a PID already reparented or recycled
|
||||||
|
// between enumeration and the birth-token snapshot.
|
||||||
|
export function isProcessDescendant(pid: number, root: number) {
|
||||||
|
const seen = new Set<number>();
|
||||||
|
while (pid > 1 && !seen.has(pid)) {
|
||||||
|
if (pid === root) return true;
|
||||||
|
seen.add(pid);
|
||||||
|
if (process.platform === 'linux') {
|
||||||
|
const stat = readFileSync(`/proc/${pid}/stat`, 'utf8');
|
||||||
|
pid = Number(stat.slice(stat.lastIndexOf(')') + 2).split(' ')[1]);
|
||||||
|
} else {
|
||||||
|
pid = Number(readBsdProcessField(pid, 'ppid'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// For an individual legacy instance the task ID alone also matches its siblings.
|
||||||
|
export function verifyCronProcess(pid: number, cronId: number, identity?: string | null, started?: number,
|
||||||
|
instance?: { logPath?: string }) {
|
||||||
|
const conflict = () => Object.assign(new Error('Task process identity could not be verified'), { status: 409 });
|
||||||
|
if (!Number.isInteger(pid) || pid <= 1) throw conflict();
|
||||||
|
if (identity) {
|
||||||
|
// A birth token for this PID proves the original process is gone if the
|
||||||
|
// kernel now reports a different birth. Access errors remain retryable.
|
||||||
|
if (!identity.startsWith(`crond:${pid}:`)) throw conflict();
|
||||||
|
let current: string;
|
||||||
|
try { current = systemOwner(pid); }
|
||||||
|
catch (error) {
|
||||||
|
if ((error as NodeJS.ErrnoException).code === 'ENOENT')
|
||||||
|
throw Object.assign(new Error('Command already exited'), { code: 'ESRCH' });
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
if (current !== identity) throw Object.assign(new Error('Command PID was reused'), { code: 'PID_REUSED', status: 409 });
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (process.platform === 'linux') {
|
||||||
|
const env = readFileSync(`/proc/${pid}/environ`, 'utf8').split('\0');
|
||||||
|
if (instance && (!instance.logPath || instance.logPath === '/dev/null' ||
|
||||||
|
!env.includes('real_log_path=' + instance.logPath))) throw conflict();
|
||||||
|
if (env.includes('ID=' + cronId) && started) {
|
||||||
|
const boot = /^btime (\d+)$/m.exec(readFileSync('/proc/stat', 'utf8'));
|
||||||
|
const birth = systemOwner(pid);
|
||||||
|
const ticks = Number(birth.split(':')[2]);
|
||||||
|
const hz = Number(execFileSync('getconf', ['CLK_TCK'], { encoding: 'utf8' }));
|
||||||
|
if (boot && hz > 0 && Math.floor(Number(boot[1]) + ticks / hz) <= started + 1) return birth;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
if (['ENOENT', 'ESRCH'].includes((error as NodeJS.ErrnoException).code!)) {
|
||||||
|
// Distinguish a completed child whose receipt is still being written
|
||||||
|
// from a live PID with an unverifiable identity.
|
||||||
|
try { systemOwner(pid); } catch (probe) {
|
||||||
|
if (['ENOENT', 'ESRCH'].includes((probe as NodeJS.ErrnoException).code!))
|
||||||
|
throw Object.assign(new Error('Command already exited'), { code: 'ESRCH' });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw conflict();
|
||||||
|
}
|
||||||
|
|
||||||
|
// After a stop worker exits, descendants may already have been adopted by PID 1.
|
||||||
|
// A saved birth identity still identifies them; walking the dead root no longer can.
|
||||||
|
export async function finishStoppedTree(identities: string[], signalledAt?: number | null) {
|
||||||
|
const surviving = () => identities.flatMap(identity => {
|
||||||
|
const pid = Number(identity.split(':')[1]);
|
||||||
|
if (!Number.isSafeInteger(pid) || pid <= 1) throw new Error('Invalid saved process identity');
|
||||||
|
try { return systemOwner(pid) === identity ? [pid] : []; }
|
||||||
|
catch (error) {
|
||||||
|
if (['ESRCH', 'ENOENT'].includes((error as NodeJS.ErrnoException).code!)) return [];
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
const remainingGrace = Math.min(1000, Math.max(0, (signalledAt ?? Date.now()) + 1000 - Date.now()));
|
||||||
|
const graceDeadline = Date.now() + remainingGrace;
|
||||||
|
while (surviving().length) {
|
||||||
|
const remaining = graceDeadline - Date.now();
|
||||||
|
if (remaining <= 0) break;
|
||||||
|
await new Promise(resolve => setTimeout(resolve, Math.min(20, remaining)));
|
||||||
|
}
|
||||||
|
if (!surviving().length) return;
|
||||||
|
// Revalidate each PID immediately before signalling, including after the grace period.
|
||||||
|
for (const identity of identities) {
|
||||||
|
const pid = Number(identity.split(':')[1]);
|
||||||
|
try {
|
||||||
|
if (systemOwner(pid) === identity) process.kill(pid, 'SIGKILL');
|
||||||
|
} catch (error) {
|
||||||
|
if (!['ESRCH', 'ENOENT'].includes((error as NodeJS.ErrnoException).code!)) throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const deadline = Date.now() + 1000;
|
||||||
|
while (surviving().length) {
|
||||||
|
if (Date.now() >= deadline) throw new Error('Stopped process tree did not exit');
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 20));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import { upcomingSlots } from './cronScheduler';
|
||||||
|
|
||||||
|
// Find only the latest eligible slot even for a second-level rule after hours
|
||||||
|
// offline. Search by time, so cost is logarithmic in the configured window.
|
||||||
|
export function latestMissedSlot(
|
||||||
|
schedule: string,
|
||||||
|
since: number,
|
||||||
|
until: number,
|
||||||
|
): number | null {
|
||||||
|
let low = Math.floor(since / 1000);
|
||||||
|
let high = Math.floor(until / 1000);
|
||||||
|
let latest: number | null = null;
|
||||||
|
while (low < high) {
|
||||||
|
const middle = Math.floor((low + high) / 2);
|
||||||
|
const [next] = upcomingSlots(schedule, middle * 1000);
|
||||||
|
if (next > until) high = middle;
|
||||||
|
else {
|
||||||
|
latest = next;
|
||||||
|
low = next / 1000;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return latest;
|
||||||
|
}
|
||||||
@@ -12,6 +12,17 @@ export interface CronJob {
|
|||||||
cancel(): void;
|
cancel(): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function acceptsSlot(parsed: ReturnType<typeof parseCronSchedule>, tasks: ScheduledTask[], index: number, date: Date) {
|
||||||
|
if (tasks.length === 1) return true;
|
||||||
|
// Preserve cron-parser 4's day/weekday OR semantics, including its
|
||||||
|
// month-dependent full-day range and the global nth-week constraint.
|
||||||
|
if (parsed.nthDay && Math.ceil(date.getDate() / 7) !== parsed.nthDay)
|
||||||
|
return false;
|
||||||
|
if (index === 1) return !tasks[0].match(date);
|
||||||
|
const monthDays = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
||||||
|
return parsed.dayCount < monthDays[date.getMonth()] || tasks[1].match(date);
|
||||||
|
}
|
||||||
|
|
||||||
// node-cron owns timers and calendar calculation; Qinglong owns execution
|
// node-cron owns timers and calendar calculation; Qinglong owns execution
|
||||||
// concurrency and process lifetime. Missed in-process slots join that same queue.
|
// concurrency and process lifetime. Missed in-process slots join that same queue.
|
||||||
export function createCronJob(
|
export function createCronJob(
|
||||||
@@ -23,18 +34,8 @@ export function createCronJob(
|
|||||||
const tasks: ScheduledTask[] = [];
|
const tasks: ScheduledTask[] = [];
|
||||||
let cancelled = false;
|
let cancelled = false;
|
||||||
let started = false;
|
let started = false;
|
||||||
const accepts = (index: number, date: Date) => {
|
|
||||||
if (tasks.length === 1) return true;
|
|
||||||
// Preserve cron-parser 4's day/weekday OR semantics, including its
|
|
||||||
// month-dependent full-day range and the global nth-week constraint.
|
|
||||||
if (parsed.nthDay && Math.ceil(date.getDate() / 7) !== parsed.nthDay)
|
|
||||||
return false;
|
|
||||||
if (index === 1) return !tasks[0].match(date);
|
|
||||||
const monthDays = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
|
||||||
return parsed.dayCount < monthDays[date.getMonth()] || tasks[1].match(date);
|
|
||||||
};
|
|
||||||
const dispatch = async (index: number, date: Date, missed: boolean) => {
|
const dispatch = async (index: number, date: Date, missed: boolean) => {
|
||||||
if (cancelled || !started || !accepts(index, date)) return;
|
if (cancelled || !started || !acceptsSlot(parsed, tasks, index, date)) return;
|
||||||
if (missed) {
|
if (missed) {
|
||||||
options.logger.warn(
|
options.logger.warn(
|
||||||
t('[schedule][补执行迟到任务] 任务: %s, 计划时间: %s'),
|
t('[schedule][补执行迟到任务] 任务: %s, 计划时间: %s'),
|
||||||
@@ -88,3 +89,54 @@ export function createCronJob(
|
|||||||
if (options.start !== false) job.start();
|
if (options.start !== false) job.start();
|
||||||
return job;
|
return job;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// node-cron 4.6.0 exposes getNextRuns only from the wall clock. Its pinned
|
||||||
|
// inline matcher also accepts an explicit cursor, needed for previews and recovery.
|
||||||
|
export function upcomingSlots(schedule: string, after: number, count = 1, through?: number): number[] {
|
||||||
|
const parsed = parseCronSchedule(schedule);
|
||||||
|
const tasks: ScheduledTask[] = [];
|
||||||
|
try {
|
||||||
|
const matchers = parsed.patterns.map((pattern) => {
|
||||||
|
const task = createTask(pattern, () => {});
|
||||||
|
tasks.push(task);
|
||||||
|
return (task as ScheduledTask & {
|
||||||
|
timeMatcher: { getNextMatch(date: Date): Date; expressions: number[][] };
|
||||||
|
}).timeMatcher;
|
||||||
|
});
|
||||||
|
const next = matchers.map((matcher) => matcher.getNextMatch(new Date(after)));
|
||||||
|
// All calendar branches share their clock fields. For bounded audit ranges,
|
||||||
|
// resolve each matching minute once, including the matcher's DST choice.
|
||||||
|
const seconds = through == null ? [] : [...new Set(matchers[0].expressions[0])].sort((a,b) => a-b);
|
||||||
|
const slots: number[] = [];
|
||||||
|
let horizon = after + 366 * 100 * 86400000;
|
||||||
|
while (slots.length < count) {
|
||||||
|
const time = Math.min(...next.map(Number));
|
||||||
|
if (through != null && time > through) break;
|
||||||
|
if (!Number.isFinite(time) || time > horizon) throw new Error('No matching calendar date');
|
||||||
|
let accepted = false;
|
||||||
|
next.forEach((date, index) => {
|
||||||
|
if (+date !== time) return;
|
||||||
|
const valid = acceptsSlot(parsed, tasks, index, date);
|
||||||
|
accepted ||= valid;
|
||||||
|
// Both branches share the clock fields. Rejection applies to the
|
||||||
|
// entire local day, including schedules that fire every second.
|
||||||
|
const cursor = new Date(time);
|
||||||
|
if (!valid) cursor.setHours(23, 59, 59, 999);
|
||||||
|
else if (through != null) cursor.setTime(time - time % 60000 + 59999);
|
||||||
|
next[index] = matchers[index].getNextMatch(cursor);
|
||||||
|
});
|
||||||
|
if (accepted) {
|
||||||
|
if (through == null) slots.push(time);
|
||||||
|
else for (const second of seconds) {
|
||||||
|
const slot = time - time % 60000 + second * 1000;
|
||||||
|
if (slot >= time && slot <= through && slots.length < count) slots.push(slot);
|
||||||
|
}
|
||||||
|
horizon = time + 366 * 100 * 86400000;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return slots;
|
||||||
|
} finally {
|
||||||
|
tasks.forEach((task) => task.destroy());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,57 @@
|
|||||||
|
import dayjs from 'dayjs';
|
||||||
|
import { QueryTypes, Sequelize, Transaction } from 'sequelize';
|
||||||
|
|
||||||
|
// Hold the receipt claim and the daily increment in the same write transaction.
|
||||||
|
export async function recordCronResult(
|
||||||
|
database: Sequelize, cronId: number, _code: number, elapsedMs: number,
|
||||||
|
finishedAt: number, triggerId: string, transaction?: Transaction,
|
||||||
|
): Promise<void> {
|
||||||
|
if (!triggerId) throw new Error('Execution identity required');
|
||||||
|
if (!transaction) {
|
||||||
|
return database.transaction((tx) =>
|
||||||
|
recordCronResult(database, cronId, _code, elapsedMs, finishedAt, triggerId, tx));
|
||||||
|
}
|
||||||
|
const bind = { ref: cronId, id: triggerId };
|
||||||
|
const claimed = await database.query(
|
||||||
|
"UPDATE CronTriggers SET counted=1,exit_pending=NULL WHERE cron_id=$ref AND id=$id AND counted=0 AND started_at IS NOT NULL AND state IN ('finished','failed','stopped')",
|
||||||
|
{ bind, type: QueryTypes.BULKUPDATE, transaction },
|
||||||
|
);
|
||||||
|
if (!claimed) {
|
||||||
|
const rows = await database.query('SELECT 1 FROM CronTriggers WHERE cron_id=$ref AND id=$id',
|
||||||
|
{ bind, type: QueryTypes.SELECT, transaction });
|
||||||
|
if (!rows.length) throw new Error('Unknown trigger');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const [receipt] = await database.query<{ state: string; finished_at: number; reversible: number }>(
|
||||||
|
`SELECT state,finished_at,EXISTS(SELECT 1 FROM CronLegacyExitBookings WHERE trigger_id=$id) AS reversible
|
||||||
|
FROM CronTriggers WHERE cron_id=$ref AND id=$id`,
|
||||||
|
{ bind, type: QueryTypes.SELECT, transaction });
|
||||||
|
const day = dayjs(receipt.finished_at ?? finishedAt).format('YYYY-MM-DD');
|
||||||
|
await addDailyExit(database, cronId, day, receipt.state, elapsedMs, Boolean(receipt.reversible), transaction);
|
||||||
|
if (receipt.reversible) {
|
||||||
|
await database.query(`UPDATE CronLegacyExitBookings SET stopped=$stopped,
|
||||||
|
stat_id=(SELECT id FROM CrontabStats WHERE ref_id=$ref AND date=$day
|
||||||
|
AND NOT EXISTS (SELECT 1 FROM CronStatRetention WHERE id=1 AND first_day>$day))
|
||||||
|
WHERE trigger_id=$id`, { bind: { ...bind, day, stopped: Number(receipt.state === 'stopped') }, transaction });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function addDailyExit(database: Sequelize, cronId: number, day: string, state: string,
|
||||||
|
elapsedMs: number, reversible: boolean, transaction: Transaction) {
|
||||||
|
await database.query(
|
||||||
|
`INSERT INTO "CrontabStats"
|
||||||
|
(ref_id, date, run_count, success_count, fail_count, total_time, max_time, fixed_max_time, createdAt, updatedAt)
|
||||||
|
SELECT $id, $date, 1, $success, $fail, $elapsed, $elapsed, $fixedMax, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
|
||||||
|
WHERE NOT EXISTS (SELECT 1 FROM CronStatRetention WHERE id=1 AND first_day>$date)
|
||||||
|
ON CONFLICT(ref_id, date) DO UPDATE SET
|
||||||
|
run_count = COALESCE(run_count, 0) + 1,
|
||||||
|
success_count = COALESCE(success_count, 0) + excluded.success_count,
|
||||||
|
fail_count = COALESCE(fail_count, 0) + excluded.fail_count,
|
||||||
|
total_time = COALESCE(total_time, 0) + excluded.total_time,
|
||||||
|
max_time = MAX(COALESCE(max_time, 0), excluded.max_time),
|
||||||
|
fixed_max_time = MAX(COALESCE(fixed_max_time,max_time,0),excluded.fixed_max_time),
|
||||||
|
updatedAt = excluded.updatedAt`,
|
||||||
|
{ transaction, bind: { id: cronId, date: day, fixedMax: reversible ? 0 : elapsedMs,
|
||||||
|
success: Number(state === 'finished'), fail: Number(state === 'failed'), elapsed: elapsedMs } },
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
import { EventEmitter } from 'events';
|
||||||
|
import { QueueAddOptions } from 'p-queue-cjs';
|
||||||
|
|
||||||
|
type Job = () => Promise<unknown>;
|
||||||
|
export interface CronQueueOptions extends QueueAddOptions {
|
||||||
|
waitingSignal?: EventEmitter;
|
||||||
|
onRemoved?: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
// p-queue checks its signal only after dequeue. Remove expired waits while the
|
||||||
|
// execution slots are still occupied, without aborting a running child.
|
||||||
|
export class CronWaitQueue {
|
||||||
|
private entries: { run: Job; priority: number; detach: () => void }[] = [];
|
||||||
|
|
||||||
|
get size() { return this.entries.length; }
|
||||||
|
|
||||||
|
enqueue(run: Job, options: Partial<CronQueueOptions> = {}) {
|
||||||
|
const { priority = 0, waitingSignal, onRemoved } = options;
|
||||||
|
const remove = () => {
|
||||||
|
const index = this.entries.indexOf(entry);
|
||||||
|
if (index < 0) return;
|
||||||
|
this.entries.splice(index, 1);
|
||||||
|
onRemoved?.();
|
||||||
|
};
|
||||||
|
const entry = { run, priority, detach: () => waitingSignal?.off('expired', remove) };
|
||||||
|
const last = this.entries[this.size - 1];
|
||||||
|
if (!last || last.priority >= priority) this.entries.push(entry);
|
||||||
|
else this.entries.splice(this.entries.findIndex((job) => job.priority < priority), 0, entry);
|
||||||
|
waitingSignal?.once('expired', remove);
|
||||||
|
}
|
||||||
|
|
||||||
|
dequeue() {
|
||||||
|
const next = this.entries.shift();
|
||||||
|
next?.detach();
|
||||||
|
return next?.run;
|
||||||
|
}
|
||||||
|
|
||||||
|
filter(options: Partial<CronQueueOptions>) {
|
||||||
|
return this.entries.filter((entry) => entry.priority === options.priority).map((entry) => entry.run);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,111 @@
|
|||||||
|
import { promises as fs } from 'fs';
|
||||||
|
import path from 'path';
|
||||||
|
import { resolveFileAccess } from './fileAccess';
|
||||||
|
|
||||||
|
export type DirectoryVersion = [string, string | null];
|
||||||
|
|
||||||
|
function isGone(error: unknown) {
|
||||||
|
const code = (error as NodeJS.ErrnoException).code;
|
||||||
|
return code === 'ENOENT' || code === 'ENOTDIR';
|
||||||
|
}
|
||||||
|
|
||||||
|
async function directoryVersion(root: string, parent: string, blacklist: string[]) {
|
||||||
|
const dir = parent ? resolveFileAccess(root, [parent], blacklist) : root;
|
||||||
|
const denied = () => Object.assign(new Error('Invalid directory'), { status: 403 });
|
||||||
|
if (!dir) throw denied();
|
||||||
|
try {
|
||||||
|
if (await fs.realpath(dir) !== path.join(root, parent)) throw denied();
|
||||||
|
const stat = await fs.lstat(dir, { bigint: true });
|
||||||
|
return stat.isDirectory() ? [stat.dev, stat.ino, stat.mtimeNs, stat.ctimeNs].join(':') : null;
|
||||||
|
} catch (error) {
|
||||||
|
if (isGone(error)) return null;
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function checkDirectoryVersions(root: string, blacklist: string[], versions: DirectoryVersion[]) {
|
||||||
|
const realRoot = await fs.realpath(root);
|
||||||
|
const matches = await Promise.all(versions.map(async ([parent, expected]) => {
|
||||||
|
try {
|
||||||
|
return await directoryVersion(realRoot, parent, blacklist) === expected;
|
||||||
|
} catch (error) {
|
||||||
|
// A formerly accessible directory may now be a symlink outside the root.
|
||||||
|
if ((error as { status?: number }).status === 403) return false;
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
return matches.every(Boolean);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function readDirectoryPage(root: string, blacklist: string[], directory: string, after: string,
|
||||||
|
search: string, foldersOnly: boolean, newestFirst: boolean, limit = 50) {
|
||||||
|
const blocked = new Set(blacklist.map(name => name.toLowerCase()));
|
||||||
|
const realRoot = await fs.realpath(root);
|
||||||
|
const versions: DirectoryVersion[] = [];
|
||||||
|
async function entries(parent: string) {
|
||||||
|
const version = await directoryVersion(realRoot, parent, blacklist);
|
||||||
|
versions.push([parent, version]);
|
||||||
|
if (version === null) return [];
|
||||||
|
try {
|
||||||
|
return (await fs.readdir(path.join(realRoot, parent), { withFileTypes: true })).filter(e =>
|
||||||
|
!blocked.has(e.name.toLowerCase()) && (e.isDirectory() || (!foldersOnly && e.isFile())));
|
||||||
|
} catch (error) {
|
||||||
|
if (isGone(error)) return [];
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const compare = (a: string, b: string) => a < b ? -1 : a > b ? 1 : 0;
|
||||||
|
const rows = [];
|
||||||
|
let next: string | null = null;
|
||||||
|
async function row(parent: string, title: string, directory: boolean) {
|
||||||
|
const key = path.join(parent, title);
|
||||||
|
if (directory) return { title, parent, key, type: 'directory', isLeaf: false };
|
||||||
|
try {
|
||||||
|
const stat = await fs.lstat(path.join(root, key));
|
||||||
|
if (stat.isFile()) return { title, parent, key, type: 'file', isLeaf: true, size: stat.size };
|
||||||
|
} catch (error) { if (!isGone(error)) throw error; }
|
||||||
|
}
|
||||||
|
if (!search) {
|
||||||
|
const sorted = (await entries(directory)).sort((a,b) =>
|
||||||
|
Number(b.isDirectory()) - Number(a.isDirectory()) ||
|
||||||
|
compare(a.name, b.name) * (newestFirst && !a.isDirectory() ? -1 : 1));
|
||||||
|
const selected = sorted.filter(e => {
|
||||||
|
if (!after) return true;
|
||||||
|
const group = e.isDirectory() ? '0' : '1';
|
||||||
|
return group > after[0] || (group === after[0] &&
|
||||||
|
compare(e.name, after.slice(1)) * (newestFirst && !e.isDirectory() ? -1 : 1) > 0);
|
||||||
|
});
|
||||||
|
const page = selected.slice(0, limit);
|
||||||
|
const data = (await Promise.all(page.map(e => row(directory, e.name, e.isDirectory())))).filter(Boolean);
|
||||||
|
if (selected.length > limit) {
|
||||||
|
const last = page[page.length - 1];
|
||||||
|
next = (last.isDirectory() ? '0' : '1') + last.name;
|
||||||
|
}
|
||||||
|
return { data, next, versions };
|
||||||
|
}
|
||||||
|
// Resume a depth-first search at the last visited entry, including on pages with zero matches.
|
||||||
|
async function* walk(parent: string, resume: string[]): AsyncGenerator<{ parent: string; name: string; dir: boolean }> {
|
||||||
|
const items = (await entries(parent)).sort((a,b) => compare(a.name,b.name));
|
||||||
|
for (const item of items) {
|
||||||
|
if (resume.length && item.name < resume[0]) continue;
|
||||||
|
const same = item.name === resume[0];
|
||||||
|
if (!same) yield { parent, name: item.name, dir: item.isDirectory() };
|
||||||
|
if (item.isDirectory()) {
|
||||||
|
try { yield* walk(path.join(parent, item.name), same ? resume.slice(1) : []); }
|
||||||
|
catch (error) {
|
||||||
|
if (!isGone(error) && (error as { status?: number }).status !== 403) throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let visited = 0;
|
||||||
|
for await (const entry of walk('', after ? after.split('/') : [])) {
|
||||||
|
const key = path.join(entry.parent, entry.name);
|
||||||
|
if (key.toLowerCase().includes(search.toLowerCase())) {
|
||||||
|
const item = await row(entry.parent, entry.name, entry.dir);
|
||||||
|
if (item) rows.push(item);
|
||||||
|
}
|
||||||
|
if (++visited >= 512 || rows.length === limit) { next = key; break; }
|
||||||
|
}
|
||||||
|
return { data: rows, next, versions };
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
import { celebrate, Joi } from 'celebrate';
|
||||||
|
import { Request, Response, NextFunction } from 'express';
|
||||||
|
import { checkDirectoryVersions, readDirectoryPage } from './directoryPage';
|
||||||
|
|
||||||
|
export function directoryCheckHandler(root: () => string, blacklist: string[]) {
|
||||||
|
return [
|
||||||
|
celebrate({ body: Joi.array().items(Joi.array().ordered(
|
||||||
|
Joi.string().allow('').max(4096).required(), Joi.string().max(128).allow(null).required(),
|
||||||
|
).length(2)).min(1).max(128).required() }),
|
||||||
|
async (req: Request, res: Response, next: NextFunction) => {
|
||||||
|
try {
|
||||||
|
const current = await checkDirectoryVersions(root(), blacklist, req.body);
|
||||||
|
res.send({ code: 200, current });
|
||||||
|
} catch (error) { next(error); }
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function directoryPageHandler(root: () => string, blacklist: string[], newestFirst: boolean) {
|
||||||
|
return [
|
||||||
|
celebrate({ query: Joi.object({
|
||||||
|
path: Joi.string().allow(''), after: Joi.string().allow(''), search: Joi.string().allow(''),
|
||||||
|
folders: Joi.boolean(), t: Joi.string(),
|
||||||
|
}) }),
|
||||||
|
async (req: Request, res: Response, next: NextFunction) => {
|
||||||
|
try {
|
||||||
|
const { path = '', after = '', search = '', folders } = req.query;
|
||||||
|
const page = await readDirectoryPage(root(), blacklist, String(path), String(after),
|
||||||
|
String(search), String(folders) === 'true', newestFirst);
|
||||||
|
res.send({ code: 200, ...page });
|
||||||
|
} catch (error) { next(error); }
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
@@ -2,3 +2,10 @@
|
|||||||
export function errStack(error: unknown): string {
|
export function errStack(error: unknown): string {
|
||||||
return error instanceof Error && error.stack ? error.stack : String(error);
|
return error instanceof Error && error.stack ? error.stack : String(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class PendingCronInstall extends Error {
|
||||||
|
readonly status = 503;
|
||||||
|
constructor(readonly cronId: number, message: string) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
// The original scheduled CLI wire format maps to its existing persisted trigger ID.
|
||||||
|
export const executionIdPattern = /^(?:client-|legacy-system:\d{1,12}:)[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/;
|
||||||
|
export function executionTriggerId(id: string) {
|
||||||
|
return id.replace('legacy-system:', 'system-cli-').replace(':', '-');
|
||||||
|
}
|
||||||
|
|
||||||
|
// A header lets upgraded clients keep the same identified body on older panels.
|
||||||
|
export function receiptFinishTime(header?: string) {
|
||||||
|
if (header && /^\d{1,13}$/.test(header)) {
|
||||||
|
const time = Number(header);
|
||||||
|
if (time > 0 && time <= Date.now()) return time;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -24,8 +24,11 @@ export function resolveFileAccess(
|
|||||||
const resolvedRoot = path.resolve(root);
|
const resolvedRoot = path.resolve(root);
|
||||||
const target = path.resolve(resolvedRoot, ...parts);
|
const target = path.resolve(resolvedRoot, ...parts);
|
||||||
if (target === resolvedRoot || !isWithin(resolvedRoot, target)) return '';
|
if (target === resolvedRoot || !isWithin(resolvedRoot, target)) return '';
|
||||||
|
// File systems such as default APFS can resolve a differently cased spelling
|
||||||
|
// of the same entry. Check every protected name without case sensitivity.
|
||||||
|
const blockedNames = new Set(blacklist.map((part) => part.toLowerCase()));
|
||||||
const isBlocked = (relative: string) =>
|
const isBlocked = (relative: string) =>
|
||||||
relative.split(path.sep).some((part) => blacklist.includes(part));
|
relative.split(path.sep).some((part) => blockedNames.has(part.toLowerCase()));
|
||||||
if (isBlocked(path.relative(resolvedRoot, target))) return '';
|
if (isBlocked(path.relative(resolvedRoot, target))) return '';
|
||||||
try {
|
try {
|
||||||
const realRoot = fs.realpathSync(resolvedRoot);
|
const realRoot = fs.realpathSync(resolvedRoot);
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
const messages: Record<string, Record<string, string>> = {
|
const messages: Record<string, Record<string, string>> = {
|
||||||
zh: {},
|
zh: {},
|
||||||
en: {
|
en: {
|
||||||
|
'系统定时规则安装失败,旧规则可能仍在运行,正在重试': 'System crontab installation failed. The previous rules may still run. Retrying.',
|
||||||
|
'Cron表达式格式有误': 'Invalid cron expression',
|
||||||
'升级任务启动失败,请检查日志后重试':
|
'升级任务启动失败,请检查日志后重试':
|
||||||
'Failed to start the update. Check the logs and try again.',
|
'Failed to start the update. Check the logs and try again.',
|
||||||
'[schedule][停止已运行任务] 任务ID: %s, PID: %s':
|
'[schedule][停止已运行任务] 任务ID: %s, PID: %s':
|
||||||
@@ -23,6 +25,7 @@ const messages: Record<string, Record<string, string>> = {
|
|||||||
'代理层数不能超过 20': 'Proxy hop count cannot exceed 20',
|
'代理层数不能超过 20': 'Proxy hop count cannot exceed 20',
|
||||||
'环境变量 QL_TRUST_PROXY 已生效,系统设置不可覆盖':
|
'环境变量 QL_TRUST_PROXY 已生效,系统设置不可覆盖':
|
||||||
'QL_TRUST_PROXY is active and cannot be overridden by system settings',
|
'QL_TRUST_PROXY is active and cannot be overridden by system settings',
|
||||||
|
环境变量名称不可使用: 'Environment variable name is reserved or invalid',
|
||||||
系统配置不存在: 'System configuration does not exist',
|
系统配置不存在: 'System configuration does not exist',
|
||||||
'历史版本仅支持不超过 1 MiB 的 UTF-8 文本脚本':
|
'历史版本仅支持不超过 1 MiB 的 UTF-8 文本脚本':
|
||||||
'Version history only supports UTF-8 text scripts up to 1 MiB',
|
'Version history only supports UTF-8 text scripts up to 1 MiB',
|
||||||
@@ -137,6 +140,7 @@ const messages: Record<string, Record<string, string>> = {
|
|||||||
验证失败: 'Verification failed',
|
验证失败: 'Verification failed',
|
||||||
实例不存在或已停止: 'Instance does not exist or stopped',
|
实例不存在或已停止: 'Instance does not exist or stopped',
|
||||||
实例已停止: 'Instance stopped',
|
实例已停止: 'Instance stopped',
|
||||||
|
实例已结束: 'Instance already ended',
|
||||||
确认停止实例: 'Confirm to stop instance',
|
确认停止实例: 'Confirm to stop instance',
|
||||||
确认停止运行实例: 'Confirm to stop running instance',
|
确认停止运行实例: 'Confirm to stop running instance',
|
||||||
确认停止: 'Confirm to stop',
|
确认停止: 'Confirm to stop',
|
||||||
@@ -260,6 +264,8 @@ const messages: Record<string, Record<string, string>> = {
|
|||||||
'执行after命令...': 'Execute after command...',
|
'执行after命令...': 'Execute after command...',
|
||||||
'## 执行结束... %s 耗时 %s 秒':
|
'## 执行结束... %s 耗时 %s 秒':
|
||||||
'## Execution finished... %s elapsed %s seconds',
|
'## Execution finished... %s elapsed %s seconds',
|
||||||
|
'原调度未能恢复,正在重试': 'Could not restore the previous schedule. Retrying.',
|
||||||
|
'任务已修改,请刷新后重试': 'The task has changed. Refresh and try again.',
|
||||||
'调度器注册失败,任务创建已回滚':
|
'调度器注册失败,任务创建已回滚':
|
||||||
'Scheduler registration failed, task creation rolled back',
|
'Scheduler registration failed, task creation rolled back',
|
||||||
'调度器注册失败,任务更新已回滚':
|
'调度器注册失败,任务更新已回滚':
|
||||||
|
|||||||
@@ -0,0 +1,172 @@
|
|||||||
|
import dayjs from 'dayjs';
|
||||||
|
import { QueryTypes, Sequelize, Transaction } from 'sequelize';
|
||||||
|
|
||||||
|
type DayCounts = {
|
||||||
|
cron_id: number;
|
||||||
|
day: string;
|
||||||
|
successes: number;
|
||||||
|
failures: number;
|
||||||
|
later_successes: number;
|
||||||
|
later_failures: number;
|
||||||
|
has_bookings: number;
|
||||||
|
saved_successes: number;
|
||||||
|
saved_failures: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Process one outcome, newest day first. Same-day receipts can only cover
|
||||||
|
// eligible exits. Surplus becomes available to earlier days after this match.
|
||||||
|
function matchDailyCounts(exits: number, saved: number, laterCredits: number, afterLastBooking = 0) {
|
||||||
|
const localMatches = Math.min(saved, exits - afterLastBooking);
|
||||||
|
const deficit = exits - localMatches;
|
||||||
|
const borrowed = Math.min(deficit, laterCredits);
|
||||||
|
return { missing: deficit - borrowed, remaining: laterCredits - borrowed + saved - localMatches };
|
||||||
|
}
|
||||||
|
|
||||||
|
// A legacy terminal status needs a matching exit code. An identified supervisor
|
||||||
|
// also knows a child failed when the OS returns a signal instead of a code.
|
||||||
|
const confirmedResult = `t.started_at IS NOT NULL AND t.finished_at>=t.started_at
|
||||||
|
AND t.state IN ('finished','failed')
|
||||||
|
AND (t.source<>'legacy' OR (
|
||||||
|
((t.state='finished' AND t.exit_code=0) OR (t.state='failed' AND t.exit_code<>0))
|
||||||
|
AND NOT EXISTS (SELECT 1 FROM RunningInstances sibling
|
||||||
|
LEFT JOIN CronTriggers linked ON linked.instance_id=sibling.id
|
||||||
|
WHERE sibling.cron_id=i.cron_id AND sibling.pid=i.pid AND sibling.finished_at=i.finished_at
|
||||||
|
AND sibling.id<>i.id AND sibling.status IN (1,3)
|
||||||
|
AND (linked.id IS NULL OR (linked.source='legacy' AND linked.owner IS NULL))
|
||||||
|
AND (sibling.started_at<>i.started_at OR COALESCE(sibling.log_path,'')<>COALESCE(i.log_path,'')
|
||||||
|
OR sibling.status<>i.status OR sibling.exit_code IS NOT i.exit_code OR linked.id IS NOT NULL))))`;
|
||||||
|
|
||||||
|
export function readDailyResults(database: Sequelize, cronId: number) {
|
||||||
|
const today = dayjs().startOf('day');
|
||||||
|
const first = today.subtract(6, 'day');
|
||||||
|
const next = today.add(1, 'day');
|
||||||
|
return database.query<{
|
||||||
|
day: string; exitSuccess: number; exitFail: number;
|
||||||
|
bookedSuccess: number; bookedFail: number; supplement: number;
|
||||||
|
}>(`WITH results AS (
|
||||||
|
SELECT date(t.finished_at/1000,'unixepoch','localtime') AS day,
|
||||||
|
SUM(t.state='finished') AS exitSuccess,SUM(t.state='failed') AS exitFail,
|
||||||
|
0 AS bookedSuccess,0 AS bookedFail,0 AS supplement
|
||||||
|
FROM CronTriggers t LEFT JOIN RunningInstances i ON i.id=t.instance_id
|
||||||
|
WHERE t.cron_id=$id AND t.finished_at >= $start AND t.finished_at < $end
|
||||||
|
AND t.reason IS NOT 'spawn' AND ${confirmedResult} GROUP BY day
|
||||||
|
UNION ALL
|
||||||
|
SELECT date,0,0,COALESCE(success_count,0),COALESCE(fail_count,0),untimed_count
|
||||||
|
FROM CrontabStats WHERE ref_id=$id AND date >= $first AND date < $next
|
||||||
|
) SELECT day,SUM(exitSuccess) AS exitSuccess,SUM(exitFail) AS exitFail,
|
||||||
|
SUM(bookedSuccess) AS bookedSuccess,SUM(bookedFail) AS bookedFail,SUM(supplement) AS supplement
|
||||||
|
FROM results GROUP BY day ORDER BY day DESC`, { type: QueryTypes.SELECT, bind: {
|
||||||
|
id: cronId, start: first.valueOf(), end: next.valueOf(),
|
||||||
|
first: first.format('YYYY-MM-DD'), next: next.format('YYYY-MM-DD'),
|
||||||
|
} });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run under the schema migration's write lock, before accepting new receipts.
|
||||||
|
export async function repairLegacyDailyStatistics(database: Sequelize, transaction: Transaction, priorVersion: 0 | 1 | 2) {
|
||||||
|
// Old panels updated every outstanding start with the reported PID. Identical
|
||||||
|
// copies of a start prove only one exit; conflicting rows cannot identify it.
|
||||||
|
const imported = await database.query(`WITH exits AS (
|
||||||
|
SELECT MIN(i.id) AS instance FROM RunningInstances i
|
||||||
|
JOIN Crontabs c ON c.id=i.cron_id
|
||||||
|
LEFT JOIN CronTriggers linked ON linked.instance_id=i.id
|
||||||
|
WHERE (linked.id IS NULL OR (linked.source='legacy' AND linked.owner IS NULL))
|
||||||
|
AND i.status IN (1,3) AND i.pid>0 AND i.started_at>0 AND i.finished_at>=i.started_at
|
||||||
|
AND i.finished_at*1000>=COALESCE((SELECT cutoff FROM CronHistoryRetention WHERE id=1),0)
|
||||||
|
GROUP BY i.cron_id,i.pid,i.finished_at
|
||||||
|
HAVING MIN(i.started_at)=MAX(i.started_at)
|
||||||
|
AND COUNT(DISTINCT COALESCE(i.log_path,''))=1 AND MIN(i.status)=MAX(i.status)
|
||||||
|
AND COUNT(i.exit_code)=COUNT(*) AND MIN(i.exit_code)=MAX(i.exit_code)
|
||||||
|
AND COUNT(linked.id)=0
|
||||||
|
)
|
||||||
|
INSERT INTO CronTriggers(id,cron_id,source,state,observed_at,started_at,finished_at,instance_id,log_path,exit_code,counted)
|
||||||
|
SELECT 'legacy-db-' || i.id,i.cron_id,'legacy',CASE i.status WHEN 1 THEN 'finished' ELSE 'failed' END,
|
||||||
|
i.started_at*1000,i.started_at*1000,i.finished_at*1000,i.id,i.log_path,i.exit_code,1
|
||||||
|
FROM exits JOIN RunningInstances i ON i.id=exits.instance
|
||||||
|
WHERE (i.status=1 AND i.exit_code=0) OR (i.status=3 AND i.exit_code<>0)`,
|
||||||
|
{ transaction, type: QueryTypes.BULKUPDATE });
|
||||||
|
|
||||||
|
// A newly imported last run must have the same owner it would acquire through
|
||||||
|
// the normal handoff. An older exit must never replace a more recent start.
|
||||||
|
if (imported) await database.query(`WITH ranked AS (
|
||||||
|
SELECT cron_id,instance_id,started_at/1000 AS start,(finished_at-started_at)/1000 AS elapsed,
|
||||||
|
ROW_NUMBER() OVER (PARTITION BY cron_id ORDER BY started_at DESC,instance_id DESC) AS position
|
||||||
|
FROM CronTriggers WHERE source='legacy' AND id LIKE 'legacy-db-%'
|
||||||
|
)
|
||||||
|
UPDATE Crontabs AS c SET (last_instance_id,last_execution_time,last_running_time)=
|
||||||
|
(SELECT instance_id,start,elapsed FROM ranked WHERE cron_id=c.id AND position=1)
|
||||||
|
WHERE EXISTS (SELECT 1 FROM ranked r WHERE r.cron_id=c.id AND r.position=1 AND
|
||||||
|
(c.last_execution_time IS NULL OR c.last_execution_time<r.start OR
|
||||||
|
(c.last_execution_time=r.start AND (c.last_instance_id IS NULL OR c.last_instance_id<r.instance_id)))
|
||||||
|
AND NOT EXISTS (SELECT 1 FROM RunningInstances i WHERE i.cron_id=c.id AND
|
||||||
|
(i.started_at>r.start OR (i.started_at=r.start AND i.id>r.instance_id))))`,
|
||||||
|
{ transaction });
|
||||||
|
|
||||||
|
// Statistics retention removes whole dates across tasks. When the old panel
|
||||||
|
// retained no earlier totals, yesterday is the oldest date even a 1-day policy keeps.
|
||||||
|
const yesterday = dayjs().subtract(1,'day').format('YYYY-MM-DD');
|
||||||
|
const days = await database.query<DayCounts>(`WITH exits AS (
|
||||||
|
SELECT t.cron_id,t.state,date(t.finished_at/1000,'unixepoch','localtime') AS day,
|
||||||
|
t.started_at,i.createdAt,t.id
|
||||||
|
FROM CronTriggers t LEFT JOIN RunningInstances i ON i.id=t.instance_id
|
||||||
|
WHERE t.counted=1 AND ${confirmedResult}
|
||||||
|
), ordered AS (
|
||||||
|
SELECT e.*, CASE WHEN e.id LIKE 'legacy-db-%' AND s.untimed_count=0
|
||||||
|
AND date(s.updatedAt,'localtime')=e.day AND julianday(s.createdAt)<=julianday(s.updatedAt)
|
||||||
|
AND e.started_at/1000>CAST(strftime('%s',s.updatedAt) AS INTEGER)
|
||||||
|
AND julianday(e.createdAt)>julianday(s.updatedAt) THEN 1 ELSE 0 END AS later
|
||||||
|
FROM exits e LEFT JOIN CrontabStats s ON s.ref_id=e.cron_id AND s.date=e.day
|
||||||
|
), evidence AS (
|
||||||
|
SELECT cron_id,day,SUM(state='finished') AS successes,SUM(state='failed') AS failures,
|
||||||
|
SUM(later*(state='finished')) AS later_successes,SUM(later*(state='failed')) AS later_failures,
|
||||||
|
0 AS has_bookings,0 AS saved_successes,0 AS saved_failures FROM ordered GROUP BY cron_id,day
|
||||||
|
UNION ALL
|
||||||
|
SELECT ref_id,date,0,0,0,0,1,COALESCE(success_count,0),COALESCE(fail_count,0) FROM CrontabStats
|
||||||
|
)
|
||||||
|
SELECT cron_id,day,MAX(has_bookings) AS has_bookings,SUM(successes) AS successes,SUM(failures) AS failures,
|
||||||
|
SUM(later_successes) AS later_successes,SUM(later_failures) AS later_failures,
|
||||||
|
SUM(saved_successes) AS saved_successes,SUM(saved_failures) AS saved_failures
|
||||||
|
FROM evidence WHERE day>=MIN($yesterday,COALESCE((SELECT MIN(date) FROM CrontabStats),$yesterday))
|
||||||
|
AND day>=COALESCE((SELECT first_day FROM CronStatRetention WHERE id=1),'')
|
||||||
|
GROUP BY cron_id,day ORDER BY cron_id,day DESC`, { type: QueryTypes.SELECT, transaction, bind:{ yesterday } });
|
||||||
|
|
||||||
|
// Anonymous receipts cannot identify a particular exit. Match nearest
|
||||||
|
// eligible days first; never move already booked counts or elapsed samples.
|
||||||
|
let task: number | undefined;
|
||||||
|
let successCredit = 0, failureCredit = 0;
|
||||||
|
let priorSuccessCredit = 0, priorFailureCredit = 0;
|
||||||
|
for (const day of days) {
|
||||||
|
if (day.cron_id !== task) {
|
||||||
|
task = day.cron_id;
|
||||||
|
successCredit = failureCredit = priorSuccessCredit = priorFailureCredit = 0;
|
||||||
|
}
|
||||||
|
const successes = matchDailyCounts(day.successes, day.saved_successes, successCredit, day.later_successes);
|
||||||
|
const failures = matchDailyCounts(day.failures, day.saved_failures, failureCredit, day.later_failures);
|
||||||
|
successCredit = successes.remaining;
|
||||||
|
failureCredit = failures.remaining;
|
||||||
|
let success = successes.missing, failure = failures.missing;
|
||||||
|
if (priorVersion === 2) {
|
||||||
|
// Replaying v2 on the current snapshot could resurrect a cleaned-up day.
|
||||||
|
// Only the extra lower bound established by v3 is new evidence.
|
||||||
|
const priorSuccess = matchDailyCounts(day.successes, day.saved_successes, priorSuccessCredit);
|
||||||
|
const priorFailure = matchDailyCounts(day.failures, day.saved_failures, priorFailureCredit);
|
||||||
|
priorSuccessCredit = priorSuccess.remaining;
|
||||||
|
priorFailureCredit = priorFailure.remaining;
|
||||||
|
success = Math.max(0, success - priorSuccess.missing);
|
||||||
|
failure = Math.max(0, failure - priorFailure.missing);
|
||||||
|
}
|
||||||
|
// Old repairs did not journal deletions. An absent task/day could have
|
||||||
|
// been cleared after a supplement. Preserve the gap on repeat upgrades.
|
||||||
|
if (priorVersion && !day.has_bookings) continue;
|
||||||
|
if (!success && !failure) continue;
|
||||||
|
// Anonymous elapsed samples have no execution identity. Preserve measured
|
||||||
|
// times; adding instance wall times here could count the same sample twice.
|
||||||
|
await database.query(`INSERT INTO CrontabStats
|
||||||
|
(ref_id,date,run_count,success_count,fail_count,untimed_count,total_time,max_time,createdAt,updatedAt)
|
||||||
|
VALUES ($task,$day,$runs,$success,$failure,$runs,0,0,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP)
|
||||||
|
ON CONFLICT(ref_id,date) DO UPDATE SET run_count=COALESCE(run_count,0)+excluded.run_count,
|
||||||
|
success_count=COALESCE(success_count,0)+excluded.success_count,
|
||||||
|
fail_count=COALESCE(fail_count,0)+excluded.fail_count,
|
||||||
|
untimed_count=untimed_count+excluded.untimed_count,updatedAt=CURRENT_TIMESTAMP`,
|
||||||
|
{ transaction, bind: { task,day:day.day,runs:success+failure,success,failure } });
|
||||||
|
}
|
||||||
|
}
|
||||||
+55
-15
@@ -1,12 +1,18 @@
|
|||||||
import fs from 'fs/promises';
|
import fs from 'fs/promises';
|
||||||
|
import { createHash } from 'crypto';
|
||||||
|
|
||||||
export const DEFAULT_LOG_CHUNK_BYTES = 256 * 1024;
|
export const DEFAULT_LOG_CHUNK_BYTES = 256 * 1024;
|
||||||
export const MAX_LOG_CHUNK_BYTES = 1024 * 1024;
|
export const MAX_LOG_CHUNK_BYTES = 1024 * 1024;
|
||||||
|
|
||||||
|
// The viewer retains at most one Mi UTF-16 code units.
|
||||||
|
const CURSOR_WINDOW_BYTES = 4 * MAX_LOG_CHUNK_BYTES;
|
||||||
|
|
||||||
export interface LogReadOptions {
|
export interface LogReadOptions {
|
||||||
offset?: number;
|
offset?: number;
|
||||||
limit?: number;
|
limit?: number;
|
||||||
tail?: boolean;
|
tail?: boolean;
|
||||||
|
follow?: boolean;
|
||||||
|
cursor?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface LogChunk {
|
export interface LogChunk {
|
||||||
@@ -15,6 +21,8 @@ export interface LogChunk {
|
|||||||
nextOffset: number;
|
nextOffset: number;
|
||||||
total: number;
|
total: number;
|
||||||
truncated: boolean;
|
truncated: boolean;
|
||||||
|
cursor?: string;
|
||||||
|
reset?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
function normalizeLimit(limit?: number) {
|
function normalizeLimit(limit?: number) {
|
||||||
@@ -49,6 +57,10 @@ function completeUtf8End(buffer: Buffer, start: number, end: number) {
|
|||||||
return end - sequenceStart < expectedLength ? sequenceStart : end;
|
return end - sequenceStart < expectedLength ? sequenceStart : end;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function logDigest(identity: string, from: number, bytes: Buffer) {
|
||||||
|
return createHash('sha256').update(`${identity}:${from}:`).update(bytes).digest('hex');
|
||||||
|
}
|
||||||
|
|
||||||
export async function readLogChunk(
|
export async function readLogChunk(
|
||||||
filePath: string,
|
filePath: string,
|
||||||
options: LogReadOptions = {},
|
options: LogReadOptions = {},
|
||||||
@@ -56,23 +68,43 @@ export async function readLogChunk(
|
|||||||
let handle: fs.FileHandle | undefined;
|
let handle: fs.FileHandle | undefined;
|
||||||
try {
|
try {
|
||||||
handle = await fs.open(filePath, 'r');
|
handle = await fs.open(filePath, 'r');
|
||||||
const { size: total } = await handle.stat();
|
const stat = await handle.stat({ bigint: true });
|
||||||
|
const total = Number(stat.size);
|
||||||
|
const identity = `${stat.dev}:${stat.ino}`;
|
||||||
const limit = normalizeLimit(options.limit);
|
const limit = normalizeLimit(options.limit);
|
||||||
const requestedOffset = Number.isFinite(options.offset)
|
const requestedOffset = Number.isFinite(options.offset)
|
||||||
? Math.trunc(options.offset!)
|
? Math.max(0, Math.trunc(options.offset!))
|
||||||
: undefined;
|
: undefined;
|
||||||
const requestedStart =
|
let start = options.tail || requestedOffset === undefined
|
||||||
options.tail || requestedOffset === undefined
|
|
||||||
? Math.max(total - limit, 0)
|
? Math.max(total - limit, 0)
|
||||||
: Math.min(Math.max(requestedOffset, 0), total);
|
: Math.min(requestedOffset, total);
|
||||||
const length = Math.min(limit + 3, total - requestedStart);
|
const resume = options.cursor && !options.tail && requestedOffset !== undefined;
|
||||||
const buffer = Buffer.allocUnsafe(length);
|
const [fromText, digest] = resume ? options.cursor!.split('.') : [];
|
||||||
const { bytesRead } = await handle.read(
|
let from = resume ? Number(fromText) : start;
|
||||||
buffer,
|
let reset = Boolean(resume && (
|
||||||
0,
|
requestedOffset !== start || !Number.isSafeInteger(from) ||
|
||||||
length,
|
from < 0 || from > start || start - from > CURSOR_WINDOW_BYTES
|
||||||
requestedStart,
|
));
|
||||||
);
|
if (reset) from = start = 0;
|
||||||
|
|
||||||
|
// Read the previously displayed bytes and their continuation together.
|
||||||
|
// A truncate followed by regrowth can leave both size and inode unchanged.
|
||||||
|
const readWindow = async (position: number, end: number) => {
|
||||||
|
const data = Buffer.allocUnsafe(Math.min(end, total) - position);
|
||||||
|
const { bytesRead } = await handle!.read(data, 0, data.length, position);
|
||||||
|
return data.subarray(0, bytesRead);
|
||||||
|
};
|
||||||
|
let window = await readWindow(from, start + limit + 3);
|
||||||
|
if (resume && !reset) {
|
||||||
|
const prefix = window.subarray(0, start - from);
|
||||||
|
if (prefix.length !== start - from || logDigest(identity, from, prefix) !== digest) {
|
||||||
|
reset = true;
|
||||||
|
start = from = 0;
|
||||||
|
window = await readWindow(0, limit + 3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const buffer = window.subarray(start - from);
|
||||||
|
const bytesRead = buffer.length;
|
||||||
let leadingBytes = 0;
|
let leadingBytes = 0;
|
||||||
while (
|
while (
|
||||||
leadingBytes < bytesRead &&
|
leadingBytes < bytesRead &&
|
||||||
@@ -80,12 +112,20 @@ export async function readLogChunk(
|
|||||||
) {
|
) {
|
||||||
leadingBytes++;
|
leadingBytes++;
|
||||||
}
|
}
|
||||||
const offset = requestedStart + leadingBytes;
|
const offset = start + leadingBytes;
|
||||||
const candidateEnd = Math.min(leadingBytes + limit, bytesRead);
|
const candidateEnd = Math.min(leadingBytes + limit, bytesRead);
|
||||||
const contentEnd = completeUtf8End(buffer, leadingBytes, candidateEnd);
|
const contentEnd = completeUtf8End(buffer, leadingBytes, candidateEnd);
|
||||||
const nextOffset = requestedStart + contentEnd;
|
const nextOffset = start + contentEnd;
|
||||||
|
|
||||||
|
let cursor: string | undefined;
|
||||||
|
if (options.follow || options.cursor) {
|
||||||
|
const retainedFrom = Math.max(from, nextOffset - CURSOR_WINDOW_BYTES);
|
||||||
|
cursor = resume && !reset && nextOffset === start ? options.cursor :
|
||||||
|
`${retainedFrom}.${logDigest(identity, retainedFrom, window.subarray(retainedFrom - from, nextOffset - from))}`;
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
...(cursor && { cursor, reset }),
|
||||||
content: buffer.subarray(leadingBytes, contentEnd).toString('utf8'),
|
content: buffer.subarray(leadingBytes, contentEnd).toString('utf8'),
|
||||||
offset,
|
offset,
|
||||||
nextOffset,
|
nextOffset,
|
||||||
|
|||||||
@@ -0,0 +1,390 @@
|
|||||||
|
// Keep the command's actual exit available until its SQLite transaction commits.
|
||||||
|
const fs = require('node:fs');
|
||||||
|
const path = require('node:path');
|
||||||
|
const { createHash, randomUUID } = require('node:crypto');
|
||||||
|
|
||||||
|
const idPattern = /^[a-z0-9-]{1,120}$/;
|
||||||
|
|
||||||
|
// A full data volume can still have room for a directory entry. In this format
|
||||||
|
// the empty file publishes the tuple atomically, including its execution-bound
|
||||||
|
// checksum. Keep the encoded name when moving it into a collector-private slot.
|
||||||
|
const metadataName = /^\.pending\.([a-z0-9-]{1,120})\.full-(\d{1,3}|SIG[A-Z0-9]+)-(\d{1,16})-([0-9a-f]{64})-([a-zA-Z0-9_]+)$/;
|
||||||
|
|
||||||
|
// The digest only guards disposal of legacy bytes. It cannot establish an exit.
|
||||||
|
const legacyCapture = /^\.pending\.([a-z0-9-]{1,120})\.l-([0-9a-f]{32})-[0-9a-f]{24}$/;
|
||||||
|
const claimName = /^\.claim(-bound)?\.([a-z0-9-]{1,120})\.[a-zA-Z0-9]{6}$/;
|
||||||
|
|
||||||
|
function metadataReceipt(file) {
|
||||||
|
const name = path.basename(file);
|
||||||
|
return metadataName.exec(name === '.pending.claimed' ? path.basename(path.dirname(file)) : name);
|
||||||
|
}
|
||||||
|
|
||||||
|
function storageFull(error) {
|
||||||
|
const codes = [error?.code, error?.original?.code, error?.parent?.code];
|
||||||
|
if (codes.some(code => ['ENOSPC', 'EDQUOT', 'SQLITE_FULL'].includes(code))) return true;
|
||||||
|
const directory = receiptDirectory();
|
||||||
|
if (directory && codes.includes('SQLITE_CANTOPEN')) {
|
||||||
|
// SQLite maps a failed journal creation to CANTOPEN even when the kernel
|
||||||
|
// reported ENOSPC. Other open failures keep their normal error semantics.
|
||||||
|
try {
|
||||||
|
const capacity = fs.statfsSync(path.dirname(directory));
|
||||||
|
return capacity.bavail === 0 || (capacity.files > 0 && capacity.ffree === 0);
|
||||||
|
} catch { return false; }
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function receiptDirectory() {
|
||||||
|
const data = process.env.QL_DATA_DIR || (process.env.QL_DIR && path.join(process.env.QL_DIR, 'data'));
|
||||||
|
if (data) return path.join(data, 'db', 'cron-exits');
|
||||||
|
}
|
||||||
|
|
||||||
|
function receiptPath(id) {
|
||||||
|
const directory = receiptDirectory();
|
||||||
|
if (directory && typeof id === 'string' && idPattern.test(id)) return path.join(directory, id);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function listReceiptFiles(directory, prefix) {
|
||||||
|
if (!directory) return [];
|
||||||
|
try {
|
||||||
|
const entries = await fs.promises.readdir(directory, { withFileTypes: true });
|
||||||
|
const files = [];
|
||||||
|
for (const entry of entries) {
|
||||||
|
if (!entry.isFile() || !entry.name.startsWith(prefix)) continue;
|
||||||
|
const id = entry.name.slice(prefix.length);
|
||||||
|
if (idPattern.test(id)) files.push({ id, file: path.join(directory, entry.name) });
|
||||||
|
}
|
||||||
|
return files;
|
||||||
|
} catch (error) {
|
||||||
|
if (error.code === 'ENOENT') return [];
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function publishedExits() {
|
||||||
|
return (await listReceiptFiles(receiptDirectory(), '')).map(entry => entry.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
// flock attaches to the open file description. The child inherits fd 3 and
|
||||||
|
// exits immediately. Closing the panel's handle (including on SIGKILL)
|
||||||
|
// releases the lock without leaving a lock file on a full volume.
|
||||||
|
async function lockClaim(handle, shared) {
|
||||||
|
const run = (program, args) => new Promise((resolve, reject) => {
|
||||||
|
const child = require('node:child_process').spawn(program, args, {
|
||||||
|
stdio: ['ignore', 'ignore', 'ignore', handle.fd],
|
||||||
|
});
|
||||||
|
child.once('error', reject);
|
||||||
|
child.once('exit', (code, signal) => {
|
||||||
|
if (code === 0 || code === 1) resolve(code === 0);
|
||||||
|
else reject(new Error(`Receipt directory lock failed (${signal || code})`));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
try {
|
||||||
|
return await run('flock', ['-n', shared ? '-s' : '-x', '3']);
|
||||||
|
} catch (error) {
|
||||||
|
if (error.code !== 'ENOENT') throw error;
|
||||||
|
// Python is also shipped with the task supervisor on minimal Linux images.
|
||||||
|
const program = 'import fcntl,sys\ntry: fcntl.flock(3, fcntl.LOCK_NB | fcntl.LOCK_' +
|
||||||
|
(shared ? 'SH' : 'EX') + ')\nexcept BlockingIOError: sys.exit(1)';
|
||||||
|
return run('python3', ['-c', program]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move the current directory entry to a fresh, recoverable name outside the
|
||||||
|
// reusable claim directory. A writer can replace that entry without flock.
|
||||||
|
// Only the bytes actually captured by this attempt authorize its final unlink.
|
||||||
|
/** @param {string} id @param {string} file @param {{outcome: string, time: number}} [booked] */
|
||||||
|
async function retireLegacyFile(id, file, booked) {
|
||||||
|
const directory = receiptDirectory();
|
||||||
|
const sourceDir = path.dirname(file);
|
||||||
|
const slot = path.basename(file);
|
||||||
|
const bound = /^\.(pending|published)\./.test(slot) && !legacyCapture.test(slot);
|
||||||
|
const metadata = metadataReceipt(file);
|
||||||
|
const parent = claimName.exec(path.basename(sourceDir));
|
||||||
|
const privateSlot = path.dirname(sourceDir) === directory &&
|
||||||
|
(parent?.[2] === id && slot === (parent[1] ? '.pending.claimed' : 'receipt') ||
|
||||||
|
metadata?.[1] === id && slot === '.pending.claimed');
|
||||||
|
if (!directory || !idPattern.test(id)) return;
|
||||||
|
const dbDir = path.dirname(directory);
|
||||||
|
const localBin = path.join(directory, '.reclaim');
|
||||||
|
const fromBin = sourceDir === dbDir || sourceDir === localBin;
|
||||||
|
if (sourceDir !== directory && !privateSlot && !(fromBin && captureId(slot) === id)) return;
|
||||||
|
|
||||||
|
const matches = exit => exit && !exit.namedExit && (exit.version === 1
|
||||||
|
? booked && exit.time === booked.time && (exit.signal ?? String(exit.code)) === booked.outcome
|
||||||
|
: !bound);
|
||||||
|
let handle, binHandle;
|
||||||
|
try {
|
||||||
|
if ((privateSlot || sourceDir === localBin) && process.platform === 'linux') {
|
||||||
|
handle = await fs.promises.open(sourceDir, fs.constants.O_DIRECTORY | fs.constants.O_NOFOLLOW);
|
||||||
|
if (privateSlot && !await lockClaim(handle, true)) return;
|
||||||
|
}
|
||||||
|
const source = handle ? `/proc/self/fd/${handle.fd}/${slot}` : file;
|
||||||
|
// A canonical publication may only be readable after the move. For a
|
||||||
|
// recovered copy, first avoid moving an unsettled witness on every scan.
|
||||||
|
const previous = await readExit(id, source, file);
|
||||||
|
if (slot !== id && !matches(previous)) return;
|
||||||
|
const nonce = randomUUID();
|
||||||
|
let name;
|
||||||
|
if (metadata) {
|
||||||
|
name = metadata[0].slice(0, -metadata[5].length) + 'r_' + nonce.replace(/-/g, '').slice(0, 16);
|
||||||
|
} else if (bound || !previous || previous.version === 1) {
|
||||||
|
name = `.pending.${id}.r-${nonce}`;
|
||||||
|
} else {
|
||||||
|
const digest = exitChecksum(id, previous.code, previous.signal, previous.time).slice(0, 32);
|
||||||
|
name = `.pending.${id}.l-${digest}-${nonce.replace(/-/g, '').slice(0, 24)}`;
|
||||||
|
}
|
||||||
|
// The database directory is outside the writers' receipt namespace and
|
||||||
|
// already exists even at zero free inodes. A separately mounted journal
|
||||||
|
// requires a disposal directory on its own filesystem.
|
||||||
|
let bin = sourceDir === localBin ? localBin : dbDir;
|
||||||
|
const pinBin = async () => {
|
||||||
|
binHandle = await fs.promises.open(localBin, fs.constants.O_DIRECTORY | fs.constants.O_NOFOLLOW);
|
||||||
|
return process.platform === 'linux' ? `/proc/self/fd/${binHandle.fd}/${name}` : path.join(localBin, name);
|
||||||
|
};
|
||||||
|
let captured = path.join(bin, name);
|
||||||
|
if (bin === localBin) captured = handle ? path.join(path.dirname(source), name) : await pinBin();
|
||||||
|
try {
|
||||||
|
await fs.promises.rename(source, captured);
|
||||||
|
} catch (error) {
|
||||||
|
if (error.code !== 'EXDEV' || bin === localBin) throw error;
|
||||||
|
await fs.promises.mkdir(localBin, { mode: 0o700, recursive: true });
|
||||||
|
bin = localBin;
|
||||||
|
captured = await pinBin();
|
||||||
|
await fs.promises.rename(source, captured);
|
||||||
|
}
|
||||||
|
const actual = await readExit(id, captured, path.join(bin, name));
|
||||||
|
// An unreadable source offers no authority to discard unbound bytes.
|
||||||
|
const sameExit = previous && actual && actual.version === previous.version &&
|
||||||
|
actual.code === previous.code && actual.signal === previous.signal && actual.time === previous.time;
|
||||||
|
if (matches(actual) && (sameExit || (!previous && actual.version === 1))) {
|
||||||
|
await fs.promises.unlink(captured);
|
||||||
|
} else if (actual !== undefined) {
|
||||||
|
// A differing, torn or unreadable file is evidence for another pass.
|
||||||
|
// Persist its new directory entry before releasing the original directory.
|
||||||
|
if (binHandle) await binHandle.sync();
|
||||||
|
else if (bin !== sourceDir || !handle) syncDirectory(bin);
|
||||||
|
if (bin === localBin) syncDirectory(directory);
|
||||||
|
if (handle) await handle.sync();
|
||||||
|
else if (sourceDir !== bin) syncDirectory(sourceDir);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
if (error.code !== 'ENOENT') throw error;
|
||||||
|
} finally {
|
||||||
|
if (binHandle) await binHandle.close();
|
||||||
|
if (handle) await handle.close();
|
||||||
|
if (privateSlot) await fs.promises.rmdir(sourceDir).catch(() => {});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function captureId(name) {
|
||||||
|
const marker = metadataName.exec(name);
|
||||||
|
if (marker && /^r_[0-9a-f]{16}$/.test(marker[5])) return marker[1];
|
||||||
|
const match = legacyCapture.exec(name) ??
|
||||||
|
/^\.pending\.([a-z0-9-]{1,120})\.r-[0-9a-f-]{36}$/.exec(name);
|
||||||
|
return match?.[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
async function receiptEntries(directory) {
|
||||||
|
try { return await fs.promises.readdir(directory, { withFileTypes: true }); }
|
||||||
|
catch (error) { if (error.code === 'ENOENT') return []; throw error; }
|
||||||
|
}
|
||||||
|
|
||||||
|
async function retiringExits() {
|
||||||
|
const dir = receiptDirectory();
|
||||||
|
if (!dir) return [];
|
||||||
|
const files = [];
|
||||||
|
const bins = [path.dirname(dir)];
|
||||||
|
for (const entry of await receiptEntries(dir)) {
|
||||||
|
if (entry.isDirectory() && entry.name === '.reclaim') {
|
||||||
|
bins.push(path.join(dir, entry.name));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const claim = claimName.exec(entry.name);
|
||||||
|
const metadata = metadataName.exec(entry.name);
|
||||||
|
if ((claim || metadata) && entry.isDirectory()) {
|
||||||
|
const directory = path.join(dir, entry.name);
|
||||||
|
try { await fs.promises.rmdir(directory); continue; } catch (error) {
|
||||||
|
if (error.code === 'ENOENT') continue;
|
||||||
|
}
|
||||||
|
files.push({ id: metadata ? metadata[1] : claim[2], file: path.join(directory, metadata || claim[1] ? '.pending.claimed' : 'receipt') });
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const match = metadata ?? /^\.(?:retiring|collecting)\.([a-z0-9-]{1,120})\.[0-9a-f-]{36}$/.exec(entry.name)
|
||||||
|
?? /^\.pending\.([a-z0-9-]{1,120})\.[a-z0-9_-]{1,64}$/.exec(entry.name)
|
||||||
|
?? /^\.published\.([a-z0-9-]{1,120})\.[0-9a-f]{64}$/.exec(entry.name);
|
||||||
|
if (entry.isFile() && match) files.push({ id:match[1], file:path.join(dir,entry.name) });
|
||||||
|
}
|
||||||
|
for (const bin of bins) {
|
||||||
|
for (const entry of await receiptEntries(bin)) {
|
||||||
|
const id = entry.isFile() && captureId(entry.name);
|
||||||
|
if (id) files.push({ id, file:path.join(bin,entry.name) });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return files;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function publishedScriptStarts() {
|
||||||
|
const directory = process.env.QL_DIR && path.join(process.env.QL_DIR, '.tmp');
|
||||||
|
try {
|
||||||
|
return await listReceiptFiles(directory, 'cron-start-');
|
||||||
|
} catch {
|
||||||
|
// Optional entry instrumentation must not prevent exit recovery.
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function validExit(code, signal, time) {
|
||||||
|
return Number.isSafeInteger(time) && time > 0 && Number.isFinite(new Date(time).getTime()) &&
|
||||||
|
((Number.isInteger(code) && code >= 0 && code <= 255 && signal === null) ||
|
||||||
|
(code === null && typeof signal === 'string' && /^SIG[A-Z0-9]+$/.test(signal)));
|
||||||
|
}
|
||||||
|
|
||||||
|
// The Python supervisor uses the same compact JSON array of ASCII values.
|
||||||
|
function exitChecksum(id, code, signal, time) {
|
||||||
|
return createHash('sha256').update(JSON.stringify([id, code, signal, time])).digest('hex');
|
||||||
|
}
|
||||||
|
|
||||||
|
function syncDirectory(directory) {
|
||||||
|
const fd = fs.openSync(directory, fs.constants.O_RDONLY | fs.constants.O_DIRECTORY);
|
||||||
|
try { fs.fsyncSync(fd); } finally { fs.closeSync(fd); }
|
||||||
|
}
|
||||||
|
|
||||||
|
function writeExit(id, code, signal, time) {
|
||||||
|
if (!validExit(code, signal, time)) throw new TypeError('Invalid command exit receipt');
|
||||||
|
const file = receiptPath(id);
|
||||||
|
if (!file) return;
|
||||||
|
fs.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 });
|
||||||
|
const checksum = exitChecksum(id, code, signal, time);
|
||||||
|
const receipt = JSON.stringify({ code, signal, time, version: 1, checksum });
|
||||||
|
// Creation is the first durable observation. No data block is needed for
|
||||||
|
// this witness. Keep the regular JSON publication for earlier readers.
|
||||||
|
const directory = path.dirname(file);
|
||||||
|
const prefix = '.pending.' + id + '.';
|
||||||
|
const marker = prefix + 'full-' + (signal ?? code) + '-' + time + '-' + checksum + '-';
|
||||||
|
const publish = (name, content) => {
|
||||||
|
for (;;) {
|
||||||
|
const target = path.join(directory, name + randomUUID().replace(/-/g, '').slice(0, 16));
|
||||||
|
try {
|
||||||
|
fs.writeFileSync(target, content, { flag: 'wx', mode: 0o600, flush: Boolean(content) });
|
||||||
|
return target;
|
||||||
|
} catch (error) {
|
||||||
|
if (error.code !== 'EEXIST') throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
publish(marker, '');
|
||||||
|
// ext4 journals the empty inode and directory entry together. Also fence the
|
||||||
|
// directory's own entry: another publisher may just have created it.
|
||||||
|
syncDirectory(directory);
|
||||||
|
syncDirectory(path.dirname(directory));
|
||||||
|
try {
|
||||||
|
if (fs.statfsSync(directory).bavail === 0) return;
|
||||||
|
const source = publish(marker, receipt);
|
||||||
|
try { fs.linkSync(source, file); }
|
||||||
|
catch (error) {
|
||||||
|
if (error.code === 'ENOENT') return;
|
||||||
|
if (error.code !== 'EEXIST') throw error;
|
||||||
|
// A panel from before metadata receipts can still discover this retry.
|
||||||
|
for (;;) {
|
||||||
|
try { fs.linkSync(source, path.join(directory, prefix + randomUUID())); break; }
|
||||||
|
catch (linkError) {
|
||||||
|
if (linkError.code === 'ENOENT') break;
|
||||||
|
if (linkError.code !== 'EEXIST') throw linkError;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Older panels find the JSON only through this newly created link.
|
||||||
|
syncDirectory(directory);
|
||||||
|
} catch (error) {
|
||||||
|
if (!storageFull(error)) throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseExit(id, value) {
|
||||||
|
if (!value || !validExit(value.code, value.signal, value.time)) return null;
|
||||||
|
const { code, signal, time, checksum, version } = value;
|
||||||
|
if (version !== undefined || checksum !== undefined) {
|
||||||
|
if (version !== 1 || checksum !== exitChecksum(id, code, signal, time)) return null;
|
||||||
|
} else if (Object.keys(value).length !== 3) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return { code, signal, time, version };
|
||||||
|
}
|
||||||
|
|
||||||
|
// undefined means no publication. null means a result cannot yet be trusted.
|
||||||
|
async function readExit(id, file = receiptPath(id), publication = file) {
|
||||||
|
if (!file) return;
|
||||||
|
try {
|
||||||
|
const handle = await fs.promises.open(file, fs.constants.O_RDONLY | fs.constants.O_NONBLOCK | fs.constants.O_NOFOLLOW);
|
||||||
|
try {
|
||||||
|
// Bound CPU and memory even if the damaged file is growing.
|
||||||
|
const buffer = Buffer.alloc(1025);
|
||||||
|
let size = 0;
|
||||||
|
while (size < buffer.length) {
|
||||||
|
const { bytesRead } = await handle.read(buffer, size, buffer.length - size, null);
|
||||||
|
if (!bytesRead) break;
|
||||||
|
size += bytesRead;
|
||||||
|
}
|
||||||
|
if (size === buffer.length) return null;
|
||||||
|
const metadata = metadataReceipt(publication);
|
||||||
|
const bytes = buffer.toString('utf8', 0, size);
|
||||||
|
let namedExit;
|
||||||
|
if (metadata && metadata[1] === id && (await handle.stat()).isFile()) {
|
||||||
|
const [, , outcome, end, checksum] = metadata;
|
||||||
|
const signal = outcome.startsWith('SIG') ? outcome : null;
|
||||||
|
const value = { code: signal ? null : Number(outcome), signal, time: Number(end), version: 1, checksum };
|
||||||
|
// Both current publishers serialize in this order. Accept a torn body
|
||||||
|
// only when every byte matches the tuple independently published by
|
||||||
|
// the filename. Validate replacement files on their own bytes below.
|
||||||
|
namedExit = parseExit(id, value);
|
||||||
|
const json = JSON.stringify(value);
|
||||||
|
if (json.startsWith(bytes)) {
|
||||||
|
return namedExit && { ...namedExit, file, namedExit: undefined };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const exit = parseExit(id, JSON.parse(bytes));
|
||||||
|
const guard = legacyCapture.exec(path.basename(publication));
|
||||||
|
if (guard && exit && exit.version !== 1 && (guard[1] !== id ||
|
||||||
|
guard[2] !== exitChecksum(id, exit.code, exit.signal, exit.time).slice(0, 32))) return null;
|
||||||
|
const conflictingName = exit?.version === 1 && namedExit && (exit.time !== namedExit.time ||
|
||||||
|
exit.code !== namedExit.code || exit.signal !== namedExit.signal);
|
||||||
|
return exit && { ...exit, file, namedExit: conflictingName ? namedExit : undefined };
|
||||||
|
} finally {
|
||||||
|
await handle.close();
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
if (error.code === 'ENOENT') return;
|
||||||
|
if (error.code || error instanceof SyntaxError) return null;
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { isLegacyCapture: file => legacyCapture.test(path.basename(file)), isMetadataReceipt: file => Boolean(metadataReceipt(file)), receiptPath, publishedExits, publishedScriptStarts, readExit, writeExit, validExit, storageFull, retireLegacyFile, retiringExits };
|
||||||
|
|
||||||
|
if (require.main === module) {
|
||||||
|
const id = process.env.QL_TRIGGER_ID;
|
||||||
|
const exit = parseExit(id, JSON.parse(process.argv[2]));
|
||||||
|
if (!idPattern.test(id || '') || !exit) throw new Error('Invalid supervisor exit receipt');
|
||||||
|
const { code, signal, time } = exit;
|
||||||
|
// An older supervisor may only have delivered this tuple in its message.
|
||||||
|
// Journal it before waiting for the SQLite writer or loading the panel.
|
||||||
|
let journaled = false;
|
||||||
|
if (exit.version === 1) {
|
||||||
|
try { writeExit(id, code, signal, time); journaled = Boolean(receiptPath(id)); }
|
||||||
|
catch (error) { console.error('[scheduler] Could not journal supervisor message:', error); }
|
||||||
|
}
|
||||||
|
// Development workers run TypeScript directly. Released panels load the
|
||||||
|
// compiled module without starting another runtime on ordinary exits.
|
||||||
|
if (!fs.existsSync(path.join(__dirname, 'triggerHistory.js'))) {
|
||||||
|
process.env.TS_NODE_PROJECT = path.join(__dirname, '../tsconfig.json');
|
||||||
|
require('ts-node/register/transpile-only');
|
||||||
|
}
|
||||||
|
const { triggerHistory } = require('./triggerHistory');
|
||||||
|
triggerHistory().complete(id, signal || code !== 0 ? 'failed' : 'finished',
|
||||||
|
signal, code, time, exit.version === 1 ? exit : 'legacy').catch(error => {
|
||||||
|
if (journaled && storageFull(error)) return;
|
||||||
|
console.error('[scheduler] Failed to settle orphaned command:', error);
|
||||||
|
process.exitCode = storageFull(error) ? 75 : 1;
|
||||||
|
}).finally(() => require('../data').sequelize.close());
|
||||||
|
}
|
||||||
+43
-5
@@ -1,5 +1,9 @@
|
|||||||
|
import { EventEmitter } from 'events';
|
||||||
import PQueue, { QueueAddOptions } from 'p-queue-cjs';
|
import PQueue, { QueueAddOptions } from 'p-queue-cjs';
|
||||||
import os from 'os';
|
import os from 'os';
|
||||||
|
import type { Sequelize } from 'sequelize';
|
||||||
|
import { QueueOccupants, recordQueueAdmission } from './queueDiagnostics';
|
||||||
|
import { CronWaitQueue, CronQueueOptions } from './cronWaitQueue';
|
||||||
import { AuthDataType, SystemModel } from '../data/system';
|
import { AuthDataType, SystemModel } from '../data/system';
|
||||||
import Logger from '../loaders/logger';
|
import Logger from '../loaders/logger';
|
||||||
import { Dependence } from '../data/dependence';
|
import { Dependence } from '../data/dependence';
|
||||||
@@ -23,7 +27,11 @@ class TaskLimit {
|
|||||||
private queuedCrons = new Map<string, ICronFn<any>[]>();
|
private queuedCrons = new Map<string, ICronFn<any>[]>();
|
||||||
private repeatCronNotifyMap = new Map<string, number>();
|
private repeatCronNotifyMap = new Map<string, number>();
|
||||||
private updateLogLimit = new PQueue({ concurrency: 1 });
|
private updateLogLimit = new PQueue({ concurrency: 1 });
|
||||||
private cronLimit = new PQueue({
|
private warnSampling = (error: Error) => Logger.warn('[queue] Could not save occupancy: %s', error.message);
|
||||||
|
private scheduledOccupants = new QueueOccupants('scheduled', () => this.cronLimit.concurrency, this.warnSampling);
|
||||||
|
private manualOccupants = new QueueOccupants('manual', () => this.manualCronoLimit.concurrency, this.warnSampling);
|
||||||
|
private cronLimit = new PQueue<CronWaitQueue, CronQueueOptions>({
|
||||||
|
queueClass: CronWaitQueue,
|
||||||
concurrency: Math.max(os.cpus().length, 4),
|
concurrency: Math.max(os.cpus().length, 4),
|
||||||
});
|
});
|
||||||
private manualCronoLimit = new PQueue({
|
private manualCronoLimit = new PQueue({
|
||||||
@@ -131,10 +139,16 @@ class TaskLimit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public get cronConcurrency() {
|
||||||
|
return this.manualCronoLimit.concurrency;
|
||||||
|
}
|
||||||
|
|
||||||
public async setCustomLimit(limit?: number) {
|
public async setCustomLimit(limit?: number) {
|
||||||
if (limit) {
|
if (limit) {
|
||||||
this.cronLimit.concurrency = limit;
|
this.cronLimit.concurrency = limit;
|
||||||
this.manualCronoLimit.concurrency = limit;
|
this.manualCronoLimit.concurrency = limit;
|
||||||
|
this.scheduledOccupants.refresh();
|
||||||
|
this.manualOccupants.refresh();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await SystemModel.sync();
|
await SystemModel.sync();
|
||||||
@@ -142,8 +156,7 @@ class TaskLimit {
|
|||||||
where: { type: AuthDataType.systemConfig },
|
where: { type: AuthDataType.systemConfig },
|
||||||
});
|
});
|
||||||
if (doc?.info?.cronConcurrency) {
|
if (doc?.info?.cronConcurrency) {
|
||||||
this.cronLimit.concurrency = doc.info.cronConcurrency;
|
await this.setCustomLimit(doc.info.cronConcurrency);
|
||||||
this.manualCronoLimit.concurrency = doc.info.cronConcurrency;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -152,6 +165,10 @@ class TaskLimit {
|
|||||||
fn: ICronFn<T>,
|
fn: ICronFn<T>,
|
||||||
options?: Partial<QueueAddOptions>,
|
options?: Partial<QueueAddOptions>,
|
||||||
isCurrent: () => boolean = () => true,
|
isCurrent: () => boolean = () => true,
|
||||||
|
onPending?: () => void,
|
||||||
|
waitingSignal?: EventEmitter,
|
||||||
|
triggerId?: string,
|
||||||
|
db?: Sequelize,
|
||||||
): Promise<T | void> {
|
): Promise<T | void> {
|
||||||
if (!isCurrent()) return;
|
if (!isCurrent()) return;
|
||||||
fn.cron = cron;
|
fn.cron = cron;
|
||||||
@@ -160,6 +177,10 @@ class TaskLimit {
|
|||||||
const runs = this.queuedCrons
|
const runs = this.queuedCrons
|
||||||
.get(cron.id)
|
.get(cron.id)
|
||||||
?.filter((run) => run.isCurrent?.() !== false);
|
?.filter((run) => run.isCurrent?.() !== false);
|
||||||
|
if (cron.skip_if_pending && runs?.length) {
|
||||||
|
onPending?.();
|
||||||
|
return;
|
||||||
|
}
|
||||||
const result = runs?.length ? [...runs, fn] : [fn];
|
const result = runs?.length ? [...runs, fn] : [fn];
|
||||||
const repeatTimes = this.repeatCronNotifyMap.get(cron.id) || 0;
|
const repeatTimes = this.repeatCronNotifyMap.get(cron.id) || 0;
|
||||||
if (result?.length > 5) {
|
if (result?.length > 5) {
|
||||||
@@ -190,14 +211,31 @@ class TaskLimit {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.queuedCrons.set(cron.id, result);
|
this.queuedCrons.set(cron.id, result);
|
||||||
return this.cronLimit.add(fn, options);
|
const run = this.trackAdmission(this.scheduledOccupants, this.cronLimit.concurrency, fn,
|
||||||
|
{ id: triggerId, name: cron.name || cron.id }, db);
|
||||||
|
if (!waitingSignal) return this.cronLimit.add(run, options);
|
||||||
|
return new Promise<T | void>((resolve, reject) => {
|
||||||
|
this.cronLimit.add(run, { ...options, waitingSignal, onRemoved: () => { this.scheduledOccupants.forget(triggerId); resolve(); } }).then(resolve, reject);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private trackAdmission<T>(pool: QueueOccupants, limit: number, fn: () => Promise<T>, occupant: { id?: string; name: string }, db?: Sequelize) {
|
||||||
|
const admission = pool.admission(limit);
|
||||||
|
if (occupant.id && admission && db) {
|
||||||
|
pool.watch(occupant.id, db);
|
||||||
|
void recordQueueAdmission(db, occupant.id, admission)
|
||||||
|
.catch(error => Logger.warn('[queue] Failed to record wait reason: %s', error.message));
|
||||||
|
}
|
||||||
|
return pool.track(fn, occupant, db);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async manualRunWithCronLimit<T>(
|
public async manualRunWithCronLimit<T>(
|
||||||
fn: () => Promise<T>,
|
fn: () => Promise<T>,
|
||||||
options?: Partial<QueueAddOptions>,
|
options?: Partial<QueueAddOptions>,
|
||||||
|
occupant: { id?: string; name: string } = { name: '—' },
|
||||||
|
db?: Sequelize,
|
||||||
): Promise<T | void> {
|
): Promise<T | void> {
|
||||||
return this.manualCronoLimit.add(fn, options);
|
return this.manualCronoLimit.add(this.trackAdmission(this.manualOccupants, this.manualCronoLimit.concurrency, fn, occupant, db), options);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async runWithSubscriptionLimit<T>(
|
public async runWithSubscriptionLimit<T>(
|
||||||
|
|||||||
@@ -0,0 +1,127 @@
|
|||||||
|
import { QueryTypes, Sequelize } from 'sequelize';
|
||||||
|
|
||||||
|
type Occupant = { id?: string; name: string };
|
||||||
|
export type Admission = {
|
||||||
|
queue: 'manual' | 'scheduled';
|
||||||
|
limit: number;
|
||||||
|
occupied: number;
|
||||||
|
holders: Occupant[];
|
||||||
|
};
|
||||||
|
|
||||||
|
// Each object belongs to exactly one execution pool in its owning process.
|
||||||
|
export class QueueOccupants {
|
||||||
|
private active = new Set<Occupant>();
|
||||||
|
private waiting = new Map<string, Sequelize>();
|
||||||
|
private writing = false;
|
||||||
|
private changed = false;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
private queue: Admission['queue'],
|
||||||
|
private limit: () => number,
|
||||||
|
private warn: (error: Error) => void,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
watch(id: string, db: Sequelize) {
|
||||||
|
this.waiting.set(id, db);
|
||||||
|
this.refresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
forget(id?: string) {
|
||||||
|
if (id) this.waiting.delete(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
refresh() {
|
||||||
|
if (!this.waiting.size) return;
|
||||||
|
this.changed = true;
|
||||||
|
if (this.writing) return;
|
||||||
|
this.writing = true;
|
||||||
|
// Coalesce synchronous PQueue handoffs and keep SQLite writes in observation order.
|
||||||
|
void Promise.resolve().then(async () => {
|
||||||
|
try {
|
||||||
|
while (this.changed) {
|
||||||
|
this.changed = false;
|
||||||
|
const context = JSON.stringify(this.snapshot(this.limit()));
|
||||||
|
const groups = new Map<Sequelize, string[]>();
|
||||||
|
for (const [id, db] of this.waiting) {
|
||||||
|
const ids = groups.get(db);
|
||||||
|
if (ids) ids.push(id);
|
||||||
|
else groups.set(db, [id]);
|
||||||
|
}
|
||||||
|
for (const [db, ids] of groups) {
|
||||||
|
await db
|
||||||
|
.query(
|
||||||
|
`UPDATE CronTriggers SET queue_live_context=$context
|
||||||
|
WHERE state='queued' AND dequeued_at IS NULL AND id IN (SELECT value FROM json_each($ids))`,
|
||||||
|
{ bind: { context, ids: JSON.stringify(ids) } },
|
||||||
|
)
|
||||||
|
.catch(this.warn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
this.writing = false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
admission(limit: number): Admission | undefined {
|
||||||
|
return this.active.size >= limit ? this.snapshot(limit) : undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
private snapshot(limit: number): Admission {
|
||||||
|
return {
|
||||||
|
queue: this.queue,
|
||||||
|
limit,
|
||||||
|
occupied: this.active.size,
|
||||||
|
holders: [...this.active].slice(0, 8),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
track<T>(job: () => Promise<T>, occupant: Occupant, db?: Sequelize) {
|
||||||
|
return async () => {
|
||||||
|
this.forget(occupant.id);
|
||||||
|
this.active.add(occupant);
|
||||||
|
this.refresh();
|
||||||
|
if (occupant.id && db) {
|
||||||
|
void db
|
||||||
|
.query(
|
||||||
|
'UPDATE CronTriggers SET dequeued_at=$at,queue_live_context=NULL WHERE id=$id AND queued_at <= $at AND dequeued_at IS NULL',
|
||||||
|
{ bind: { id: occupant.id, at: Date.now() } },
|
||||||
|
)
|
||||||
|
.catch(this.warn);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return await job();
|
||||||
|
} finally {
|
||||||
|
this.active.delete(occupant);
|
||||||
|
this.refresh();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function recordQueueAdmission(
|
||||||
|
db: Sequelize,
|
||||||
|
id: string,
|
||||||
|
admission: Admission,
|
||||||
|
) {
|
||||||
|
return db.query(
|
||||||
|
'UPDATE CronTriggers SET queue_context=$context WHERE id=$id AND queued_at <= $at AND queue_context IS NULL',
|
||||||
|
{ bind: { id, at: Date.now(), context: JSON.stringify(admission) } },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function oldestQueued(db: Sequelize) {
|
||||||
|
return db.query<{
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
source: string;
|
||||||
|
queued_at: number;
|
||||||
|
queue_context: string | null;
|
||||||
|
queue_live_context: string | null;
|
||||||
|
}>(
|
||||||
|
`SELECT t.id, COALESCE(NULLIF(c.name,''), c.command, '#' || t.cron_id) AS name, t.source,t.queued_at,t.queue_context,t.queue_live_context
|
||||||
|
FROM CronTriggers t LEFT JOIN Crontabs c ON c.id=t.cron_id
|
||||||
|
WHERE t.state='queued' AND t.dequeued_at IS NULL AND t.queued_at IS NOT NULL ORDER BY t.queued_at,t.id LIMIT 25`,
|
||||||
|
{ type: QueryTypes.SELECT },
|
||||||
|
);
|
||||||
|
}
|
||||||
+105
-17
@@ -1,5 +1,9 @@
|
|||||||
|
import { hasLiveCronProcess } from './cronProcess';
|
||||||
|
import { EventEmitter } from 'events';
|
||||||
|
import PQueue from 'p-queue-cjs';
|
||||||
|
import { triggerHistory, TriggerState } from './triggerHistory';
|
||||||
import { t } from './i18n';
|
import { t } from './i18n';
|
||||||
import { spawn } from 'cross-spawn';
|
import { spawnCronCommand, releaseCronCommand } from './cronCommandProcess';
|
||||||
import taskLimit from './pLimit';
|
import taskLimit from './pLimit';
|
||||||
import Logger from '../loaders/logger';
|
import Logger from '../loaders/logger';
|
||||||
import { ICron } from '../protos/cron';
|
import { ICron } from '../protos/cron';
|
||||||
@@ -8,14 +12,51 @@ import { RunningInstanceModel, InstanceStatus } from '../data/runningInstance';
|
|||||||
import dayjs from 'dayjs';
|
import dayjs from 'dayjs';
|
||||||
import { observeChildProcess, asError } from './childProcess';
|
import { observeChildProcess, asError } from './childProcess';
|
||||||
|
|
||||||
export function runCron(
|
const waiting = new Map<string, () => void>();
|
||||||
|
// A backlog of missed timers can otherwise open hundreds of competing SQLite
|
||||||
|
// writer transactions on wake. Only serialize history writes, never commands.
|
||||||
|
const historyWrites = new PQueue({ concurrency: 1 });
|
||||||
|
export function cancelWaitingTriggers(ids: string[]) {
|
||||||
|
for (const id of ids) waiting.get(id)?.();
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function runCron(
|
||||||
cmd: string,
|
cmd: string,
|
||||||
cron: ICron,
|
cron: ICron,
|
||||||
isCurrent: () => boolean = () => true,
|
isCurrent: () => boolean = () => true,
|
||||||
|
trigger?: { id: string; time: number; source: 'node' | 'recovery' },
|
||||||
): Promise<number | void> {
|
): Promise<number | void> {
|
||||||
|
const history = trigger && triggerHistory();
|
||||||
|
const queuedAt = Date.now();
|
||||||
|
if (trigger && history) {
|
||||||
|
const cronId = Number(cron.id);
|
||||||
|
const admitted = await historyWrites.add(() => cron.skip_if_pending
|
||||||
|
? history.enqueueUnlessPending(trigger.id, cronId, trigger.source, trigger.time, queuedAt, cron.recovery_from)
|
||||||
|
: history.enqueue(trigger.id, cronId, trigger.source, trigger.time, queuedAt));
|
||||||
|
if (!admitted) return;
|
||||||
|
}
|
||||||
|
let reason = 'capacity';
|
||||||
|
let state: TriggerState = 'skipped';
|
||||||
|
let exitCode: number | null = null;
|
||||||
|
let claimed = false;
|
||||||
|
const deadline = cron.max_queue_wait ? queuedAt + cron.max_queue_wait * 1000 : Infinity;
|
||||||
|
let timer: NodeJS.Timeout | undefined;
|
||||||
|
const expiry = trigger ? new EventEmitter() : undefined;
|
||||||
|
const alive = () => isCurrent() && Date.now() < deadline;
|
||||||
|
if (trigger && expiry && Number.isFinite(deadline)) {
|
||||||
|
timer = setTimeout(() => {
|
||||||
|
reason = 'queue_timeout';
|
||||||
|
taskLimit.removeQueuedCron(cron.id, execute);
|
||||||
|
expiry.emit('expired');
|
||||||
|
}, Math.max(0, deadline - Date.now()));
|
||||||
|
timer.unref();
|
||||||
|
}
|
||||||
const execute = async () => {
|
const execute = async () => {
|
||||||
|
if (trigger) waiting.delete(trigger.id);
|
||||||
|
reason = 'changed';
|
||||||
try {
|
try {
|
||||||
if (!isCurrent()) return;
|
if (!alive()) { reason = isCurrent() ? 'queue_timeout' : 'changed'; return; }
|
||||||
|
let multiple = false;
|
||||||
// A single-instance trigger must not interrupt work already in progress.
|
// A single-instance trigger must not interrupt work already in progress.
|
||||||
try {
|
try {
|
||||||
const existingCron = await CrontabModel.findOne({
|
const existingCron = await CrontabModel.findOne({
|
||||||
@@ -26,10 +67,9 @@ export function runCron(
|
|||||||
if (!isCurrent() || !existingCron || existingCron.isDisabled) return;
|
if (!isCurrent() || !existingCron || existingCron.isDisabled) return;
|
||||||
|
|
||||||
// Default to single instance mode (0) for backward compatibility
|
// Default to single instance mode (0) for backward compatibility
|
||||||
const allowSingleInstances =
|
multiple = existingCron.allow_multiple_instances === 1;
|
||||||
existingCron?.allow_multiple_instances === 0;
|
|
||||||
|
|
||||||
if (allowSingleInstances) {
|
if (!multiple) {
|
||||||
// A queued manual run is an active reservation, even if its stored PID
|
// A queued manual run is an active reservation, even if its stored PID
|
||||||
// belongs to a previous execution.
|
// belongs to a previous execution.
|
||||||
if (
|
if (
|
||||||
@@ -41,26 +81,20 @@ export function runCron(
|
|||||||
cron.id,
|
cron.id,
|
||||||
existingCron.pid,
|
existingCron.pid,
|
||||||
);
|
);
|
||||||
|
reason = 'overlap';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (
|
if (
|
||||||
existingCron.status === CrontabStatus.running &&
|
existingCron.status === CrontabStatus.running &&
|
||||||
existingCron.pid
|
existingCron.pid
|
||||||
) {
|
) {
|
||||||
let missing = false;
|
if (hasLiveCronProcess(existingCron.status, existingCron.pid)) {
|
||||||
try {
|
|
||||||
process.kill(existingCron.pid, 0);
|
|
||||||
} catch (error) {
|
|
||||||
const code = (error as NodeJS.ErrnoException).code;
|
|
||||||
if (code === 'ESRCH') missing = true;
|
|
||||||
else if (code !== 'EPERM') throw error;
|
|
||||||
}
|
|
||||||
if (!missing) {
|
|
||||||
Logger.info(
|
Logger.info(
|
||||||
'[schedule][Skip active single-instance task] Task ID: %s, PID: %s',
|
'[schedule][Skip active single-instance task] Task ID: %s, PID: %s',
|
||||||
cron.id,
|
cron.id,
|
||||||
existingCron.pid,
|
existingCron.pid,
|
||||||
);
|
);
|
||||||
|
reason = 'overlap';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -135,9 +169,39 @@ export function runCron(
|
|||||||
command: cmd,
|
command: cmd,
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
const cp = spawn(cmd, { shell: '/bin/bash' });
|
clearTimeout(timer);
|
||||||
|
if (Date.now() >= deadline) {
|
||||||
|
reason = 'queue_timeout';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (trigger) {
|
||||||
|
claimed = await history!.start(trigger.id, Date.now(), multiple);
|
||||||
|
if (!claimed) { reason = 'overlap'; return; }
|
||||||
|
}
|
||||||
|
if (!isCurrent() || Date.now() >= deadline) {
|
||||||
|
reason = isCurrent() ? 'queue_timeout' : 'changed';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let saveStart: Promise<void> | undefined;
|
||||||
|
const cp = spawnCronCommand(cmd, trigger?.id, trigger && history ? at => {
|
||||||
|
saveStart = history.recordCommandStart(trigger.id, at).catch(error => {
|
||||||
|
Logger.warn('[schedule] Could not record command start: %s', asError(error).message);
|
||||||
|
});
|
||||||
|
} : undefined);
|
||||||
|
|
||||||
|
let denied: string | undefined;
|
||||||
const { completed } = observeChildProcess(cp, {
|
const { completed } = observeChildProcess(cp, {
|
||||||
|
onStart: async () => {
|
||||||
|
if (!trigger) return;
|
||||||
|
try {
|
||||||
|
if (await history!.bindCommand(trigger.id, cp.pid!) && !await history!.shouldStop(trigger.id)) {
|
||||||
|
if (!alive()) denied = isCurrent() ? 'queue_timeout' : 'changed';
|
||||||
|
else await releaseCronCommand(cp);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
cp.stdin.end();
|
||||||
|
}
|
||||||
|
},
|
||||||
onStderr: async (message) => {
|
onStderr: async (message) => {
|
||||||
Logger.info(
|
Logger.info(
|
||||||
t('[schedule][任务标准错误] 命令: %s, 信息: %s'),
|
t('[schedule][任务标准错误] 命令: %s, 信息: %s'),
|
||||||
@@ -147,6 +211,11 @@ export function runCron(
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
const result = await completed;
|
const result = await completed;
|
||||||
|
await saveStart;
|
||||||
|
if (denied) { reason = denied; return; }
|
||||||
|
state = result.signal ? 'stopped' : result.error || result.code !== 0 ? 'failed' : 'finished';
|
||||||
|
reason = result.error ? 'spawn' : result.signal || '';
|
||||||
|
exitCode = result.code;
|
||||||
if (result.error) {
|
if (result.error) {
|
||||||
Logger.error(
|
Logger.error(
|
||||||
t('[schedule][执行任务失败] 命令: %s, 错误: %s'),
|
t('[schedule][执行任务失败] 命令: %s, 错误: %s'),
|
||||||
@@ -161,6 +230,8 @@ export function runCron(
|
|||||||
);
|
);
|
||||||
return { ...cron, command: cmd, pid: cp.pid, ...result } as any;
|
return { ...cron, command: cmd, pid: cp.pid, ...result } as any;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
state = 'failed';
|
||||||
|
reason = 'spawn';
|
||||||
Logger.error(
|
Logger.error(
|
||||||
t('[schedule][创建任务失败] 命令: %s, 错误: %s'),
|
t('[schedule][创建任务失败] 命令: %s, 错误: %s'),
|
||||||
cmd,
|
cmd,
|
||||||
@@ -170,5 +241,22 @@ export function runCron(
|
|||||||
taskLimit.removeQueuedCron(cron.id, execute);
|
taskLimit.removeQueuedCron(cron.id, execute);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
return taskLimit.runWithCronLimit(cron, execute, undefined, isCurrent);
|
try {
|
||||||
|
if (trigger && expiry) waiting.set(trigger.id, () => {
|
||||||
|
taskLimit.removeQueuedCron(cron.id, execute);
|
||||||
|
expiry.emit('expired');
|
||||||
|
});
|
||||||
|
return await taskLimit.runWithCronLimit(cron, execute, undefined, isCurrent, () => { reason = 'pending'; }, expiry, trigger?.id, CrontabModel.sequelize);
|
||||||
|
} finally {
|
||||||
|
clearTimeout(timer);
|
||||||
|
if (trigger) waiting.delete(trigger.id);
|
||||||
|
if (trigger && history) {
|
||||||
|
const outcomeReason = state === 'skipped' && !isCurrent() ? 'changed' : reason;
|
||||||
|
const finishedAt = Date.now();
|
||||||
|
await historyWrites.add(() => state === 'skipped' && !claimed
|
||||||
|
? history.skipQueued(trigger.id, outcomeReason, finishedAt)
|
||||||
|
: history.complete(trigger.id, state, outcomeReason || null, exitCode, finishedAt),
|
||||||
|
{ priority: 1 });
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,289 @@
|
|||||||
|
import { QueryTypes, Sequelize, Transaction } from 'sequelize';
|
||||||
|
import { AddCronRequest, DeleteCronRequest } from '../protos/cron';
|
||||||
|
import { upcomingSlots } from './cronScheduler';
|
||||||
|
import { slotId } from './triggerHistory';
|
||||||
|
|
||||||
|
type Rule = {
|
||||||
|
cron_id: string;
|
||||||
|
rule_index: number;
|
||||||
|
schedule: string;
|
||||||
|
zone: string;
|
||||||
|
since: number;
|
||||||
|
until: number | null;
|
||||||
|
open_latest: number;
|
||||||
|
close_latest: number;
|
||||||
|
};
|
||||||
|
type Receipt = {
|
||||||
|
id: string;
|
||||||
|
cron_id: number;
|
||||||
|
scheduled_at: number;
|
||||||
|
observed_at: number;
|
||||||
|
state: string;
|
||||||
|
command_started_at: number | null;
|
||||||
|
verified_exit: number;
|
||||||
|
script_started_at: number | null;
|
||||||
|
queued_at: number | null;
|
||||||
|
started_at: number | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
export async function saveSchedulePlan(
|
||||||
|
db: Sequelize,
|
||||||
|
change: AddCronRequest | DeleteCronRequest,
|
||||||
|
at?: number,
|
||||||
|
token?: string,
|
||||||
|
) {
|
||||||
|
const rules =
|
||||||
|
'crons' in change
|
||||||
|
? [
|
||||||
|
...new Map(change.crons.map((task) => [task.id, task])).values(),
|
||||||
|
].flatMap((task) =>
|
||||||
|
[
|
||||||
|
task.schedule,
|
||||||
|
...(task.extra_schedules || []).map((extra) => extra.schedule),
|
||||||
|
].map((schedule, index) => ({ task: task.id, index, schedule })),
|
||||||
|
)
|
||||||
|
: [];
|
||||||
|
const ids =
|
||||||
|
'crons' in change ? change.crons.map((task) => task.id) : change.ids;
|
||||||
|
const replace = 'replace' in change && change.replace;
|
||||||
|
await db.transaction(async (transaction) => {
|
||||||
|
const now = at ?? Date.now();
|
||||||
|
const bind = {
|
||||||
|
rules: JSON.stringify(rules),
|
||||||
|
ids: JSON.stringify(ids),
|
||||||
|
replace: Number(replace),
|
||||||
|
zone: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
||||||
|
now,
|
||||||
|
token: token ?? null,
|
||||||
|
};
|
||||||
|
if (token)
|
||||||
|
await db.query(
|
||||||
|
'INSERT INTO CronPlanSwitches(token,staged_at) VALUES($token,$now)',
|
||||||
|
{ transaction, bind: { token, now } },
|
||||||
|
);
|
||||||
|
if (replace)
|
||||||
|
await db.query(
|
||||||
|
'INSERT OR IGNORE INTO CronPlanCoverage(id,since) VALUES(1,$now)',
|
||||||
|
{ transaction, bind: { now } },
|
||||||
|
);
|
||||||
|
const desired = `WITH desired AS (SELECT json_extract(value,'$.task') AS task, json_extract(value,'$.index') AS idx,
|
||||||
|
json_extract(value,'$.schedule') AS schedule FROM json_each($rules))`;
|
||||||
|
await db.query(
|
||||||
|
`${desired} UPDATE CronPlanRules SET until=$now,closed_by=$token WHERE until IS NULL
|
||||||
|
AND ($replace OR cron_id IN (SELECT value FROM json_each($ids)))
|
||||||
|
AND (cron_id,rule_index,schedule,zone) NOT IN (SELECT task,idx,schedule,$zone FROM desired)`,
|
||||||
|
{ transaction, bind },
|
||||||
|
);
|
||||||
|
await db.query(
|
||||||
|
`${desired} INSERT OR IGNORE INTO CronPlanRules(cron_id,rule_index,schedule,zone,since,opened_by)
|
||||||
|
SELECT task,idx,schedule,$zone,$now,$token FROM desired`,
|
||||||
|
{
|
||||||
|
transaction,
|
||||||
|
bind: { rules: bind.rules, zone: bind.zone, now, token: bind.token },
|
||||||
|
},
|
||||||
|
);
|
||||||
|
// Only the current local day is displayed. Keep an extra day for zone changes.
|
||||||
|
await db.query(
|
||||||
|
`DELETE FROM CronPlanRules WHERE until < $now - 3*86400000 AND
|
||||||
|
(closed_by IS NULL OR closed_by IN (SELECT token FROM CronPlanSwitches WHERE last_at < $now - 3*86400000))`,
|
||||||
|
{ transaction, bind: { now } },
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Persisting intent and installing timers are separated by an asynchronous
|
||||||
|
// SQLite commit. Only the synchronous timer swap defines the effective edges.
|
||||||
|
export async function confirmSchedulePlan(
|
||||||
|
db: Sequelize,
|
||||||
|
token: string,
|
||||||
|
first: number,
|
||||||
|
last: number,
|
||||||
|
replace: boolean,
|
||||||
|
) {
|
||||||
|
await db.transaction(async (transaction) => {
|
||||||
|
await db.query(
|
||||||
|
`UPDATE CronPlanSwitches SET first_at=$first,last_at=$last
|
||||||
|
WHERE token=$token AND last_at IS NULL`,
|
||||||
|
{ transaction, bind: { token, first, last } },
|
||||||
|
);
|
||||||
|
if (replace) {
|
||||||
|
// After a crash the former worker's switch time is unknowable. A complete
|
||||||
|
// replacement establishes the latest possible edge for older requests.
|
||||||
|
await db.query(
|
||||||
|
`UPDATE CronPlanSwitches SET first_at=staged_at,last_at=$last
|
||||||
|
WHERE last_at IS NULL AND seq<(SELECT seq FROM CronPlanSwitches WHERE token=$token)`,
|
||||||
|
{ transaction, bind: { token, last } },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
await db.query(
|
||||||
|
`DELETE FROM CronPlanSwitches WHERE last_at < $last - 3*86400000
|
||||||
|
AND token NOT IN (SELECT opened_by FROM CronPlanRules WHERE opened_by IS NOT NULL)
|
||||||
|
AND token NOT IN (SELECT closed_by FROM CronPlanRules WHERE closed_by IS NOT NULL)`,
|
||||||
|
{ transaction, bind: { last } },
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export const blockSize = 15 * 60000;
|
||||||
|
const calendar = new Map<string, number[]>();
|
||||||
|
export function blockSlots(schedule: string, zone: string, block: number) {
|
||||||
|
const key = JSON.stringify([zone, schedule, block]);
|
||||||
|
let times = calendar.get(key);
|
||||||
|
if (!times) {
|
||||||
|
times = upcomingSlots(schedule, block - 1, Infinity, block + blockSize - 1);
|
||||||
|
if (calendar.size >= 2048) calendar.delete(calendar.keys().next().value!);
|
||||||
|
calendar.set(key, times);
|
||||||
|
}
|
||||||
|
return times;
|
||||||
|
}
|
||||||
|
|
||||||
|
function lowerBound(times: number[], time: number) {
|
||||||
|
let lo = 0,
|
||||||
|
hi = times.length;
|
||||||
|
while (lo < hi) {
|
||||||
|
const mid = Math.floor((lo + hi) / 2);
|
||||||
|
if (times[mid] < time) lo = mid + 1;
|
||||||
|
else hi = mid;
|
||||||
|
}
|
||||||
|
return lo;
|
||||||
|
}
|
||||||
|
|
||||||
|
function p95(values: number[]) {
|
||||||
|
return values.length
|
||||||
|
? values.sort((a, b) => a - b)[Math.ceil(values.length * 0.95) - 1]
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function auditSchedule(
|
||||||
|
db: Sequelize,
|
||||||
|
dayStart: number,
|
||||||
|
now = Date.now(),
|
||||||
|
) {
|
||||||
|
// All counts use the same settled plan window and the same committed snapshot.
|
||||||
|
const nextDay = new Date(dayStart);
|
||||||
|
nextDay.setHours(24, 0, 0, 0);
|
||||||
|
const through = Math.min(now - 5000, nextDay.getTime() - 1);
|
||||||
|
const snapshot = await db.transaction(
|
||||||
|
{ type: Transaction.TYPES.DEFERRED },
|
||||||
|
async (transaction) => {
|
||||||
|
const [coverage] = await db.query<{ since: number }>(
|
||||||
|
`SELECT MAX(since,$dayStart,
|
||||||
|
COALESCE((SELECT cutoff FROM CronHistoryRetention WHERE id=1),0)) AS since FROM CronPlanCoverage WHERE id=1`,
|
||||||
|
{ transaction, bind: { dayStart }, type: QueryTypes.SELECT },
|
||||||
|
);
|
||||||
|
if (!coverage || coverage.since > through) return;
|
||||||
|
const bind = { since: coverage.since, through };
|
||||||
|
const rules = await db.query<Rule>(
|
||||||
|
`SELECT r.cron_id,r.rule_index,r.schedule,r.zone,COALESCE(o.first_at,r.since) AS since,
|
||||||
|
CASE WHEN o.token IS NULL THEN r.since ELSE COALESCE(o.last_at,$through) END AS open_latest,
|
||||||
|
COALESCE(c.first_at,r.until) AS until,
|
||||||
|
CASE WHEN c.token IS NULL THEN COALESCE(r.until,$through) ELSE COALESCE(c.last_at,$through) END AS close_latest
|
||||||
|
FROM CronPlanRules r LEFT JOIN CronPlanSwitches o ON o.token=r.opened_by
|
||||||
|
LEFT JOIN CronPlanSwitches c ON c.token=r.closed_by
|
||||||
|
WHERE r.since < $through AND close_latest >= $since`,
|
||||||
|
{ transaction, bind, type: QueryTypes.SELECT },
|
||||||
|
);
|
||||||
|
const receipts = await db.query<Receipt>(
|
||||||
|
`SELECT id,cron_id,scheduled_at,observed_at,state,
|
||||||
|
command_started_at,script_started_at,queued_at,COALESCE(dequeued_at,started_at) AS started_at,
|
||||||
|
EXISTS(SELECT 1 FROM CronLegacyExitBookings b WHERE b.trigger_id=CronTriggers.id AND b.bound=1) AS verified_exit FROM CronTriggers
|
||||||
|
WHERE scheduled_at BETWEEN $since AND $through AND source IN ('node','recovery')`,
|
||||||
|
{ transaction, bind, type: QueryTypes.SELECT },
|
||||||
|
);
|
||||||
|
return { since: coverage.since, rules, receipts };
|
||||||
|
},
|
||||||
|
);
|
||||||
|
if (!snapshot) return null;
|
||||||
|
const byTaskBlock = new Map<string, Receipt[]>();
|
||||||
|
for (const row of snapshot.receipts) {
|
||||||
|
const block = row.scheduled_at - (row.scheduled_at % blockSize);
|
||||||
|
const key = row.cron_id + '/' + block;
|
||||||
|
const group = byTaskBlock.get(key);
|
||||||
|
if (group) group.push(row);
|
||||||
|
else byTaskBlock.set(key, [row]);
|
||||||
|
}
|
||||||
|
const deviations: number[] = [],
|
||||||
|
starts: number[] = [],
|
||||||
|
entries: number[] = [],
|
||||||
|
waits: number[] = [];
|
||||||
|
const zone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
||||||
|
let partial = false;
|
||||||
|
let expected = 0,
|
||||||
|
onTime = 0,
|
||||||
|
skipped = 0,
|
||||||
|
queued = 0,
|
||||||
|
dispatched = 0;
|
||||||
|
for (const rule of snapshot.rules) {
|
||||||
|
const first = Math.max(snapshot.since, rule.since + 1);
|
||||||
|
const last = Math.min(through, rule.close_latest);
|
||||||
|
const certainFirst = Math.max(first, rule.open_latest + 1);
|
||||||
|
const certainLast = Math.min(last, rule.until ?? last);
|
||||||
|
if (first > last) continue;
|
||||||
|
// A wall clock rule registered in another time zone cannot be replayed
|
||||||
|
// against the current local calendar.
|
||||||
|
if (rule.zone !== zone) return null;
|
||||||
|
for (
|
||||||
|
let block = first - (first % blockSize);
|
||||||
|
block <= last;
|
||||||
|
block += blockSize
|
||||||
|
) {
|
||||||
|
const times = blockSlots(rule.schedule, zone, block);
|
||||||
|
const possible = lowerBound(times, last + 1) - lowerBound(times, first);
|
||||||
|
const certain = Math.max(
|
||||||
|
0,
|
||||||
|
lowerBound(times, certainLast + 1) - lowerBound(times, certainFirst),
|
||||||
|
);
|
||||||
|
if (certain < possible) partial = true;
|
||||||
|
expected += certain;
|
||||||
|
for (const row of byTaskBlock.get(rule.cron_id + '/' + block) ?? []) {
|
||||||
|
const time = row.scheduled_at;
|
||||||
|
if (
|
||||||
|
time < certainFirst ||
|
||||||
|
time > certainLast ||
|
||||||
|
times[lowerBound(times, time)] !== time ||
|
||||||
|
row.id !== slotId(rule.cron_id, rule.rule_index, rule.schedule, time)
|
||||||
|
)
|
||||||
|
continue;
|
||||||
|
const deviation = Math.abs(row.observed_at - time);
|
||||||
|
deviations.push(deviation);
|
||||||
|
if (deviation <= 5000) onTime++;
|
||||||
|
if (
|
||||||
|
row.command_started_at != null ||
|
||||||
|
row.script_started_at != null ||
|
||||||
|
row.verified_exit
|
||||||
|
) {
|
||||||
|
dispatched++;
|
||||||
|
if (row.command_started_at != null && row.command_started_at >= time)
|
||||||
|
starts.push(row.command_started_at - time);
|
||||||
|
} else if (row.state === 'skipped') skipped++;
|
||||||
|
else if (row.state === 'queued') queued++;
|
||||||
|
if (row.script_started_at != null && row.script_started_at >= time)
|
||||||
|
entries.push(row.script_started_at - time);
|
||||||
|
if (row.started_at != null && row.queued_at != null && row.started_at >= row.queued_at)
|
||||||
|
waits.push(row.started_at - row.queued_at);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const observed = deviations.length;
|
||||||
|
return {
|
||||||
|
since: snapshot.since,
|
||||||
|
through,
|
||||||
|
zone,
|
||||||
|
partial,
|
||||||
|
expected,
|
||||||
|
observed,
|
||||||
|
missing: expected - observed,
|
||||||
|
onTime,
|
||||||
|
skipped,
|
||||||
|
queued,
|
||||||
|
dispatched,
|
||||||
|
commandSamples: starts.length,
|
||||||
|
unconfirmed: observed - dispatched - skipped - queued,
|
||||||
|
triggerDeviationP95: p95(deviations),
|
||||||
|
waitP95: p95(waits),
|
||||||
|
waitSamples: waits.length,
|
||||||
|
commandStartP95: p95(starts),
|
||||||
|
scriptStartP95: p95(entries),
|
||||||
|
scriptSamples: entries.length,
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,5 +1,10 @@
|
|||||||
|
import dayjs from 'dayjs';
|
||||||
|
import { installTriggerHistory } from './triggerHistory';
|
||||||
|
import { repairLegacyDailyStatistics } from './legacyDailyStatistics';
|
||||||
import { QueryTypes, Sequelize } from 'sequelize';
|
import { QueryTypes, Sequelize } from 'sequelize';
|
||||||
|
|
||||||
|
export const prunedExitMigrationId = 'preserve-pruned-bound-exits-v1';
|
||||||
|
|
||||||
// Append new entries; IDs are persisted and must not be renumbered or reused.
|
// Append new entries; IDs are persisted and must not be renumbered or reused.
|
||||||
const columns = [
|
const columns = [
|
||||||
{
|
{
|
||||||
@@ -25,6 +30,13 @@ const columns = [
|
|||||||
{ table: 'Envs', column: 'isPinned', type: 'NUMBER' },
|
{ table: 'Envs', column: 'isPinned', type: 'NUMBER' },
|
||||||
{ table: 'Envs', column: 'labels', type: 'JSON' },
|
{ table: 'Envs', column: 'labels', type: 'JSON' },
|
||||||
{ table: 'Crontabs', column: 'queued_token', type: 'VARCHAR(255)' },
|
{ table: 'Crontabs', column: 'queued_token', type: 'VARCHAR(255)' },
|
||||||
|
{ table: 'Crontabs', column: 'max_queue_wait', type: 'INTEGER' },
|
||||||
|
{ table: 'Crontabs', column: 'catchup_seconds', type: 'INTEGER' },
|
||||||
|
{ table: 'Crontabs', column: 'skip_if_pending', type: 'BOOLEAN' },
|
||||||
|
{ table: 'Crontabs', column: 'recovery_from', type: 'BIGINT' },
|
||||||
|
{ table: 'Crontabs', column: 'last_instance_id', type: 'INTEGER' },
|
||||||
|
{ table: 'CrontabStats', column: 'untimed_count', type: 'INTEGER NOT NULL DEFAULT 0' },
|
||||||
|
{ table: 'CrontabStats', column: 'fixed_max_time', type: 'NUMBER' },
|
||||||
];
|
];
|
||||||
|
|
||||||
export async function migrateSchema(database: Sequelize): Promise<void> {
|
export async function migrateSchema(database: Sequelize): Promise<void> {
|
||||||
@@ -33,6 +45,7 @@ export async function migrateSchema(database: Sequelize): Promise<void> {
|
|||||||
'CREATE TABLE IF NOT EXISTS "SchemaMigrations" ("id" TEXT PRIMARY KEY, "applied_at" TEXT NOT NULL)',
|
'CREATE TABLE IF NOT EXISTS "SchemaMigrations" ("id" TEXT PRIMARY KEY, "applied_at" TEXT NOT NULL)',
|
||||||
{ transaction },
|
{ transaction },
|
||||||
);
|
);
|
||||||
|
await installTriggerHistory(database, transaction);
|
||||||
const applied = await database.query<{ id: string }>(
|
const applied = await database.query<{ id: string }>(
|
||||||
'SELECT "id" FROM "SchemaMigrations"',
|
'SELECT "id" FROM "SchemaMigrations"',
|
||||||
{ type: QueryTypes.SELECT, transaction },
|
{ type: QueryTypes.SELECT, transaction },
|
||||||
@@ -66,5 +79,52 @@ export async function migrateSchema(database: Sequelize): Promise<void> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const repairId = 'repair-legacy-daily-statistics-v3';
|
||||||
|
const prior = appliedIds.has('repair-legacy-daily-statistics-v2') ? 2
|
||||||
|
: appliedIds.has('repair-legacy-daily-statistics-v1') ? 1 : 0;
|
||||||
|
const retentionId = 'preserve-cron-stat-retention-v1';
|
||||||
|
if (!appliedIds.has(retentionId)) {
|
||||||
|
if (prior || appliedIds.has(repairId)) {
|
||||||
|
// Old cleanup removed complete date prefixes but kept no cutoff. Every
|
||||||
|
// surviving date and yesterday (the minimum supported retention) is
|
||||||
|
// safe to keep. Older empty dates cannot be distinguished from deletions.
|
||||||
|
await database.query(`INSERT OR IGNORE INTO CronStatRetention(id,first_day)
|
||||||
|
SELECT 1, MIN(COALESCE(MIN(date),$yesterday),$yesterday) FROM CrontabStats`,
|
||||||
|
{ bind: { yesterday: dayjs().subtract(1,'day').format('YYYY-MM-DD') }, transaction });
|
||||||
|
}
|
||||||
|
await database.query('INSERT INTO SchemaMigrations(id,applied_at) VALUES ($id,CURRENT_TIMESTAMP)',
|
||||||
|
{ bind: { id: retentionId }, transaction });
|
||||||
|
}
|
||||||
|
if (!appliedIds.has(repairId)) {
|
||||||
|
await repairLegacyDailyStatistics(database, transaction, prior);
|
||||||
|
// This reconciliation includes v1 and v2. Record all on fresh installations
|
||||||
|
// so rolling back and upgrading again cannot replay the superseded repair.
|
||||||
|
await database.query(`INSERT OR IGNORE INTO SchemaMigrations(id,applied_at)
|
||||||
|
VALUES ('repair-legacy-daily-statistics-v1',CURRENT_TIMESTAMP),('repair-legacy-daily-statistics-v2',CURRENT_TIMESTAMP),($id,CURRENT_TIMESTAMP)`,
|
||||||
|
{ transaction, bind: { id: repairId } });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!appliedIds.has(prunedExitMigrationId)) {
|
||||||
|
// Earlier versions could reclaim the last file before upgrading. The
|
||||||
|
// surviving bound booking still identifies the original exit exactly.
|
||||||
|
await database.query(`INSERT OR IGNORE INTO CronUnmatchedBoundExits(trigger_id,outcome,finished_at)
|
||||||
|
SELECT b.trigger_id,b.outcome,b.finished_at FROM CronLegacyExitBookings b
|
||||||
|
WHERE b.bound=1 AND NOT EXISTS (SELECT 1 FROM CronTriggers WHERE id=b.trigger_id)
|
||||||
|
AND NOT EXISTS (SELECT 1 FROM CronBoundExitConflicts WHERE trigger_id=b.trigger_id)`, { transaction });
|
||||||
|
await database.query('INSERT INTO SchemaMigrations(id,applied_at) VALUES ($id,CURRENT_TIMESTAMP)',
|
||||||
|
{ bind: { id: prunedExitMigrationId }, transaction });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Older databases can contain non-JSON values in Envs.labels. Sequelize
|
||||||
|
// parses this column on every environment read, including HTTP startup.
|
||||||
|
await database.query(
|
||||||
|
`UPDATE "Envs" SET "labels" = '[]'
|
||||||
|
WHERE CASE WHEN json_valid("labels")
|
||||||
|
THEN json_type("labels") <> 'array'
|
||||||
|
ELSE 1
|
||||||
|
END`,
|
||||||
|
{ transaction },
|
||||||
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,320 @@
|
|||||||
|
import { QueryTypes, Sequelize, Transaction } from 'sequelize';
|
||||||
|
import { blockSize, blockSlots } from './scheduleAudit';
|
||||||
|
|
||||||
|
export type SystemRule = { id: number; schedule: string; name?: string };
|
||||||
|
type Install = {
|
||||||
|
id: number;
|
||||||
|
staged: number;
|
||||||
|
confirmed: number | null;
|
||||||
|
zone: string;
|
||||||
|
rules: string;
|
||||||
|
};
|
||||||
|
type Receipt = {
|
||||||
|
id: string;
|
||||||
|
cron_id: number;
|
||||||
|
system_rule: string | null;
|
||||||
|
observed_at: number;
|
||||||
|
system_dispatch_at: number | null;
|
||||||
|
launched: number;
|
||||||
|
state: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export async function stageSystemPlan(
|
||||||
|
db: Sequelize,
|
||||||
|
rules: SystemRule[],
|
||||||
|
at = Date.now(),
|
||||||
|
) {
|
||||||
|
const bind = {
|
||||||
|
at,
|
||||||
|
zone: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
||||||
|
rules: JSON.stringify([...rules].sort((a, b) => a.id - b.id)),
|
||||||
|
};
|
||||||
|
return db.transaction(
|
||||||
|
{ type: Transaction.TYPES.IMMEDIATE },
|
||||||
|
async (transaction) => {
|
||||||
|
const [pending] = await db.query<{ id: number }>(
|
||||||
|
`SELECT id FROM (SELECT * FROM CronSystemPlans ORDER BY id DESC LIMIT 1)
|
||||||
|
WHERE confirmed IS NULL AND zone=$zone AND rules=$rules`,
|
||||||
|
{
|
||||||
|
bind: { zone: bind.zone, rules: bind.rules },
|
||||||
|
transaction,
|
||||||
|
type: QueryTypes.SELECT,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
if (pending) return pending.id;
|
||||||
|
const [id] = await db.query(
|
||||||
|
'INSERT INTO CronSystemPlans(staged,zone,rules) VALUES ($at,$zone,$rules)',
|
||||||
|
{ bind, transaction, type: QueryTypes.INSERT },
|
||||||
|
);
|
||||||
|
return id;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// On a switch to Node the service can no longer attest the installed daemon rules.
|
||||||
|
export async function suspendSystemPlan(db: Sequelize) {
|
||||||
|
await db.query(
|
||||||
|
`INSERT INTO CronSystemPlans(staged,zone,rules)
|
||||||
|
SELECT $now,zone,'[]' FROM (SELECT zone,confirmed FROM CronSystemPlans ORDER BY id DESC LIMIT 1)
|
||||||
|
WHERE confirmed IS NOT NULL`,
|
||||||
|
{ bind: { now: Date.now() } },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function confirmSystemPlan(
|
||||||
|
db: Sequelize,
|
||||||
|
id: number,
|
||||||
|
at = Date.now(),
|
||||||
|
) {
|
||||||
|
await db.transaction(async (transaction) => {
|
||||||
|
await db.query(
|
||||||
|
'UPDATE CronSystemPlans SET confirmed=$at WHERE id=$id AND confirmed IS NULL',
|
||||||
|
{ transaction, bind: { id, at } },
|
||||||
|
);
|
||||||
|
// Keep the effective snapshot preceding the retention window.
|
||||||
|
await db.query(
|
||||||
|
'DELETE FROM CronSystemPlans WHERE id < (SELECT MAX(id) FROM CronSystemPlans WHERE staged < $cutoff AND confirmed IS NOT NULL)',
|
||||||
|
{ transaction, bind: { cutoff: at - 3 * 86400000 } },
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function systemScheduleAudit(
|
||||||
|
db: Sequelize,
|
||||||
|
dayStart: number,
|
||||||
|
now = Date.now(),
|
||||||
|
) {
|
||||||
|
const next = new Date(dayStart);
|
||||||
|
next.setHours(24, 0, 0, 0);
|
||||||
|
const through = Math.min(now - 5000, +next - 1);
|
||||||
|
const { since, installs, receipts } = await db.transaction(
|
||||||
|
{ type: Transaction.TYPES.DEFERRED },
|
||||||
|
async (transaction) => {
|
||||||
|
const [retention] = await db.query<{ since: number }>(
|
||||||
|
'SELECT MAX($dayStart,COALESCE((SELECT cutoff FROM CronHistoryRetention WHERE id=1),0)) AS since',
|
||||||
|
{ transaction, bind: { dayStart }, type: QueryTypes.SELECT },
|
||||||
|
);
|
||||||
|
const since = retention.since;
|
||||||
|
const installs = await db.query<Install>(
|
||||||
|
`SELECT * FROM CronSystemPlans WHERE id >= COALESCE((SELECT MAX(id) FROM CronSystemPlans
|
||||||
|
WHERE confirmed < $since - 60000),0) AND staged <= $now ORDER BY id`,
|
||||||
|
{ transaction, bind: { since, now }, type: QueryTypes.SELECT },
|
||||||
|
);
|
||||||
|
const receipts = await db.query<Receipt>(
|
||||||
|
`SELECT id,cron_id,system_rule,system_dispatch_at,observed_at,state,
|
||||||
|
(command_started_at IS NOT NULL OR script_started_at IS NOT NULL OR
|
||||||
|
(started_at IS NOT NULL AND EXISTS (SELECT 1 FROM CronLegacyExitBookings b
|
||||||
|
WHERE b.trigger_id=CronTriggers.id AND b.bound=1 AND b.disputed=0))) AS launched
|
||||||
|
FROM CronTriggers
|
||||||
|
WHERE source='system' AND COALESCE(system_dispatch_at,observed_at) >= $since
|
||||||
|
AND COALESCE(system_dispatch_at,observed_at) < $end ORDER BY observed_at,id`,
|
||||||
|
{
|
||||||
|
transaction,
|
||||||
|
bind: {
|
||||||
|
since,
|
||||||
|
end: Math.min(
|
||||||
|
+next,
|
||||||
|
Math.floor(through / 60000) * 60000 + 60000,
|
||||||
|
now + 1,
|
||||||
|
),
|
||||||
|
},
|
||||||
|
type: QueryTypes.SELECT,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
return { since, installs, receipts };
|
||||||
|
},
|
||||||
|
);
|
||||||
|
if (!installs.length && !receipts.length) return null;
|
||||||
|
const zone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
||||||
|
const minute = 60000;
|
||||||
|
const plans = installs.map((plan) => ({
|
||||||
|
...plan,
|
||||||
|
tasks: new Map(
|
||||||
|
(JSON.parse(plan.rules) as SystemRule[]).map((rule) => [rule.id, rule]),
|
||||||
|
),
|
||||||
|
}));
|
||||||
|
const groups = new Map<string, Receipt[]>();
|
||||||
|
const dispatchedTasks = new Set<string>();
|
||||||
|
const legacyThrough = new Map<string, number>();
|
||||||
|
const receiptKey = (task: number, rule: string | null, time: number) =>
|
||||||
|
JSON.stringify([task, rule, time]);
|
||||||
|
for (const row of receipts) {
|
||||||
|
if (row.system_dispatch_at == null || !row.system_rule) {
|
||||||
|
const key = JSON.stringify([row.cron_id, row.system_rule]);
|
||||||
|
legacyThrough.set(
|
||||||
|
key,
|
||||||
|
Math.max(legacyThrough.get(key) ?? 0, row.observed_at),
|
||||||
|
);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
dispatchedTasks.add(
|
||||||
|
row.cron_id + '/' + Math.floor(row.system_dispatch_at / minute),
|
||||||
|
);
|
||||||
|
const key = receiptKey(
|
||||||
|
row.cron_id,
|
||||||
|
row.system_rule,
|
||||||
|
Math.floor(row.system_dispatch_at / minute) * minute,
|
||||||
|
);
|
||||||
|
const group = groups.get(key) ?? [];
|
||||||
|
group.push(row);
|
||||||
|
groups.set(key, group);
|
||||||
|
}
|
||||||
|
let expected = 0,
|
||||||
|
observed = 0,
|
||||||
|
dispatched = 0,
|
||||||
|
skipped = 0,
|
||||||
|
missing = 0,
|
||||||
|
duplicates = 0,
|
||||||
|
matchedReceipts = 0;
|
||||||
|
let uncertain = 0,
|
||||||
|
awaiting = 0,
|
||||||
|
switchSlots = 0,
|
||||||
|
partial = false;
|
||||||
|
type Slot = {
|
||||||
|
time: number;
|
||||||
|
task: number;
|
||||||
|
name?: string;
|
||||||
|
status: string;
|
||||||
|
triggerCount: number;
|
||||||
|
startCount: number;
|
||||||
|
skipCount: number;
|
||||||
|
unknownExitCount: number;
|
||||||
|
id?: string;
|
||||||
|
};
|
||||||
|
const recent: Slot[] = [];
|
||||||
|
const dst =
|
||||||
|
new Date(dayStart).getTimezoneOffset() !==
|
||||||
|
new Date(+next - 1).getTimezoneOffset();
|
||||||
|
let baseline = -1,
|
||||||
|
last = -1;
|
||||||
|
const calendar = new Map<string, Set<number>>();
|
||||||
|
for (
|
||||||
|
let time =
|
||||||
|
Math.ceil(Math.max(since, plans[0]?.staged ?? since) / minute) * minute;
|
||||||
|
time <= through;
|
||||||
|
time += minute
|
||||||
|
) {
|
||||||
|
// A successful crontab install acknowledges the file. crond reads changes at
|
||||||
|
// its minute tick. Around that tick either file can still explain a callback.
|
||||||
|
while (last + 1 < plans.length && plans[last + 1].staged <= time + 5000)
|
||||||
|
last++;
|
||||||
|
while (baseline + 1 <= last) {
|
||||||
|
let confirmed = baseline + 1;
|
||||||
|
while (confirmed <= last && plans[confirmed].confirmed == null)
|
||||||
|
confirmed++;
|
||||||
|
if (confirmed > last || plans[confirmed].confirmed! >= time - minute)
|
||||||
|
break;
|
||||||
|
baseline = confirmed;
|
||||||
|
}
|
||||||
|
if (last < 0) continue;
|
||||||
|
const possible = plans.slice(Math.max(0, baseline), last + 1);
|
||||||
|
if (dst || possible.some((plan) => plan.zone !== zone)) {
|
||||||
|
partial = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const tasks = new Set<number>();
|
||||||
|
for (const plan of possible)
|
||||||
|
for (const id of plan.tasks.keys()) tasks.add(id);
|
||||||
|
const block = time - (time % blockSize);
|
||||||
|
if (time === block) calendar.clear();
|
||||||
|
const entries: Slot[] = [];
|
||||||
|
for (const task of tasks) {
|
||||||
|
const schedules = new Set<string>();
|
||||||
|
let name: string | undefined;
|
||||||
|
for (const plan of possible) {
|
||||||
|
const rule = plan.tasks.get(task);
|
||||||
|
if (!rule) continue;
|
||||||
|
name = rule.name;
|
||||||
|
let times = calendar.get(rule.schedule);
|
||||||
|
if (!times) {
|
||||||
|
times = new Set(blockSlots(rule.schedule, zone, block));
|
||||||
|
calendar.set(rule.schedule, times);
|
||||||
|
}
|
||||||
|
if (times.has(time)) schedules.add(rule.schedule);
|
||||||
|
}
|
||||||
|
if (!schedules.size) continue;
|
||||||
|
const [schedule] = schedules;
|
||||||
|
const stable =
|
||||||
|
baseline >= 0 &&
|
||||||
|
schedules.size === 1 &&
|
||||||
|
possible.every(
|
||||||
|
(plan) =>
|
||||||
|
plan.confirmed != null &&
|
||||||
|
plan.tasks.get(task)?.schedule === schedule,
|
||||||
|
);
|
||||||
|
const group = [...schedules].flatMap(
|
||||||
|
(rule) => groups.get(receiptKey(task, rule, time)) ?? [],
|
||||||
|
);
|
||||||
|
let status: string;
|
||||||
|
const launched = group.find((row) => row.launched === 1);
|
||||||
|
const skip = group.find((row) => row.state === 'skipped');
|
||||||
|
if (group.length) {
|
||||||
|
++expected;
|
||||||
|
++observed;
|
||||||
|
matchedReceipts += group.length;
|
||||||
|
duplicates += group.length - 1;
|
||||||
|
if (launched) ++dispatched;
|
||||||
|
else if (skip) ++skipped;
|
||||||
|
status = launched ? 'started' : skip ? 'skipped' : 'unconfirmed';
|
||||||
|
} else if (!stable) {
|
||||||
|
++switchSlots;
|
||||||
|
partial = true;
|
||||||
|
status = 'switch';
|
||||||
|
} else {
|
||||||
|
++expected;
|
||||||
|
const lastUndated = Math.max(
|
||||||
|
legacyThrough.get(JSON.stringify([task, null])) ?? 0,
|
||||||
|
legacyThrough.get(JSON.stringify([task, schedule])) ?? 0,
|
||||||
|
);
|
||||||
|
if (
|
||||||
|
time <= lastUndated ||
|
||||||
|
dispatchedTasks.has(task + '/' + time / minute)
|
||||||
|
) {
|
||||||
|
++uncertain;
|
||||||
|
status = 'uncertain';
|
||||||
|
} else if (time + minute > now) {
|
||||||
|
++awaiting;
|
||||||
|
status = 'awaiting';
|
||||||
|
} else {
|
||||||
|
++missing;
|
||||||
|
status = 'missing';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
entries.push({
|
||||||
|
time,
|
||||||
|
task,
|
||||||
|
name,
|
||||||
|
status,
|
||||||
|
triggerCount: group.length,
|
||||||
|
startCount: group.filter((row) => row.launched === 1).length,
|
||||||
|
skipCount: group.filter((row) => row.state === 'skipped').length,
|
||||||
|
unknownExitCount: group.filter((row) => row.launched === 1 && row.state === 'unknown').length,
|
||||||
|
id: (launched || skip || group[0])?.id,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (entries.length)
|
||||||
|
recent.unshift(...entries.sort((a, b) => a.task - b.task).slice(0, 25));
|
||||||
|
recent.splice(25);
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
since: Math.max(since, plans[0]?.staged ?? since),
|
||||||
|
through,
|
||||||
|
confirmedAt:
|
||||||
|
[...plans].reverse().find((p) => p.confirmed != null)?.confirmed ?? null,
|
||||||
|
pending: plans.at(-1)?.confirmed == null,
|
||||||
|
partial,
|
||||||
|
expected,
|
||||||
|
observed,
|
||||||
|
dispatched,
|
||||||
|
skipped,
|
||||||
|
missing,
|
||||||
|
duplicates,
|
||||||
|
uncertain,
|
||||||
|
awaiting,
|
||||||
|
switchSlots,
|
||||||
|
actualSkips: receipts.filter((row) => row.state === 'skipped').length,
|
||||||
|
actualStarts: receipts.filter((row) => row.launched === 1).length,
|
||||||
|
unmatched: receipts.length - matchedReceipts,
|
||||||
|
recent,
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
import { t } from './i18n';
|
||||||
|
|
||||||
|
// These names control the task runner or the interpreters it starts. Values
|
||||||
|
// managed through the environment API must remain task data, not executable
|
||||||
|
// configuration.
|
||||||
|
const reservedNames = new Set([
|
||||||
|
'TASK_BEFORE',
|
||||||
|
'TASK_AFTER',
|
||||||
|
'WORK_DIR',
|
||||||
|
'TASK_SHELL_PARAMS',
|
||||||
|
'SCRIPT_PARAMS',
|
||||||
|
'WHICH_PROGRAM',
|
||||||
|
'ISJSORPYTHONFILE',
|
||||||
|
'EXPORTED_VARIABLES',
|
||||||
|
'FOUND_DOUBLE_DASH',
|
||||||
|
'LOG_DIR',
|
||||||
|
'LOG_PATH',
|
||||||
|
'ID',
|
||||||
|
'BEGIN_TIMESTAMP',
|
||||||
|
'TIME_FORMAT',
|
||||||
|
'MTIME_FORMAT',
|
||||||
|
'IS_MACOS',
|
||||||
|
'RANDOMDELAY',
|
||||||
|
'RANDOMDELAYFILEEXTENSIONS',
|
||||||
|
'RANDOMDELAYIGNOREDMINUTES',
|
||||||
|
'COMMAND_TIMEOUT_TIME',
|
||||||
|
'SET_U_ON',
|
||||||
|
'_TASK_EXIT_CODE',
|
||||||
|
'PATH',
|
||||||
|
'HOME',
|
||||||
|
'SHELL',
|
||||||
|
'ENV',
|
||||||
|
'BASH_ENV',
|
||||||
|
'BASHOPTS',
|
||||||
|
'SHELLOPTS',
|
||||||
|
'IFS',
|
||||||
|
'CDPATH',
|
||||||
|
'GLOBIGNORE',
|
||||||
|
'PROMPT_COMMAND',
|
||||||
|
'PS4',
|
||||||
|
'NODE_OPTIONS',
|
||||||
|
'NODE_PATH',
|
||||||
|
'PREV_NODE_OPTIONS',
|
||||||
|
'PREV_NODE_PATH',
|
||||||
|
'PREV_PYTHONPATH',
|
||||||
|
'GIT_SSH',
|
||||||
|
'GIT_SSH_COMMAND',
|
||||||
|
'GIT_ASKPASS',
|
||||||
|
'GIT_EXEC_PATH',
|
||||||
|
'GIT_TEMPLATE_DIR',
|
||||||
|
'SSH_ASKPASS',
|
||||||
|
]);
|
||||||
|
|
||||||
|
export function isAllowedTaskEnvName(name: unknown): name is string {
|
||||||
|
if (typeof name !== 'string' || !/^[a-zA-Z_][0-9a-zA-Z_]*$/.test(name)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const upper = name.toUpperCase();
|
||||||
|
return (
|
||||||
|
!reservedNames.has(upper) &&
|
||||||
|
!upper.startsWith('BASH_') &&
|
||||||
|
!upper.startsWith('QL') &&
|
||||||
|
!upper.startsWith('FILE_') &&
|
||||||
|
!upper.startsWith('DIR_') &&
|
||||||
|
!upper.startsWith('LD_') &&
|
||||||
|
!upper.startsWith('DYLD_') &&
|
||||||
|
!upper.startsWith('PYTHON') &&
|
||||||
|
!upper.startsWith('GIT_CONFIG_')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function assertAllowedTaskEnvName(name: unknown): asserts name is string {
|
||||||
|
if (!isAllowedTaskEnvName(name)) {
|
||||||
|
throw Object.assign(new Error(t('环境变量名称不可使用')), {
|
||||||
|
status: 400,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -70,14 +70,10 @@ export const commonCronSchema = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// For other absolute paths, ensure they are within the safe log directory
|
// For other absolute paths, ensure they are within the safe log directory
|
||||||
const normalizedValue = path.normalize(value);
|
const relative = path.relative(config.logPath, value);
|
||||||
const normalizedLogPath = path.normalize(config.logPath);
|
if (!relative || relative === '..' || relative.startsWith('..' + path.sep) || path.isAbsolute(relative)) {
|
||||||
|
|
||||||
if (!normalizedValue.startsWith(normalizedLogPath)) {
|
|
||||||
return helpers.error('string.unsafePath');
|
return helpers.error('string.unsafePath');
|
||||||
}
|
}
|
||||||
|
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
@@ -97,6 +93,9 @@ export const commonCronSchema = {
|
|||||||
'string.max': '日志名称不能超过100个字符',
|
'string.max': '日志名称不能超过100个字符',
|
||||||
'string.unsafePath': '绝对路径必须在日志目录内或使用 /dev/null',
|
'string.unsafePath': '绝对路径必须在日志目录内或使用 /dev/null',
|
||||||
}),
|
}),
|
||||||
|
skip_if_pending: Joi.boolean().optional().allow(null),
|
||||||
|
max_queue_wait: Joi.number().integer().min(0).max(86400).allow(null),
|
||||||
|
catchup_seconds: Joi.number().integer().min(0).max(86400).allow(null),
|
||||||
allow_multiple_instances: Joi.number().optional().valid(0, 1).allow(null),
|
allow_multiple_instances: Joi.number().optional().valid(0, 1).allow(null),
|
||||||
work_dir: Joi.string().optional().allow('').allow(null),
|
work_dir: Joi.string().optional().allow('').allow(null),
|
||||||
};
|
};
|
||||||
|
|||||||
+1
-1
@@ -38,7 +38,7 @@ check installs dependencies, repairs and reloads; inspect its before/after obser
|
|||||||
|
|
||||||
On a panel containing the selection loader, set `QL_CLI_ROOT=/absolute/path/to/built/cli` to the internal build directory containing the full dist tree, **not an npm installation**. Restart to create private ~/bin ql/task/cron wrappers and prioritize them for panel processes. Independent terminals should use the selected absolute path. Unset the variable and restart to restore Shell entries. Invalid paths report errors without silent fallback. Original Shell entries remain the default.
|
On a panel containing the selection loader, set `QL_CLI_ROOT=/absolute/path/to/built/cli` to the internal build directory containing the full dist tree, **not an npm installation**. Restart to create private ~/bin ql/task/cron wrappers and prioritize them for panel processes. Independent terminals should use the selected absolute path. Unset the variable and restart to restore Shell entries. Invalid paths report errors without silent fallback. Original Shell entries remain the default.
|
||||||
|
|
||||||
dist/startup.js retains startup/reload compatibility; dist/compat.js and dist/subscription-worker.js are internal adapters, not npm commands. Lifecycle fields follow the installed panel version; customized versions may select QL_CLI_LIFECYCLE=legacy|extended.
|
dist/startup.js retains startup/reload compatibility; dist/compat.js and dist/subscription-worker.js are internal adapters, not npm commands. The CLI negotiates receipt fields with the running panel. Set `QL_CLI_LIFECYCLE=legacy` to force the legacy format.
|
||||||
|
|
||||||
The separate [qinglong-local skill](skills/qinglong-local/SKILL.md) covers execution and maintenance. Use qinglong-cli for remote API management. QL_LANG=en selects English help/messages while JSON fields remain stable.
|
The separate [qinglong-local skill](skills/qinglong-local/SKILL.md) covers execution and maintenance. Use qinglong-cli for remote API management. QL_LANG=en selects English help/messages while JSON fields remain stable.
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -40,7 +40,7 @@ repo/raw 沿用位置参数,使用 QL_DIR/QL_DATA_DIR,不接受 --root/--jso
|
|||||||
|
|
||||||
包含选择加载器的面板设置 `QL_CLI_ROOT=/absolute/path/to/built/cli`,指向包含完整 dist 的面板工具构建目录,**不能指向 npm 包**。重启后加载器在 ~/bin 创建私有 ql/task/cron 包装器,并为面板进程优先使用它们;独立终端应显式使用该路径。清除变量并重启恢复原 Shell 入口。无效路径报错,不静默降级。默认仍保留旧 Shell 入口。
|
包含选择加载器的面板设置 `QL_CLI_ROOT=/absolute/path/to/built/cli`,指向包含完整 dist 的面板工具构建目录,**不能指向 npm 包**。重启后加载器在 ~/bin 创建私有 ql/task/cron 包装器,并为面板进程优先使用它们;独立终端应显式使用该路径。清除变量并重启恢复原 Shell 入口。无效路径报错,不静默降级。默认仍保留旧 Shell 入口。
|
||||||
|
|
||||||
`dist/startup.js` 兼容旧启动/reload;`dist/compat.js` 与 `dist/subscription-worker.js` 保留内部适配用途,不注册为 npm 命令。生命周期根据已安装面板版本选择字段,定制版本可设置 QL_CLI_LIFECYCLE=legacy|extended。
|
`dist/startup.js` 兼容旧启动/reload;`dist/compat.js` 与 `dist/subscription-worker.js` 保留内部适配用途,不注册为 npm 命令。CLI 根据运行中的面板对回执字段的校验结果选择协议。设置 `QL_CLI_LIFECYCLE=legacy` 可强制使用旧格式。
|
||||||
|
|
||||||
独立 Skill 位于 [skills/qinglong-local](skills/qinglong-local/SKILL.md),包含完整执行与维护参考。远程管理另用 qinglong-cli Skill。`QL_LANG=en` 切换帮助和执行提示;JSON 字段保持一致。
|
独立 Skill 位于 [skills/qinglong-local](skills/qinglong-local/SKILL.md),包含完整执行与维护参考。远程管理另用 qinglong-cli Skill。`QL_LANG=en` 切换帮助和执行提示;JSON 字段保持一致。
|
||||||
|
|
||||||
|
|||||||
+3
-3
@@ -43,12 +43,12 @@ Use command `--help` and `QL_LANG=en` for English. Command names and JSON fields
|
|||||||
|
|
||||||
## Complete OpenAPI management
|
## Complete OpenAPI management
|
||||||
|
|
||||||
The CLI also supports task/subscription CRUD, application and secret management, environment variables, configuration, scripts, logs, dependencies, system, dashboard and user APIs. `ql api routes --json` lists all 143 active routes; three retired file-reading endpoints are excluded. CI compares the catalogue against backend routes. See the [complete bilingual reference](skills/qinglong-cli/references/openapi.md) for every command and payload.
|
The CLI also supports task/subscription CRUD, application and secret management, environment variables, configuration, scripts, logs, dependencies, system, dashboard and user APIs. `ql api routes --json` lists the active routes; three retired file-reading endpoints are excluded. CI compares the catalogue against backend routes. See the [complete bilingual reference](skills/qinglong-cli/references/openapi.md) for every command and payload.
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
ql task create --name demo --command 'task demo.js' --schedule '0 0 * * *' --json
|
ql task create --name demo --command 'task demo.js' --schedule '0 0 * * *' --json
|
||||||
ql subscription create --type public-repo --url https://example.com/repo.git --alias demo --schedule-type crontab --schedule '0 0 * * *' --json
|
ql subscription create --type public-repo --url https://example.com/repo.git --alias demo --schedule-type crontab --schedule '0 0 * * *' --json
|
||||||
ql app create --name agent --scopes crons,subscriptions --show-secrets --json
|
ql app create --name agent --scopes crons,subscriptions,envs --show-secrets --json
|
||||||
ql env create --data @envs.json --json
|
ql env create --data @envs.json --json
|
||||||
ql api request PUT /open/crons/run --data '[12,13]' --json
|
ql api request PUT /open/crons/run --data '[12,13]' --json
|
||||||
```
|
```
|
||||||
@@ -68,7 +68,7 @@ Protected requests use `Authorization: Bearer <token>`. Two credential sources a
|
|||||||
|
|
||||||
### Application login
|
### Application login
|
||||||
|
|
||||||
Create a dedicated panel application with the required scopes, such as crons, subscriptions or envs. `login` and `auth login` are equivalent. Interactive login hides the Client Secret; secret command-line arguments are not supported.
|
Create a dedicated panel application with the required scopes. Existing scopes retain their original permissions. For read-only access, use `crons:read`, `subscriptions:read`, `envs:read`, `configs:read`, `scripts:read`, `dependencies:read`, `logs:read`, or `dashboard:read`. Script and log read-only scopes also allow downloads and directory version checks via POST. `GET /crons/import` writes tasks and is excluded from `crons:read`. `login` and `auth login` are equivalent. Interactive login hides the Client Secret; secret command-line arguments are not supported.
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
ql login --url https://ql.example.com
|
ql login --url https://ql.example.com
|
||||||
|
|||||||
+3
-3
@@ -43,12 +43,12 @@ ql auth logout --json
|
|||||||
|
|
||||||
## 全量 OpenAPI 管理
|
## 全量 OpenAPI 管理
|
||||||
|
|
||||||
现在还支持任务/订阅创建、修改、删除,应用管理与密钥重置,以及环境变量、配置、脚本、日志、依赖、系统、仪表盘和用户管理。`ql api routes --json` 列出全部 143 条有效路由;3 条已下线文件读取接口不包含在内。新增命令在 [完整双语参考](skills/qinglong-cli/references/openapi.md) 中逐项列出,路由覆盖由 CI 与后端代码核对。
|
现在还支持任务/订阅创建、修改、删除,应用管理与密钥重置,以及环境变量、配置、脚本、日志、依赖、系统、仪表盘和用户管理。`ql api routes --json` 列出当前版本的有效路由;3 条已下线文件读取接口不包含在内。新增命令在 [完整双语参考](skills/qinglong-cli/references/openapi.md) 中逐项列出,路由覆盖由 CI 与后端代码核对。
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
ql task create --name demo --command 'task demo.js' --schedule '0 0 * * *' --json
|
ql task create --name demo --command 'task demo.js' --schedule '0 0 * * *' --json
|
||||||
ql subscription create --type public-repo --url https://example.com/repo.git --alias demo --schedule-type crontab --schedule '0 0 * * *' --json
|
ql subscription create --type public-repo --url https://example.com/repo.git --alias demo --schedule-type crontab --schedule '0 0 * * *' --json
|
||||||
ql app create --name agent --scopes crons,subscriptions --show-secrets --json
|
ql app create --name agent --scopes crons,subscriptions,envs --show-secrets --json
|
||||||
ql env create --data @envs.json --json
|
ql env create --data @envs.json --json
|
||||||
ql api request PUT /open/crons/run --data '[12,13]' --json
|
ql api request PUT /open/crons/run --data '[12,13]' --json
|
||||||
```
|
```
|
||||||
@@ -68,7 +68,7 @@ ql api request PUT /open/crons/run --data '[12,13]' --json
|
|||||||
|
|
||||||
### 应用凭据登录
|
### 应用凭据登录
|
||||||
|
|
||||||
在面板创建专用应用,按需授予 crons、subscriptions、envs 等权限。`login` 与 `auth login` 等价;交互输入 Client ID 和不回显的 Client Secret,密钥不支持命令行参数。
|
在面板创建专用应用,按需授予 crons、subscriptions 等权限。旧 scope 保留原有权限;仅读取时可选用 `crons:read`、`subscriptions:read`、`envs:read`、`configs:read`、`scripts:read`、`dependencies:read`、`logs:read`、`dashboard:read`。脚本和日志的只读权限也允许通过 POST 下载文件和校验目录版本;`GET /crons/import` 会写入任务,不属于只读操作。`login` 与 `auth login` 等价;交互输入 Client ID 和不回显的 Client Secret,密钥不支持命令行参数。
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
ql login --url https://ql.example.com
|
ql login --url https://ql.example.com
|
||||||
|
|||||||
@@ -1,16 +1,28 @@
|
|||||||
# OpenAPI 全量参考 / Complete OpenAPI reference
|
# OpenAPI 全量参考 / Complete OpenAPI reference
|
||||||
|
|
||||||
CLI 对应本版本后端的 146 条有效路由。通过 `ql api routes --json` 查看方法、路径、对应命令、位置参数、请求体与上传字段;CLI 测试与 back/api 路由逐项核对。GET configs/:file、scripts/:file、logs/:file 已返回 410,改用 detail 命令,不再暴露旧入口。旧版面板可能没有新路由,以服务端响应为准。
|
CLI 对应本版本后端的有效路由。通过 `ql api routes --json` 查看方法、路径、对应命令、位置参数、请求体与上传字段;CLI 测试与 back/api 路由逐项核对。GET configs/:file、scripts/:file、logs/:file 已返回 410,改用 detail 命令,不再暴露旧入口。旧版面板可能没有新路由,以服务端响应为准。
|
||||||
|
|
||||||
The catalogue covers 146 active routes in the current backend. `ql api routes --json` lists methods, paths, commands, positional parameters, bodies and upload fields. A route-coverage test detects drift. Three retired GET :file routes return 410; use detail-based commands. Older panel versions may not implement newer routes.
|
The catalogue covers the active routes in the current backend. `ql api routes --json` lists methods, paths, commands, positional parameters, bodies and upload fields. A route-coverage test detects drift. Three retired GET :file routes return 410; use detail-based commands. Older panel versions may not implement newer routes.
|
||||||
|
|
||||||
|
日志和脚本分页返回 `versions`。应用可将已浏览目录的版本按每组 1 到 128 项交给同一资源的 `entries-check --data`。返回 `current: false` 时,从第一页重新读取。
|
||||||
|
|
||||||
|
Log and script pages return `versions`. Pass the visited directories’ versions to the matching `entries-check --data` command in groups of 1 to 128. Restart pagination when it returns `current: false`.
|
||||||
|
|
||||||
|
`ql task instances <id>` 默认返回该任务的完整运行历史,按启动时间倒序。显式传 `--query '{"paged":true,"limit":25}'` 后按实例 ID 倒序分页(默认 25,最多 100),将返回的 `next` 作为下一页的 `before`,直到 `next` 为 `null`。刷新指定实例可传 `{"paged":true,"ids":"12,13"}`。触发记录使用独立的 `task triggers` 接口,日志和脚本目录分页使用 `log entries` 和 `script entries`。
|
||||||
|
|
||||||
|
`ql task instances <id>` returns the full history ordered by start time by default. Opt in with `--query '{"paged":true,"limit":25}'` to page by descending instance ID (default 25, maximum 100). Pass `next` as `before` until `next` is `null`. Use `{"paged":true,"ids":"12,13"}` to refresh specific rows. Trigger history and directory pagination use separate endpoints.
|
||||||
|
|
||||||
|
直接调用 `PUT /open/crons/stop` 时,请求体为任务 ID 数组。需要安全重试的调用方应在首次发送前生成 UUID,放入 `Idempotency-Key` 请求头,并在该操作的所有重试中复用。相同键只处理第一次受理时的实例和排队记录,换用不同任务集合会返回 409。主动发起新的停止操作时使用新键。未携带键的旧客户端每次调用都会停止当前任务实例。
|
||||||
|
|
||||||
## 认证与权限 / Authentication and permissions
|
## 认证与权限 / Authentication and permissions
|
||||||
|
|
||||||
常规使用 `ql login` 的应用凭据。scope 是 /open 后首个路径段:crons、subscriptions、envs、configs、scripts、logs、dependencies、system、dashboard 等。应用管理需要 apps;user、health、update 等路由同样接受服务端权限校验。面板 UI 当前没有列出全部这些 scope,不能把路由存在理解成现有应用已获授权。
|
常规使用 `ql login` 的应用凭据。scope 通常对应 /open 后首个路径段:crons、subscriptions、envs、configs、scripts、logs、dependencies、system、dashboard 等。旧 scope 保留原有权限;上述资源除 system 外可选用对应的 `:read` scope。`crons:read` 不包含会写入任务的 `GET /crons/import`;`scripts:read`、`logs:read` 包含使用 POST 的下载和目录版本校验,不包含写入、删除或执行。`dashboard:read` 不允许 POST 统计上报。外部应用的 `system` scope 仅允许读取系统状态和发送通知。应用管理需要 apps;`GET /apps` 会返回 Client Secret,因此不提供 `apps:read`。user、health、update 等路由同样接受服务端权限校验。路由存在不代表现有应用已获授权。
|
||||||
|
|
||||||
认证步骤、环境变量优先级、应用登录、面板会话与双因素验证、退出行为,统一见 [认证参考](panel.md#authentication)。应用管理可用 apps 权限的应用或授权面板会话;QL_URL/QL_ACCESS_TOKEN 不保存、不刷新,不自动退回应用配置。
|
认证步骤、环境变量优先级、应用登录、面板会话与双因素验证、退出行为,统一见 [认证参考](panel.md#authentication)。应用管理可用已有 apps 权限的应用或授权面板会话;后端创建和更新应用时拒绝授予 apps 权限。QL_URL/QL_ACCESS_TOKEN 不保存、不刷新,不自动退回应用配置。
|
||||||
|
|
||||||
See the [authentication reference](panel.md#authentication) for application login, direct-token precedence, owner sessions/2FA and logout. App management accepts apps-scoped credentials or an authorized owner session. Direct tokens are not saved/refreshed and never fall back to application configuration.
|
See the [authentication reference](panel.md#authentication) for application login, direct-token precedence, owner sessions/2FA and logout. App management accepts existing apps-scoped credentials or an authorized owner session; the backend rejects granting apps scope when creating or updating an application. Direct tokens are not saved/refreshed and never fall back to application configuration.
|
||||||
|
|
||||||
|
Existing scopes retain their original access. Use the resource's `:read` scope for read-only access to crons, subscriptions, envs, configs, scripts, dependencies, logs, or dashboard. `crons:read` excludes the mutating `GET /crons/import`; `scripts:read` and `logs:read` include downloads and directory version checks (POST), but exclude writes, deletion, and execution. `dashboard:read` excludes statistics reporting (POST). External `system` applications may only read system status and send notifications. `GET /apps` returns Client Secrets, so there is no `apps:read` scope.
|
||||||
|
|
||||||
`auth status --scope <scope>` tests a representative read; update has no read endpoint and is not offered by status. This does not prove permission for every write. `app list/create/update/reset-secret` 默认隐藏 client_secret/tokens;只有明确需要凭据时才加 --show-secrets,并避免把输出记录到共享日志。其他资源/通用请求可能含环境值、配置、脚本、订阅凭据和会话,按敏感数据处理。
|
`auth status --scope <scope>` tests a representative read; update has no read endpoint and is not offered by status. This does not prove permission for every write. `app list/create/update/reset-secret` 默认隐藏 client_secret/tokens;只有明确需要凭据时才加 --show-secrets,并避免把输出记录到共享日志。其他资源/通用请求可能含环境值、配置、脚本、订阅凭据和会话,按敏感数据处理。
|
||||||
|
|
||||||
@@ -49,16 +61,24 @@ ql script get --query '{"file":"demo.js","path":""}' --json
|
|||||||
ql script history-list --query '{"filename":"demo.js","path":""}' --json
|
ql script history-list --query '{"filename":"demo.js","path":""}' --json
|
||||||
ql script history-detail --query '{"filename":"demo.js","path":"","id":"VERSION_UUID"}' --json
|
ql script history-detail --query '{"filename":"demo.js","path":"","id":"VERSION_UUID"}' --json
|
||||||
ql script history-restore --data '{"filename":"demo.js","path":"","id":"VERSION_UUID","expectedHash":"CURRENT_SHA256"}' --json
|
ql script history-restore --data '{"filename":"demo.js","path":"","id":"VERSION_UUID","expectedHash":"CURRENT_SHA256"}' --json
|
||||||
|
# Requires the full configs scope or a panel session token.
|
||||||
ql config save --data '{"name":"example.sh","content":"# example"}' --json
|
ql config save --data '{"name":"example.sh","content":"# example"}' --json
|
||||||
ql config get --query '{"path":"example.sh"}' --json
|
ql config get --query '{"path":"example.sh"}' --json
|
||||||
ql log download --data '{"filename":"example.log","path":"example"}' --output ./example.log --json
|
ql log download --data '{"filename":"example.log","path":"example"}' --output ./example.log --json
|
||||||
ql dependency create --data '[{"name":"example-package","type":0}]' --json
|
ql dependency create --data '[{"name":"example-package","type":0}]' --json
|
||||||
|
ql dependency refresh --data '{"type":"python3"}' --json
|
||||||
|
ql dependency refresh-active --json
|
||||||
|
ql dependency refresh-status <JOB_UUID> --json
|
||||||
ql system data-export --data '{}' --output ./panel.tgz --json
|
ql system data-export --data '{}' --output ./panel.tgz --json
|
||||||
ql system data-import --file ./panel.tgz --json
|
ql system data-import --file ./panel.tgz --json
|
||||||
ql api request PUT /open/crons/run --data '[12,13]' --json
|
ql api request PUT /open/crons/run --data '[12,13]' --json
|
||||||
ql api request GET /open/crons --query '{"page":1,"size":100,"searchValue":"demo"}' --json
|
ql api request GET /open/crons --query '{"page":1,"size":100,"searchValue":"demo"}' --json
|
||||||
```
|
```
|
||||||
|
|
||||||
|
依赖 refresh 的 type 为 nodejs 或 python3,立即返回后台任务 id 与 state;用 refresh-status 查询该 UUID,直到 completed 或 failed。refresh-active 返回运行中的任务或 null,普通 list 不触发检查。
|
||||||
|
|
||||||
|
Dependency refresh accepts type nodejs or python3 and immediately returns a background job id and state. Poll refresh-status with that UUID until completed or failed. refresh-active returns the running job or null; listing dependencies does not trigger checks.
|
||||||
|
|
||||||
任务 create/update 需要 command/schedule;name、labels、sub_id、extra_schedules、task_before/after、log_name、allow_multiple_instances、work_dir 等字段可通过 --data 提交。订阅 create 需要 type/url/alias/schedule_type,update 需要 id/type/url/alias;interval_schedule、pull_option、dependences、extensions、sub_before/after、proxy、autoAddCron/autoDelCron 等用 --data。private-repo 凭据仅通过受保护文件或 stdin 提供。环境变量与依赖 create 接受对象数组,应用 scopes 为字符串数组。其余准确字段/枚举由当前后端 Joi schema 校验:见 [back/api](https://github.com/whyour/qinglong/tree/develop/back/api) 和 [定时规则 schema](https://github.com/whyour/qinglong/blob/develop/back/validation/schedule.ts)。
|
任务 create/update 需要 command/schedule;name、labels、sub_id、extra_schedules、task_before/after、log_name、allow_multiple_instances、work_dir 等字段可通过 --data 提交。订阅 create 需要 type/url/alias/schedule_type,update 需要 id/type/url/alias;interval_schedule、pull_option、dependences、extensions、sub_before/after、proxy、autoAddCron/autoDelCron 等用 --data。private-repo 凭据仅通过受保护文件或 stdin 提供。环境变量与依赖 create 接受对象数组,应用 scopes 为字符串数组。其余准确字段/枚举由当前后端 Joi schema 校验:见 [back/api](https://github.com/whyour/qinglong/tree/develop/back/api) 和 [定时规则 schema](https://github.com/whyour/qinglong/blob/develop/back/validation/schedule.ts)。
|
||||||
|
|
||||||
Task creation/update needs command/schedule; additional fields use --data. Subscription creation needs type/url/alias/schedule_type; updates need id/type/url/alias. Interval schedules, private repository credentials, filters, hooks and booleans use --data. Env/dependency creation accepts arrays; application scopes is a string array. The server validates field types/enums; the linked backend schemas are authoritative.
|
Task creation/update needs command/schedule; additional fields use --data. Subscription creation needs type/url/alias/schedule_type; updates need id/type/url/alias. Interval schedules, private repository credentials, filters, hooks and booleans use --data. Env/dependency creation accepts arrays; application scopes is a string array. The server validates field types/enums; the linked backend schemas are authoritative.
|
||||||
@@ -80,6 +100,7 @@ System/user operations below execute remotely through the panel API. They do not
|
|||||||
| `ql task view-enable <id...>` | PUT | `crons/views/enable` | --query |
|
| `ql task view-enable <id...>` | PUT | `crons/views/enable` | --query |
|
||||||
| `ql task list ` | GET | `crons` | --query via api request |
|
| `ql task list ` | GET | `crons` | --query via api request |
|
||||||
| `ql task detail ` | GET | `crons/detail` | --query |
|
| `ql task detail ` | GET | `crons/detail` | --query |
|
||||||
|
| `ql task preview` | GET | `crons/preview` | --query '{"schedule":"0 6 * * *"}' |
|
||||||
| `ql task create ` | POST | `crons` | --data, --query |
|
| `ql task create ` | POST | `crons` | --data, --query |
|
||||||
| `ql task run <id...>` | PUT | `crons/run` | --query via api request |
|
| `ql task run <id...>` | PUT | `crons/run` | --query via api request |
|
||||||
| `ql task stop <id...>` | PUT | `crons/stop` | --query via api request |
|
| `ql task stop <id...>` | PUT | `crons/stop` | --query via api request |
|
||||||
@@ -95,6 +116,8 @@ System/user operations below execute remotely through the panel API. They do not
|
|||||||
| `ql task import ` | GET | `crons/import` | --query |
|
| `ql task import ` | GET | `crons/import` | --query |
|
||||||
| `ql task get <id>` | GET | `crons/:id` | --query via api request |
|
| `ql task get <id>` | GET | `crons/:id` | --query via api request |
|
||||||
| `ql task status ` | PUT | `crons/status` | --data, --query |
|
| `ql task status ` | PUT | `crons/status` | --data, --query |
|
||||||
|
| `ql task daily-results <id>` | GET | `crons/:id/daily-results` | Verified exits and booked results for the last seven days |
|
||||||
|
| `ql task triggers <id>` | GET | `crons/:id/triggers` | --query '{"offset":0}' |
|
||||||
| `ql task instances <id>` | GET | `crons/:id/instances` | --query |
|
| `ql task instances <id>` | GET | `crons/:id/instances` | --query |
|
||||||
| `ql task instance-stop <id> <instanceId>` | POST | `crons/:id/instances/:instanceId/stop` | --query |
|
| `ql task instance-stop <id> <instanceId>` | POST | `crons/:id/instances/:instanceId/stop` | --query |
|
||||||
| `ql task log-files <id>` | GET | `crons/:id/logs` | --query |
|
| `ql task log-files <id>` | GET | `crons/:id/logs` | --query |
|
||||||
@@ -136,6 +159,8 @@ System/user operations below execute remotely through the panel API. They do not
|
|||||||
| `ql config save ` | POST | `configs/save` | --data, --query |
|
| `ql config save ` | POST | `configs/save` | --data, --query |
|
||||||
| `ql script list ` | GET | `scripts` | --query |
|
| `ql script list ` | GET | `scripts` | --query |
|
||||||
| `ql script get ` | GET | `scripts/detail` | --query |
|
| `ql script get ` | GET | `scripts/detail` | --query |
|
||||||
|
| `ql script entries ` | GET | `scripts/entries` | --query |
|
||||||
|
| `ql script entries-check ` | POST | `scripts/entries/check` | --data, --query |
|
||||||
| `ql script history-list ` | GET | `scripts/history` | --query |
|
| `ql script history-list ` | GET | `scripts/history` | --query |
|
||||||
| `ql script history-detail ` | GET | `scripts/history/detail` | --query |
|
| `ql script history-detail ` | GET | `scripts/history/detail` | --query |
|
||||||
| `ql script history-restore ` | PUT | `scripts/history/restore` | --data, --query |
|
| `ql script history-restore ` | PUT | `scripts/history/restore` | --data, --query |
|
||||||
@@ -148,9 +173,14 @@ System/user operations below execute remotely through the panel API. They do not
|
|||||||
| `ql script rename ` | PUT | `scripts/rename` | --data, --query |
|
| `ql script rename ` | PUT | `scripts/rename` | --data, --query |
|
||||||
| `ql log list ` | GET | `logs` | --query |
|
| `ql log list ` | GET | `logs` | --query |
|
||||||
| `ql log get ` | GET | `logs/detail` | --query |
|
| `ql log get ` | GET | `logs/detail` | --query |
|
||||||
|
| `ql log entries ` | GET | `logs/entries` | --query |
|
||||||
|
| `ql log entries-check ` | POST | `logs/entries/check` | --data, --query |
|
||||||
| `ql log delete ` | DELETE | `logs` | --data, --query |
|
| `ql log delete ` | DELETE | `logs` | --data, --query |
|
||||||
| `ql log download ` | POST | `logs/download` | --data, --output, --query |
|
| `ql log download ` | POST | `logs/download` | --data, --output, --query |
|
||||||
| `ql dependency list ` | GET | `dependencies` | --query |
|
| `ql dependency list ` | GET | `dependencies` | --query |
|
||||||
|
| `ql dependency refresh ` | POST | `dependencies/refresh` | --data, --query |
|
||||||
|
| `ql dependency refresh-active ` | GET | `dependencies/refresh/active` | --query |
|
||||||
|
| `ql dependency refresh-status <id>` | GET | `dependencies/refresh/:id` | --query |
|
||||||
| `ql dependency create ` | POST | `dependencies` | --data, --query |
|
| `ql dependency create ` | POST | `dependencies` | --data, --query |
|
||||||
| `ql dependency update <id>` | PUT | `dependencies` | --data, --query |
|
| `ql dependency update <id>` | PUT | `dependencies` | --data, --query |
|
||||||
| `ql dependency delete <id...>` | DELETE | `dependencies` | --query |
|
| `ql dependency delete <id...>` | DELETE | `dependencies` | --query |
|
||||||
@@ -211,7 +241,6 @@ System/user operations below execute remotely through the panel API. They do not
|
|||||||
| `ql user notification-get ` | GET | `user/notification` | --query |
|
| `ql user notification-get ` | GET | `user/notification` | --query |
|
||||||
| `ql user notification-set ` | PUT | `user/notification` | --data, --query |
|
| `ql user notification-set ` | PUT | `user/notification` | --data, --query |
|
||||||
| `ql user init ` | PUT | `user/init` | --data, --query |
|
| `ql user init ` | PUT | `user/init` | --data, --query |
|
||||||
| `ql user notification-init ` | PUT | `user/notification/init` | --data, --query |
|
|
||||||
| `ql user avatar ` | PUT | `user/avatar` | --data, --file (avatar), --query |
|
| `ql user avatar ` | PUT | `user/avatar` | --data, --file (avatar), --query |
|
||||||
| `ql system apply-reload ` | PUT | `update/reload` | --query |
|
| `ql system apply-reload ` | PUT | `update/reload` | --query |
|
||||||
| `ql system apply-system ` | PUT | `update/system` | --query |
|
| `ql system apply-system ` | PUT | `update/system` | --query |
|
||||||
|
|||||||
@@ -18,9 +18,11 @@ export const nodePreloadSource: string = [
|
|||||||
' return;',
|
' return;',
|
||||||
' }',
|
' }',
|
||||||
'',
|
'',
|
||||||
|
" const clientModule = require.cache[require.resolve('./client.js')];",
|
||||||
' const originalResolveFilename = Module._resolveFilename;',
|
' const originalResolveFilename = Module._resolveFilename;',
|
||||||
' Module._resolveFilename = function (request, parent, isMain, options) {',
|
' Module._resolveFilename = function (request, parent, isMain, options) {',
|
||||||
' if (',
|
' if (',
|
||||||
|
' !clientModule.loadingPanelDependencies &&',
|
||||||
' !Module.builtinModules.includes(request) &&',
|
' !Module.builtinModules.includes(request) &&',
|
||||||
" !request.startsWith('node:') &&",
|
" !request.startsWith('node:') &&",
|
||||||
" !request.startsWith('.') &&",
|
" !request.startsWith('.') &&",
|
||||||
|
|||||||
@@ -1,13 +1,12 @@
|
|||||||
import { extendedLifecycle } from '../runtime/lifecycle';
|
import { LifecycleReceipt } from '../runtime/lifecycle';
|
||||||
import fs from 'node:fs/promises';
|
import fs from 'node:fs/promises';
|
||||||
import { writeSync } from 'node:fs';
|
import { writeSync } from 'node:fs';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
import os from 'node:os';
|
import os from 'node:os';
|
||||||
import { randomInt, randomUUID } from 'node:crypto';
|
import { randomInt } from 'node:crypto';
|
||||||
import { setTimeout as delay } from 'node:timers/promises';
|
import { setTimeout as delay } from 'node:timers/promises';
|
||||||
import type { LocalContext } from '../runtime/context';
|
import type { LocalContext } from '../runtime/context';
|
||||||
import { sourceEnvironment } from '../runtime/context';
|
import { sourceEnvironment } from '../runtime/context';
|
||||||
import { LocalApi } from '../runtime/api';
|
|
||||||
import { cancellationExitCode, runProcess } from '../runtime/process';
|
import { cancellationExitCode, runProcess } from '../runtime/process';
|
||||||
import { within } from '../runtime/files';
|
import { within } from '../runtime/files';
|
||||||
import { fail } from '../../shared/errors';
|
import { fail } from '../../shared/errors';
|
||||||
@@ -207,11 +206,8 @@ export async function executeTask(
|
|||||||
const started = Math.floor(start / 1000);
|
const started = Math.floor(start / 1000);
|
||||||
const first = options.argv[0]!;
|
const first = options.argv[0]!;
|
||||||
const taskId = await taskIdentity(context, first);
|
const taskId = await taskIdentity(context, first);
|
||||||
const extended = await extendedLifecycle(context);
|
const receipt = new LifecycleReceipt(context, started);
|
||||||
const executionId =
|
const triggerId = receipt.triggerId;
|
||||||
extended && context.env.QL_EXECUTION_ORIGIN === 'scheduled_system'
|
|
||||||
? `legacy-system:${started}:${randomUUID()}`
|
|
||||||
: undefined;
|
|
||||||
const fileName = path.basename(first, path.extname(first));
|
const fileName = path.basename(first, path.extname(first));
|
||||||
const parent = first.includes('/') ? path.basename(path.dirname(first)) : '';
|
const parent = first.includes('/') ? path.basename(path.dirname(first)) : '';
|
||||||
const folder =
|
const folder =
|
||||||
@@ -221,7 +217,7 @@ export async function executeTask(
|
|||||||
folder === '/dev/null'
|
folder === '/dev/null'
|
||||||
? '/dev/null'
|
? '/dev/null'
|
||||||
: context.env.real_log_path ||
|
: context.env.real_log_path ||
|
||||||
path.join(folder, `${timestamp(new Date(start))}.log`);
|
path.join(folder, `${timestamp(new Date(start))}${triggerId ? "-" + triggerId : ""}.log`);
|
||||||
const fullLog =
|
const fullLog =
|
||||||
logPath === '/dev/null'
|
logPath === '/dev/null'
|
||||||
? logPath
|
? logPath
|
||||||
@@ -245,20 +241,17 @@ export async function executeTask(
|
|||||||
process.stderr.write(value);
|
process.stderr.write(value);
|
||||||
})
|
})
|
||||||
: undefined;
|
: undefined;
|
||||||
const api = new LocalApi(context);
|
|
||||||
const report = async (final: boolean, code = 0, duration = 0) => {
|
const report = async (final: boolean, code = 0, duration = 0) => {
|
||||||
if (!taskId) return;
|
if (!taskId) return;
|
||||||
try {
|
try {
|
||||||
await api.call('crons/status', 'PUT', {
|
await receipt.status({
|
||||||
ids: [taskId],
|
ids: [taskId],
|
||||||
status: final ? '1' : '0',
|
status: final ? '1' : '0',
|
||||||
pid: String(process.pid),
|
pid: String(process.pid),
|
||||||
log_path: logPath,
|
log_path: logPath,
|
||||||
last_execution_time: started,
|
last_execution_time: started,
|
||||||
last_running_time: duration,
|
last_running_time: duration,
|
||||||
...(final && extended ? { exit_code: code } : {}),
|
}, final ? code : undefined);
|
||||||
...(executionId ? { execution_id: executionId } : {}),
|
|
||||||
});
|
|
||||||
} catch {
|
} catch {
|
||||||
output(
|
output(
|
||||||
Buffer.from(
|
Buffer.from(
|
||||||
@@ -266,13 +259,9 @@ export async function executeTask(
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (final && extended) {
|
if (final) {
|
||||||
try {
|
try {
|
||||||
await api.call('dashboard/record', 'POST', {
|
await receipt.statistics(taskId, code, duration);
|
||||||
ref_id: taskId,
|
|
||||||
code,
|
|
||||||
elapsed: duration,
|
|
||||||
});
|
|
||||||
} catch {
|
} catch {
|
||||||
output(
|
output(
|
||||||
Buffer.from(
|
Buffer.from(
|
||||||
@@ -282,6 +271,7 @@ export async function executeTask(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
let durationSeconds = 0;
|
||||||
let exitCode = 1,
|
let exitCode = 1,
|
||||||
timedOut = false;
|
timedOut = false;
|
||||||
let languageDirectory: string | undefined;
|
let languageDirectory: string | undefined;
|
||||||
@@ -504,7 +494,7 @@ export async function executeTask(
|
|||||||
} finally {
|
} finally {
|
||||||
if (languageDirectory)
|
if (languageDirectory)
|
||||||
await fs.rm(languageDirectory, { recursive: true, force: true });
|
await fs.rm(languageDirectory, { recursive: true, force: true });
|
||||||
const durationSeconds = Math.max(
|
durationSeconds = Math.max(
|
||||||
1,
|
1,
|
||||||
Math.floor((Date.now() - start) / 1000),
|
Math.floor((Date.now() - start) / 1000),
|
||||||
);
|
);
|
||||||
@@ -534,8 +524,8 @@ export async function executeTask(
|
|||||||
return {
|
return {
|
||||||
exitCode,
|
exitCode,
|
||||||
logPath,
|
logPath,
|
||||||
durationSeconds: Math.max(1, Math.floor((Date.now() - start) / 1000)),
|
durationSeconds,
|
||||||
executionId,
|
executionId: taskId ? receipt.executionId : undefined,
|
||||||
taskId,
|
taskId,
|
||||||
timedOut,
|
timedOut,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -167,7 +167,7 @@ async function readState(tmp) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function getStatus(tmp) {
|
async function getStatus(tmp) {
|
||||||
const state = await readState(tmp);
|
let state = await readState(tmp);
|
||||||
if (active.has(state.status)) {
|
if (active.has(state.status)) {
|
||||||
let alive = false;
|
let alive = false;
|
||||||
if (Number.isSafeInteger(state.pid) && state.pid > 0) {
|
if (Number.isSafeInteger(state.pid) && state.pid > 0) {
|
||||||
@@ -179,10 +179,16 @@ async function getStatus(tmp) {
|
|||||||
}
|
}
|
||||||
} else alive = Date.now() - state.startedAt < 10000;
|
} else alive = Date.now() - state.startedAt < 10000;
|
||||||
if (!alive) {
|
if (!alive) {
|
||||||
|
// The worker can publish its terminal state and exit after our first read.
|
||||||
|
const previous = state;
|
||||||
|
state = await readState(tmp);
|
||||||
|
if (active.has(state.status) && state.id === previous.id &&
|
||||||
|
state.pid === previous.pid && state.startedAt === previous.startedAt) {
|
||||||
state.status = 'failed';
|
state.status = 'failed';
|
||||||
state.error = '升级任务意外中断,请重试';
|
state.error = '升级任务意外中断,请重试';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// Bound reads even when an installer produces a very large log.
|
// Bound reads even when an installer produces a very large log.
|
||||||
const handle = await fs.open(logFile(tmp), 'r').catch(() => undefined);
|
const handle = await fs.open(logFile(tmp), 'r').catch(() => undefined);
|
||||||
if (handle) {
|
if (handle) {
|
||||||
|
|||||||
@@ -45,6 +45,22 @@ function dependencyFailure(error) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Once the group has received its final signal, its numeric ID can be reused.
|
||||||
|
function childGroupSignaler(child) {
|
||||||
|
let finished = false;
|
||||||
|
return (signal) => {
|
||||||
|
if (finished) return;
|
||||||
|
try {
|
||||||
|
if (process.platform !== 'win32' && child.pid) process.kill(-child.pid, signal);
|
||||||
|
else child.kill(signal);
|
||||||
|
if (signal === 'SIGKILL') finished = true;
|
||||||
|
} catch (error) {
|
||||||
|
if (error.code !== 'ESRCH') throw error;
|
||||||
|
finished = true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
async function prestartBackend(
|
async function prestartBackend(
|
||||||
selected,
|
selected,
|
||||||
{ target, signal, limits = probeLimits(target) },
|
{ target, signal, limits = probeLimits(target) },
|
||||||
@@ -136,14 +152,9 @@ async function prestartBackend(
|
|||||||
try {
|
try {
|
||||||
if (child.pid) os.setPriority(child.pid, 10);
|
if (child.pid) os.setPriority(child.pid, 10);
|
||||||
} catch {}
|
} catch {}
|
||||||
|
const signalGroup = childGroupSignaler(child);
|
||||||
const kill = (signalName) => {
|
const kill = (signalName) => {
|
||||||
try {
|
try { signalGroup(signalName); } catch (error) { failure ||= error; }
|
||||||
if (process.platform !== 'win32' && child.pid)
|
|
||||||
process.kill(-child.pid, signalName);
|
|
||||||
else child.kill(signalName);
|
|
||||||
} catch (error) {
|
|
||||||
if (error.code !== 'ESRCH') failure ||= error;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
const stop = (error) => {
|
const stop = (error) => {
|
||||||
failure ||= error;
|
failure ||= error;
|
||||||
@@ -179,8 +190,6 @@ async function prestartBackend(
|
|||||||
// Reap the group on exit rather than waiting for those pipes to close.
|
// Reap the group on exit rather than waiting for those pipes to close.
|
||||||
child.once('exit', () => kill('SIGKILL'));
|
child.once('exit', () => kill('SIGKILL'));
|
||||||
child.on('close', (code, exitSignal) => {
|
child.on('close', (code, exitSignal) => {
|
||||||
// Reap descendants even when the backend exits unexpectedly.
|
|
||||||
kill('SIGKILL');
|
|
||||||
if (failure) return finish(failure);
|
if (failure) return finish(failure);
|
||||||
if (code !== 0 || !report?.ok)
|
if (code !== 0 || !report?.ok)
|
||||||
return finish(
|
return finish(
|
||||||
@@ -335,12 +344,9 @@ async function runRepair(program, args, options) {
|
|||||||
detached: process.platform !== 'win32',
|
detached: process.platform !== 'win32',
|
||||||
stdio: ['ignore', 'pipe', 'pipe'],
|
stdio: ['ignore', 'pipe', 'pipe'],
|
||||||
});
|
});
|
||||||
|
const signalGroup = childGroupSignaler(child);
|
||||||
const kill = (sig) => {
|
const kill = (sig) => {
|
||||||
try {
|
try { signalGroup(sig); } catch {}
|
||||||
if (child.pid && process.platform !== 'win32')
|
|
||||||
process.kill(-child.pid, sig);
|
|
||||||
else child.kill(sig);
|
|
||||||
} catch {}
|
|
||||||
};
|
};
|
||||||
const stop = (error) => {
|
const stop = (error) => {
|
||||||
failure ||= error;
|
failure ||= error;
|
||||||
@@ -365,9 +371,9 @@ async function runRepair(program, args, options) {
|
|||||||
clearTimeout(timer);
|
clearTimeout(timer);
|
||||||
clearTimeout(killTimer);
|
clearTimeout(killTimer);
|
||||||
options.signal?.removeEventListener('abort', abort);
|
options.signal?.removeEventListener('abort', abort);
|
||||||
kill('SIGKILL');
|
|
||||||
};
|
};
|
||||||
child.on('error', (error) => {
|
child.on('error', (error) => {
|
||||||
|
kill('SIGKILL');
|
||||||
cleanup();
|
cleanup();
|
||||||
reject(error);
|
reject(error);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -7,6 +7,12 @@ import { fail } from '../../shared/errors';
|
|||||||
import { isRecord } from '../../shared/record';
|
import { isRecord } from '../../shared/record';
|
||||||
import { operationSignal, requestCancellation } from './cancellation';
|
import { operationSignal, requestCancellation } from './cancellation';
|
||||||
|
|
||||||
|
export class ReceiptValidationError extends Error {
|
||||||
|
constructor(readonly field: string) {
|
||||||
|
super('The running panel rejected the lifecycle field ' + field);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export class LocalApi {
|
export class LocalApi {
|
||||||
private token?: string;
|
private token?: string;
|
||||||
constructor(private readonly context: LocalContext) {}
|
constructor(private readonly context: LocalContext) {}
|
||||||
@@ -65,14 +71,20 @@ export class LocalApi {
|
|||||||
endpoint: string,
|
endpoint: string,
|
||||||
method = 'GET',
|
method = 'GET',
|
||||||
body?: unknown,
|
body?: unknown,
|
||||||
|
finishedAt?: number,
|
||||||
): Promise<Record<string, unknown>> {
|
): Promise<Record<string, unknown>> {
|
||||||
operationSignal()?.throwIfAborted();
|
operationSignal()?.throwIfAborted();
|
||||||
const token = await this.credential();
|
const token = await this.credential();
|
||||||
const port = this.context.env.QlPort || '5700';
|
const port = this.context.env.QlPort || '5700';
|
||||||
if (!/^\d+$/.test(port) || Number(port) < 1 || Number(port) > 65535)
|
if (!/^\d+$/.test(port) || Number(port) < 1 || Number(port) > 65535)
|
||||||
fail(translate(this.context.env, '本机面板端口无效。'), 2);
|
fail(translate(this.context.env, '本机面板端口无效。'), 2);
|
||||||
const request = requestCancellation(30000);
|
const retryReceipt = isRecord(body) && !!(body.execution_id || body.trigger_id) &&
|
||||||
|
((method === 'PUT' && endpoint === 'crons/status') ||
|
||||||
|
(method === 'POST' && endpoint === 'dashboard/record'));
|
||||||
|
const payload = body === undefined ? undefined : JSON.stringify(body);
|
||||||
let result: unknown;
|
let result: unknown;
|
||||||
|
for (let attempt = 0; attempt < 2; attempt++) {
|
||||||
|
const request = requestCancellation(30000);
|
||||||
try {
|
try {
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
`http://127.0.0.1:${port}/open/${endpoint}`,
|
`http://127.0.0.1:${port}/open/${endpoint}`,
|
||||||
@@ -80,17 +92,28 @@ export class LocalApi {
|
|||||||
method,
|
method,
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${token}`,
|
Authorization: `Bearer ${token}`,
|
||||||
|
...(finishedAt !== undefined && { 'X-QL-Finished-At': String(finishedAt) }),
|
||||||
...(body !== undefined
|
...(body !== undefined
|
||||||
? { 'Content-Type': 'application/json' }
|
? { 'Content-Type': 'application/json' }
|
||||||
: {}),
|
: {}),
|
||||||
},
|
},
|
||||||
body: body === undefined ? undefined : JSON.stringify(body),
|
body: payload,
|
||||||
redirect: 'error',
|
redirect: 'error',
|
||||||
signal: request.signal,
|
signal: request.signal,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
if (attempt === 0 && method === 'PUT' && endpoint === 'crons/status' && response.status === 400) {
|
||||||
|
const rejection: unknown = await response.json();
|
||||||
|
const validation = isRecord(rejection) && rejection.validation;
|
||||||
|
const detail = isRecord(validation) && validation.body;
|
||||||
|
if (isRecord(detail) && detail.source === 'body' && Array.isArray(detail.keys) &&
|
||||||
|
detail.keys.length === 1 && ['trigger_id', 'execution_id', 'exit_code'].includes(detail.keys[0])) {
|
||||||
|
throw new ReceiptValidationError(detail.keys[0]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
await response.body?.cancel();
|
await response.body?.cancel();
|
||||||
|
}
|
||||||
fail(
|
fail(
|
||||||
translate(
|
translate(
|
||||||
this.context.env,
|
this.context.env,
|
||||||
@@ -100,7 +123,10 @@ export class LocalApi {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
result = await response.json();
|
result = await response.json();
|
||||||
} catch {
|
break;
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof ReceiptValidationError) throw error;
|
||||||
|
if (attempt === 0 && retryReceipt && request.signal.aborted && !operationSignal()?.aborted) continue;
|
||||||
fail(
|
fail(
|
||||||
translate(
|
translate(
|
||||||
this.context.env,
|
this.context.env,
|
||||||
@@ -110,6 +136,7 @@ export class LocalApi {
|
|||||||
} finally {
|
} finally {
|
||||||
request.dispose();
|
request.dispose();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (!isRecord(result) || result.code !== 200)
|
if (!isRecord(result) || result.code !== 200)
|
||||||
fail(
|
fail(
|
||||||
translate(
|
translate(
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
import { translate } from '../../shared/i18n/index';
|
import { translate } from '../../shared/i18n/index';
|
||||||
import { extendedLifecycle } from './lifecycle';
|
import { LifecycleReceipt } from './lifecycle';
|
||||||
import { within } from './files';
|
import { within } from './files';
|
||||||
import fs from 'node:fs/promises';
|
import fs from 'node:fs/promises';
|
||||||
import { writeSync } from 'node:fs';
|
import { writeSync } from 'node:fs';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
import { randomUUID } from 'node:crypto';
|
|
||||||
import type { LocalContext } from './context';
|
import type { LocalContext } from './context';
|
||||||
import { LocalApi } from './api';
|
|
||||||
import { CliError } from '../../shared/errors';
|
import { CliError } from '../../shared/errors';
|
||||||
import { withOperationOutput } from './output';
|
import { withOperationOutput } from './output';
|
||||||
import { cancellableOperation, interruptedCode } from './cancellation';
|
import { cancellableOperation, interruptedCode } from './cancellation';
|
||||||
@@ -32,7 +30,9 @@ export async function loggedOperation<T>(
|
|||||||
]
|
]
|
||||||
.map((value) => String(value).padStart(2, '0'))
|
.map((value) => String(value).padStart(2, '0'))
|
||||||
.join('-');
|
.join('-');
|
||||||
const logPath = context.env.real_log_path || `${action}/${stamp}.log`;
|
const receipt = new LifecycleReceipt(context, started);
|
||||||
|
const triggerId = receipt.triggerId;
|
||||||
|
const logPath = context.env.real_log_path || `${action}/${stamp}${triggerId ? '-' + triggerId : ''}.log`;
|
||||||
const file = within(context.paths.dir_log!, logPath, context.env);
|
const file = within(context.paths.dir_log!, logPath, context.env);
|
||||||
const persist = context.env.real_time !== 'true';
|
const persist = context.env.real_time !== 'true';
|
||||||
if (persist) await fs.mkdir(path.dirname(file), { recursive: true });
|
if (persist) await fs.mkdir(path.dirname(file), { recursive: true });
|
||||||
@@ -45,25 +45,17 @@ export async function loggedOperation<T>(
|
|||||||
process.stderr.write(chunk);
|
process.stderr.write(chunk);
|
||||||
};
|
};
|
||||||
const id = Number(context.env.ID);
|
const id = Number(context.env.ID);
|
||||||
const extended = await extendedLifecycle(context);
|
|
||||||
const executionId =
|
|
||||||
extended && context.env.QL_EXECUTION_ORIGIN === 'scheduled_system'
|
|
||||||
? `legacy-system:${started}:${randomUUID()}`
|
|
||||||
: undefined;
|
|
||||||
const api = new LocalApi(context);
|
|
||||||
const report = async (final: boolean, code: number) => {
|
const report = async (final: boolean, code: number) => {
|
||||||
if (!Number.isSafeInteger(id) || id <= 0) return;
|
if (!Number.isSafeInteger(id) || id <= 0) return;
|
||||||
try {
|
try {
|
||||||
await api.call('crons/status', 'PUT', {
|
await receipt.status({
|
||||||
ids: [id],
|
ids: [id],
|
||||||
status: final ? '1' : '0',
|
status: final ? '1' : '0',
|
||||||
pid: String(process.pid),
|
pid: String(process.pid),
|
||||||
log_path: logPath,
|
log_path: logPath,
|
||||||
last_execution_time: started,
|
last_execution_time: started,
|
||||||
last_running_time: final ? Math.floor((Date.now() - start) / 1000) : 0,
|
last_running_time: final ? Math.floor((Date.now() - start) / 1000) : 0,
|
||||||
...(final && extended ? { exit_code: code } : {}),
|
}, final ? code : undefined);
|
||||||
...(executionId ? { execution_id: executionId } : {}),
|
|
||||||
});
|
|
||||||
} catch {
|
} catch {
|
||||||
output(
|
output(
|
||||||
Buffer.from(
|
Buffer.from(
|
||||||
|
|||||||
@@ -1,51 +1,63 @@
|
|||||||
import fs from 'node:fs/promises';
|
import { randomUUID } from 'node:crypto';
|
||||||
import path from 'node:path';
|
|
||||||
import type { LocalContext } from './context';
|
import type { LocalContext } from './context';
|
||||||
|
import { LocalApi, ReceiptValidationError } from './api';
|
||||||
|
|
||||||
// The released 2.20 API rejects unknown lifecycle fields and has no dashboard record API.
|
// Negotiate with the worker that validates the real receipt. Keep the agreed
|
||||||
// Read the installed panel version, never the independently versioned CLI package.
|
// wire format for this execution, even if the panel changes before it exits.
|
||||||
export async function extendedLifecycle(
|
export class LifecycleReceipt {
|
||||||
context: LocalContext,
|
private readonly api: LocalApi;
|
||||||
): Promise<boolean> {
|
private identity?: { trigger_id: string } | { execution_id: string };
|
||||||
if (context.env.QL_CLI_LIFECYCLE === 'legacy') return false;
|
private extended: boolean;
|
||||||
if (context.env.QL_CLI_LIFECYCLE === 'extended') return true;
|
private startAttempted = false;
|
||||||
let version: string | undefined;
|
private acceptedStart = false;
|
||||||
|
private finishedAt?: number;
|
||||||
|
readonly triggerId?: string;
|
||||||
|
|
||||||
|
constructor(context: LocalContext, started: number) {
|
||||||
|
this.api = new LocalApi(context);
|
||||||
|
this.extended = context.env.QL_CLI_LIFECYCLE !== 'legacy';
|
||||||
|
const parent = context.env.QL_TRIGGER_ID;
|
||||||
|
if (!this.extended) return;
|
||||||
|
if (parent && /^[a-z0-9-]{1,120}$/.test(parent)) {
|
||||||
|
this.triggerId = parent;
|
||||||
|
this.identity = { trigger_id: parent };
|
||||||
|
} else {
|
||||||
|
const prefix = context.env.QL_EXECUTION_ORIGIN === 'scheduled_system'
|
||||||
|
? `legacy-system:${started}:` : 'client-';
|
||||||
|
this.identity = { execution_id: prefix + randomUUID() };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
get executionId() {
|
||||||
|
return this.identity && 'execution_id' in this.identity ? this.identity.execution_id : undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
async status(body: Record<string, unknown>, exitCode?: number) {
|
||||||
|
const starting = exitCode === undefined;
|
||||||
|
const firstStart = starting && !this.startAttempted;
|
||||||
|
if (starting) this.startAttempted = true;
|
||||||
|
if (!starting && this.identity) this.finishedAt ??= Date.now();
|
||||||
try {
|
try {
|
||||||
const manifest: unknown = JSON.parse(
|
await this.api.call('crons/status', 'PUT', {
|
||||||
await fs.readFile(path.join(context.root, 'package.json'), 'utf8'),
|
...body, ...this.identity, ...(!starting && this.extended && { exit_code: exitCode }),
|
||||||
);
|
}, starting ? undefined : this.finishedAt);
|
||||||
if (
|
} catch (error) {
|
||||||
manifest &&
|
if (!(error instanceof ReceiptValidationError)) throw error;
|
||||||
typeof manifest === 'object' &&
|
if (firstStart && this.identity && error.field in this.identity) {
|
||||||
'version' in manifest &&
|
// The old schema rejected this start before its handler could write.
|
||||||
typeof manifest.version === 'string'
|
this.identity = undefined;
|
||||||
)
|
} else if (!starting && !this.identity && this.extended && error.field === 'exit_code') {
|
||||||
version = manifest.version;
|
this.extended = false;
|
||||||
} catch {}
|
} else {
|
||||||
if (version === undefined) {
|
throw error;
|
||||||
try {
|
}
|
||||||
// Official 2.x images may omit package.version. Read only the top-level
|
await this.api.call('crons/status', 'PUT', body);
|
||||||
// scalar release field; no YAML dependency or expression evaluation.
|
}
|
||||||
const release = await fs.readFile(
|
if (starting) this.acceptedStart = true;
|
||||||
path.join(context.root, 'version.yaml'),
|
}
|
||||||
'utf8',
|
|
||||||
);
|
async statistics(refId: number, code: number, elapsed: number) {
|
||||||
const entries = release
|
if (this.extended && (this.identity || this.acceptedStart))
|
||||||
.replace(/^\uFEFF/, '')
|
await this.api.call('dashboard/record', 'POST', { ref_id: refId, code, elapsed, ...this.identity }, this.finishedAt);
|
||||||
.split(/\r?\n/)
|
|
||||||
.filter((line) => /^version:/.test(line));
|
|
||||||
if (entries.length !== 1) return false;
|
|
||||||
const scalar =
|
|
||||||
/^version:[ \t]*(?:"([^"]+)"|'([^']+)'|([^\s#]+))[ \t]*(?:#.*)?$/.exec(
|
|
||||||
entries[0]!,
|
|
||||||
);
|
|
||||||
version = scalar?.[1] ?? scalar?.[2] ?? scalar?.[3];
|
|
||||||
} catch {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const match = /^2\.(\d+)\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/.exec(
|
|
||||||
version ?? '',
|
|
||||||
);
|
|
||||||
return !!match && Number(match[1]) >= 21;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -199,7 +199,8 @@ async function reconcile(
|
|||||||
await api.call(
|
await api.call(
|
||||||
'crons',
|
'crons',
|
||||||
'DELETE',
|
'DELETE',
|
||||||
obsolete.map((row) => row.id),
|
obsolete.map((row) => ({ id: row.id, command: row.command, script: taskPath(row.command), sub_id: row.sub_id ?? null,
|
||||||
|
...(typeof row.recovery_from === 'number' && { revision: row.recovery_from }) })),
|
||||||
);
|
);
|
||||||
for (const row of obsolete) {
|
for (const row of obsolete) {
|
||||||
await fs.rm(
|
await fs.rm(
|
||||||
@@ -236,6 +237,22 @@ async function reconcile(
|
|||||||
return { added, removed };
|
return { added, removed };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Branches can contain slashes. Reject a linked namespace component before
|
||||||
|
// reading an old checkout or moving the staged tree into place.
|
||||||
|
async function checkNamespace(root: string, name: string, env: NodeJS.ProcessEnv) {
|
||||||
|
let current = root;
|
||||||
|
for (const part of name.split('/')) {
|
||||||
|
current = path.join(current, part);
|
||||||
|
try {
|
||||||
|
if ((await fs.lstat(current)).isSymbolicLink())
|
||||||
|
fail(translate(env, '路径必须位于其管理目录内部。'), 2);
|
||||||
|
} catch (error) {
|
||||||
|
if ((error as NodeJS.ErrnoException).code === 'ENOENT') return;
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function syncRepository(
|
export async function syncRepository(
|
||||||
context: LocalContext,
|
context: LocalContext,
|
||||||
input: SubscriptionInput,
|
input: SubscriptionInput,
|
||||||
@@ -243,6 +260,9 @@ export async function syncRepository(
|
|||||||
const name = repositoryName(input.url, input.branch, context.env);
|
const name = repositoryName(input.url, input.branch, context.env);
|
||||||
const destination = within(context.paths.dir_scripts!, name, context.env);
|
const destination = within(context.paths.dir_scripts!, name, context.env);
|
||||||
const repoDestination = within(context.paths.dir_repo!, name, context.env);
|
const repoDestination = within(context.paths.dir_repo!, name, context.env);
|
||||||
|
const checkPaths = () => Promise.all([context.paths.dir_scripts!, context.paths.dir_repo!]
|
||||||
|
.map(root => checkNamespace(root, name, context.env)));
|
||||||
|
await checkPaths();
|
||||||
const extensions = (
|
const extensions = (
|
||||||
input.extensions ||
|
input.extensions ||
|
||||||
context.env.RepoFileExtensions ||
|
context.env.RepoFileExtensions ||
|
||||||
@@ -325,6 +345,7 @@ export async function syncRepository(
|
|||||||
path.join(checkout, file),
|
path.join(checkout, file),
|
||||||
within(scripts, file, context.env),
|
within(scripts, file, context.env),
|
||||||
);
|
);
|
||||||
|
await checkPaths();
|
||||||
await replaceDirectory(scripts, destination);
|
await replaceDirectory(scripts, destination);
|
||||||
await replaceDirectory(checkout, repoDestination);
|
await replaceDirectory(checkout, repoDestination);
|
||||||
const relativePaths = selected.map((file) =>
|
const relativePaths = selected.map((file) =>
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ export interface OpenOperation {
|
|||||||
method: 'GET' | 'POST' | 'PUT' | 'DELETE';
|
method: 'GET' | 'POST' | 'PUT' | 'DELETE';
|
||||||
path: string;
|
path: string;
|
||||||
params?: string[];
|
params?: string[];
|
||||||
|
uuidParams?: string[];
|
||||||
body?: 'ids' | 'json' | 'object-id';
|
body?: 'ids' | 'json' | 'object-id';
|
||||||
upload?: string;
|
upload?: string;
|
||||||
download?: boolean;
|
download?: boolean;
|
||||||
@@ -58,6 +59,7 @@ export const openOperations: readonly OpenOperation[] = [
|
|||||||
},
|
},
|
||||||
{ name: 'task list', method: 'GET', path: 'crons', existing: true },
|
{ name: 'task list', method: 'GET', path: 'crons', existing: true },
|
||||||
{ name: 'task detail', method: 'GET', path: 'crons/detail' },
|
{ name: 'task detail', method: 'GET', path: 'crons/detail' },
|
||||||
|
{ name: 'task preview', method: 'GET', path: 'crons/preview' },
|
||||||
{ name: 'task create', method: 'POST', path: 'crons', body: 'json' },
|
{ name: 'task create', method: 'POST', path: 'crons', body: 'json' },
|
||||||
{
|
{
|
||||||
name: 'task run',
|
name: 'task run',
|
||||||
@@ -113,6 +115,13 @@ export const openOperations: readonly OpenOperation[] = [
|
|||||||
existing: true,
|
existing: true,
|
||||||
},
|
},
|
||||||
{ name: 'task status', method: 'PUT', path: 'crons/status', body: 'json' },
|
{ name: 'task status', method: 'PUT', path: 'crons/status', body: 'json' },
|
||||||
|
{ name: 'task daily-results', method: 'GET', path: 'crons/:id/daily-results', params: ['id'] },
|
||||||
|
{
|
||||||
|
name: 'task triggers',
|
||||||
|
method: 'GET',
|
||||||
|
path: 'crons/:id/triggers',
|
||||||
|
params: ['id'],
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: 'task instances',
|
name: 'task instances',
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
@@ -281,6 +290,8 @@ export const openOperations: readonly OpenOperation[] = [
|
|||||||
{ name: 'config save', method: 'POST', path: 'configs/save', body: 'json' },
|
{ name: 'config save', method: 'POST', path: 'configs/save', body: 'json' },
|
||||||
{ name: 'script list', method: 'GET', path: 'scripts' },
|
{ name: 'script list', method: 'GET', path: 'scripts' },
|
||||||
{ name: 'script get', method: 'GET', path: 'scripts/detail' },
|
{ name: 'script get', method: 'GET', path: 'scripts/detail' },
|
||||||
|
{ name: 'script entries', method: 'GET', path: 'scripts/entries' },
|
||||||
|
{ name: 'script entries-check', method: 'POST', path: 'scripts/entries/check', body: 'json' },
|
||||||
{ name: 'script history-list', method: 'GET', path: 'scripts/history' },
|
{ name: 'script history-list', method: 'GET', path: 'scripts/history' },
|
||||||
{
|
{
|
||||||
name: 'script history-detail',
|
name: 'script history-detail',
|
||||||
@@ -325,6 +336,8 @@ export const openOperations: readonly OpenOperation[] = [
|
|||||||
},
|
},
|
||||||
{ name: 'log list', method: 'GET', path: 'logs' },
|
{ name: 'log list', method: 'GET', path: 'logs' },
|
||||||
{ name: 'log get', method: 'GET', path: 'logs/detail' },
|
{ name: 'log get', method: 'GET', path: 'logs/detail' },
|
||||||
|
{ name: 'log entries', method: 'GET', path: 'logs/entries' },
|
||||||
|
{ name: 'log entries-check', method: 'POST', path: 'logs/entries/check', body: 'json' },
|
||||||
{ name: 'log delete', method: 'DELETE', path: 'logs', body: 'json' },
|
{ name: 'log delete', method: 'DELETE', path: 'logs', body: 'json' },
|
||||||
{
|
{
|
||||||
name: 'log download',
|
name: 'log download',
|
||||||
@@ -334,6 +347,24 @@ export const openOperations: readonly OpenOperation[] = [
|
|||||||
download: true,
|
download: true,
|
||||||
},
|
},
|
||||||
{ name: 'dependency list', method: 'GET', path: 'dependencies' },
|
{ name: 'dependency list', method: 'GET', path: 'dependencies' },
|
||||||
|
{
|
||||||
|
name: 'dependency refresh',
|
||||||
|
method: 'POST',
|
||||||
|
path: 'dependencies/refresh',
|
||||||
|
body: 'json',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'dependency refresh-active',
|
||||||
|
method: 'GET',
|
||||||
|
path: 'dependencies/refresh/active',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'dependency refresh-status',
|
||||||
|
method: 'GET',
|
||||||
|
path: 'dependencies/refresh/:id',
|
||||||
|
params: ['id'],
|
||||||
|
uuidParams: ['id'],
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: 'dependency create',
|
name: 'dependency create',
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
@@ -589,13 +620,6 @@ export const openOperations: readonly OpenOperation[] = [
|
|||||||
body: 'json',
|
body: 'json',
|
||||||
anonymous: true,
|
anonymous: true,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
name: 'user notification-init',
|
|
||||||
method: 'PUT',
|
|
||||||
path: 'user/notification/init',
|
|
||||||
body: 'json',
|
|
||||||
anonymous: true,
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
name: 'user avatar',
|
name: 'user avatar',
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
|
|||||||
@@ -107,6 +107,14 @@ export async function auth(
|
|||||||
{
|
{
|
||||||
crons: 'crons?page=1&size=1',
|
crons: 'crons?page=1&size=1',
|
||||||
configs: 'configs/files',
|
configs: 'configs/files',
|
||||||
|
'envs:read': 'envs',
|
||||||
|
'configs:read': 'configs/files',
|
||||||
|
'scripts:read': 'scripts',
|
||||||
|
'crons:read': 'crons?page=1&size=1',
|
||||||
|
'subscriptions:read': 'subscriptions',
|
||||||
|
'dependencies:read': 'dependencies',
|
||||||
|
'logs:read': 'logs',
|
||||||
|
'dashboard:read': 'dashboard/overview',
|
||||||
dashboard: 'dashboard/overview',
|
dashboard: 'dashboard/overview',
|
||||||
} as Record<string, string>
|
} as Record<string, string>
|
||||||
)[scope] ?? scope,
|
)[scope] ?? scope,
|
||||||
|
|||||||
@@ -49,12 +49,19 @@ async function input(value: string | boolean | undefined): Promise<unknown> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const UUID_PATTERN =
|
||||||
|
'[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}';
|
||||||
|
|
||||||
function route(method: string, path: string): OpenOperation {
|
function route(method: string, path: string): OpenOperation {
|
||||||
const match = openOperations.find(
|
const match = openOperations.find(
|
||||||
(op) =>
|
(op) =>
|
||||||
op.method === method &&
|
op.method === method &&
|
||||||
new RegExp(
|
new RegExp(
|
||||||
'^' + op.path.replace(/:[A-Za-z]+/g, '[1-9][0-9]*') + '$',
|
'^' +
|
||||||
|
op.path.replace(/:([A-Za-z]+)/g, (_match, name) =>
|
||||||
|
op.uuidParams?.includes(name) ? UUID_PATTERN : '[1-9][0-9]*',
|
||||||
|
) +
|
||||||
|
'$',
|
||||||
).test(path),
|
).test(path),
|
||||||
);
|
);
|
||||||
if (!match)
|
if (!match)
|
||||||
@@ -63,7 +70,8 @@ function route(method: string, path: string): OpenOperation {
|
|||||||
'Method/path is not a supported OpenAPI route.',
|
'Method/path is not a supported OpenAPI route.',
|
||||||
);
|
);
|
||||||
for (const [index, part] of match.path.split('/').entries())
|
for (const [index, part] of match.path.split('/').entries())
|
||||||
if (part.startsWith(':')) integer(path.split('/')[index]!);
|
if (part.startsWith(':') && !match.uuidParams?.includes(part.slice(1)))
|
||||||
|
integer(path.split('/')[index]!);
|
||||||
return match;
|
return match;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,7 +115,11 @@ export async function openCommand(
|
|||||||
if (!generic) {
|
if (!generic) {
|
||||||
endpoint = op.path;
|
endpoint = op.path;
|
||||||
for (const [index, name] of (op.params ?? []).entries()) {
|
for (const [index, name] of (op.params ?? []).entries()) {
|
||||||
const value = integer(command.positionals[index]!);
|
const positional = command.positionals[index]!;
|
||||||
|
const isUuid = op.uuidParams?.includes(name);
|
||||||
|
if (isUuid && !new RegExp(`^${UUID_PATTERN}$`).test(positional))
|
||||||
|
usage('任务 ID 必须为 UUID。', 'Job ID must be a UUID.');
|
||||||
|
const value = isUuid ? positional : integer(positional);
|
||||||
endpoint = endpoint.replace(`:${name}`, String(value));
|
endpoint = endpoint.replace(`:${name}`, String(value));
|
||||||
if (op.body === 'object-id') {
|
if (op.body === 'object-id') {
|
||||||
if (body !== undefined && !isRecord(body))
|
if (body !== undefined && !isRecord(body))
|
||||||
|
|||||||
@@ -26,6 +26,14 @@ export const commands: readonly CommandSpec[] = [
|
|||||||
default: 'crons',
|
default: 'crons',
|
||||||
choices: [
|
choices: [
|
||||||
...new Set(openOperations.map((op) => op.path.split('/')[0]!)),
|
...new Set(openOperations.map((op) => op.path.split('/')[0]!)),
|
||||||
|
'envs:read',
|
||||||
|
'configs:read',
|
||||||
|
'scripts:read',
|
||||||
|
'crons:read',
|
||||||
|
'subscriptions:read',
|
||||||
|
'dependencies:read',
|
||||||
|
'logs:read',
|
||||||
|
'dashboard:read',
|
||||||
].filter((scope) => !['auth', 'update'].includes(scope)),
|
].filter((scope) => !['auth', 'update'].includes(scope)),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -4,33 +4,8 @@ const fs = require('node:fs/promises');
|
|||||||
const os = require('node:os');
|
const os = require('node:os');
|
||||||
const path = require('node:path');
|
const path = require('node:path');
|
||||||
const { createContext } = require('../../dist/internal/runtime/context');
|
const { createContext } = require('../../dist/internal/runtime/context');
|
||||||
const { extendedLifecycle } = require('../../dist/internal/runtime/lifecycle');
|
|
||||||
const { loggedOperation } = require('../../dist/internal/runtime/commandLog');
|
const { loggedOperation } = require('../../dist/internal/runtime/commandLog');
|
||||||
|
|
||||||
test('installed panel version selects lifecycle schema and unknown versions stay conservative', async (t) => {
|
|
||||||
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'ql-lifecycle-'));
|
|
||||||
t.after(() => fs.rm(root, { recursive: true, force: true }));
|
|
||||||
const context = createContext({ root }, {});
|
|
||||||
assert.equal(await extendedLifecycle(context), false);
|
|
||||||
for (const [version, expected] of [
|
|
||||||
['2.20.1', false],
|
|
||||||
['2.19.0', false],
|
|
||||||
['2.21.0-14', true],
|
|
||||||
['3.0.0', false],
|
|
||||||
['invalid', false],
|
|
||||||
]) {
|
|
||||||
await fs.writeFile(
|
|
||||||
path.join(root, 'package.json'),
|
|
||||||
JSON.stringify({ version }),
|
|
||||||
);
|
|
||||||
assert.equal(await extendedLifecycle(context), expected);
|
|
||||||
}
|
|
||||||
context.env.QL_CLI_LIFECYCLE = 'extended';
|
|
||||||
assert.equal(await extendedLifecycle(context), true);
|
|
||||||
context.env.QL_CLI_LIFECYCLE = 'legacy';
|
|
||||||
assert.equal(await extendedLifecycle(context), false);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('command log preserves scheduler path and rejects escaping the log directory', async (t) => {
|
test('command log preserves scheduler path and rejects escaping the log directory', async (t) => {
|
||||||
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'ql-scheduler-log-'));
|
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'ql-scheduler-log-'));
|
||||||
t.after(() => fs.rm(root, { recursive: true, force: true }));
|
t.after(() => fs.rm(root, { recursive: true, force: true }));
|
||||||
@@ -50,85 +25,82 @@ test('command log preserves scheduler path and rejects escaping the log director
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('released version.yaml selects lifecycle only when package version is absent', async (t) => {
|
test('managed CLI tasks and maintenance commands report against the parent receipt', async t => {
|
||||||
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'ql-release-version-'));
|
|
||||||
t.after(() => fs.rm(root, { recursive: true, force: true }));
|
|
||||||
const context = createContext({ root }, {});
|
|
||||||
await fs.writeFile(
|
|
||||||
path.join(root, 'package.json'),
|
|
||||||
'{"name":"released-panel"}',
|
|
||||||
);
|
|
||||||
for (const [release, expected] of [
|
|
||||||
['version: 2.20.1\nchangeLog: |\n version: 2.99.0\n', false],
|
|
||||||
['version: 2.21.0-14\npublishTime: 2026-09-25\n', true],
|
|
||||||
['version: "2.21.1" # release\n', true],
|
|
||||||
["\ufeffversion: '2.22.0'\r\n", true],
|
|
||||||
['version: 3.0.0\n', false],
|
|
||||||
['version: 2.21.invalid\n', false],
|
|
||||||
['version: 2.21.0\nversion: 2.20.1\n', false],
|
|
||||||
['changeLog: |\n version: 2.21.0\n', false],
|
|
||||||
['version: !custom 2.21.0\n', false],
|
|
||||||
]) {
|
|
||||||
await fs.writeFile(path.join(root, 'version.yaml'), release);
|
|
||||||
assert.equal(await extendedLifecycle(context), expected, release);
|
|
||||||
}
|
|
||||||
await fs.writeFile(path.join(root, 'version.yaml'), 'version: 2.21.1\n');
|
|
||||||
await fs.writeFile(path.join(root, 'package.json'), '{"version":"2.20.1"}');
|
|
||||||
assert.equal(await extendedLifecycle(context), false);
|
|
||||||
context.env.QL_CLI_LIFECYCLE = 'extended';
|
|
||||||
assert.equal(await extendedLifecycle(context), true);
|
|
||||||
context.env.QL_CLI_LIFECYCLE = 'legacy';
|
|
||||||
assert.equal(await extendedLifecycle(context), false);
|
|
||||||
delete context.env.QL_CLI_LIFECYCLE;
|
|
||||||
await fs.rm(path.join(root, 'package.json'));
|
|
||||||
assert.equal(await extendedLifecycle(context), true);
|
|
||||||
await fs.rm(path.join(root, 'version.yaml'));
|
|
||||||
assert.equal(await extendedLifecycle(context), false);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('release-only version metadata controls actual task and maintenance status payloads', async (t) => {
|
|
||||||
const { LocalApi } = require('../../dist/internal/runtime/api');
|
const { LocalApi } = require('../../dist/internal/runtime/api');
|
||||||
const { executeTask } = require('../../dist/internal/execution/taskRunner');
|
const { executeTask } = require('../../dist/internal/execution/taskRunner');
|
||||||
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'ql-release-payload-'));
|
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'ql-managed-cli-'));
|
||||||
t.after(() => fs.rm(root, { recursive: true, force: true }));
|
t.after(() => fs.rm(root, { recursive: true, force: true }));
|
||||||
const context = createContext(
|
const id = 'parent-receipt-7';
|
||||||
{ root },
|
const context = createContext({ root }, {
|
||||||
{ PATH: process.env.PATH, ID: '7', no_tee: 'true' },
|
PATH: process.env.PATH, ID: '7', no_tee: 'true', QL_TRIGGER_ID: id,
|
||||||
);
|
QL_CLI_LIFECYCLE: 'extended', QL_EXECUTION_ORIGIN: 'scheduled_system',
|
||||||
|
});
|
||||||
await fs.mkdir(context.paths.dir_scripts, { recursive: true });
|
await fs.mkdir(context.paths.dir_scripts, { recursive: true });
|
||||||
await fs.writeFile(
|
await fs.writeFile(path.join(context.paths.dir_scripts, 'fixture.sh'), 'return 7');
|
||||||
path.join(context.paths.dir_scripts, 'fixture.sh'),
|
|
||||||
'return 7',
|
|
||||||
);
|
|
||||||
const calls = [];
|
const calls = [];
|
||||||
t.mock.method(LocalApi.prototype, 'call', async (endpoint, method, body) => {
|
t.mock.method(LocalApi.prototype, 'call', async (endpoint, method, body) => {
|
||||||
calls.push({ endpoint, body });
|
calls.push({ endpoint, body });
|
||||||
return { code: 200 };
|
return { code: 200 };
|
||||||
});
|
});
|
||||||
for (const version of ['2.20.1', '2.21.0-14']) {
|
const result = await executeTask(context, { argv: ['fixture.sh'], mode: 'now' });
|
||||||
await fs.writeFile(
|
|
||||||
path.join(root, 'version.yaml'),
|
|
||||||
`version: ${version}\n`,
|
|
||||||
);
|
|
||||||
calls.length = 0;
|
|
||||||
const result = await executeTask(context, {
|
|
||||||
argv: ['fixture.sh'],
|
|
||||||
mode: 'now',
|
|
||||||
});
|
|
||||||
assert.equal(result.exitCode, 7);
|
assert.equal(result.exitCode, 7);
|
||||||
const final = calls
|
assert.deepEqual(calls.map(r => r.endpoint), ['crons/status', 'crons/status', 'dashboard/record']);
|
||||||
.filter((row) => row.endpoint === 'crons/status')
|
|
||||||
.at(-1).body;
|
|
||||||
assert.equal(final.exit_code, version === '2.20.1' ? undefined : 7);
|
|
||||||
assert.equal(
|
|
||||||
calls.some((row) => row.endpoint === 'dashboard/record'),
|
|
||||||
version !== '2.20.1',
|
|
||||||
);
|
|
||||||
calls.length = 0;
|
|
||||||
await loggedOperation(context, 'extra', async () => true);
|
await loggedOperation(context, 'extra', async () => true);
|
||||||
assert.equal(
|
assert.equal(calls.length, 5);
|
||||||
calls.at(-1).body.exit_code,
|
for (const { body } of calls) {
|
||||||
version === '2.20.1' ? undefined : 0,
|
assert.equal(body.trigger_id, id);
|
||||||
);
|
assert.equal(body.execution_id, undefined);
|
||||||
}
|
}
|
||||||
|
calls.length = 0;
|
||||||
|
context.env.QL_CLI_LIFECYCLE = 'legacy';
|
||||||
|
await loggedOperation(context, 'extra', async () => true);
|
||||||
|
assert.ok(calls.length);
|
||||||
|
assert.ok(calls.every(r => !('trigger_id' in r.body)));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('distinct managed triggers in the same clock tick have separate maintenance and task logs', async t => {
|
||||||
|
const { executeTask } = require('../../dist/internal/execution/taskRunner');
|
||||||
|
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'ql-same-tick-'));
|
||||||
|
t.after(() => fs.rm(root, { recursive: true, force: true }));
|
||||||
|
t.mock.method(Date, 'now', () => 1800000000000);
|
||||||
|
const run = async id => {
|
||||||
|
const context = createContext({ root }, { QL_CLI_LIFECYCLE: 'extended', QL_TRIGGER_ID: id,
|
||||||
|
PATH: process.env.PATH, no_tee: 'true', log_name: 'shared' });
|
||||||
|
await fs.mkdir(context.paths.dir_scripts, { recursive: true });
|
||||||
|
await fs.writeFile(path.join(context.paths.dir_scripts, id + '.sh'), 'echo ' + id);
|
||||||
|
const command = await loggedOperation(context, 'extra', async () => {});
|
||||||
|
const task = await executeTask(context, { argv: [id + '.sh'], mode: 'now' });
|
||||||
|
return { command: command.logPath, file: task.logPath,
|
||||||
|
content: await fs.readFile(path.join(context.paths.dir_log, task.logPath), 'utf8') };
|
||||||
|
};
|
||||||
|
const [a, b] = await Promise.all([run('marker-alpha'), run('marker-bravo')]);
|
||||||
|
assert.notEqual(a.command, b.command);
|
||||||
|
assert.notEqual(a.file, b.file);
|
||||||
|
assert.match(a.content, /marker-alpha/);
|
||||||
|
assert.doesNotMatch(a.content, /marker-bravo/);
|
||||||
|
assert.match(b.content, /marker-bravo/);
|
||||||
|
assert.doesNotMatch(b.content, /marker-alpha/);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('same-tick standalone tasks and commands share a receipt only within one invocation', async t => {
|
||||||
|
const { LocalApi } = require('../../dist/internal/runtime/api');
|
||||||
|
const { executeTask } = require('../../dist/internal/execution/taskRunner');
|
||||||
|
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'ql-client-receipts-'));
|
||||||
|
t.after(() => fs.rm(root, { recursive: true, force: true }));
|
||||||
|
t.mock.method(Date, 'now', () => 1800000000000);
|
||||||
|
const context = createContext({root}, {ID:'5', QL_CLI_LIFECYCLE:'extended', no_tee:'true',
|
||||||
|
real_log_path:'shared.log', PATH:process.env.PATH});
|
||||||
|
await fs.mkdir(context.paths.dir_scripts, {recursive:true});
|
||||||
|
await fs.writeFile(path.join(context.paths.dir_scripts, 'receipt.sh'), 'exit 0');
|
||||||
|
const groups = new Map();
|
||||||
|
t.mock.method(LocalApi.prototype, 'call', async (endpoint, method, body) => {
|
||||||
|
const id = body.execution_id;
|
||||||
|
assert.match(id, /^client-[0-9a-f-]{36}$/);
|
||||||
|
groups.set(id, [...(groups.get(id) || []), body.status ?? 'stat']);
|
||||||
|
return {code:200};
|
||||||
|
});
|
||||||
|
await Promise.all([executeTask(context, {argv:['receipt.sh'], mode:'now'}),
|
||||||
|
executeTask(context, {argv:['receipt.sh'], mode:'now'}),
|
||||||
|
loggedOperation(context, 'extra', async () => {})]);
|
||||||
|
assert.deepEqual([...groups.values()].map(sequence => sequence.join(',')).sort(), ['0,1','0,1,stat','0,1,stat']);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -759,6 +759,7 @@ test('lifecycle reports share execution identity and statistics survive a reject
|
|||||||
ref_id: 12,
|
ref_id: 12,
|
||||||
code: 9,
|
code: 9,
|
||||||
elapsed: result.durationSeconds,
|
elapsed: result.durationSeconds,
|
||||||
|
execution_id: result.executionId,
|
||||||
});
|
});
|
||||||
assert.match(
|
assert.match(
|
||||||
fs.readFileSync(path.join(context.paths.dir_log, result.logPath), 'utf8'),
|
fs.readFileSync(path.join(context.paths.dir_log, result.logPath), 'utf8'),
|
||||||
|
|||||||
@@ -287,3 +287,122 @@ for (const language of ['zh', 'en', 'unsupported']) {
|
|||||||
assert.deepEqual(seen, ['/open/http', '/open/rejected']);
|
assert.deepEqual(seen, ['/open/http', '/open/rejected']);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test('a lifecycle timeout retries the identical payload once and other writes are not retried', async t => {
|
||||||
|
const context = await fixture(t, () => {});
|
||||||
|
await token(context);
|
||||||
|
const requests = [];
|
||||||
|
const timer = global.setTimeout;
|
||||||
|
t.mock.method(global, 'setTimeout', (fn, ms, ...args) => timer(fn, ms === 30000 ? 5 : ms, ...args));
|
||||||
|
t.mock.method(global, 'fetch', async (_, {body, signal}) => {
|
||||||
|
requests.push(body);
|
||||||
|
return new Promise((_, reject) => signal.addEventListener('abort', () => reject(signal.reason), {once:true}));
|
||||||
|
});
|
||||||
|
const api = new LocalApi(context);
|
||||||
|
for (const [endpoint, method, body, attempts] of [
|
||||||
|
['crons/status','PUT',{ execution_id:'client-' + require('node:crypto').randomUUID(), status:'0' },2],
|
||||||
|
['dashboard/record','POST',{ trigger_id:'managed-fixture', code:0 },2],
|
||||||
|
['crons/status','PUT',{ status:'0' },1],
|
||||||
|
['crons','POST',{ trigger_id:'unrelated' },1],
|
||||||
|
]) {
|
||||||
|
requests.length = 0;
|
||||||
|
await assert.rejects(api.call(endpoint, method, body));
|
||||||
|
assert.deepEqual(requests, Array(attempts).fill(JSON.stringify(body)));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('the live status schema selects every receipt on old, intermediate and upgraded panels', async t => {
|
||||||
|
const { executeTask } = require('../../dist/internal/execution/taskRunner');
|
||||||
|
const { loggedOperation } = require('../../dist/internal/runtime/commandLog');
|
||||||
|
let protocol;
|
||||||
|
const written = [], rejected = [];
|
||||||
|
const context = await fixture(t, async (req, res) => {
|
||||||
|
let raw = '';
|
||||||
|
for await (const chunk of req) raw += chunk;
|
||||||
|
const body = JSON.parse(raw);
|
||||||
|
const forbidden = req.url.endsWith('/status') && Object.keys(body).find(field =>
|
||||||
|
(protocol !== 'identified' && ['execution_id','trigger_id'].includes(field)) ||
|
||||||
|
(protocol === 'legacy' && field === 'exit_code'));
|
||||||
|
if (forbidden) {
|
||||||
|
rejected.push(forbidden);
|
||||||
|
res.writeHead(400).end(JSON.stringify({validation:{body:{source:'body',keys:[forbidden]}}}));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
written.push([req.url,body]);
|
||||||
|
res.end('{"code":200}');
|
||||||
|
});
|
||||||
|
await token(context);
|
||||||
|
Object.assign(context.env, { ID: '12', no_tee: 'true' });
|
||||||
|
await fs.mkdir(context.paths.dir_scripts, {recursive:true});
|
||||||
|
await fs.writeFile(path.join(context.paths.dir_scripts,'probe.sh'), 'return 7');
|
||||||
|
// Even a full newer installation on disk cannot speak on behalf of the old process.
|
||||||
|
await fs.mkdir(path.join(context.root,'static/build/shared'), {recursive:true});
|
||||||
|
await fs.writeFile(path.join(context.root,'static/build/shared/executionIdentity.js'), '');
|
||||||
|
for (protocol of ['legacy', 'exit-code', 'identified']) {
|
||||||
|
await fs.writeFile(path.join(context.root,'package.json'), JSON.stringify({version:protocol === 'identified' ? '2.20.1' : '2.25.0'}));
|
||||||
|
for (const managed of [false,true]) {
|
||||||
|
if (managed) context.env.QL_TRIGGER_ID = 'parent-123';
|
||||||
|
else delete context.env.QL_TRIGGER_ID;
|
||||||
|
for (const task of [true,false]) {
|
||||||
|
written.length = rejected.length = 0;
|
||||||
|
if (task) assert.equal((await executeTask(context,{argv:['probe.sh'],mode:'now'})).exitCode,7);
|
||||||
|
else await loggedOperation(context,'extra',async()=>{});
|
||||||
|
const [start,end] = written.map(r=>r[1]);
|
||||||
|
const key = managed ? 'trigger_id' : 'execution_id';
|
||||||
|
assert.deepEqual(rejected, protocol === 'legacy' ? [key,'exit_code'] : protocol === 'exit-code' ? [key] : []);
|
||||||
|
assert.equal(start.status,'0'); assert.equal(end.status,'1');
|
||||||
|
assert.equal(end.exit_code, protocol === 'legacy' ? undefined : task ? 7 : 0);
|
||||||
|
assert.equal(written.length, task && protocol !== 'legacy' ? 3 : 2);
|
||||||
|
for (const [,body] of written) assert.equal(body[key],start[key]);
|
||||||
|
if (protocol === 'identified') assert.ok(managed ? start[key] === 'parent-123' : /^client-/.test(start[key]));
|
||||||
|
else assert.equal(start[key],undefined);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a missing start response or a non-schema failure never switches a receipt to anonymous', async t => {
|
||||||
|
const { LifecycleReceipt } = require('../../dist/internal/runtime/lifecycle');
|
||||||
|
const calls = [];
|
||||||
|
let rejectedField;
|
||||||
|
const ctx = await fixture(t, async (req,res) => {
|
||||||
|
let raw=''; for await (const chunk of req) raw+=chunk;
|
||||||
|
calls.push(JSON.parse(raw));
|
||||||
|
if (!rejectedField) { res.destroy(); return; }
|
||||||
|
res.writeHead(400).end(JSON.stringify({ validation: {body: {source:'body',keys:[rejectedField]}} }));
|
||||||
|
});
|
||||||
|
await token(ctx);
|
||||||
|
const receipt = new LifecycleReceipt(ctx,10);
|
||||||
|
await assert.rejects(receipt.status({ids:[5],status:'0'}));
|
||||||
|
rejectedField = 'execution_id';
|
||||||
|
await assert.rejects(receipt.status({ids:[5],status:'1'},7));
|
||||||
|
assert.equal(calls.length,2);
|
||||||
|
assert.equal(calls[0].execution_id,calls[1].execution_id);
|
||||||
|
rejectedField = 'pid';
|
||||||
|
await assert.rejects(new LifecycleReceipt(ctx,11).status({ids:[5],status:'0'}));
|
||||||
|
assert.equal(calls.length,3);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('dated status, statistics and retries share an exit instant without changing the identified body schema', async t => {
|
||||||
|
const { LifecycleReceipt } = require('../../dist/internal/runtime/lifecycle');
|
||||||
|
const writes=[];
|
||||||
|
const ctx=await fixture(t,async(req,res)=>{
|
||||||
|
let raw=''; for await(const chunk of req)raw+=chunk;
|
||||||
|
const body=JSON.parse(raw);
|
||||||
|
writes.push({body, time:req.headers['x-ql-finished-at']});
|
||||||
|
res.end(JSON.stringify({code:200}));
|
||||||
|
});
|
||||||
|
await token(ctx);
|
||||||
|
const receipt=new LifecycleReceipt(ctx,Math.floor(Date.now()/1000));
|
||||||
|
await receipt.status({status:'0'});
|
||||||
|
await receipt.status({status:'1'},17);
|
||||||
|
await receipt.status({status:'1'},17);
|
||||||
|
await receipt.statistics(1,17,2);
|
||||||
|
assert.equal(writes[0].time,undefined);
|
||||||
|
assert.match(writes[1].time,/^\d{13}$/);
|
||||||
|
for (const {body,time} of writes.slice(1)) {
|
||||||
|
assert.equal(time,writes[1].time);
|
||||||
|
assert.equal(body.execution_id,writes[0].body.execution_id);
|
||||||
|
assert.equal(body.finished_at,undefined);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|||||||
@@ -164,7 +164,7 @@ test('subscription reconciliation scopes removals to canonical paths and the cur
|
|||||||
});
|
});
|
||||||
assert.equal(result.added, 1);
|
assert.equal(result.added, 1);
|
||||||
assert.equal(result.removed, 1);
|
assert.equal(result.removed, 1);
|
||||||
assert.deepEqual(requests.find((r) => r.method === 'DELETE').body, [1]);
|
assert.deepEqual(requests.find((r) => r.method === 'DELETE').body, [{ id: 1, script: 'owner_repository/old.js', command: rows[0].command, sub_id: 7 }]);
|
||||||
assert.deepEqual(requests.find((r) => r.method === 'POST').body, {
|
assert.deepEqual(requests.find((r) => r.method === 'POST').body, {
|
||||||
name: 'New task',
|
name: 'New task',
|
||||||
schedule: '0 9 * * *',
|
schedule: '0 9 * * *',
|
||||||
@@ -359,6 +359,7 @@ test('subscription copy precedence preserves local dependencies while selected s
|
|||||||
env: {
|
env: {
|
||||||
...context.env,
|
...context.env,
|
||||||
SOURCE: source,
|
SOURCE: source,
|
||||||
|
dir_shell: path.resolve(__dirname, '../../../shell'),
|
||||||
dir_scripts: legacyRoot,
|
dir_scripts: legacyRoot,
|
||||||
dir_list_tmp: context.paths.dir_list_tmp,
|
dir_list_tmp: context.paths.dir_list_tmp,
|
||||||
dir_dep: context.paths.dir_dep,
|
dir_dep: context.paths.dir_dep,
|
||||||
@@ -431,3 +432,22 @@ test('cron metadata precedence agrees with unchanged legacy add_cron', (t) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('a slash in a branch cannot publish through a linked namespace parent', async t => {
|
||||||
|
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ql-nested-namespace-'));
|
||||||
|
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
|
||||||
|
const context = createContext({ root }, { PATH: process.env.PATH });
|
||||||
|
const outside = path.join(root, 'outside');
|
||||||
|
fs.mkdirSync(outside);
|
||||||
|
fs.writeFileSync(path.join(outside, 'sentinel'), 'untouched');
|
||||||
|
for (const dir of [context.paths.dir_repo, context.paths.dir_scripts]) {
|
||||||
|
fs.mkdirSync(dir, { recursive: true });
|
||||||
|
const link = path.join(dir, 'owner_repo_feature');
|
||||||
|
fs.symlinkSync(outside, link);
|
||||||
|
await assert.rejects(syncRepository(context, {
|
||||||
|
url: 'https://example.invalid/owner/repo.git', branch: 'feature/branch',
|
||||||
|
}), /管理目录|managed directory/);
|
||||||
|
assert.deepEqual(fs.readdirSync(outside), ['sentinel']);
|
||||||
|
fs.unlinkSync(link);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|||||||
@@ -28,12 +28,12 @@ const fs = require('node:fs/promises');
|
|||||||
assert.equal(result.status, 0, args.slice(0, 2).join(' ') + ': ' + result.stderr);
|
assert.equal(result.status, 0, args.slice(0, 2).join(' ') + ': ' + result.stderr);
|
||||||
return JSON.parse(result.stdout).data;
|
return JSON.parse(result.stdout).data;
|
||||||
};
|
};
|
||||||
const app = cli(['app', 'create', '--name', 'cli-fixture', '--scopes', 'crons,subscriptions,envs,configs,scripts,logs,dependencies,system,dashboard,apps', '--show-secrets'], true);
|
const app = cli(['app', 'create', '--name', 'cli-fixture', '--scopes', 'crons,subscriptions,envs,configs,scripts,logs,dependencies,system,dashboard', '--show-secrets'], true);
|
||||||
assert.ok(app.client_id && app.client_secret);
|
assert.ok(app.client_id && app.client_secret);
|
||||||
cli(['login', '--url', url], false, { QL_CLIENT_ID: app.client_id, QL_CLIENT_SECRET: app.client_secret });
|
cli(['login', '--url', url], false, { QL_CLIENT_ID: app.client_id, QL_CLIENT_SECRET: app.client_secret });
|
||||||
cli(['auth', 'status', '--scope', 'apps']);
|
cli(['auth', 'status', '--scope', 'crons']);
|
||||||
cli(['app', 'update', String(app.id), '--name', 'cli-updated', '--scopes', 'crons,subscriptions,envs,configs,scripts,logs,dependencies,system,dashboard,apps']);
|
cli(['app', 'update', String(app.id), '--name', 'cli-updated', '--scopes', 'crons,subscriptions,envs,configs,scripts,logs,dependencies,system,dashboard'], true);
|
||||||
assert.ok(cli(['app', 'list']).every(item => !('client_secret' in item) && !('tokens' in item)));
|
assert.ok(cli(['app', 'list'], true).every(item => !('client_secret' in item) && !('tokens' in item)));
|
||||||
const task = cli(['task', 'create', '--name', 'cli-task', '--command', 'echo OPENAPI_FIXTURE', '--schedule', '0 0 * * *']);
|
const task = cli(['task', 'create', '--name', 'cli-task', '--command', 'echo OPENAPI_FIXTURE', '--schedule', '0 0 * * *']);
|
||||||
cli(['task', 'update', String(task.id), '--name', 'cli-task-updated', '--command', 'echo OPENAPI_UPDATED', '--schedule', '0 1 * * *']);
|
cli(['task', 'update', String(task.id), '--name', 'cli-task-updated', '--command', 'echo OPENAPI_UPDATED', '--schedule', '0 1 * * *']);
|
||||||
cli(['task', 'disable', String(task.id)]); cli(['task', 'enable', String(task.id)]);
|
cli(['task', 'disable', String(task.id)]); cli(['task', 'enable', String(task.id)]);
|
||||||
|
|||||||
@@ -24,11 +24,11 @@ exports.verifyAccountMaintenance = async function verifyAccountMaintenance(
|
|||||||
assert.equal(result.status, 0, `${action} failed: ${result.stderr}`);
|
assert.equal(result.status, 0, `${action} failed: ${result.stderr}`);
|
||||||
assert.equal(JSON.parse(result.stdout).data.completed, true);
|
assert.equal(JSON.parse(result.stdout).data.completed, true);
|
||||||
};
|
};
|
||||||
const login = async () => {
|
const login = async (password = credentials.password) => {
|
||||||
const response = await fetch('http://127.0.0.1:5700/api/user/login', {
|
const response = await fetch('http://127.0.0.1:5700/api/user/login', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify(credentials),
|
body: JSON.stringify({ ...credentials, password }),
|
||||||
signal: AbortSignal.timeout(5000),
|
signal: AbortSignal.timeout(5000),
|
||||||
});
|
});
|
||||||
return response.json();
|
return response.json();
|
||||||
@@ -36,18 +36,23 @@ exports.verifyAccountMaintenance = async function verifyAccountMaintenance(
|
|||||||
local('resetname', credentials.username);
|
local('resetname', credentials.username);
|
||||||
local('resetpwd', credentials.password);
|
local('resetpwd', credentials.password);
|
||||||
assert.equal((await login()).code, 200, 'New credentials were not saved');
|
assert.equal((await login()).code, 200, 'New credentials were not saved');
|
||||||
await api('system/auth/reset', 'PUT', { retries: 4 });
|
const incorrect = randomUUID();
|
||||||
assert.notEqual(
|
for (let attempt = 0; attempt < 3; attempt++) {
|
||||||
|
assert.equal((await login(incorrect)).code, 400, 'Expected a rejected password before throttling');
|
||||||
|
}
|
||||||
|
assert.equal(
|
||||||
(await login()).code,
|
(await login()).code,
|
||||||
200,
|
410,
|
||||||
'Fixture failed to activate login limit',
|
'Fixture failed to activate login limit',
|
||||||
);
|
);
|
||||||
local('resetlet');
|
local('resetlet');
|
||||||
assert.equal((await login()).code, 200, 'Login limit was not cleared');
|
const session = await login();
|
||||||
await api('system/auth/reset', 'PUT', { twoFactorActivated: true });
|
assert.equal(session.code, 200, 'Login limit was not cleared');
|
||||||
assert.notEqual(
|
assert.ok(session.data.token, 'Fresh owner session required');
|
||||||
|
await api('system/auth/reset', 'PUT', { twoFactorActivated: true }, session.data.token);
|
||||||
|
assert.equal(
|
||||||
(await login()).code,
|
(await login()).code,
|
||||||
200,
|
420,
|
||||||
'Fixture failed to activate second factor',
|
'Fixture failed to activate second factor',
|
||||||
);
|
);
|
||||||
local('resettfa');
|
local('resettfa');
|
||||||
|
|||||||
@@ -2,7 +2,8 @@ const assert = require('node:assert/strict');
|
|||||||
const fs = require('node:fs/promises');
|
const fs = require('node:fs/promises');
|
||||||
const path = require('node:path');
|
const path = require('node:path');
|
||||||
const os = require('node:os');
|
const os = require('node:os');
|
||||||
const { spawnSync } = require('node:child_process');
|
const { spawn, spawnSync } = require('node:child_process');
|
||||||
|
const { once } = require('node:events');
|
||||||
|
|
||||||
exports.verifyOperator = async function verifyOperator(api) {
|
exports.verifyOperator = async function verifyOperator(api) {
|
||||||
const ql = path.join(os.homedir(), 'bin/ql');
|
const ql = path.join(os.homedir(), 'bin/ql');
|
||||||
@@ -26,12 +27,18 @@ exports.verifyOperator = async function verifyOperator(api) {
|
|||||||
schedule: '0 0 1 1 *',
|
schedule: '0 0 1 1 *',
|
||||||
})
|
})
|
||||||
).data;
|
).data;
|
||||||
|
const child = spawn('sleep', ['60']);
|
||||||
|
const ended = once(child, 'exit');
|
||||||
|
const start = Math.floor(Date.now() / 1000);
|
||||||
try {
|
try {
|
||||||
await api('crons/status', 'PUT', {
|
await api('crons/status', 'PUT', {
|
||||||
ids: [task.id],
|
ids: [task.id],
|
||||||
status: '0',
|
status: '0',
|
||||||
|
pid: String(child.pid),
|
||||||
|
last_execution_time: start,
|
||||||
log_path: 'cli-retention/2000-01-01-kept.log',
|
log_path: 'cli-retention/2000-01-01-kept.log',
|
||||||
});
|
});
|
||||||
|
assert.equal((await api(`crons/${task.id}`)).data.log_path, 'cli-retention/2000-01-01-kept.log');
|
||||||
const result = invoke(['rmlog', '7']);
|
const result = invoke(['rmlog', '7']);
|
||||||
assert.ok(result.removed.includes('cli-retention/2000-01-01-unused.log'));
|
assert.ok(result.removed.includes('cli-retention/2000-01-01-unused.log'));
|
||||||
assert.ok(result.retained.includes('cli-retention/2000-01-01-kept.log'));
|
assert.ok(result.retained.includes('cli-retention/2000-01-01-kept.log'));
|
||||||
@@ -41,6 +48,8 @@ exports.verifyOperator = async function verifyOperator(api) {
|
|||||||
'referenced',
|
'referenced',
|
||||||
);
|
);
|
||||||
} finally {
|
} finally {
|
||||||
|
child.kill();
|
||||||
|
await ended;
|
||||||
await api('crons', 'DELETE', [task.id]);
|
await api('crons', 'DELETE', [task.id]);
|
||||||
}
|
}
|
||||||
const service = invoke(['reload']);
|
const service = invoke(['reload']);
|
||||||
|
|||||||
@@ -16,12 +16,12 @@ let restoreEntrypoints;
|
|||||||
);
|
);
|
||||||
const base = 'http://127.0.0.1:5700';
|
const base = 'http://127.0.0.1:5700';
|
||||||
let token;
|
let token;
|
||||||
async function api(endpoint, method = 'GET', body) {
|
async function api(endpoint, method = 'GET', body, sessionToken = token) {
|
||||||
const response = await fetch(`${base}/api/${endpoint}`, {
|
const response = await fetch(`${base}/api/${endpoint}`, {
|
||||||
method,
|
method,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
...(sessionToken ? { Authorization: `Bearer ${sessionToken}` } : {}),
|
||||||
},
|
},
|
||||||
body: body === undefined ? undefined : JSON.stringify(body),
|
body: body === undefined ? undefined : JSON.stringify(body),
|
||||||
signal: AbortSignal.timeout(10000),
|
signal: AbortSignal.timeout(10000),
|
||||||
|
|||||||
@@ -81,6 +81,13 @@ async function fixture(t) {
|
|||||||
code: 200,
|
code: 200,
|
||||||
data: { data: [{ id: 12, name: 'example' }], total: 1 },
|
data: { data: [{ id: 12, name: 'example' }], total: 1 },
|
||||||
});
|
});
|
||||||
|
else if (
|
||||||
|
['/panel/open/envs', '/panel/open/configs/files', '/panel/open/scripts',
|
||||||
|
'/panel/open/subscriptions', '/panel/open/dependencies',
|
||||||
|
'/panel/open/logs', '/panel/open/dashboard/overview']
|
||||||
|
.includes(url.pathname)
|
||||||
|
)
|
||||||
|
json({ code: 200, data: [] });
|
||||||
else if (
|
else if (
|
||||||
['/panel/open/crons/run', '/panel/open/crons/stop'].includes(
|
['/panel/open/crons/run', '/panel/open/crons/stop'].includes(
|
||||||
url.pathname,
|
url.pathname,
|
||||||
@@ -155,6 +162,21 @@ test('compiled entry: login aliases, private storage, status, refresh and local
|
|||||||
assert.equal(JSON.parse(status.out).data.scopeChecked, 'crons');
|
assert.equal(JSON.parse(status.out).data.scopeChecked, 'crons');
|
||||||
assert.doesNotMatch(status.out, /test-secret|test-token/);
|
assert.doesNotMatch(status.out, /test-secret|test-token/);
|
||||||
assert.equal(f.requests.at(-1).url.pathname, '/panel/open/crons');
|
assert.equal(f.requests.at(-1).url.pathname, '/panel/open/crons');
|
||||||
|
for (const [scope, pathname] of [
|
||||||
|
['envs:read', '/panel/open/envs'],
|
||||||
|
['configs:read', '/panel/open/configs/files'],
|
||||||
|
['scripts:read', '/panel/open/scripts'],
|
||||||
|
['crons:read', '/panel/open/crons'],
|
||||||
|
['subscriptions:read', '/panel/open/subscriptions'],
|
||||||
|
['dependencies:read', '/panel/open/dependencies'],
|
||||||
|
['logs:read', '/panel/open/logs'],
|
||||||
|
['dashboard:read', '/panel/open/dashboard/overview'],
|
||||||
|
]) {
|
||||||
|
const checked = await f.invoke(['auth', 'status', '--scope', scope, '--json']);
|
||||||
|
assert.equal(checked.code, 0, checked.err);
|
||||||
|
assert.equal(JSON.parse(checked.out).data.scopeChecked, scope);
|
||||||
|
assert.equal(f.requests.at(-1).url.pathname, pathname);
|
||||||
|
}
|
||||||
const config = JSON.parse(fs.readFileSync(f.file));
|
const config = JSON.parse(fs.readFileSync(f.file));
|
||||||
config.expiration = 1;
|
config.expiration = 1;
|
||||||
fs.writeFileSync(f.file, JSON.stringify(config));
|
fs.writeFileSync(f.file, JSON.stringify(config));
|
||||||
|
|||||||
@@ -83,6 +83,27 @@ test('script history commands send exact query parameters and restore payloads',
|
|||||||
assert.equal(f.requests.length, count, 'missing restore payload must not issue a request');
|
assert.equal(f.requests.length, count, 'missing restore payload must not issue a request');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('dependency refresh commands send the dependency type and address background jobs', async t => {
|
||||||
|
const f = await fixture(t);
|
||||||
|
success(await f.run(['dependency', 'refresh', '--data', '{"type":"python3"}']));
|
||||||
|
assert.equal(f.requests[0].method, 'POST');
|
||||||
|
assert.equal(f.requests[0].url, '/panel/open/dependencies/refresh');
|
||||||
|
assert.deepEqual(JSON.parse(f.requests[0].body), { type: 'python3' });
|
||||||
|
const id = '32ec8b22-2c48-4bcf-85d4-b394198417ba';
|
||||||
|
success(await f.run(['dependency', 'refresh-status', id]));
|
||||||
|
assert.equal(f.requests[1].method, 'GET');
|
||||||
|
assert.equal(f.requests[1].url, `/panel/open/dependencies/refresh/${id}`);
|
||||||
|
success(await f.run(['dependency', 'refresh-active']));
|
||||||
|
assert.equal(f.requests[2].method, 'GET');
|
||||||
|
assert.equal(f.requests[2].url, '/panel/open/dependencies/refresh/active');
|
||||||
|
success(await f.run(['api', 'request', 'GET', `/open/dependencies/refresh/${id}`]));
|
||||||
|
assert.equal(f.requests[3].url, `/panel/open/dependencies/refresh/${id}`);
|
||||||
|
assert.equal((await f.run(['dependency', 'refresh-status', '7'])).code, 2);
|
||||||
|
assert.equal((await f.run(['api', 'request', 'GET', '/open/dependencies/refresh/7'])).code, 2);
|
||||||
|
assert.equal((await f.run(['dependency', 'refresh'])).code, 2);
|
||||||
|
assert.equal(f.requests.length, 4, 'invalid arguments must not issue a request');
|
||||||
|
});
|
||||||
|
|
||||||
test('OpenAPI catalogue covers every active registered backend route; retired 410 routes are explicit', async () => {
|
test('OpenAPI catalogue covers every active registered backend route; retired 410 routes are explicit', async () => {
|
||||||
const root = path.resolve(__dirname, '../../../back/api');
|
const root = path.resolve(__dirname, '../../../back/api');
|
||||||
const expected = [];
|
const expected = [];
|
||||||
@@ -132,7 +153,8 @@ test('all added named operations reach their registered method/path, including u
|
|||||||
const file = path.join(f.root, 'fixture.json'); await fs.writeFile(file, '{}');
|
const file = path.join(f.root, 'fixture.json'); await fs.writeFile(file, '{}');
|
||||||
for (const op of openOperations.filter(op => !op.existing)) {
|
for (const op of openOperations.filter(op => !op.existing)) {
|
||||||
const args = op.name.split(' ');
|
const args = op.name.split(' ');
|
||||||
for (const _ of op.params ?? []) args.push('7');
|
const parameter = name => op.uuidParams?.includes(name) ? '32ec8b22-2c48-4bcf-85d4-b394198417ba' : '7';
|
||||||
|
for (const name of op.params ?? []) args.push(parameter(name));
|
||||||
if (op.body === 'ids') args.push('7', '8');
|
if (op.body === 'ids') args.push('7', '8');
|
||||||
else if (op.body) {
|
else if (op.body) {
|
||||||
const data = op.name.startsWith('task ') ? { command: 'echo fixture', schedule: '0 0 * * *' }
|
const data = op.name.startsWith('task ') ? { command: 'echo fixture', schedule: '0 0 * * *' }
|
||||||
@@ -144,7 +166,7 @@ test('all added named operations reach their registered method/path, including u
|
|||||||
success(await f.run(args));
|
success(await f.run(args));
|
||||||
const last = f.requests.at(-1);
|
const last = f.requests.at(-1);
|
||||||
assert.equal(last.method, op.method, op.name);
|
assert.equal(last.method, op.method, op.name);
|
||||||
assert.equal(last.url, '/panel/open/' + op.path.replace(/:[A-Za-z]+/g, '7'), op.name);
|
assert.equal(last.url, '/panel/open/' + op.path.replace(/:([A-Za-z]+)/g, (_match, name) => parameter(name)), op.name);
|
||||||
assert.equal(last.headers.authorization, op.anonymous ? undefined : 'Bearer fixture-token', op.name);
|
assert.equal(last.headers.authorization, op.anonymous ? undefined : 'Bearer fixture-token', op.name);
|
||||||
if (op.upload) assert.ok(last.body.includes(`name="${op.upload}"; filename="fixture.json"`), op.name);
|
if (op.upload) assert.ok(last.body.includes(`name="${op.upload}"; filename="fixture.json"`), op.name);
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -118,9 +118,9 @@ ENV PNPM_HOME=${QL_DIR}/data/dep_cache/node \
|
|||||||
ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PNPM_HOME}:${PYTHON_HOME}/bin:${HOME}/bin \
|
ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PNPM_HOME}:${PYTHON_HOME}/bin:${HOME}/bin \
|
||||||
NODE_PATH=/usr/local/bin:/usr/local/lib/node_modules \
|
NODE_PATH=/usr/local/bin:/usr/local/lib/node_modules \
|
||||||
PIP_CACHE_DIR=${PYTHON_HOME}/pip \
|
PIP_CACHE_DIR=${PYTHON_HOME}/pip \
|
||||||
PYTHONPATH=${PYTHON_HOME}:${PYTHON_HOME}/lib/python${PYTHON_SHORT_VERSION}:${PYTHON_HOME}/lib/python${PYTHON_SHORT_VERSION}/site-packages
|
PYTHONPATH=${PYTHON_HOME}:${PYTHON_HOME}/lib/python${PYTHON_SHORT_VERSION}:${PYTHON_HOME}/lib/python${PYTHON_SHORT_VERSION}/site-packages:${QL_DIR}/python-base/lib/python${PYTHON_SHORT_VERSION}/site-packages
|
||||||
|
|
||||||
RUN pip3 install --prefix ${PYTHON_HOME} requests
|
RUN pip3 install --ignore-installed --prefix ${QL_DIR}/python-base requests
|
||||||
|
|
||||||
COPY --from=builder /tmp/build/node_modules/. /ql/node_modules/
|
COPY --from=builder /tmp/build/node_modules/. /ql/node_modules/
|
||||||
|
|
||||||
|
|||||||
@@ -112,9 +112,9 @@ ENV PNPM_HOME=${QL_DIR}/data/dep_cache/node \
|
|||||||
ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PNPM_HOME}:${PYTHON_HOME}/bin:${HOME}/bin \
|
ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PNPM_HOME}:${PYTHON_HOME}/bin:${HOME}/bin \
|
||||||
NODE_PATH=/usr/local/bin:/usr/local/lib/node_modules \
|
NODE_PATH=/usr/local/bin:/usr/local/lib/node_modules \
|
||||||
PIP_CACHE_DIR=${PYTHON_HOME}/pip \
|
PIP_CACHE_DIR=${PYTHON_HOME}/pip \
|
||||||
PYTHONPATH=${PYTHON_HOME}:${PYTHON_HOME}/lib/python${PYTHON_SHORT_VERSION}:${PYTHON_HOME}/lib/python${PYTHON_SHORT_VERSION}/site-packages
|
PYTHONPATH=${PYTHON_HOME}:${PYTHON_HOME}/lib/python${PYTHON_SHORT_VERSION}:${PYTHON_HOME}/lib/python${PYTHON_SHORT_VERSION}/site-packages:${QL_DIR}/python-base/lib/python${PYTHON_SHORT_VERSION}/site-packages
|
||||||
|
|
||||||
RUN pip3 install --prefix ${PYTHON_HOME} requests
|
RUN pip3 install --ignore-installed --prefix ${QL_DIR}/python-base requests
|
||||||
|
|
||||||
COPY --chown=qinglong:qinglong --from=builder /tmp/build/node_modules/. /ql/node_modules/
|
COPY --chown=qinglong:qinglong --from=builder /tmp/build/node_modules/. /ql/node_modules/
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ Node 调度路径使用固定版本 `node-cron@4.6.0`,普通任务(含额外
|
|||||||
- 保留旧版 1–6 字段补全、五种宏、月份/星期名称、数字起点步长、L 和 #。另外明确新增 `@annually`(等同 `@yearly`)、`@midnight`(等同 `@daily`)、`@minutely`(每分钟第 0 秒)。宏按进程时区解释。`?` 仅允许日期和星期字段;裸 `/N`、H 和其他未列出的宏仍拒绝。
|
- 保留旧版 1–6 字段补全、五种宏、月份/星期名称、数字起点步长、L 和 #。另外明确新增 `@annually`(等同 `@yearly`)、`@midnight`(等同 `@daily`)、`@minutely`(每分钟第 0 秒)。宏按进程时区解释。`?` 仅允许日期和星期字段;裸 `/N`、H 和其他未列出的宏仍拒绝。
|
||||||
- 日期与星期同时指定时保留 OR 语义,不采用 node-cron 默认的 AND。两个原生任务分别处理日期和星期,并按计划时间过滤交集,避免重复执行;保留旧解析器的月份全日期范围及全局 nth-week 限制。
|
- 日期与星期同时指定时保留 OR 语义,不采用 node-cron 默认的 AND。两个原生任务分别处理日期和星期,并按计划时间过滤交集,避免重复执行;保留旧解析器的月份全日期范围及全局 nth-week 限制。
|
||||||
- 原生任务先暂停创建并验证,再替换旧任务。构造失败时销毁已准备的实例并保留旧快照。
|
- 原生任务先暂停创建并验证,再替换旧任务。构造失败时销毁已准备的实例并保留旧快照。
|
||||||
- 进程内迟到的触发通过 `execution:missed` 加入既有执行队列,记录补执行日志。并发和单任务积压上限仍由青龙队列控制;这不提供进程退出期间的持久化补跑或分布式 exactly-once 保证。
|
- 进程内迟到的触发通过 `execution:missed` 加入既有执行队列,记录补执行日志。并发和单任务积压上限仍由青龙队列控制;进程退出期间的可选补跑和触发记录见[定时任务执行第一版](./scheduler-reliability-v1.md);不提供分布式 exactly-once 保证。
|
||||||
- 保留 node-cron 已支持的列表、范围和名称,不调用 cron-parser 的 `stringify()` 重写整条表达式。先校验整条表达式,通过则跳过逐字段探测;失败时才检查并展开不支持的字段(如数字/名称起点步长)。保留原有 `?` 和 `#` 兼容处理,避免有效的 `6,12,18` 被压缩为不兼容的 `6/6`;日期/星期拆分后的最终校验仍然保留。
|
- 保留 node-cron 已支持的列表、范围和名称,不调用 cron-parser 的 `stringify()` 重写整条表达式。先校验整条表达式,通过则跳过逐字段探测;失败时才检查并展开不支持的字段(如数字/名称起点步长)。保留原有 `?` 和 `#` 兼容处理,避免有效的 `6,12,18` 被压缩为不兼容的 `6/6`;日期/星期拆分后的最终校验仍然保留。
|
||||||
- 星期步长范围以 `7` 结尾时,先按 cron-parser 的解析值展开。例如 `2-7/4` 在现有解析器中包含星期日,而 node-cron 原样执行会遗漏星期日;仅通过语法校验不足以保证执行含义一致。
|
- 星期步长范围以 `7` 结尾时,先按 cron-parser 的解析值展开。例如 `2-7/4` 在现有解析器中包含星期日,而 node-cron 原样执行会遗漏星期日;仅通过语法校验不足以保证执行含义一致。
|
||||||
- 单月规则先剔除超出该月最大日期的值再计算日期数量,保留旧版日期/星期的通配计数语义。被判为通配的字段不再作为另一侧的 AND 约束,避免 `2-31 4 MON` 和包含 `L`、重复星期日的边界规则漏执行。
|
- 单月规则先剔除超出该月最大日期的值再计算日期数量,保留旧版日期/星期的通配计数语义。被判为通配的字段不再作为另一侧的 AND 约束,避免 `2-31 4 MON` 和包含 `L`、重复星期日的边界规则漏执行。
|
||||||
|
|||||||
@@ -0,0 +1,576 @@
|
|||||||
|
# 定时任务执行:第一版优化方案与验收
|
||||||
|
|
||||||
|
目标:用户设定一个时间后,能知道规则是否已生效、这一轮何时被调度、为什么等待或跳过,以及最后一次执行的结果。短等待是核心体验;同时要保住该跑的任务,并看得见那些没启动的任务。只比较已启动任务的平均等待,会让“丢掉慢任务”的实现看起来更快。
|
||||||
|
|
||||||
|
## 两个项目给青龙的启发
|
||||||
|
|
||||||
|
| 参考实现 | 有价值的设计 | 青龙第一版的取舍 |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| [白虎执行器](https://github.com/engigu/baihu-panel/blob/67ec1974d5f445a244f27b77bd50b119127b7724/internal/executor/scheduler.go) | worker 数和队列容量各自有界,严格队列有明确的拒绝行为;生命周期钩子区分准备、执行、结束 | 保留青龙现有队列和并发限制,把每次入队、积压拒绝和超时变成用户能查到的记录。满载时不额外启动进程。 |
|
||||||
|
| [呆呆调度器](https://github.com/linzixuanzz/daidai-panel/blob/3abb06a7373eeb38fb0cd5d550be77df6b021e90/server/service/scheduler_v2.go) 与[退出处理](https://github.com/linzixuanzz/daidai-panel/blob/3abb06a7373eeb38fb0cd5d550be77df6b021e90/server/service/scheduler_manager.go) | 延迟请求不占执行槽,关停时处理未结束的进程和记录 | 青龙区分排队与已开始:只有没有启动证据的最近一轮可自动补跑;可能已经产生副作用的执行保留为“结果未知”。 |
|
||||||
|
|
||||||
|
青龙已有脚本环境、系统 crond、秒级 Node 定时、手动运行和订阅生态。第一版沿用它们,先解决同一配置在不同入口解释不同、执行过程查不到、无法从运行记录核实统计次数的问题。
|
||||||
|
|
||||||
|
## 已落地的实现顺序
|
||||||
|
|
||||||
|
1. **规则发布。** 系统 crontab 写入或安装失败时,API 返回失败,保留待同步标记,触发调度器恢复。失败期间旧系统规则仍可能执行。系统规则转交 Node 时先移除旧规则,确认后才注册新规则;其他任务保存成功不能替它确认注册。
|
||||||
|
2. **统一时间。** 表单预览、列表、详情从服务端获取时间。常规和附加规则共享 Node 注册使用的日历与兼容过滤,列表取所有规则中最早的下一次。停用、待同步、手动和开机任务没有下次定时。保留系统 crond 可执行的五字段子集;扩展表达式和启用管理策略的规则走 Node。
|
||||||
|
3. **能查看每次触发并核实执行结果。** 每次可观测触发保存来源、可取得的计划、入队、派发、脚本入口和结束时间、状态、原因及可用的日志路径和退出码。任务详情可分页追踪。Shell 报告以触发 ID 关联;日统计通过 SQLite 原子累加,相同触发 ID 的重复上报只计一次。旧客户端由能定位实例的退出回执结算。升级时按已有退出记录与日汇总补足可证明的缺口,匿名统计重试仅作确认。已有日汇总保留在原入账日。同一任务和结果先抵扣当天可能对应的退出,多余的匿名次数按从近到远的顺序抵扣较早日期,每次最多使用一次。缺少耗时样本的补计只参与次数和成功率。
|
||||||
|
4. **减少没有价值的等待。** 任务可分别开启“上一轮仍在排队或运行时跳过”和最长排队时间。前者抑制同一任务的重复定时触发,不让它们排到其他任务前面;后者限制一次排队的有效期。两者是跳过策略,不能缩短脚本的实际运行时间。默认维持已有 FIFO、并发和单任务积压上限。手动及开机运行沿用手动队列,不会因定时跳过策略被丢弃。
|
||||||
|
5. **有限补跑。** 默认关闭;配置 1–86400 秒后,在全量恢复时只检查每条规则在窗口内的最近一次,且不得早于最近一次创建、编辑或启用。任务 ID、规则序号、表达式和计划时刻组成稳定 ID,正常触发和补跑争用同一条记录。已经启动、执行完毕、结果未知或因策略跳过的轮次不自动重放。用户主动再次运行是一次新触发。
|
||||||
|
|
||||||
|
## 如何判断等待是不是变短了
|
||||||
|
|
||||||
|
| 指标 | 口径 | 应如何解释 |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| 排队等待 P95 | 当天观察到、且既有入队又有启动记录的触发,取“启动 − 入队”的第 95 百分位;毫秒,无样本显示 — | 衡量已获执行机会的任务排了多久。启动取调度器开始派发进程的时间,不代表脚本已完成初始化。 |
|
||||||
|
| 准时触发率 | 调度器按时收到的次数除以按规则推算的应触发次数 | 漏触发和迟到降低准时率。按时收到后被策略跳过,会同时记入按时触发和触发后跳过。 |
|
||||||
|
| 计划到脚本入口 P95、五秒内到达入口 | 对有计划时刻与脚本入口的触发,计算毫秒延迟分布和五秒内抵达的比例 | 同时显示入口样本数。手动和系统 crond 任务的可观测耗时见任务详情。 |
|
||||||
|
| 观察数、跳过数及原因 | 当天的触发记录;区分重叠、重复、积压上限、等待过期、配置变更和重启 | 与 P95 一起看,防止以跳过变多换取漂亮的等待数字。某任务的当前等待以及终态可在详情中查看。 |
|
||||||
|
|
||||||
|
例如 08:00:00 计划、08:00:01 入队、08:00:11 启动:调度唤醒延迟 1 秒,排队 10 秒,计划到启动 11 秒。此时优先看占用执行槽的任务;提高并发可能让 CPU、内存或下游接口更拥挤,无法凭这个样本判断应当扩容。
|
||||||
|
|
||||||
|
本地做了一个可解释的对照:并发槽为 1,暂停队列后依次提交某任务 5 次(每次执行真实的 `/bin/sleep 0.1`),再提交一个独立任务并放开队列。三轮中,独立任务在默认 FIFO 下分别等待 557、575、565 ms;开启重复跳过后分别等待 117、112、110 ms。每轮都少执行了 4 次重复任务。这说明队列被可丢弃的重复工作占满时,跳过策略如何让另一任务早点启动;如果 5 次都必须执行,就应保留默认策略。该实验用单机内存 SQLite 记录时间,不代表生产 P95。
|
||||||
|
|
||||||
|
## 验收与发布边界
|
||||||
|
|
||||||
|
- 对真正的 SQLite 文件并发发送当天首次与后续完成报告,核对成功、失败、总次数、总耗时、最大耗时及重复报告去重。
|
||||||
|
- 注入系统安装失败,观察创建、修改、停用、跨调度器转交,再恢复快照。使用假 crontab 安装器,不改开发机的真实 crontab。
|
||||||
|
- 用同一组日历边界在 UTC 与上海时区验证宏、主/附加规则、月末、闰年及日期/星期语义。升级 node-cron 时必须重跑:指定历史游标的预览依赖当前锁定版本的日期匹配器。
|
||||||
|
- 堵住真实单槽队列,验证重叠、积压、排队过期、同任务合并以及另一任务仍能执行;被拒绝的任务没有启动脚本。用 protobuf 恢复请求和真实子进程验证重复恢复后只写一次文件。
|
||||||
|
- 仪表盘的“今日系统定时任务(crond)”单独展示系统计划和实际触发记录。已确认安装的五字段规则按生效区间和面板时区枚举计划分钟,用系统执行命令行采集的派发分钟、任务 ID、规则匹配回执。包装进程延迟或跨日入库仍可核对这一分钟。仅有旧包装进程时间的回执保留为分钟归属待核实。安装未确认、时区不一致和夏令时切换日保留为待核实。旧格式回执有脚本入口或已核实退出证据时可计入实际启动。每个计划分钟只匹配一次,多余触发和未匹配回执分别展示。
|
||||||
|
- 手动池和定时池分别记录入队时的并发上限、占用数和最早占槽的八个执行实例。仪表盘展示等待最久的 25 条,等待期间随并发上限与实际占槽实例更新最近采样。取得执行槽即固定等待时长,命令准备时间单独留在后续时间线上,结束后可回看入队快照。
|
||||||
|
- 数据按已有运行实例保留天数清理,只清理已结束记录;清理为零天时不删。触发记录的去重受保留期约束。网络请求、转账等外部副作用不能靠面板保证只发生一次,需要脚本与下游自行做幂等。
|
||||||
|
- 部署到不同规格的机器后,可用同一组脚本复测等待 P95、准时率和跳过原因,决定是否调整并发或任务策略。
|
||||||
|
|
||||||
|
## 2026-11-13 Linux 容器验收
|
||||||
|
|
||||||
|
在 Docker Desktop 的 Linux ARM64 容器内启动完整青龙面板,使用 Alpine 3.24.4、BusyBox crond、Node 24.22.1 和独立的 SQLite 数据库。容器限 2 核、1 GiB,调度并发设为 2,时区为上海。运行的是本次工作区重新编译的后端和 Shell,基础镜像的依赖锁文件与仓库一致。
|
||||||
|
|
||||||
|
**真实 crond 与重启。** 通过面板 API 安装每分钟执行一次的脚本,核对系统表及脚本实际写入。将这一规则改成秒级时,故意让 BusyBox crontab 使用不存在的 spool 目录,安装实际失败。API 返回 503,任务标记为待同步,旧表仍保留原记录。撤掉故障后,后台自行恢复,系统活动条目数变为零,Node 开始执行。切回五字段规则并重启容器后,crond 在下一分钟执行一次。再次切到 Node 并重启,跨过下一个整分边界观察到 18 个不同计划时刻,crond 活动条目始终为零。整个实验里脚本实际留下 22 条不同的记录,正好对应 2 次系统执行和 20 次 Node 执行。
|
||||||
|
|
||||||
|
**负载与计数口径。** 四个脚本均通过面板 API 注册,由真实定时器启动。Shell 轮询等待 3 秒以模拟外部 I/O,Python 运行 12 万轮 PBKDF2,Node.js 序列化、解析并筛选 2 万条 JSON 数据,另一个 Shell 脚本写入并读取 1 MiB 临时文件。错峰组每个脚本运行 6 次。同秒拥塞组在 30 秒内每秒触发轮询,其余三项每 5 秒触发一次;对照组仅为轮询开启“上一轮仍在排队或运行时跳过”。两组均使用 2 个执行槽,并允许任务多实例运行。P95 对有启动记录的样本取最近秩;准时率用计划后 5 秒内启动数除以全部预定时刻,包括跳过的轮次。每组都按配置枚举计划时刻,与落库记录逐个核对。
|
||||||
|
|
||||||
|
| 场景 | 计划时刻 | 成功完成 | 跳过 | 排队 P95 | 计划后 5 秒内启动 |
|
||||||
|
| --- | ---: | ---: | ---: | ---: | ---: |
|
||||||
|
| 错峰运行,默认策略 | 24 | 24 | 0 | 130 ms | 24/24(100%) |
|
||||||
|
| 同秒拥塞,默认策略 | 48 | 35 | 13(积压上限) | 9,512 ms | 5/48(10.4%) |
|
||||||
|
| 同秒拥塞,仅轮询开启重复跳过 | 48 | 26 | 22(已有轮询排队或运行) | 787 ms | 26/48(54.2%) |
|
||||||
|
|
||||||
|
拥塞两组中,轮询之外的 18 次任务都成功完成,其排队 P95 从 8,063 ms 降至 1,559 ms,五秒内启动从 3/18 升至 18/18。轮询本身成功执行的次数从 17/30 降至 8/30,是否启用此策略取决于业务能否丢弃中间轮次。
|
||||||
|
|
||||||
|
三组负载的 120 个计划时刻均逐项对上触发 ID 和计划时间。计入切换与重启实验后,13 个任务共 142 条记录,其中 107 次成功、35 次跳过;每个任务的每日统计与其完成数一致。当天仪表盘与独立读取 SQLite 的计算一致:105 个可测等待样本,排队 P95 为 7,688 ms,有计划时间的 140 次触发中 75 次在五秒内启动。两次系统 crond 触发按设计没有计划及入队时间。以上是该容器单轮各场景的实测值。
|
||||||
|
|
||||||
|
## 复审意见与下一轮优先级(2026-11-13)
|
||||||
|
|
||||||
|
此前的 Linux 验收证明了正常发布、跨调度器切换、成功回报及指定负载下的等待表现。复审在另一份干净的 Alpine 容器中测试了并发争抢、完成回报故障、在线全量恢复。以下 P1 表示建议列为正式发布前的修复项;P2 表示有明确影响的后续改进。下表保留修复前的复现步骤和验收条件。修复结果见文末。
|
||||||
|
|
||||||
|
| 优先级 | 实际触发条件和结果 | 建议改动与验收条件 |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| P1 单实例可重复执行 | 关闭多实例,执行并发设为 4,让主规则和附加规则同秒到期。两个不同 PID 在同一秒写入开始标记,3 秒后都正常退出。两条触发各有合法 ID,但当前检查任务状态与登记本轮启动之间可以交错。 | 在派发进程前,按任务原子取得单实例执行权,以本轮标识释放。两条规则都要留记录,脚本只执行一次,另一轮标为重叠。覆盖手动与定时交错、已停止或失联的旧实例,并单独确定系统 crond 的执行约束。同步修订表单中“自动停止旧任务”的文案,当前 Node 路径意图是跳过新一轮。 |
|
||||||
|
| P1 回报中断时失败会记成成功 | 真实脚本写入标记并退出 17,故障仅阻断 Shell 到状态及日统计两个上报接口。Node 最终记录了成功、退出码 0,当天该任务没有统计行。外层 task 的正常收尾固定返回 0。 | 托管执行透传脚本结果,覆盖直接输出、日志重定向及 tee。让调度父进程与 Shell 使用同一触发 ID 幂等结算在本地可确认的终态和统计。验收时分别断开两个接口、同时断开、恢复后重复回报,均只记一次失败,退出码为 17。系统 crond 缺少父进程兜底,须另行验证可重放回执或明确标记未确认。 |
|
||||||
|
| P1 恢复连带跳过无关任务 | 并发为 1,任务 A 占槽,任务 B 已入队。另一条系统规则安装失败后自动全量恢复,B 的定义和启用状态没变,却被标为 `skipped/changed`,实际脚本未写入。恢复时替换全部定时器,使原队列里检查注册对象身份的回调失效。 | 全量恢复按有效定义对比,保留未变任务的注册及已入队请求,只撤销确实变更、删除或停用的任务。以 A、B 和无关规则分别验证在线恢复、编辑 B、停用 B 及重复恢复,B 的副作用次数应依次为 1、旧定义 0、0、最多 1。 |
|
||||||
|
| P2 超时记录继续阻止新触发 | 同时开启最长等待 2 秒和重复跳过。第一轮已记录 `queue_timeout`,第二轮仍立即得到 `pending`。当时执行槽约 1.2 秒后释放,早于第二轮自己的截止时间。 | 超时后释放该请求的等待资格,并防止它随后被执行。验证第二轮可以等到槽位,已超时的第一轮不会挤占重复触发名额。 |
|
||||||
|
|
||||||
|
创建系统任务时还验证了“先返回 503、自动恢复后再原样提交”。再次提交返回 409,数据库和系统表中各有一条。这里的产品问题是调用方很难从报错定位已经创建的任务。建议在待同步响应中提供任务 ID 和可查询的应用状态,保留安装失败的反馈。
|
||||||
|
|
||||||
|
### 两个参考项目可借鉴的具体行为
|
||||||
|
|
||||||
|
对比依据为白虎 `67ec197` 与呆呆 `3abb06a` 的源码。只比较实现行为,本次没有对两者跑同机性能基准。
|
||||||
|
|
||||||
|
| 能力 | 源码中能确认的行为 | 对青龙的取舍 |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| 执行互斥与队列容量 | 呆呆在同一把锁内检查并登记单实例,队列满明确返回错误。白虎提供严格队列选项,非严格模式队满可直接并发执行。[呆呆执行权](https://github.com/linzixuanzz/daidai-panel/blob/3abb06a7373eeb38fb0cd5d550be77df6b021e90/server/service/scheduler_v2.go#L417-L431) · [呆呆入队](https://github.com/linzixuanzz/daidai-panel/blob/3abb06a7373eeb38fb0cd5d550be77df6b021e90/server/service/scheduler_v2.go#L450-L461) · [白虎队满处理](https://github.com/engigu/baihu-panel/blob/67ec1974d5f445a244f27b77bd50b119127b7724/internal/executor/scheduler.go#L315-L334) | 先修单实例竞争。现有每任务最多 5 个请求限制不了大量不同任务同时到期。应以多任务突发测试决定总等待上限,明确保留、拒绝、恢复策略。定时并发设为 1 时,本次实测定时与手动各自启动一个进程且重叠 2 秒。若要提供整机上限,需跨队列协调并说明系统 crond 的覆盖范围。 |
|
||||||
|
| 等待与执行槽 | 呆呆把执行前的随机等待交回延时队列,worker 可处理其他任务。[延时入队](https://github.com/linzixuanzz/daidai-panel/blob/3abb06a7373eeb38fb0cd5d550be77df6b021e90/server/service/scheduler_v2.go#L345-L363) | 青龙的随机等待发生在 Shell 进程内,此时已占槽。设置随机延迟 1 秒后,实测排队 25 ms,计划到派发 33 ms,派发到 JavaScript 第一行 1,798 ms,日志确认其中包含 1 秒配置延迟。建议把主动延迟、资源排队、包装进程启动与脚本开始区分开。迁移随机等待前验证扩展名、忽略分钟、手动立即执行及配置热更新。 |
|
||||||
|
| 失败后的动作 | 白虎为任务设置重试次数、间隔并延迟重新提交;呆呆任务模型还支持超时和成功退出码。[白虎重试](https://github.com/engigu/baihu-panel/blob/67ec1974d5f445a244f27b77bd50b119127b7724/internal/services/tasks/executor_service.go#L432-L456) · [呆呆任务定义](https://github.com/linzixuanzz/daidai-panel/blob/3abb06a7373eeb38fb0cd5d550be77df6b021e90/server/model/task.go#L46-L57) | 青龙已有命令级超时及重启补跑。先解决失败结果结算,再考虑显式开启的失败重试。一次计划触发可有多次尝试,需要区分计划数和尝试数。只重试明确允许的失败类型,停止与结果未知不自动重放。 |
|
||||||
|
| 运行诊断与存储 | 白虎执行生命周期提供心跳;呆呆为单个任务提供覆盖全局日志保留期的配置。[白虎执行钩子](https://github.com/engigu/baihu-panel/blob/67ec1974d5f445a244f27b77bd50b119127b7724/internal/services/tasks/executor_service.go#L469-L479) · [呆呆日志保留](https://github.com/linzixuanzz/daidai-panel/blob/3abb06a7373eeb38fb0cd5d550be77df6b021e90/server/model/task.go#L49-L56) | 先补失败回报对账、队列中最久等待及按入口展示样本数,再按高频任务量评估分层保留与统计聚合。心跳用于区分失联和仍在运行,不能仅据心跳超时重放有副作用的脚本。 |
|
||||||
|
|
||||||
|
### 对验收标准的修订
|
||||||
|
|
||||||
|
- **区分业务义务。** 全部 48 个计划时刻依然要对账。对于保证执行的 18 个非轮询任务,另报成功 18/18、准时 3/18 或 18/18。对于允许跳过的轮询,另报执行次数、最长未成功间隔和跳过原因。避免把少执行轮询所得的 P95 改善解释成整个平台吞吐提升。
|
||||||
|
- **下一轮拥塞实验保留全部工作量。** 在同一资源配额、相同脚本和相同到期顺序下,对照 FIFO 与按任务轮转,每组交替重复,报告成功数、完成全部工作的总耗时、等待 P95、最老等待以及长任务等待。再单独测开启重复跳过的负载。公平排队、优先级或扩容是否值得做,取决于这组结果。
|
||||||
|
- **明确可见范围。** 分别展示从计划到派发、从入队到派发、从派发到脚本开始;给每个指标附样本数。系统 crond 的不可测时间保持空值。基于已观察触发的比例无法揭示未留下记录的漏触发,故障验收继续用独立枚举的计划时刻、脚本副作用和日统计三方核对。
|
||||||
|
|
||||||
|
## 复审修复与实测(2026-11-13)
|
||||||
|
|
||||||
|
| 场景 | 实现与验收结果 |
|
||||||
|
| --- | --- |
|
||||||
|
| 单实例并发 | Node、手动和系统 crond 按任务在 SQLite 里原子取得执行权。同一秒的主规则、附加规则以及随后到达的手动请求一共留下三条记录,脚本只写入一次,其余两条为 `skipped/overlap`。多实例仍可并行。Linux crond 的执行权绑定包装进程的 PID、进程启动时刻和系统启动标识,进程消失后的记录转为结果未知。用仅有运行状态和 PID 的旧任务复测,存活时手动和系统请求均跳过,进程退出后下一个真实分钟成功执行。 |
|
||||||
|
| 故障回报 | 定时脚本退出 17。单独阻断状态接口、单独阻断统计接口或同时阻断两个接口,每次触发的终态均为失败,日统计各增加一次失败。恢复后重复提交同一回执,计数保持为一。手动和真实 crond 在两个回报接口均中断时也取得了退出码 17。系统 crond 保留空的计划时间和入队时间。 |
|
||||||
|
| 在线恢复 | 一个任务占槽,另一个任务已入队。在第三个任务创建时注入系统安装失败,API 返回 503、已落库的任务 ID 和 `saved: false`。可用 `GET /api/crons/{id}` 查询恢复结果。全量恢复保留定义未变的注册,原队列中的脚本执行一次。回归测试另覆盖编辑、删除注册、重复恢复和进程重启。 |
|
||||||
|
| 排队截止时间 | 并发为 1,首轮等待超过 2 秒,立即移除该等待项并记录 `queue_timeout`。第二轮在自己的截止时间内获得执行槽,第三轮正常执行。三轮计划留下两次脚本写入。 |
|
||||||
|
|
||||||
|
Linux Docker 配额为 2 CPU、1 GiB。BusyBox crond 在系统模式重启后产生新的系统回执,切换到 Node 并再次重启后,独立观察跨过下一个分钟边界,系统表中没有残留的有效规则。遇到系统安装故障时,API 返回 503,仍可在实际 crontab 中看到上一次成功安装的规则。
|
||||||
|
|
||||||
|
在并发为 2 的错峰负载中,Shell 轮询、Python 哈希、Node.js 解析和文件读写各执行 6 次。两轮独立枚举的 24 个计划时刻均有成功回执,每轮五秒内派发 24/24,排队 P95 分别为 38 ms、20 ms。排队等待从入队算到派发,五秒准时率从计划时刻算到派发。同秒拥塞使用前面单独实验的口径。
|
||||||
|
|
||||||
|
最终回归覆盖 905 项,897 项通过,8 项跳过,0 项失败。前端生产构建和后端 TypeScript 编译通过,npm 打包清单包含 crond 的 Shell 入口及编译后的回执模块。
|
||||||
|
|
||||||
|
## 停止操作、运行状态和 Docker 页面复验(2026-11-13)
|
||||||
|
|
||||||
|
**发布判断。** 保留第一版的单实例原子抢占、按触发 ID 结算、队列等待时限和增量恢复。本轮用当前工作区前后端构建启动 Linux 容器,发现下列三个 P1。建议将它们加入第一版发布条件。下表保留修复前的复现步骤和通过条件。本轮修复与验收结果见下节。
|
||||||
|
|
||||||
|
| 优先级与场景 | Docker 复现 | 修订方案与通过条件 |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| P1 停错实例 | 任务 7 启动了实例 9。向 `POST /api/crons/2/instances/9/stop` 提交请求,收到 200 与“实例已停止”。任务 7 的触发转为停止,脚本末尾的标记没有写入。两个任务属于同一个测试账号。 | 先用任务 ID 和实例 ID 共同定位执行,再按该执行的身份发送信号。区分已收到停止请求和已确认结束。验收覆盖正确归属、错误归属、重复停止及已结束实例。错误归属必须返回未找到且目标进程继续运行。 |
|
||||||
|
| P1 已停止的排队定时任务仍产生副作用 | 并发 1,任务 A 占槽,任务 B 已有 `queued` 触发。此时实时面板返回排队 0,任务 B 的主记录为空闲。调用 `PUT /api/crons/stop` 返回 200,B 仍在队列中;等待 14,933 ms 后实际写入文件并标为成功。 | 任务级停止应说明作用于哪些已有触发。按触发 ID 原子取消未派发请求,释放队列名额,让取消与启动只有一方胜出。验收用脚本标记确认停止后执行 0 次,未来独立计划仍能启动。面板分别展示等待触发数和涉及任务数。 |
|
||||||
|
| P1 详情、历史和仪表盘对同一执行给出不同状态 | 仅阻断 Shell 的结束状态回报,父进程正确记录失败 17,日统计计一次失败,但任务和实例仍为运行中。另一轮在完成后重放两次合法开始回报,面板为同一 PID 新增两行运行中,两个场景中的 PID 都已消失。即使没有注入故障,同一任务两个并行副本中较短的先结束,主记录便显示空闲,另一个副本仍在执行。 | 逐步给实例关联触发 ID,兼容存量无 ID 行。使开始和结束回报幂等,让本地完成回执一并结算本次实例,根据其余活动实例推导任务状态。已确认终态应挡住迟到开始回报。三种复现均须从任务详情、触发历史、运行实例、日统计核对,且各执行只计一次。 |
|
||||||
|
| P2 容器资源显示误导调参 | Docker 配额为 2 CPU、1 GiB,容器内 `cpu.max` 和 `memory.max` 确认相同上限。面板接口却显示 8 CPU 和 4,106,604,544 字节总内存。两个执行队列还分别用可见 CPU 数计算默认并发。 | 将实际配额与宿主可见资源标清。验证 cgroup v1、v2、未设限及显式并发配置。决定是否变更默认并发之前,先用保持全部工作量的拥塞负载测吞吐、等待 P95 和最老等待。 |
|
||||||
|
|
||||||
|
**竞品依据。** 白虎在运行时按日志 ID 登记和取消具体执行,监控接口从实际队列读取长度。呆呆分别从队列和受锁保护的运行列表统计等待数和运行数。青龙可复用已有触发 ID 联结取消、结束和展示,避免每个页面分别猜测任务状态。此处的对照是固定版本源码,性能复测仅针对青龙。[白虎按执行停止](https://github.com/engigu/baihu-panel/blob/67ec1974d5f445a244f27b77bd50b119127b7724/internal/executor/scheduler.go#L623-L632) · [白虎监控接口](https://github.com/engigu/baihu-panel/blob/67ec1974d5f445a244f27b77bd50b119127b7724/internal/controllers/monitor_controller.go#L127-L132) · [呆呆运行与队列计数](https://github.com/linzixuanzz/daidai-panel/blob/3abb06a7373eeb38fb0cd5d550be77df6b021e90/server/service/scheduler_v2.go#L809-L821)
|
||||||
|
|
||||||
|
**容器调参依据。** 白虎已有从 cgroup v2 读取内存使用和限额、回退到 v1 的实现。青龙可以据此改进容量展示,CPU 配额还需另行读取和验证。[白虎容器内存读取](https://github.com/engigu/baihu-panel/blob/67ec1974d5f445a244f27b77bd50b119127b7724/internal/memopt/cgroup_linux.go#L15-L40)
|
||||||
|
|
||||||
|
**页面验收。** 两个隔离容器分别承载故障场景和正常负载,均使用 2 CPU、1 GiB 配额。截图来自登录后的浏览器页面,故障组中 60% 的五秒准时率为 3 个准时触发除以 5 个带计划时刻的触发。系统 crond 和手动任务各有一条,仅计入已观察触发。已启动且有入队时刻的 5 条样本中,P95 为 14,933 ms。截图可在本次工作区的 `output/playwright/scheduler-review-20261113/` 查看。
|
||||||
|
|
||||||
|
独立性能容器的并发设为 2,Shell 轮询、Python 哈希、Node.js 解析和文件读写各执行 6 次。24 个计划时刻与数据库逐条匹配,全部成功,0 次跳过,排队 P95 为 32 ms,五秒内派发 24/24。结束后停用四条测试规则再截图,因此页面显示 4 个已禁用任务。计划到派发、入队到派发、脚本首行时间分别计量,本轮已测前两项。
|
||||||
|
|
||||||
|
| 截图 | 核对内容 |
|
||||||
|
| --- | --- |
|
||||||
|
| [典型负载仪表盘](../../output/playwright/scheduler-review-20261113/05-typical-performance.png) | 四类任务各运行六次,24 次成功,P95 为 32 ms |
|
||||||
|
| [故障场景仪表盘](../../output/playwright/scheduler-review-20261113/01-dashboard.png) | 7 次观察、1 次跳过、五秒内启动 60%,排队 P95 为 14,933 ms |
|
||||||
|
| [实时运行态](../../output/playwright/scheduler-review-20261113/02-runtime.png) | 同一 PID 525 显示两行,PID 469 和 525 在容器中均已消失 |
|
||||||
|
| [失败回执](../../output/playwright/scheduler-review-20261113/04-failed-receipt.png) | 同一弹窗中顶部显示运行中,下方已有结束时刻和退出码 17 |
|
||||||
|
| [单实例并发](../../output/playwright/scheduler-review-20261113/03-single-instance.png) | 同秒两次触发,一次成功、一次因重叠跳过,脚本只写入一次 |
|
||||||
|
|
||||||
|
## 脚本入口计时与停止操作的最终验收(2026-11-13)
|
||||||
|
|
||||||
|
### 用户如何判断脚本何时开始
|
||||||
|
|
||||||
|
仪表盘“今日面板定时任务”按计划日期核对已登记的面板定时规则。调度器先保存变更,再记录实际启用、替换或取消定时器的时间范围。第一次全量登记是可核对历史的起点。如果切换期间服务中断,页面只统计规则归属明确的计划时刻。有到期时刻落入不确定区间时,页面提示覆盖不完整,并隐藏整体准时率。服务停机期间,已经确认生效且持续有效的规则仍会产生应触发次数。
|
||||||
|
|
||||||
|
每条规则使用与调度相同的日历推算已到期的时刻,再按任务、规则序号、表达式和计划毫秒匹配触发记录。最近五秒留给正在落库的触发。跨午夜补跑仍按原计划日期核对,迟到的记录可修正前一天的未收到次数。手动、开机和系统 crond 的执行可在任务详情查看。
|
||||||
|
|
||||||
|
| 指标 | 起止点与分母 |
|
||||||
|
| --- | --- |
|
||||||
|
| 未收到触发记录 | 应触发减去已有匹配记录的次数。迟到回执出现后重新计算。 |
|
||||||
|
| 准时触发率 | 调度器接收时间与计划相差不超过五秒的次数除以应触发次数。没有快照、没有应触发次数时不显示百分比。 |
|
||||||
|
| 触发后跳过、仍在排队、命令已启动、启动未确认 | 四项相加等于已触发次数。派发并启动可由命令 Shell 启动回报、目标脚本入口或绑定本次执行的已核实退出账目证明。只有启动回报带有命令进程启动毫秒。启动未确认包含启动失败、取消及没有可用启动回报的记录。 |
|
||||||
|
| 触发时间偏差 P95 | 有匹配记录的触发按实际接收与计划时刻的绝对偏差排序。 |
|
||||||
|
| 排队等待 P95 | 从入队到取得执行资格,两者都有时间的触发参与。 |
|
||||||
|
| 计划到命令启动 P95 | 从计划到监督进程观测到执行命令的 Shell 创建成功。直接命令和二进制均可计入,Shell 内可能执行多条命令。 |
|
||||||
|
| 计划到脚本开始 P95 | 从计划到已接入的目标脚本入口,包含调度迟到、排队、前置命令与运行时初始化。 |
|
||||||
|
|
||||||
|
两个启动 P95 都只使用具有对应时间且延迟非负的记录。将毫秒数从小到大排列,取第 `ceil(0.95 × 样本数)` 个值。没有样本时显示“—”。直接执行 `/bin/sleep 3` 只贡献命令进程启动时间,三秒等待属于执行时长。
|
||||||
|
|
||||||
|
Shell 在载入目标脚本前采样,Python 在目标模块首个行事件采样,Node.js 和 TypeScript 在目标文件首个可执行位置采样。定时任务使用计划时刻,手动任务详情使用入队时刻,系统 crond 详情使用包装进程可观测到的派发时刻。历史行、未接入的命令和没有到达入口的执行保留空值。
|
||||||
|
|
||||||
|
### Docker 结果
|
||||||
|
|
||||||
|
在配额为 2 CPU、1 GiB 的独立容器中设置并发 2,分别运行 Shell 轮询、Python 哈希、Node.js 解析和文件读写各六次。逐项匹配 24 个预先枚举的计划时刻,并用 SQLite 原始记录复算仪表盘。
|
||||||
|
|
||||||
|
| 计划次数 | 成功 | 跳过 | 排队等待 P95 | 计划到脚本开始 P95 | 五秒内派发 | 五秒内到达入口 |
|
||||||
|
| ---: | ---: | ---: | ---: | ---: | ---: | ---: |
|
||||||
|
| 24 | 24 | 0 | 30 ms | 585 ms | 24/24 | 24/24 |
|
||||||
|
|
||||||
|
另让五个目标脚本同秒到期、各带一秒前置命令。Shell、CommonJS、Python、ESM、TypeScript 首行各自写入触发 ID 和当前时间,与同一触发的入口记录相差 0 至 1 ms。其中 CommonJS 的排队等待为 48 ms,计划至入口为 1,814 ms。
|
||||||
|
|
||||||
|
| 验收项 | 实测结果 |
|
||||||
|
| --- | --- |
|
||||||
|
| 实例归属 | 指定其他任务 ID 停止实例返回 404,目标脚本正常写入尾部标记。正确停止返回 200,重复请求返回 404,已停止脚本退出码为 143。 |
|
||||||
|
| 停止等待中的定时任务 | 并发 1 时,列表、详情及状态筛选将待派发任务显示为队列中。通过浏览器点击停止,当前轮无派发时刻、无脚本写入,随后独立计划正常执行一次。 |
|
||||||
|
| 回执丢失、迟到与并行结束 | 丢弃 Shell 结束回报后,父进程结算退出码 17,失败日统计一次,活动实例归零。重放开始回报无新增实例。两个并行副本中一个结束时仍有一个活动实例,任务保持运行中,最后一个退出后恢复空闲。 |
|
||||||
|
| 容器资源 | 页面显示容器可用 2 CPU、1.00 GiB,同时标明运行环境可见的 8 CPU、3.82 GiB。回归覆盖嵌套 cgroup v2、v1 和无限额。默认并发调参仍需在相同全量负载下比较吞吐和最长等待。 |
|
||||||
|
| 真实 Linux crond | 故意令安装失败后 API 返回 503、任务待同步、旧系统规则仍存在。恢复后切换至 Node、切回系统并重启,系统规则只安装一份且实际触发。再次切到 Node 并重启,跨分钟边界取得 18 个不同的秒级计划回执,系统表中该任务为零条。 |
|
||||||
|
|
||||||
|
前端生产构建及后端 TypeScript 编译通过。最终分组运行全部 914 项测试,906 项通过,8 项跳过,0 项失败。macOS 并行运行时,已有升级预启动用例两次遇到进程组回收返回 `EPERM`。该文件的 11 项用例已单独通过,其余测试另组通过。
|
||||||
|
|
||||||
|
以下为本次工作区中真实浏览器的截图。
|
||||||
|
|
||||||
|
- [典型任务仪表盘](../../output/playwright/scheduler-entry-20261113/01-typical-dashboard.png)
|
||||||
|
- [定时脚本入口明细](../../output/playwright/scheduler-entry-20261113/02-script-entry.png)
|
||||||
|
- [手动运行及停止回执](../../output/playwright/scheduler-entry-20261113/03-stopped-instance.png)
|
||||||
|
- [有效容器配额和运行态](../../output/playwright/scheduler-entry-20261113/04-container-resources.png)
|
||||||
|
- [队列中任务的停止入口](../../output/playwright/scheduler-entry-20261113/05-queued-task.png)
|
||||||
|
- [系统 crond 与 Node 切换后的触发回执](../../output/playwright/scheduler-entry-20261113/06-linux-crond.png)
|
||||||
|
|
||||||
|
## 缩短 Node 脚本启动时间(2026-11-13)
|
||||||
|
|
||||||
|
之前的 585 ms 是 24 次混合任务“计划到脚本入口”的 P95。当时 6 次 Node 解析任务从派发到入口用了 460–561 ms。同一容器中的三个独立 Node 进程仅载入面板 gRPC 客户端就用了 144–257 ms。现在托管 Node 脚本首次调用 QLAPI 的 RPC 方法时才初始化传输、解析协议并建连。JS、ESM 和 TS 共用这个客户端。全局安装包的解析优先级、面板依赖来源和 mTLS 均有回归覆盖。
|
||||||
|
|
||||||
|
同镜像、2 CPU、1 GiB、并发 2,每版分别运行三轮。每轮 24 个计划时刻,Shell 轮询、文件读写、Python 哈希、Node 解析各 6 次。六轮按旧、新、新、旧、旧、新的顺序运行,均无跳过。每轮计划至入口 P95 依次为 551、288、467、582、563、274 ms。下表从各版本 72 条原始记录合并计算。
|
||||||
|
|
||||||
|
| 项目 | 优化前 | 优化后 |
|
||||||
|
| --- | ---: | ---: |
|
||||||
|
| 计划到脚本开始 P95 | 582 ms | 467 ms |
|
||||||
|
| 计划到脚本入口中位数 | 144 ms | 146.5 ms |
|
||||||
|
| 入队到派发 P95 | 34 ms | 36 ms |
|
||||||
|
| Node 解析任务计划到入口中位数(各 18 次) | 522.5 ms | 272.5 ms |
|
||||||
|
| 五秒内到达脚本入口 | 72/72 | 72/72 |
|
||||||
|
|
||||||
|
另外各跑 10 次首行发起两个并发 QLAPI 查询的脚本,核对两个返回值。计划到脚本入口中位数从 662 ms 降至 255 ms,脚本入口到取得两个查询结果的中位数从 49.5 ms 增至 138.5 ms,计划到取得结果的中位数从 706 ms 降至 397 ms。各阶段分别取中位数。RPC 初始化失败会在首次调用处返回可重试的错误。
|
||||||
|
|
||||||
|
此前单轮探索中两版混合 P95 分别为 605 与 655 ms,当轮两类脚本出现了较慢样本。单轮 24 次的 P95 取第二慢的执行,容易随负载波动。这里的改善以交替运行三轮的合并数据为准。Node 最明显,Shell 和 Python 的启动路径仍可独立分析。[优化前的 Docker 仪表盘](../../output/playwright/scheduler-startup-20261113/before.png) · [优化后的 Docker 仪表盘](../../output/playwright/scheduler-startup-20261113/after.png)
|
||||||
|
|
||||||
|
## 发布前整体复核(2026-11-13,启动优化后)
|
||||||
|
|
||||||
|
**发布判断。** 保留规则安装失败反馈、统一预览、原子日统计、触发记录、脚本入口采样和 Node 客户端延迟初始化。当前版本仍有四个会影响实际执行结果或进程控制的问题。下面列为第一版发布条件,均已用独立 Linux Docker 容器复现。此节记录复核结果与待实现的修复。
|
||||||
|
|
||||||
|
### 正确性与回归条件
|
||||||
|
|
||||||
|
| 优先级 | Docker 复现 | 修复与验收 |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| P1 复合命令提前报成功 | 手动运行 `task phase1.sh && sleep 5 && echo tail >> /tmp/ql-review6-tail && exit 17`。首段完成即显示任务空闲、回执成功和退出码 0。五秒后尾段写入文件,父进程日志记录退出 17,日统计仍为成功 1、失败 0。 | 有外层调度进程时,由观察整条命令退出的一方提交最终状态和日统计。单独处理系统 crond 入口的整条命令与退出码。覆盖先成功后失败、先失败后恢复、单实例重叠,以及包装脚本回报丢失、重复和迟到。核对实际副作用、活动实例、触发回执与日统计。 |
|
||||||
|
| P1 手动多实例排队丢轮次 | 并发 1,先运行一个八秒任务占槽。对允许多实例的另一任务连续提交三次,三个请求都成功,三个触发最初均为 queued。最终只有一条实际写入,两条回执为 skipped/changed。期间没有修改或取消任务。 | 每轮按触发 ID 单独领取与取消。任务级汇总状态不能覆盖已接受轮次。回归必须经过真实手动入口,验收三次执行、三条不同回执,另测取消全部等待与真正修改配置。 |
|
||||||
|
| P1 HTTP 重启使存活实例从活动列表消失 | 真实 crond 启动 32 秒脚本。只重启 HTTP worker,脚本 PID 仍存活,触发仍为 running,实例行却已标为 stopped,仪表盘也失去该实例。随后调用整任务停止,接口返回成功,脚本仍写下结束标记。最终触发成功,而实例记录停在 stopped、退出码为空。 | 启动清理需按执行归属和进程出生标识逐行恢复,存活实例保留可见、可停止。分别验收仅重启 HTTP、仅重启调度器、重启整个容器三种情况,核对实际 PID、实例、触发和仪表盘。已有存活声明的单元测试未经过 HTTP 完整初始化。 |
|
||||||
|
| P1 整任务停止可能误杀复用 PID 的其他进程 | 在验收库注入一条过期实例身份,让其数字 PID 指向独立的一次性 sleep。按实例停止返回 409,进程仍存活;按任务停止返回 200,独立进程被终止。这里用确定性的过期记录模拟 PID 复用。 | 两个停止入口共用进程身份检查,对身份失配的进程不发信号,返回可区分的结果。覆盖同时有真实副本和过期 PID、旧版记录缺少身份、实际停止失败,核对无关进程仍存活。 |
|
||||||
|
|
||||||
|
白虎以每次执行的日志 ID 查找取消句柄。呆呆分别从实际队列和活跃执行集合取数。青龙已有触发 ID、实例 ID 和进程出生标识,可以用这些数据贯穿排队、停止及状态恢复。上述四项验收针对青龙自身的运行语义。[白虎执行停止](https://github.com/engigu/baihu-panel/blob/67ec1974d5f445a244f27b77bd50b119127b7724/internal/executor/scheduler.go#L623-L632) · [呆呆运行与队列计数](https://github.com/linzixuanzz/daidai-panel/blob/3abb06a7373eeb38fb0cd5d550be77df6b021e90/server/service/scheduler_v2.go#L809-L821)
|
||||||
|
|
||||||
|
### 启动收益与满载表现
|
||||||
|
|
||||||
|
既有六轮交替测试的 144 条逐次记录复算一致。每版各 72 个计划全部完成。计划到入口 P95 从 582 降到 467 ms,Node 解析任务中位数从 522.5 降到 272.5 ms。混合中位数为 144 和 146.5 ms,排队 P95 为 34 和 36 ms。每版另测 10 次首行 QLAPI 查询,入口到取得结果的中位数由 49.5 增至 138.5 ms,计划到取得结果的中位数由 706 降至 397 ms。对本次错峰任务和五秒准时目标,可以接受现有启动耗时。亚秒目标应单独以目标脚本与资源配额验收。
|
||||||
|
|
||||||
|
在当前工作区重新编译后端、复制 Shell,在 2 CPU、1 GiB 容器中依次运行四组相同的同秒拥塞负载。每组 30 个三秒 I/O 轮询、6 个 Python 哈希、6 个 Node 解析和 6 个文件读写,共 48 个计划。逐个核对计划、入口与终态。P95 为已派发或已采到入口的样本最近秩,入口准时率为五秒内到达入口的数量除以全部 48 个计划。
|
||||||
|
|
||||||
|
| 设置 | 完成 | 跳过 | 排队 P95 | 最长排队 | 计划到入口 P95 | 五秒内到达入口 / 全部计划 |
|
||||||
|
| --- | ---: | ---: | ---: | ---: | ---: | ---: |
|
||||||
|
| FIFO,并发 2 | 39 | 9 达到积压上限 | 6,063 ms | 6,215 ms | 6,150 ms | 30/48(62.5%) |
|
||||||
|
| 并发 2,仅轮询跳过重复 | 26 | 22 轮询重复 | 1,372 ms | 1,789 ms | 1,816 ms | 26/48(54.2%) |
|
||||||
|
| FIFO,并发 4 | 48 | 0 | 563 ms | 940 ms | 1,135 ms | 48/48(100%) |
|
||||||
|
| FIFO,自动并发 8 | 48 | 0 | 97 ms | 574 ms | 1,484 ms | 48/48(100%) |
|
||||||
|
|
||||||
|
并发 2 时,重复跳过使其余三种任务五秒内到达入口从 13/18 增至 18/18,同时轮询完成数从 21/30 降至 8/30。并发 4 和自动 8 的完成数相同,8 的排队更短,入口 P95 更长。每项配置仅一轮,此次结果可用于选择复测范围。改动默认值前应在多轮交错顺序及 CPU 密集负载下重复,并核对峰值内存、吞吐、跳过和最长等待。
|
||||||
|
|
||||||
|
**下一轮优化。** 先让并发设置页能输入后台支持的 1、2 等正整数,再对齐页面说明、实际有效并发和容器配额。本次 Node 可见 8 个 CPU、容器配额 2 核,自动模式按 8 计。手动队列与定时队列分别限流,配置项不能解释为两者合计的总上限。仪表盘继续展示已采样入口的时延分布,并补充带分子分母的计划达成率和入口覆盖情况,区分已知跳过、未采样、停机期间未观测的时刻。业务允许丢弃旧轮询时可启用重复跳过,每轮必须执行的任务应检查容量和下游承载。
|
||||||
|
|
||||||
|
[复合命令的退出码截图](../../output/playwright/scheduler-review-20261113/02-compound-command.png) · [手动多实例排队截图](../../output/playwright/scheduler-review-20261113/01-manual-queue.png) · [启动优化后的错峰仪表盘](../../output/playwright/scheduler-startup-20261113/after.png)
|
||||||
|
|
||||||
|
Docker 仪表盘包含 192 个拥塞计划和 7 条功能复现触发。四组拥塞计划中,五秒内到达脚本入口的是 152/192(79.2%),同一批样本在页面的“已采样”口径为 152/161(94.4%)。两者相差的 31 个计划均有明确跳过原因。此次重新运行现有调度专项,18/18 通过;四个新场景已列入待补回归。[本轮 Docker 汇总仪表盘](../../output/playwright/scheduler-review-20261113/06-overload-dashboard.png)
|
||||||
|
|
||||||
|
## 复核问题的修复验收(2026-11-13)
|
||||||
|
|
||||||
|
本节更新上一节的四项 P1 和两项页面优化。手动、Node 定时及系统 crond 的受管执行均以整条命令的退出结果结算。内层脚本仍可报告日志位置与首次进入脚本的时间。手动多实例分别持有触发 ID 和日志文件;启动恢复和停止均使用外层进程的身份。Linux 旧记录缺少进程出生标识时,停止前核对进程的任务 ID 和启动时间;无法确认归属时返回 409。
|
||||||
|
|
||||||
|
| 验收项 | Linux Docker 结果 |
|
||||||
|
| --- | --- |
|
||||||
|
| 复合命令 | 手动运行首段成功、尾段退出 17 的命令。尾段运行时回执保持运行中,结束后退出码 17,日统计成功 0、失败 1。真实 crond 运行同类命令同样得到 17。回归还覆盖首段失败后外层恢复为成功、迟到与重复回报。 |
|
||||||
|
| 手动多实例 | 单槽被占用时提交三次,三次均有单独回执和日志,脚本实际写入三次。回归另覆盖等待时取消或编辑命令。 |
|
||||||
|
| 独立工作进程重启 | 分别只重启 HTTP 和 gRPC 工作进程。仍存活的 crond、Node 脚本继续在仪表盘可见,分别按整任务、按实例成功停止,回执退出码 143,脚本尾段未执行。 |
|
||||||
|
| PID 复用 | 在隔离库中让一条过期记录指向无关进程。两个停止入口都返回 409,无关进程继续运行。回归验证同批的有效副本仍能停止,Linux 另实测旧记录的身份匹配及失配路径。 |
|
||||||
|
| crond 安装与切换 | 注入 BusyBox 安装失败后 API 返回 503,旧规则继续存在。恢复、切换到 Node、切回系统并重启后,系统任务实际执行且只安装一份。再次切到 Node 并重启,跨整分观察到 19 个不同的秒级计划,系统活动条目为零。 |
|
||||||
|
| 并发设置 | 从浏览器依次提交并发 1 和 2,页面读取实际生效值。设置说明分别展示手动、定时队列的执行上限和容器可用 CPU。本轮使用 2 CPU、1 GiB 配额。 |
|
||||||
|
|
||||||
|
仪表盘保留已采样入口的准时率,增加以已记录计划为分母的五秒入口达成率及采样覆盖率。实测同秒拥塞的 48 个计划,40 个执行完成,8 个因达到积压上限跳过。31 个计划在五秒内到达入口,页面显示计划达成率 31/48(64.6%)、已采样准时率 31/40(77.5%)、覆盖率 40/48(83.3%)。该批排队 P95 为 5,633 ms,计划到入口 P95 为 5,684 ms。仪表盘的排队 P95 还包含当日手动任务。停机期间没有记录的计划仍无法纳入页面分母。
|
||||||
|
|
||||||
|
后端 TypeScript 与前端生产构建通过。最终分组运行前后端测试共 918 项,910 项通过、8 项跳过、0 项失败。[复合命令退出](../../output/playwright/scheduler-fixes-20261113/02-compound-command.png) · [多实例三次回执](../../output/playwright/scheduler-fixes-20261113/01-manual-queue.png) · [重启后停止 crond](../../output/playwright/scheduler-fixes-20261113/05-restarted-crond.png) · [Docker 仪表盘](../../output/playwright/scheduler-fixes-20261113/03-dashboard.png) · [并发设置](../../output/playwright/scheduler-fixes-20261113/04-concurrency.png)
|
||||||
|
|
||||||
|
## 调度回归修复与 Docker 复验(2026-11-13)
|
||||||
|
|
||||||
|
| 场景 | 本轮结果 |
|
||||||
|
| --- | --- |
|
||||||
|
| 旧任务编辑 | 由浏览器提交迁移前的 `skip_if_pending=null`,保存返回 200,持久化为关闭。 |
|
||||||
|
| 手动任务排队时修改命令 | 用占槽任务固定排队窗口。旧回执记为配置已变更,任务回到空闲,排队令牌清空。回归另验证旧回调无法清除后来提交的轮次。 |
|
||||||
|
| 运行中重启调度进程 | 脚本运行 12 秒,每 2 秒触发。单独重启 gRPC 后,5 次重复触发被跳过。脚本记录依次为开始、结束、开始、结束,两轮间隔 2 秒。旧轮次因父进程丢失退出码显示结果未知。 |
|
||||||
|
| CLI 和 Shell 结算 | 经受管任务调用 CLI 脚本,最终退出码 7,日统计为运行 1、成功 0、失败 1。`ql extra` 回归覆盖实时输出、仅写文件和 tee 三种模式,核对脚本退出码、状态回报和统计。 |
|
||||||
|
| 订阅重复同步 | 对系统 crond 包装命令和旧格式任务连续同步两轮,无重复新增或误删。覆盖禁用行、带引号的参数、显式解释器和执行前钩子。 |
|
||||||
|
| 真实 crond 安装与切换 | BusyBox 安装故障返回 503,旧规则仍在。恢复后切到 Node,切回系统并重启后由系统实际执行。再次切到 Node 并重启,跨分钟共观察到 13 个不同计划,系统活动规则为零。 |
|
||||||
|
|
||||||
|
压测使用并发 2、30 秒计划窗口,任务包含 Shell 轮询、Python 哈希、Node 解析和文件操作,仅轮询开启重复跳过。48 个计划中 26 次实际执行,22 次轮询按策略跳过。实际执行的排队 P95 为 770 毫秒,计划到脚本入口 P95 为 876 毫秒。五秒内进入脚本的比例按实际执行为 26/26,按全部计划为 26/48(54.2%)。这是一次复验样本。
|
||||||
|
|
||||||
|
[旧任务保存](../../output/playwright/scheduler-review-fixes-20261113/01-legacy-saved.png) · [排队时修改命令](../../output/playwright/scheduler-review-fixes-20261113/02-edited-queue.png) · [重启防重](../../output/playwright/scheduler-review-fixes-20261113/03-restart-pending.png) · [crond 切换](../../output/playwright/scheduler-review-fixes-20261113/04-crond-switch.png) · [CLI 退出码](../../output/playwright/scheduler-review-fixes-20261113/05-cli-exit-code.png)
|
||||||
|
|
||||||
|
## 编辑、启动和订阅清理的并发边界(2026-11-13)
|
||||||
|
|
||||||
|
| 复现条件 | 实现和验收结果 |
|
||||||
|
| --- | --- |
|
||||||
|
| 保存任务时,排队的执行已经结束 | 保存和注册失败后的回滚仅写配置字段。回归覆盖两条路径。在 Docker 中暂停保存,让实际任务先完成,再继续保存,确认任务空闲、排队令牌为空,触发记录已完成。 |
|
||||||
|
| 手动回调读到命令后,用户在派发前完成编辑 | 在同一个 SQLite 写事务中校验配置版本、命令、实例模式和单实例排队令牌,再登记派发。回归覆盖单实例及多实例。Docker 回执显示旧轮次因任务变更跳过,重新提交只写入一次新命令的标记。 |
|
||||||
|
| 创建进程后、进程身份入库前,工作进程被强杀 | 子进程在独立管道等待授权,身份入库后才进入 Bash。Docker 对隔离的调度工作进程注入 SIGKILL,确认等待的子进程退出,重试是唯一写入标记的执行。旧回执标为服务重启、结果未知。回归另核对 Bash 启动钩子只在授权后运行一次。 |
|
||||||
|
| 订阅清理遇到相同的参数、带空格的文件名或越界路径 | 从实际脚本参数取得任务 ID,按完整路径核对清单,删除文件前核对物理路径。Docker 使用已安装的 crontab 和面板删除接口,7 个候选中只删除 1 个失效任务。仅引用脚本的任务、带空格的脚本、模拟包装命令的普通参数,以及越界路径均获得保留,仓库外哨兵文件内容完整。 |
|
||||||
|
| `ql extra` 在日志管道中改变用户脚本的退出码 | 日志包装单独取得命令和 tee 的退出码,保留用户自己的管道选项。Docker 面板分别记录 `false | true` 成功和 `return 17` 失败,回归覆盖实时、仅文件和 tee 三种模式。 |
|
||||||
|
|
||||||
|
真实 Linux crond 安装规则后成功执行。切换到 Node 并重启容器后,系统活动规则为零,收到新的 Node 回执。切回系统并再次重启,保留一条有效系统规则,收到新的 crond 回执,没有出现重复的 Node 回执。
|
||||||
|
|
||||||
|
[编辑后派发](../../output/playwright/scheduler-atomic-fixes-20261113/01-edited-command.png) · [工作进程重启](../../output/playwright/scheduler-atomic-fixes-20261113/02-worker-restart.png) · [extra 退出码](../../output/playwright/scheduler-atomic-fixes-20261113/03-extra-exit-code.png) · [完成后保存](../../output/playwright/scheduler-atomic-fixes-20261113/04-completed-save.png) · [订阅清理](../../output/playwright/scheduler-atomic-fixes-20261113/05-subscription-cleanup.png)
|
||||||
|
|
||||||
|
并发 2 下,用 Shell 轮询、Python 哈希、Node 解析和文件操作在 30 秒内触发 48 个计划,仅轮询启用重复跳过。单独运行这组任务时,26 次执行、22 次按策略跳过,入队到派发 P95 为 1596 毫秒,计划到脚本入口 P95 为 2115 毫秒,5 秒内进入脚本为 26/26(全部计划为 26/48)。宿主机同时运行测试时,25 次执行、23 次按策略跳过,两项 P95 分别为 2522 和 3229 毫秒,5 秒内进入脚本为 25/25。两次均核对了全部 48 个计划时刻。这是包含队列竞争的混合负载测量。
|
||||||
|
|
||||||
|
## 失败路径的循环复核(2026-11-13)
|
||||||
|
|
||||||
|
本轮验收把同一任务的配置行、活动定时器、订阅清单、脚本副作用、触发记录和日志放在一起核对。此前单查字段或一次成功响应,会漏掉“数据库仍有任务但定时器已消失”和“外层 Shell 返回 0 但脚本从未执行”。
|
||||||
|
|
||||||
|
| 操作 | 回归条件与 Docker 实测 |
|
||||||
|
| --- | --- |
|
||||||
|
| 制造名称、命令和规则的唯一键冲突 | 同一 Node 后端编辑不预先撤掉旧定时器。真实 API 返回 409,原任务与对照任务接下来连续 4 个两秒计划时刻一一对应。跨后端撤销旧定时器后若落库失败则恢复旧配置;恢复失败返回 503,并标记待同步。 |
|
||||||
|
| 禁用后编辑,再使用旧订阅快照清理 | 编辑会重新发布清单;自动删除同时核对任务 ID、脚本及可取得的版本。故意重放旧清单后,API 拒绝整批,任务和旧文件均保留;用当前清单清理则只删除目标任务和目标文件。 |
|
||||||
|
| 非法过滤器与目录链接 | 包含、排除和依赖过滤器分别传入非法 POSIX 正则,均返回非零且保留原文件。目录、文件和原始下载的写入不跟随目标链接;硬链接采用替换文件的方式。Docker 核对了订阅外哨兵文件的原始内容。 |
|
||||||
|
| 含空格和单引号的订阅脚本 | 同一脚本手动运行时写出标记、日志并返回 0;定时运行时真实返回 7,页面标记失败,日志与入口时间均可查。 |
|
||||||
|
| 身份登记期间取消或过期 | 在 Linux 上故意挂起进程身份登记。使定时器失效或等待超过期限后,子进程未获得运行授权,文件标记不存在。记录分别是“变更”和“等待超时”,均无启动时间和退出码。 |
|
||||||
|
|
||||||
|
最终构建在 BusyBox crond 与 Node 之间来回切换并分别重启:系统活动规则数依次为 **1、0、0、1、1**,回到系统后没有新的 Node 回执。四种脚本、并发 2 的同秒拥塞复测中,48 个计划全部对账,26 次完成、22 次按配置跳过;排队 P95 **584 ms**,计划至实际脚本入口 P95 **1,001 ms**。26 次执行都在计划后 5 秒内进入脚本;以全部计划为分母则为 **26/48**。这是一次容器测量,不能用作所有部署的延迟承诺。
|
||||||
|
|
||||||
|
### 旧客户端统计回执的归属
|
||||||
|
|
||||||
|
旧版 Shell 单独发送的 `POST /dashboard/record` 只有 `ref_id/code/elapsed`。相同报文既可能是网络重试,也可能来自结果和耗时相同的两次执行。服务端返回 `code: 200`、`data: { counted: false, reason: "missing_execution_identity" }`,不修改日统计,也不查找“最近运行”。只发送这种报文的客户端,应改为发送已分配的 `trigger_id`,或发送可核对的开始及结束状态。升级前已入账的匿名统计保留原入账日,升级迁移结合退出记录补足可确认的总次数缺口。
|
||||||
|
|
||||||
|
任务详情的「日统计」按面板时区显示最近七天保留的退出和入账记录。可辨认的旧退出需要成功或失败状态与退出码一致。同一退出留下的相同开始副本只认一次,身份冲突的记录不能当作独立执行。旧账同时有重报和漏报时,次数相等也不能确认逐笔对应。如果脚本开始秒数和面板保存开始的时间都晚于当日最后入账,已有当日次数不能对应这次执行。迁移扣除后续日期仍可用的同结果次数后才补记。无法确定原始更新时间或已补过的统计行仅按可证明的总数缺口处理。已执行过旧补计的数据库只追加新证据证明的差额。旧匿名次数、入账日期和耗时样本均保留,因此已入账次数可以高于已核实退出数。
|
||||||
|
|
||||||
|
日统计清理会在同一事务保存最早保留日期,后续放宽保留天数也沿用已清理的边界。迟到的退出回执仍可核实保留实例的结果,记账日期取该实例已保存的结束时间。早于清理边界的统计不会重新生成,跨越边界后才结束的长任务按结束日正常入账。旧补计版本没有保存日统计清理边界。升级时以全库最早保留的统计日期和昨天中较早的日期作为保守边界,旧迁移已经运行而当前缺少的任务日账也保留为空。这可能留下无法区分历史清理与漏报的缺口,可从仍保留的退出记录核对。
|
||||||
|
|
||||||
|
旧版 Shell 的开始与结束状态通过任务、PID、开始时间及可用的日志路径匹配实例。独立脚本的终态、触发记录和日统计在一个事务内提交;属于调度器进程树的脚本由外层完整命令结算。只有 PID、没有开始时间的退出回执不能据以关闭实例。旧 CLI 系统任务发送的 `legacy-system:<秒>:<UUID>` 在状态入口转换为同一个触发 ID。重放已结算的回执不会重复计数。
|
||||||
|
|
||||||
|
旧版 Shell 的退出回执可以先于开始到达。服务端直接保存终态和对应的运行历史,随后重试开始只会命中同一实例。相同 PID 和日志路径下,不同开始秒数区分不同执行。进程已退出或出生时间晚于所报开始时间时,单独的开始回执保留为“结果未知”,可由后到的退出码结算。旧协议时间精度为秒,若同一任务的 PID、日志路径和开始秒数全部相同,则报文本身无法区分两次执行;这类客户端应携带独立执行 ID。
|
||||||
|
|
||||||
|
新版 Shell 和本机 CLI 为每次独立执行生成一次 `execution_id`(`client-<UUID>`)。受调度器管理的执行沿用父进程的 `trigger_id`,系统 CLI 保留已有的 `legacy-system:<秒>:<UUID>` 格式。开始、退出和单独发送的统计回执共用本次执行的标识。带标识的回执等待响应超时后最多重发一次,重发使用原请求体。CLI 根据运行中的面板对首条状态回执的校验结果选择字段。只有服务端明确拒绝标识字段时才重发匿名开始;若旧面板明确拒绝退出码,CLI 会重发兼容的退出回执,并跳过独立统计请求。超时、认证失败或其他校验错误均不触发协议降级。
|
||||||
|
|
||||||
|
新旧客户端共用任务、PID、日志路径和开始秒数时,带标识的独立执行按 ID 结算,匿名回执只匹配兼容实例或经核对的托管执行。两个完全匿名且所有匹配字段都相同的执行仍然无法拆分。
|
||||||
|
|
||||||
|
任务摘要保存最近一次开始回执对应的实例。开始秒数相同时,最后登记的开始回执取得摘要;该实例结束前时长为空,结束后可以为 0 秒。缺少开始回执的同秒历史退出只记入自己的记录和统计。
|
||||||
|
|
||||||
|
### 跨午夜清理与退出回执(2026-11-15)
|
||||||
|
|
||||||
|
历史清理从同一时钟快照计算触发记录和运行实例的截止秒,并保留仍在运行的实例。手动任务和内置调度任务在授权后启动 Bash。监督进程记录整条命令的退出码或信号以及结束时间,先将回执原子写入数据目录,再由工作进程或重启恢复事务结算。工作进程提前退出时,监督进程继续排空输出,避免任务的最后一次日志写入改变退出码。
|
||||||
|
|
||||||
|
独立 CLI 和 Shell 使用 `X-QL-Finished-At` 携带结束时的 Unix 毫秒时间戳。状态回执、统计回执和超时重试共享该时间,旧面板可以按原请求体处理。日账取面板时区的结束日期。已经清理的日期保留为空,其余日期按执行标识只结算一次。旧客户端缺少可靠结束时间时,沿用已有结束记录。
|
||||||
|
|
||||||
|
Docker 验收在两个跨午夜实例运行时清理历史,并发提交清理、强杀 HTTP 工作进程及释放任务。另将两个独立 CLI 的退出回执延迟至清理和重启后交错重放,并用真实 crond 验证 SQLite 写锁跨午夜时的归账日期。[清理时仍在运行的实例](../../output/playwright/live-midnight-review/running.png) · [最终触发记录](../../output/playwright/live-midnight-review/history.png) · [逐日对账](../../output/playwright/live-midnight-review/daily.png) · [独立 CLI 迟到回执](../../output/playwright/live-midnight-review/cli-late.png)
|
||||||
|
|
||||||
|
### 退出回执已落盘、日账尚未提交时重启(2026-11-15)
|
||||||
|
|
||||||
|
监督进程按执行 ID 原子发布第一份完整退出回执。服务启动和历史清理在 SQLite 写事务内先处理已发布的回执,再恢复状态或删除过期记录。结算沿用实际退出时间。停止操作仍在核验进程树时保留回执,已经清理的触发记录和日账遵守原保留期限。只有从独立的已提交快照确认执行已经结算或触发记录已经删除,才移除对应退出文件。SQLite 提交失败时释放写连接,原服务可以重试。
|
||||||
|
|
||||||
|
Docker 验收用 SQLite 写锁将四个真实实例停在已发布回执、尚未入账的窗口,两组分别强杀 HTTP 工作进程和整个容器。容器测试时钟跨过两次上海时区的午夜。恢复时,11 月 16 日失败一次,17 日成功和失败各一次,18 日成功一次。按保留期清理后,再交错重投八次退出回执,16 日的日账继续为空,17、18 日计数保持正确。原进程的 PID 和启动标识、触发记录的退出码与结束毫秒均已逐一核对。[触发记录](../../output/playwright/receipt-journal-review/history.png) · [清理重试后日统计](../../output/playwright/receipt-journal-review/daily-after-cleanup.png) · [逐实例对账数据](../../output/playwright/receipt-journal-review/verification.json)
|
||||||
|
|
||||||
|
### 日账提交后、回执删除前重启(2026-11-15)
|
||||||
|
|
||||||
|
恢复和重试按执行 ID 查询 SQLite。已入账或已清理的执行可以直接回收退出文件,即使旧文件不可读也能继续处理其他实例。开始时间已经提交的采样文件也会回收。触发记录已清理时,只有同 ID 的监督进程退出文件可以证明孤立的开始采样属于已退出的执行。尚未登记的新采样继续保留。采样临时目录不可用时,退出恢复仍可独立完成。
|
||||||
|
|
||||||
|
Docker 验收在两笔日账由独立连接读到、两个退出文件均存在时强杀整个容器。重启后同一任务又启动两个并发实例,用写锁使它们在第二次午夜前后发布退出回执,再同时放行历史清理、旧回执重放和新实例结算。旧文件回收后单独核对了两个新文件的退出码和结束毫秒。再次交错重放八次,11 月 16 日的已清理日账仍为空,17 日成功和失败各一次,18 日成功一次。[强杀前触发记录](../../output/playwright/postcommit-review/committed.png) · [最终触发记录](../../output/playwright/postcommit-review/history.png) · [最终日统计](../../output/playwright/postcommit-review/daily.png) · [进程与数据库逐项核对](../../output/playwright/postcommit-review/verification.json)
|
||||||
|
|
||||||
|
### 未入账的退出回执不可读或损坏时恢复(2026-11-15)
|
||||||
|
|
||||||
|
回执按执行 ID 独立核验。读取失败、格式错误或校验值不符时,面板保留该执行和触发记录,显示“等待退出回执恢复”,同任务的其他实例继续结算。历史清理和服务重启都保留待核实标记。服务每轮检查结束 15 秒后再次检查。原始回执恢复可读后,退出码、结束毫秒和保留期内的日账在同一事务中结算。持续损坏的回执会保持待核实。新监督进程写入绑定执行 ID、退出码、信号和结束时间的 SHA-256 校验值。旧回执仍可读取,但旧格式中符合字段约束的数值改动无法完全识别。
|
||||||
|
|
||||||
|
Docker 验收使用上海时区的容器测试时钟。先让同一任务的四个真实实例并发运行并跨午夜退出,再对一个回执注入读取权限错误,损坏另一个回执,期间强杀容器两次、清理历史并启动第五个实例。恢复原始文件后,后台在 4.77 秒内完成结算。逐一核对进程启动标识、实际退出码和结束毫秒,重放八次后,11 月 16 日和 17 日均成功、失败各一次,18 日成功一次。再次清理并重放后,16 日的日账保持为空。[等待回执恢复](../../output/playwright/pending-exit-review/pending.png) · [恢复后触发记录](../../output/playwright/pending-exit-review/history.png) · [逐日对账](../../output/playwright/pending-exit-review/daily.png) · [进程、回执与数据库核验数据](../../output/playwright/pending-exit-review/verification.json)
|
||||||
|
|
||||||
|
### 新旧退出回执混用与同秒退出(2026-11-15)
|
||||||
|
|
||||||
|
旧监督进程的三字段文件只有退出码、信号和结束毫秒。两个文件被互换,或退出码被改成另一个合法值后,仅凭文件名、时间顺序和进程已经退出,都无法确定每个实例的结果。重启和清理会保留实例并显示“结果未知 · 等待核实退出结果”。即使只剩一个旧文件,也需要来自同一执行的独立回报,逐项匹配退出码、信号和结束毫秒,才能按原日期入账。重复读取或重复投递同一份文件不会增加证据。
|
||||||
|
|
||||||
|
新格式的文件和监督进程独立送达的原始消息均校验绑定执行 ID 的 SHA-256。同一执行的有效新文件和新消息按退出码或信号、结束毫秒逐项比对。文件损坏时,可用一致的有效新消息恢复结果。已提交的终态按原记录结算。停止过程中,已核实的退出时间和退出码会与核实标记一并暂存,等待进程树结束。旧面板留下的暂存值本身不能证明旧文件正确。独立重试的 crond 回报先核对任务和来源,文件替换期间的重试也不会把旧参数写成新格式的有效文件。
|
||||||
|
|
||||||
|
Docker 验收在隔离容器中运行五个真实实例,使用上海时区的测试时钟跨过两次午夜。两个旧协议监督进程在同一秒分别取得退出码 17 和 0,测试保留了它们在内存中生成的原始回报。互换磁盘文件并篡改其中一个退出码,强杀容器两次,期间清理历史并重放。恢复其中一份原件并补发匹配的独立回报后,成功的旧实例按 17 日结算,证据冲突的失败实例继续待核实。其余三个新格式实例正常结算。清理 16 日统计后再重放四次,已清理的日期保持为空。[冲突时的触发记录](../../output/playwright/mixed-receipt-review/pending.png) · [恢复后的触发记录](../../output/playwright/mixed-receipt-review/history.png) · [日统计](../../output/playwright/mixed-receipt-review/daily.png) · [逐进程和逐日核验数据](../../output/playwright/mixed-receipt-review/verification.json)
|
||||||
|
|
||||||
|
### 旧监督进程的独立退出回报先到(2026-11-15)
|
||||||
|
|
||||||
|
旧格式文件缺失、不可读或与回报冲突时,面板在同一 SQLite 事务里保存待核实标记和独立回报。回报按执行 ID、退出码或信号、结束毫秒去重。文件里的字段不能充当独立回报。历史清理会保留待核实的执行和实例。启动、后台轮询或清理读到恢复后的旧文件时,会按原 ID 和完整结果核对已经保存的回报。文件恢复后无需客户端再次上报。统计提交时一并移除待核实回报,提交失败时仍保留证据。已清理日期仍遵守原日账保留期。停止中的实例等待核实时,触发列表暂不展示退出码与结束时间。
|
||||||
|
|
||||||
|
Docker 验收让同一任务的四个真实实例跨午夜并发运行,其中两次旧协议执行在同一秒分别以 17 和 0 退出。先后注入文件互换、文件缺失和重复回报,确认两份独立证据提交后才强杀容器。两次强杀之间执行历史清理,随后又运行第五个实例。先恢复失败实例的原文件,期间只重放成功实例的消息。再恢复成功实例的文件,随后只读数据库等待后台完成结算。按上海时区,16 日失败一次,17 日成功两次、失败一次,18 日成功一次。再清理 16 日日账并重复投递,已清理的日期保持为空。[待核实记录](../../output/playwright/early-exit-review/pending.png) · [仅一个旧实例恢复](../../output/playwright/early-exit-review/partial.png) · [恢复后触发记录](../../output/playwright/early-exit-review/history.png) · [逐日统计](../../output/playwright/early-exit-review/daily.png) · [独立回报、进程及逐日核验数据](../../output/playwright/early-exit-review/verification.json)
|
||||||
|
|
||||||
|
### 同一旧实例收到互相矛盾的退出回报(2026-11-15)
|
||||||
|
|
||||||
|
旧格式文件只含退出码或信号和结束毫秒。按执行 ID 去重后,只有全部独立回报一致且与文件逐项相符,才按文件的退出日期入账。出现两种结果时,即使文件恰好匹配其中一份,触发记录也显示“结果未知 · 退出回报冲突”。触发记录和运行历史均隐藏待核实的退出码和结束时间。重复次数、先后顺序和同秒其他实例的回报都不能裁决冲突。随后取得校验通过且绑定该执行 ID 的监督进程回执,才可确认结果。
|
||||||
|
|
||||||
|
本版为每笔按旧格式核对的入账保留执行 ID、原结果、结束毫秒和对应日账行。迟到冲突会在同一事务内撤回该实例的次数和耗时,并依据同日其他已入账执行重新计算最长耗时。若仅明细已清理,仍可纠正保留的日账;已清理的明细和日期都不会随重试重建。旧版本已经结算且没有逐笔依据的历史汇总沿用原数。数据库提交失败时,恢复出的新格式回执继续保留,供下一次重试。
|
||||||
|
|
||||||
|
Docker 验收在上海时区的隔离测试时钟下运行五个真实实例。三个旧实例于 17 日同一秒退出。一个实例先收到退出码 17 和 0,再恢复原文件;另一个实例已按退出码 0 入账,随后收到结束时间相差 1 毫秒的回报。两次强杀容器、历史清理和并发重放后,两者均保持结果未知。第三个旧实例重复上报后仍只计一次成功。16 日失败一次,17 日仅有一次可核实的成功,18 日成功一次。再清理 16 日并重放,日账继续为空。五个实例的脚本和监督进程均按 PID 和启动标识核对退出。[第二份冲突回报到达前](../../output/playwright/legacy-conflict-review/before.png) · [两条冲突记录](../../output/playwright/legacy-conflict-review/conflict.png) · [运行历史](../../output/playwright/legacy-conflict-review/instances.png) · [日统计](../../output/playwright/legacy-conflict-review/daily.png) · [进程及逐日核验数据](../../output/playwright/legacy-conflict-review/verification.json)
|
||||||
|
|
||||||
|
### 撤账后由绑定执行 ID 的回执纠正退出日期(2026-11-15)
|
||||||
|
|
||||||
|
原日账和触发明细可以分别到期。只要任务还在,按旧格式核对过、仍可能纠正的执行就保留一条内部关联,供以后核对任务、开始时间和原入账。撤账只扣原来实际收到这笔贡献的日账行。绑定该执行 ID 的新版回执到达后,面板在同一 SQLite 事务中消费关联,按真实退出日的保留期限决定是否入账。过期的目标日期继续为空;已经清理的触发明细和运行历史也继续为空。旧版本如果已经清除了全部关联,仅凭迟到的文件无法再确定应归属的任务和时长。
|
||||||
|
|
||||||
|
恢复轮询会检查已有回执文件对应的旧执行,包括触发明细已经清理的执行。回收旧文件时先把文件原子移动到同目录的唯一名称,再检查实际移走的内容。若移动到的是刚恢复的新版回执,启动恢复和后台轮询均可接续结算。只有独立的数据库快照确认已提交,才移除这份回执。单份回收文件暂时无法删除时,其他实例继续恢复。
|
||||||
|
|
||||||
|
Docker 验收在隔离的上海时区测试时钟下运行五个真实任务。三个同任务实例跨午夜在 17 日同一秒退出,其中两个的原版监督进程回执先保存,再注入记作 16 日的旧格式文件和回报。两个实例均发生冲突撤账,其中一个的明细在撤账前已清理。16 日日账清理后恢复新版回执,分别验证运行中的后台轮询,以及旧回报回收文件后立即强杀容器的恢复路径。17 日实际成功两次、失败一次,18 日成功一次。再次强杀、并发重放和清理纠正后的明细后,逐日入账保持一致。[冲突撤账后](../../output/playwright/corrected-exit-day/conflict.png) · [恢复后的触发记录](../../output/playwright/corrected-exit-day/history.png) · [日统计](../../output/playwright/corrected-exit-day/daily.png) · [轮询核验数据](../../output/playwright/corrected-exit-day/poll-verification.json) · [强杀核验数据](../../output/playwright/corrected-exit-day/verification.json)
|
||||||
|
|
||||||
|
### 同一执行出现两份互相矛盾的新版回执(2026-11-15)
|
||||||
|
|
||||||
|
原路径、回收中的临时文件和监督进程送达的新版消息均按执行 ID 校验。退出码或信号、结束毫秒完全相同的副本只结算一次。只要有两种校验通过的结果,面板就按执行 ID、退出结果和结束毫秒保存每种不同的值,触发记录显示“结果未知 · 退出回报冲突”,运行历史隐藏结束时间和退出码。后续收到第三种结果时追加证据。相同结果重复投递只保留一条,首次冲突撤账后,后续投递维持结果未知。
|
||||||
|
|
||||||
|
本版为通过新版回执入账的执行保存对应日账行。发现迟到的矛盾回执时,在一个事务内记录冲突并撤回可核实的次数和耗时,更新该日最长耗时。历史与原日账均过期后,仍可按执行 ID 保留已经看到的冲突证据,已清理的明细或日账继续为空。升级前没有逐笔入账依据的汇总沿用原数。相同副本在提交后先原子移动,再核对实际移走的内容,以保护删除窗口内刚替换的新文件。
|
||||||
|
|
||||||
|
Docker 验收包含五个真实 Linux 任务,其中三个在容器测试时钟跨过午夜后同秒退出。分别注入退出日期不同、退出码不同以及完全一致的两份有效回执,核对脚本退出、监督进程、触发记录和日账。两次强杀之间清理旧日期并并发重放。另对明细和日账均已清理的执行补投冲突文件,暂时移走其中一份,再检查剩余文件和两组已保存证据。[触发记录](../../output/playwright/bound-receipt-conflict/history.png) · [清理后的冲突记录](../../output/playwright/bound-receipt-conflict/conflict.png) · [运行历史](../../output/playwright/bound-receipt-conflict/instances.png) · [日统计](../../output/playwright/bound-receipt-conflict/daily.png) · [逐实例核验数据](../../output/playwright/bound-receipt-conflict/verification.json)
|
||||||
|
|
||||||
|
监督进程和独立回报进程先用独占文件名写入本次回执,然后尝试发布到原路径。原路径已占用时保留自己的文件,重启也能发现完整且校验通过的内容。旧版按校验值命名的备用文件继续按内容和执行 ID 校验。回收时先将文件移动到本次回收独占的路径,复核实际内容后才删除。重启会重新扫描未完成回收的文件。升级会在同一事务内将旧冲突表中的两份证据迁移为两条记录。
|
||||||
|
|
||||||
|
在第三份回执的 Docker 验收中,五个真实任务跨两次午夜退出。两个执行 ID 各保留三种有效结果,其中一个先计成功,随后在撤账事务提交前强杀整个容器。三次强杀重启、历史清理和并发重放后,16 日已清理的失败记录未重建,17 日和 18 日各保留一次成功。两条冲突触发记录均保持结果未知。[触发记录](../../output/playwright/third-bound-receipt/history.png) · [日统计](../../output/playwright/third-bound-receipt/daily.png) · [清理后的触发记录](../../output/playwright/third-bound-receipt/conflict.png) · [进程、回执和逐日核对明细](../../output/playwright/third-bound-receipt/verification.json)
|
||||||
|
|
||||||
|
冲突执行有多个相同的重试副本时,仅在独立数据库连接读到该退出结果和结束毫秒已提交、且再次核实保留文件后,才回收冗余副本。备用文件里另一份绑定同一执行的有效结果也会单独保存。若只留下未写完的文件,面板保留待核实状态。完整回执恢复后按脚本实际退出日期结算。
|
||||||
|
|
||||||
|
备用路径碰撞的 Docker 验收中,两个执行 ID 各收到两次并发重试。强杀前确认四个独立文件均含正确的第三份回执。重启和清理后,每个 ID 的这次重试各保留一份原始文件。损坏的旧备用文件未入账,被替换文件中通过校验的第四种结果也已留存。独立任务的退出码 23 和结束日期在修复半写文件后得到核实。[触发记录](../../output/playwright/alternate-receipt/history.png) · [日统计](../../output/playwright/alternate-receipt/daily.png) · [清理后的结果](../../output/playwright/alternate-receipt/conflict.png) · [逐笔核验明细](../../output/playwright/alternate-receipt/verification.json)
|
||||||
|
|
||||||
|
### 回执从写入到清理的故障边界
|
||||||
|
|
||||||
|
验收要求单个执行最多贡献一次日统计,每种互相矛盾的有效结果都能追溯到执行 ID,清理过的日期在重放后保持为空。
|
||||||
|
|
||||||
|
| 边界 | 故障注入与检查 |
|
||||||
|
| --- | --- |
|
||||||
|
| Node 回报进程、Python 监督进程发布回执 | 两种写入端分别停在首次写前、半写、首次写完、兼容副本写前和写后、硬链接创建前和创建后、发布函数完成。替换正式路径并并发重报两次,再强杀发布进程。硬链接建成后另测原地覆盖。第一次完整写入和正式路径使用独立 inode。 |
|
||||||
|
| 首次入账、冲突撤账 | 分别在登记逐笔贡献、领取计数、更新日账、插入冲突、扣除原贡献及各自事务提交前后强杀。独立读连接检查未提交修改不可见,再让两个重试连接开始写事务。恢复后核对每种结果的退出码与结束毫秒、其他实例的计数及日账。 |
|
||||||
|
| 回收和历史清理 | 在独占回收目录创建、移动、读取、删除和目录释放前后强杀。旧版公开的回收路径仍可恢复。另在删除日账、推进日账和历史保留界限、删除触发记录、清理逐笔关联及提交前后强杀。每次恢复均在下一轮清理之前和之后核对日账。 |
|
||||||
|
| 停止与证据不足 | 已获得可核实的私有回执时,单停保留自然退出。已发出并确认停止时,私有回执可提供实际退出码。绑定的监督进程在脚本退出证据产生前被强杀,面板显示结果未知,后续有效回执可以结算。 |
|
||||||
|
|
||||||
|
Docker 使用隔离的上海时区时钟运行同一任务的五个真实实例。11 月 17 日同秒退出三个实例,其中两笔注入相互冲突的证据,保留为结果未知。进程身份、SQL 日账、运行实例和页面 API 分别核对。统计清理后,16 日的失败日账保持为空。17 日已清理触发明细,原日账仍有一次成功。18 日的保留明细和日账各有一次成功。[逐笔核对](../../output/playwright/receipt-boundaries/verification.json) · [Linux 边界测试结果](../../output/playwright/receipt-boundaries/boundaries.json) · [故障注入用例](../../test/back/cron-first-version.test.cjs) · [清理前日统计](../../output/playwright/receipt-boundaries/daily.png) · [清理后日统计](../../output/playwright/receipt-boundaries/daily-pruned.png)
|
||||||
|
|
||||||
|
逐点强杀的对象是发布进程或持有 SQLite 连接的工作进程。整容器强杀另验三次。宿主断电时的刷盘顺序、网络文件系统、磁盘写满,以及具有相同权限的外部程序直接篡改回收私有目录,均在当时那轮验证范围之外。磁盘写满的后续验收见下节。所有独立证据都丢失时,无法从监督进程退出或单一历史汇总反推脚本退出码。
|
||||||
|
|
||||||
|
### 磁盘写满、恢复空间与跨日重放
|
||||||
|
|
||||||
|
监督进程和回报进程先独占创建一个空文件,将执行 ID、退出码或信号、结束毫秒、SHA-256 校验值和随机后缀编码在文件名里。目录还能分配条目时,这一步就能留下完整观测。随后发布兼容旧面板的 JSON。新格式正文若只写出一部分,恢复时核对文件名校验值及正文的每一个已有字节。若恢复进来的正文和文件名各自校验通过但结果相互矛盾,两份证据均随该执行保留。回收目录继续携带原观测,移动后强杀也能恢复。
|
||||||
|
|
||||||
|
目录项无法创建时,存活的 Python 监督进程和系统 crond 包装脚本仍可将实际结束时取得的同一份结果提交 SQLite。两个位置都因容量不足无法写入时,生产者保留原观测并重试。SQLite 在创建回滚日志时可能返回 `SQLITE_CANTOPEN`,此时还要核实数据库文件系统确实耗尽可用块或 inode。生产者和容器在第一次成功持久化前一起终止,恢复只能将执行标为结果未知。触发记录和运行历史均隐藏未经确认的结束时间和退出码。带有原始退出结果的独立回报可以在空间恢复后补计。只有截断正文的旧格式回执仍需要其他证据。
|
||||||
|
|
||||||
|
| 故障点 | 验收检查 |
|
||||||
|
| --- | --- |
|
||||||
|
| 回执正文写到 12 字节 | 保留已写前缀,在同一文件的下一数据页取得内核 `ENOSPC`。容器退出前并发重报;重启后核对实际退出日期、次数以及残留文件回收。 |
|
||||||
|
| SQLite 提交 | 在真实入账事务中加入 512 KiB 测试数据以保证需要新页,提交前填满专用卷,检查 `COMMIT` 返回 `SQLITE_FULL`。独立连接确认计数与测试行一起回滚。 |
|
||||||
|
| 已提交后的回收 | 耗尽 inode,使回收独占目录创建失败。核对数据库已计数、原文件仍存在;再强杀和重放。 |
|
||||||
|
| 日账清理与迟到回报 | 保留期外的统计与明细都不重建。将另一份合法正文放进已清理执行的元数据回执路径,重复上报后两种冲突结果均有留证,保留日的兄弟实例计数保持一致。 |
|
||||||
|
|
||||||
|
Linux 验收使用 16 MiB、4096 个 inode 的专用 tmpfs,另一个容器保持该卷挂载,跨越面板容器的两次强杀。使用上海时区的隔离时钟,分别检查脚本进程、监督进程、执行 ID、原始结束毫秒、数据库和页面。短写测试人为拆分系统调用并跨数据页申请空间,确保几十字节的回执也可稳定触及真实容量错误。宿主断电、磁盘介质 I/O 错误和网络文件系统语义仍需分别验证。[逐实例核对](../../output/playwright/disk-full-review/verification.json) · [回归测试](../../test/back/cron-first-version.test.cjs) · [限额卷故障注入](../../test/helpers/receipt-volume-full.cjs)
|
||||||
|
|
||||||
|
### ext4 满盘和虚拟机断电
|
||||||
|
|
||||||
|
本轮在专用 ARM64 Linux 虚拟机内运行 Docker 面板。数据库和回执位于同一块 32 MiB 的独立 virtio 磁盘,格式为 ext4,块大小 4 KiB,共 1024 个 inode,保留块为 0。数据盘使用 QEMU writethrough 和 ext4 的 ordered 模式。将后台日志提交间隔延长到 600 秒,使依赖内核后台刷盘的错误可复现。每次断电均从宿主向专用 QEMU 进程发送 SIGKILL,使用原磁盘重新引导,并通过 Linux boot ID 确认内核已更换。控制文件位于独立根盘。
|
||||||
|
|
||||||
|
旧版发布函数成功返回时可以列出三个名字,立即断电后目录为空。现在,Node 和 Python 发布者在尝试写兼容正文前,同步含有执行 ID 和退出元组的目录项,以及回执目录在上一级的条目。目录同步失败时仍可用同一份观测走数据库回报。可写入兼容正文时,先同步正文,再建立链接并同步其目录。两种发布者分别断电验证,按旧协议直接打开正式路径,均读回与断电前一致的 JSON。空间只够存目录项时,恢复需要理解该格式的面板。Linux 对文件与目录分别提供同步语义。[掉电前后对照](../../output/playwright/ext4-power-review/checks.json) · [旧格式读取实测](../../output/playwright/ext4-power-review/legacy-read.json) · [Linux fsync 手册](https://man7.org/linux/man-pages/man2/fsync.2.html)
|
||||||
|
|
||||||
|
面板在每个 SQLite 原生连接第一次查询前等待 `PRAGMA synchronous=EXTRA` 生效,包含独立的事务连接。同一连接上的并发请求共享初始化结果,失败后下一次请求可重试。SQLite 的 DELETE 日志模式在此设置下还会同步删除回滚日志的目录操作,适用于入账、撤账及保留策略清理。[SQLite synchronous 说明](https://www.sqlite.org/pragma.html#pragma_synchronous)
|
||||||
|
|
||||||
|
| 故障点 | 断电与恢复观测 |
|
||||||
|
| --- | --- |
|
||||||
|
| 回执正文只写出 12 字节 | 在同一打开文件上跨过预分配范围申请新块,得到真实 ENOSPC。保持文件描述符打开,切断客体。重启后先确认可用数据块为 0,再释放空间,核对实际退出毫秒和残留文件的回收。 |
|
||||||
|
| SQLite COMMIT 返回 SQLITE_FULL | 独立连接确认首次入账和扩大事务的 512 KiB 测试行均已回滚。满盘重启、释放空间后,各实例按实际退出日期结算一次。 |
|
||||||
|
| SQLite 仍在执行 COMMIT | 只在验收工作进程中加载写调用探针。目标数据库的真实 write 返回 ENOSPC 后,立即暂停进程并切断客体。重启时确认磁盘仍满、热回滚日志头仍在。释放空间,先用独立 SQLite 连接恢复,检查完整性、未提交行及计数,再启动面板补计并重复上报。[逐笔记录](../../output/playwright/ext4-power-review/sqlite-in-flight.json) · [独立实例复查](../../output/playwright/ext4-power-review/sqlite-in-flight-repeat.json) · [Linux 探针](../../test/helpers/receipt-sqlite-power.c) |
|
||||||
|
| 已入账、回收时 inode 耗尽 | 回收目录创建失败,原回执仍在。客体重启后确认可用 inode 仍为 0,然后释放空间。相同执行 ID 的并发重放维持单笔贡献。此时另一实例的退出只存在于生产者内存,重启后显示结果未知;验收程序独立保存原回报并发补送两次,只入账一次。 |
|
||||||
|
| 日账和触发明细清理后断电 | 从清理提交后的同一块盘重新启动,再并发重放旧回报,另为已清理执行恢复一份相互冲突的合法正文。两份观测均留证,已清理的统计与明细保持为空。 |
|
||||||
|
|
||||||
|
六个真实实例按上海时区的退出日期逐笔对账。11 月 16 日一次失败(退出码 23),17 日两次成功、一次失败(退出码 17),18 日两次成功。清理后只保留 17 日及 18 日日账。原始 PID 和 Linux 进程启动标识、执行 ID、退出毫秒、触发记录和日账均保存在核对记录中。[逐实例核对](../../output/playwright/ext4-power-review/verification.json) · [触发记录](../../output/playwright/ext4-power-review/history.png) · [清理后的日统计](../../output/playwright/ext4-power-review/daily-pruned.png)
|
||||||
|
|
||||||
|
实测覆盖客体内核及其页缓存突然消失、原 ext4 日志重放、恢复后释放空间。物理宿主掉电、SSD 易失写缓存或设备忽略 flush、扇区撕裂、介质 I/O 错误、网络文件系统,以及 ext4 其他日志模式仍未实测。
|
||||||
|
|
||||||
|
### ext4 回收时移动和删除回执
|
||||||
|
|
||||||
|
在上述磁盘配置上重新运行 Docker 面板。每轮先让 SQLite 提交,再在生产回收函数调用文件系统之前或返回之后暂停,强杀虚拟机。六次都核对了重启前后的 boot ID 和剩余块数。重启后仍有 0 个可用块,同一个执行 ID 的三个独立回报进程先发布两份旧回报和一份冲突的新回报,然后并发提交,同时释放空间。每轮又运行同一任务的一个真实新实例,让它与再次重放的回报交错结束。[逐轮原始记录](../../output/playwright/ext4-reclaim-review/verification.json) · [断点与注入代码](../../test/helpers/receipt-reclaim-ext4.cjs)
|
||||||
|
|
||||||
|
| 回收边界 | 真实 ext4 观测 |
|
||||||
|
| --- | --- |
|
||||||
|
| 移动前、移动成功后断电 | 空回收目录在 0 可用块时仍能接收文件。移动后重新占用发布路径并同步,断电重启后从该路径读到新回执。 |
|
||||||
|
| 移动返回 ENOSPC | 用 253 个等长的硬链接名填满目标目录已有的目录块,再移动回执,内核实际返回 errno -28。原文件仍在,目标文件未创建。 |
|
||||||
|
| 删除前、删除成功后断电 | 删除前在发布路径放入新证据,重启后仍可读。另一轮中,删除已成功返回,断电前私有文件已消失,ext4 重放日志后旧文件再次出现。重复处理后,已清理的日账和触发明细均未恢复。 |
|
||||||
|
| 删除返回 ENOSPC | 在块耗尽、inode 耗尽、两者都耗尽时,分别删除有其他硬链接的文件、仍被打开的最后一个链接、已关闭的最后一个链接。9 次原生 unlink 全部成功。另在满盘 ext4 上从回收函数的 unlink 调用处显式注入 ENOSPC,核对旧文件保留,并在断电后重放。[原生删除观测](../../output/playwright/ext4-reclaim-review/native-unlink.json) · [探针](../../test/helpers/receipt-unlink-ext4.py) |
|
||||||
|
|
||||||
|
复现并修复了另一处容量故障。已搬入私有目录且已完成入账的回执在 inode 耗尽时,会因重试还需要再创建回收目录而留在盘上。Linux 现在可以打开已有的私有目录,通过该目录描述符复核并删除与已提交结果相符的回执。发布路径被重新占用、回收目录名被复用、或者元数据名和正文给出不同结果时,均有独立回归覆盖。在真实事务提交后的回收中,确认 0 个可用 inode 时创建目录返回 ENOSPC,回收随后释放两个 inode,日账不变。[实际事务与 inode 核对](../../output/playwright/ext4-reclaim-review/private-claim.json) · [回归用例](../../test/back/cron-first-version.test.cjs)
|
||||||
|
|
||||||
|
隔离时钟按上海时区运行。15 个真实实例中,11 月 16 日结束 6 个(4 成功、2 失败),17 日结束 2 个(均成功,两个实例都跨了午夜),18 日结束 7 个(均成功)。保留策略清理后,16 日的日账和触发记录均为空,17 日保留日账 2 笔并已清理触发明细,18 日保留日账和触发明细各 7 笔。6 个已清理实例各保留两份相冲突的退出观测,没有产生新的统计贡献。逐实例记录包含脚本和监督进程的 PID、Linux 启动标识、实际退出码与入账结果。[清理前日统计](../../output/playwright/ext4-reclaim-review/daily.png) · [最终日统计](../../output/playwright/ext4-reclaim-review/daily-pruned.png) · [最终触发记录](../../output/playwright/ext4-reclaim-review/final-history.png)
|
||||||
|
|
||||||
|
### 删除成功后断电、回收目录同名复用
|
||||||
|
|
||||||
|
旧实现在有可用空间时,会把已经移入私有目录的回执再移动一次。实测中,第一个回收进程已核对退出码,随后它核对过的目录被改名,原名重新指向含新回执的目录。两个回收进程放行后,新文件被删除了。[修复前的进程与文件记录](../../output/playwright/ext4-reuse/before-fix.json)
|
||||||
|
|
||||||
|
上一轮实现直接在已有私有目录中复核回执。对新分配的目录也从移动前一直持有目录描述符到删除结束,用该描述符定位子文件。回收器通过同一个目录 inode 上的共享锁并发读取;向目录放入文件的回收器需要排他锁并检查槽位为空。锁忙时保留原文件供重试,进程退出后内核释放锁。实现使用系统 flock,缺少该命令时通过监督进程使用的 Python 运行时调用 fcntl.flock。公开路径仍先移动再复核,磁盘回执格式兼容已有版本。[实现](../../back/shared/managedExit.cjs) · [并发进程回归](../../test/helpers/receipt-claim-reuse-worker.cjs)
|
||||||
|
|
||||||
|
复查还发现,触发和入账记录均已清理后,如果两份相互冲突的回执分开到达,先到的文件可能在第二份到达前被回收。现在 SQLite 为失去原入账关联的有效回执保留执行 ID、结果和结束毫秒。回收前重新读取已提交的记录;提交失败时保留文件。相同回报匹配同一条记录,有差异时在同一事务内转存为冲突证据。这一笔内部观测没有日统计贡献,也不会重建触发记录。首次迟到的执行 ID 按需占用一行,没有时间到期自动删除;否则无法保证任意迟到的第二份回报仍可比对。[退出结算](../../back/shared/triggerHistory.ts) · [提交失败及两种到达顺序的回归](../../test/back/cron-first-version.test.cjs)
|
||||||
|
|
||||||
|
验收使用 Docker 面板和专用虚拟机上的真实 ext4。原生 unlink 成功后、目录变更尚未刷盘时,强杀虚拟机;重新挂载后先确认仍有 0 个可用块,并从原路径读回相同回执。释放空间后,两个独立进程在生产结算代码中均已复核旧文件且即将删除,此时控制程序在同一文件系统上改名旧目录,重建同名目录并写入与同一执行 ID 绑定的新回执。放行后,旧 inode 上的回执消失,新目录和新文件的 inode 与旧值不同,新回执的 SHA-256 保持不变。另用已清理的执行 ID 验证先收到单份新结果、清理并重启服务、再重复送达旧回报的顺序。[逐轮文件、进程及日账](../../output/playwright/ext4-reuse/verification.json)
|
||||||
|
|
||||||
|
11 个真实脚本按上海时区的实际结束日核对。11 月 16 日 4 成功、2 失败,该日日账和触发明细均已清理。17 日两个跨午夜实例成功,保留日账 2 笔,触发明细已清理。18 日 3 个实例均成功,最终保留 3 条触发和 3 笔入账。目录名复用和并发停点由验收程序明确安排;文件改名、重建、删除、SQLite 提交和 ext4 重放均实际执行。没有等待系统随机分配出重复的目录名。测量边界限于客体断电和遵循回收协议的进程,物理设备断电和第三方直接改写已打开的私有目录中的文件未实测。[清理前日账](../../output/playwright/ext4-reuse/daily.png) · [最终日账](../../output/playwright/ext4-reuse/daily-pruned.png) · [触发记录](../../output/playwright/ext4-reuse/final-history.png)
|
||||||
|
|
||||||
|
另外验证了目录分配与移动之间的窗口。A 的 mkdtemp 已返回、尚未打开目录;B 通过另一次真实 mkdtemp 分配了不同 inode,已取得排他锁,正要 rename。在 ext4 上移动两个目录,使 A 原本的名字指向 B 持有的 inode,再放行 A。A 保留退出码 17 的源文件,B 的目标仍是退出码 42。[分配器实测](../../output/playwright/ext4-reuse/allocators.json)
|
||||||
|
|
||||||
|
同一客体交错回收已入账的单份公开回执,每种实现预热 4 次、采样 32 次。只固定目录描述符时中位数 1.38 ms、P95 10.54 ms;加 inode 锁后中位数 2.63 ms、P95 16.66 ms。计时范围为事务提交后的一次文件回收。[回收耗时样本汇总](../../output/playwright/ext4-reuse/timings.json) · [最终 SQLite 独立核对](../../output/playwright/ext4-reuse/database.json)
|
||||||
|
|
||||||
|
### 同一私有目录中的回执被替换
|
||||||
|
|
||||||
|
旧回收器的共享锁固定了目录 inode。两个回收进程都读到旧文件后,独立写入进程在同一目录用 `rename` 替换了 `receipt` 并同步目录。目录 inode 仍是 19,新回执的 inode 是 20;两个回收器均返回成功,新回执却已消失。[修复前原始记录](../../output/playwright/ext4-slot/before-fix.json)
|
||||||
|
|
||||||
|
回收器现在将源目录项原子移到本次尝试专用的随机名字,通常位于数据库目录。核对移走的实际内容后,才从这个新名字删除与已确认结果相符的副本。与原观测不一致的文件留给恢复扫描处理。已读到的旧格式元组在移动时带上摘要,以便中途退出后仍能识别文件是否被换过。摘要只用于核对旧格式内容,无法证明脚本退出。回执目录独立挂载时,在同一文件系统的 `.reclaim` 目录完成移动。在专用 ext4 卷上实测跨设备移动失败,内核返回 `EXDEV`;随后从备用目录读回退出码 42,并在结果匹配后正常回收。[实现](../../back/shared/managedExit.cjs) · [跨设备原始记录](../../output/playwright/ext4-slot/exdev.json) · [双进程 ext4 探针](../../test/helpers/receipt-allocation-reuse-ext4.cjs)
|
||||||
|
|
||||||
|
最终构建在 Docker 内执行 11 个真实脚本,使用独立 QEMU 客体和 ext4 数据盘,挂载选项为 `data=ordered,commit=600`。控制程序按上海时区推进容器时钟,两个实际运行中的脚本跨过午夜。故障轮次使用三个已经清理的实例。独立写入程序给每个实例各放入一份校验通过、与真实退出冲突的回执,仅把旧结果送达两次,并行执行历史清理。文件替换和断电时机通过屏障控制,移动、删除、目录同步、SQLite 提交、强杀客体和重新挂载均实际执行。
|
||||||
|
|
||||||
|
| 第三方写入与断电位置 | 重启后读回的证据 |
|
||||||
|
| --- | --- |
|
||||||
|
| 两个回收器均已核对源文件,替换后放行两者,再切断客体电源 | 一次性名字保留了 42,与原始退出码 17 一起存为冲突。 |
|
||||||
|
| 两个回收器核对后替换,第一个完成移动、尚未复读,第二个仍在等待,此时断电 | ext4 将文件恢复到旧槽位,读回 43,原始退出码 0 也保留。 |
|
||||||
|
| 第一个回收器已核对移走的旧文件、即将删除,第二个仍持有旧目录;此时重新占用源路径,再放行两者并断电 | 新文件在独立名字下读回 44,原始退出码 23 也保留。 |
|
||||||
|
|
||||||
|
11 月 16 日实际结束 6 个实例,4 成功、2 失败;清理后日账和触发明细均为空。17 日两个跨午夜实例成功,日账保留 2 笔,触发明细已清理。18 日实际成功 3 次,最终留有 3 条已结束的触发和 3 笔成功。验证按执行 ID 对照脚本与监督进程 PID、内核启动刻度、脚本退出码和结束日。最终 SQLite 完整性检查通过,运行中实例为 0;三个已清理 ID 共留下六个不同的冲突元组。每份替换回执都只有一个发布来源。[各阶段文件、账目及进程记录](../../output/playwright/ext4-slot/verification.json) · [独立 SQLite 核对](../../output/playwright/ext4-slot/database.json) · [清理前日统计](../../output/playwright/ext4-slot/daily.png) · [最终日统计](../../output/playwright/ext4-slot/daily-pruned.png) · [最终触发记录](../../output/playwright/ext4-slot/final-history.png)
|
||||||
|
|
||||||
|
实测的外部写入范围包括原发布名和旧私有目录内的槽位。能够定位并直接覆盖数据库目录或备用目录里本次新分配的最终删除名、通过已有可写文件描述符原地修改内容、以及直接破坏数据库的写入者,超出本轮保护范围。本轮强杀的是虚拟机,未做物理磁盘断电、设备写缓存丢失或扇区撕裂测试。这一组数据来自有可用空间的卷,下一节记录相同窗口的满盘验收。
|
||||||
|
|
||||||
|
### 同一槽位被替换时的 ext4 满盘与断电
|
||||||
|
|
||||||
|
在专用 32 MiB ext4 数据盘上重复上一节的三个窗口。每个窗口各运行两次,第一次耗尽可用数据块,第二次同时耗尽可用块和 inode。每轮两个独立的生产结算进程先核对同一个目录 inode 和文件 inode,再由第三方在同一目录原子替换文件。替换回执只有一份,正文在填盘前写完并同步,替换和目录同步发生在零可用块时。额外的旧文件硬链接使删除旧内容不会意外释放容量。恢复程序不会扫描这个硬链接。
|
||||||
|
|
||||||
|
六轮普通文件写入均实际收到内核 ENOSPC,后三轮创建文件也实际收到 ENOSPC。包括下述两轮目录项耗尽测试在内,八轮客体强杀前后记录到不同的内核启动 ID,且 `f_bavail` 均为 0。六轮窗口测试中的后三轮同时测得 `f_favail` 为 0。此盘当时的 `f_bfree` 为 163,单独保留该读数。释放空间后,多进程只重报旧元组,并与历史清理并发提交。[逐轮容量、文件与提交结果](../../output/playwright/ext4-cap/verification.json) · [专用 ext4 填盘探针](../../test/helpers/receipt-slot-capacity-ext4.py)
|
||||||
|
|
||||||
|
| 第三方替换及客体断电的窗口 | 仅可用块耗尽 | 可用块和 inode 同时耗尽 |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| 两个回收器核对源文件,替换后都返回,再断电 | 从回收路径找回 42 | 从回收路径找回 45 |
|
||||||
|
| 替换后第一个回收器已移动、尚未复读,第二个仍等待,此时断电 | 从原槽位找回 43 | 从原槽位找回 46 |
|
||||||
|
| 第一个已核对移走的旧文件、正要删除,第二个仍在等待;此时重新占用源路径,放行两者再断电 | 找回 44 | 已删除的旧副本与新回执 47 同时重现,两种证据各保存一次 |
|
||||||
|
|
||||||
|
另做两轮目标目录空间耗尽测试。回收器停在移动前,在目标目录添加与回收文件名等长的硬链接,直到新增等长目录项实际返回 ENOSPC。每轮两个回收器实际调用 `rename`,共四次返回内核 errno -28。替换回执仍位于原槽位,重挂载后才释放填充空间。两个仍在保留期的实例原先分别入账为成功和失败,收到同一执行 ID 的矛盾证据后,各撤销一笔,状态变为“结果未知”。再次重启、清理并重放后,两个实例的成功和失败贡献均为 0。
|
||||||
|
|
||||||
|
最后一轮中,同任务的另一真实脚本恰在满盘时返回 0。断电前其触发记录仍为运行中、计数标记为 0,监督进程已写下带校验和的元数据回执。重启后没有重报该实例。恢复扫描按回执的原结束毫秒计为成功,计数标记为 1。独立 SQLite 完整性检查通过,运行中实例为 0,八个发生冲突的执行 ID 保留了十六条不同的回执证据。[最终数据库核对](../../output/playwright/ext4-cap/database.json) · [回收进程断点与系统调用结果](../../test/helpers/receipt-claim-reuse-worker.cjs) · [自动回归](../../test/back/cron-first-version.test.cjs)
|
||||||
|
|
||||||
|
| 容器内结束日期(上海) | 脚本实际退出 | 最终日账 | 最终触发记录 |
|
||||||
|
| --- | --- | --- | --- |
|
||||||
|
| 11 月 16 日 | 4 成功、2 失败 | 已清理 | 已清理 |
|
||||||
|
| 11 月 17 日 | 2 成功,均跨午夜 | 2 成功 | 已清理 |
|
||||||
|
| 11 月 18 日 | 5 成功、1 失败 | 4 成功、0 失败 | 4 已完成、2 结果未知 |
|
||||||
|
|
||||||
|
容器内应用时钟按上海时区推进,脚本、监督进程、SQLite、ext4 和重新挂载均实际运行。上述真实系统调用失败覆盖写入、创建以及目标目录耗尽时的移动。常规回归另注入目录同步和最终 unlink 返回 ENOSPC。未在这八轮中观察到这两个系统调用自然返回 ENOSPC。物理设备掉电及缓存丢失、存储介质损坏、第三方定向覆盖回收器新分配的最终删除名、两种容量均耗尽时从零创建新回执且无外部重报,均超出这八轮的验证范围。[清理前日统计](../../output/playwright/ext4-cap/daily.png) · [最终日统计](../../output/playwright/ext4-cap/daily-pruned.png) · [最终触发记录](../../output/playwright/ext4-cap/final-history.png)
|
||||||
|
|
||||||
|
### ext4 零可用块、零可用 inode 时首次发布失败
|
||||||
|
|
||||||
|
在同一任务的三个真实脚本仍运行时,让隔离 Docker 虚拟机的上海时钟跨过午夜。将 32 MiB 的 ext4 数据盘填至零可用块和零可用 inode,再释放一个 inode。第一个监督进程用它留下零字节回执,文件名绑定执行 ID、退出码 29、结束毫秒和校验值。另两个脚本分别以 0 和 31 退出,监督进程的首次排他创建以及 SQLite 回滚日志的创建均实测返回 ENOSPC。脚本在独立观测盘记录了实际退出,应用恢复流程没有读取这些观测文件。三个执行 ID 均未从测试程序重报。[系统调用、重挂载和逐实例核对](../../output/playwright/ext4-origin/verification.json)
|
||||||
|
|
||||||
|
直接强杀 QEMU 后重新挂载同一数据盘。面板尚未启动时,两个容量指标仍为零,三个执行均未入账;其中两个执行在回执目录及四张证据表均无记录。释放填充文件后,四个独立进程同时重放已清理和已保留执行的旧回报。带回执的 29 只入账一次,其余两个执行的触发记录和运行历史均显示结果未知。再次启动同任务的两个实例,重放期间它们的进程身份保持一致,以 37、0 交错退出后各计一次。第二次整机强杀重启并重放后,数据库完整性检查通过。[最终触发记录](../../output/playwright/ext4-origin/final-history.png) · [日统计截图](../../output/playwright/ext4-origin/daily-pruned.png)
|
||||||
|
|
||||||
|
| 结束日(应用上海时钟) | 主任务脚本实际退出 | 清理和重放后的日账 | 保留的触发记录 |
|
||||||
|
| --- | --- | --- | --- |
|
||||||
|
| 11 月 16 日 | 4 成功、2 失败 | 已清理 | 已清理 |
|
||||||
|
| 11 月 17 日 | 2 成功 | 2 成功 | 已清理 |
|
||||||
|
| 11 月 18 日 | 1 成功 | 1 成功 | 1 成功 |
|
||||||
|
| 11 月 19 日 | 2 成功、3 失败 | 1 成功、2 失败 | 1 成功、2 失败、2 结果未知 |
|
||||||
|
|
||||||
|
另一个任务验证旧客户端的单文件格式。启动扫描未见回执,先标记结果未知。仅恢复这个执行 ID 的原始、校验通过的 JSON 文件后,HTTP 服务的 15 秒轮询按回执的结束毫秒入账一次。回归测试覆盖两个轮询重叠和再次启动。普通轮询仅检查磁盘上已出现的 ID,无须逐一打开所有未知实例。[单文件恢复观测](../../output/playwright/ext4-origin/late-canonical.json) · [三个重启后的总核对](../../output/playwright/ext4-origin/last-boot.json)
|
||||||
|
|
||||||
|
上述断电发生在虚拟机边界。跨午夜通过推进隔离应用时钟完成,满盘、系统调用失败、真实进程退出和 ext4 重新挂载均实际发生。物理磁盘断电、设备缓存丢写、扇区撕裂及其他文件系统仍未实测。观测盘只供验收使用;如果持久证据全部缺失且没有独立重报,重启后无法还原真实退出码和结束时间。
|
||||||
|
|
||||||
|
### 首次创建返回 ENOSPC、监督进程尚未重试即断电
|
||||||
|
|
||||||
|
15 个真实脚本由隔离 Docker 面板启动。数据盘为单独的 32 MiB ext4;释放跨午夜脚本前,实测可用块和可用 inode 均为零。两个 Linux 跟踪器按执行 ID 匹配监督进程首次创建元数据回执的排他 openat,直接检查内核返回值 -28。两名监督进程均停在 syscall-exit,/proc 状态为 tracing stop,子进程列表为空。此时直接强杀 QEMU,它们没有机会回到 Python 处理异常或启动回报进程。重新挂载后仍为双零。独立对照盘记录的真实退出码为 0、31;整个验收没有向应用重报这两次执行。[跟踪器](../../test/helpers/receipt-enospc-syscall-stop.py) · [系统调用和重挂载证据](../../output/playwright/ext4-firstfail/verification.json)
|
||||||
|
|
||||||
|
另两个实例产生了原始、可校验的回执。验收暂存这些文件,并在两轮清理中各恢复一个实例的原始单文件。两轮均在清理事务扫描完回执、持有 SQLite 写锁且尚未删除触发记录时,同时放行四个真实旧回报进程并恢复文件。第一轮清掉一个旧日账,仍运行的两个新实例以各自的执行 ID 完成。第二轮清掉七条触发记录和七条运行历史,保留日期仍补入一笔成功。再次重复上报、清理和整机重启后,五笔保留入账的执行 ID 和退出码均与脚本记录吻合,结束毫秒与各自监督进程的原始回执完全一致。全部原进程身份已消失,SQLite 完整性检查通过。[逐笔入账](../../output/playwright/ext4-firstfail/bookings.json) · [最终数据库](../../output/playwright/ext4-firstfail/database.json)
|
||||||
|
|
||||||
|
| 结束日期(应用上海时钟) | 脚本实际退出 | 三轮断电后的日账 |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| 11 月 16 日 | 4 成功、2 失败 | 已清理 |
|
||||||
|
| 11 月 17 日 | 2 成功 | 已清理 |
|
||||||
|
| 11 月 18 日 | 1 成功 | 1 成功 |
|
||||||
|
| 11 月 19 日 | 3 成功、3 失败 | 2 成功、2 失败;无证据的两笔未计入 |
|
||||||
|
|
||||||
|
保留期内,缺少持久证据的执行显示结果未知,退出码与结束时间留空。历史到期后清除可见记录。如果日账保留期更长,清理在同一事务保留执行 ID、任务、来源、开始时间及确认进程消失的日期。迟到的校验回执可据此计入原日账;日统计清理线越过最后可能的退出日期后,删除未用的关联。升级前已经彻底失去关联的执行不能追溯补计。[触发记录](../../output/playwright/ext4-firstfail/final-history.png) · [历史清理后的日统计](../../output/playwright/ext4-firstfail/daily-pruned.png)
|
||||||
|
|
||||||
|
满盘和系统调用错误来自真实 ext4。测试暂停了跟踪线程以精确命中断电点,隔离应用时钟用于跨午夜,三次关机均为强杀整台虚拟机。本轮没有实测物理设备断电时的缓存丢写、扇区撕裂、其他文件系统或所有内核调度顺序。
|
||||||
|
|
||||||
|
### 触发历史清理后,单文件补记已提交、文件尚未回收即断电
|
||||||
|
|
||||||
|
在独立的 32 MiB ext4 数据盘上,通过 Docker 面板运行同一任务的 16 个真实脚本。保留日账、清除触发记录和运行历史后,仅恢复三个执行各自的原始 JSON 单文件。结算事务提交后,在第一个 afterCommit 回调处暂停验收进程,此时尚未执行文件回收。另一条只读 SQLite 连接确认三个执行已经按各自的原始结束毫秒入账,三个原文件均存在。随后强杀整台 QEMU。重挂载时先读取磁盘,再启动回放,逐笔确认日账、执行凭据、原文件内容与校验值一致。[断电前后快照](../../output/playwright/ext4-bookcut/verification.json)
|
||||||
|
|
||||||
|
重启后让清理事务扫描原文件并取得写锁,再替换一个原路径上的单文件,并同时放行四个独立回报进程。只收到重复回报的成功实例保留一笔;原退出码 31 的实例遇到另一份校验通过的矛盾回执,撤销自身的一笔失败。原日账已清理的退出码 29 与跨日冲突也被识别,两份证据都保留。再次重启重放后,保留日的次数、成功失败数、总时长、最长时长均与各自未撤账的实例吻合,两个新实例分别以 41 和 0 交错退出,原进程身份均已消失。[数据库逐笔核对](../../output/playwright/ext4-bookcut/database.json) · [保留日账](../../output/playwright/ext4-bookcut/daily-pruned.png) · [触发记录](../../output/playwright/ext4-bookcut/final-history.png)
|
||||||
|
|
||||||
|
| 结束日(隔离应用上海时钟) | 脚本真实退出 | 第一次日账清理、重放及新实例结束后 | 升级验证再清理后 |
|
||||||
|
| --- | --- | --- | --- |
|
||||||
|
| 11 月 16 日 | 4 成功、2 失败 | 已清理 | 已清理 |
|
||||||
|
| 11 月 17 日 | 2 成功 | 已清理 | 已清理 |
|
||||||
|
| 11 月 18 日 | 1 成功、1 失败 | 已清理 | 已清理 |
|
||||||
|
| 11 月 19 日 | 2 成功、2 失败 | 2 成功、1 失败 | 已清理 |
|
||||||
|
| 11 月 21 日 | 1 成功、1 失败 | 1 成功、1 失败 | 1 成功、1 失败 |
|
||||||
|
|
||||||
|
触发历史清理后,面板在核实绑定执行 ID 的回执时,在同一事务保存执行 ID、结果和结束毫秒。日账到期后,这三项仍可用于判定重复与冲突。升级迁移只从尚存、已经核实且触发记录已清理的账目提取原结果,不增加日统计。另以没有原文件的真实旧账验证迁移:恢复不同结束毫秒的回执与清理并行,重新挂载后只保留 11 月 21 日两笔新实例的日账。[迁移前后与重启核对](../../output/playwright/ext4-bookcut/upgrade.json) · [最终日统计](../../output/playwright/ext4-bookcut/daily-upgrade.png)
|
||||||
|
|
||||||
|
本轮用隔离应用时钟跨午夜,用真实 ext4、SQLite 和虚拟机强制断电验证提交及回收窗口。没有实测物理设备掉电后的缓存丢写、扇区撕裂、其他文件系统及所有调度顺序。如果升级前文件、原账目和独立可信回报都已丢失,不能还原该执行的原始结果。
|
||||||
|
|
||||||
|
### 旧回执已经回收,迁移保存退出证据时断电
|
||||||
|
|
||||||
|
复现顺序为:原回执和触发记录均已清理,只剩绑定执行 ID 的已核实账目;迁移事务写入退出证据后、提交前断电;重启后日账清理先拿到写锁。原清理逻辑会删掉最后一份可比对的原结果。修复后,清理仍让日账按时到期,但会将旧账目的执行 ID、退出结果和结束毫秒保留到证据迁移提交。迁移与完成标记在同一 SQLite 事务写入,迁移本身不改日账。
|
||||||
|
|
||||||
|
Docker 中的同一任务共执行 17 个真实脚本,数据库位于独立的 ext4 盘。为构造旧库,验收镜像仅跳过证据迁移,真实结算、文件回收和历史清理照常运行。第一次强杀 Linux 虚拟机前,迁移事务内可见 9 条证据及完成标记,独立连接及重新挂载后均可见 0 条证据、无标记,原 9 条账目仍在。日账清理先执行,随后并发提交一组重复及冲突回报。一个执行已转入冲突表,重新运行迁移后有 8 条可比对证据和完成标记。第二次在提交后立即强杀,重挂载后的内容与提交时相同。[事务与重启快照](../../output/playwright/ext4-migcut/verification.json)
|
||||||
|
|
||||||
|
在清理先持锁、回报先持锁两种顺序下重放,最终三个冲突执行各留下两个不同的结果或结束毫秒。再次重启和重放后,已过期的 11 月 16、17、18 日日账均未恢复。19 日原有两次成功和一次失败,冲突仅撤掉该执行的一次失败;21 日两个新进程分别以 41 和 0 退出,触发记录与一成功、一失败的日账对应。逐笔核对了保留日账的贡献、总时长和最长时长,所有被测进程都已退出。[逐笔数据库核对](../../output/playwright/ext4-migcut/database.json) · [日统计页面](../../output/playwright/ext4-migcut/daily-pruned.png) · [触发记录页面](../../output/playwright/ext4-migcut/final-history.png)
|
||||||
|
|
||||||
|
跨午夜使用隔离应用时钟,断电通过强杀整台 QEMU 后重挂载原盘复现。未实测物理设备断电与缓存丢写、SQLite 写入扇区时的撕裂、各历史发布版镜像逐版升级,或升级中继续运行未修复清理器的混部署。
|
||||||
@@ -31,12 +31,16 @@ ESM loader. There are no separate built-in Ruby, Go, Java or PHP preload modules
|
|||||||
All these task paths still pass through shared dependency-path setup: non-JS
|
All these task paths still pass through shared dependency-path setup: non-JS
|
||||||
scripts and their hooks may themselves call Node or use the QLAPI bridge.
|
scripts and their hooks may themselves call Node or use the QLAPI bridge.
|
||||||
|
|
||||||
The before hooks are deliberately still synchronous: they can export environment
|
Before hooks run synchronously so the script sees their exports and side effects.
|
||||||
variables or perform work required by the user's script. Python's preload still
|
Python starts a child shell and interpreter to capture exported values. Node
|
||||||
starts a child shell/Python to capture that environment; Node's preload starts a
|
starts a child shell and Node process for the same purpose.
|
||||||
child shell/Node. Node also still loads its gRPC client eagerly. These are remaining
|
|
||||||
startup costs, not work performed by the cron library, and are not removed by this
|
Node exposes QLAPI methods during preload. The first RPC loads the panel's
|
||||||
change.
|
gRPC libraries, reads the protocol and TLS configuration, and creates one channel
|
||||||
|
per script process. Concurrent calls share that channel. A script that only uses
|
||||||
|
local libraries pays none of this cost. Closing QLAPI before the first call also
|
||||||
|
avoids loading the transport. Calls after close reject with the usual closed-channel
|
||||||
|
error. An initialization failure rejects the calling promise and permits a retry.
|
||||||
|
|
||||||
Shell scripts run in a child shell that inherits the wrapper's environment and
|
Shell scripts run in a child shell that inherits the wrapper's environment and
|
||||||
before-hook functions. This lets the wrapper run its after hooks and report the
|
before-hook functions. This lets the wrapper run its after hooks and report the
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
# 青龙 2.25.0-rc.0 开发候选
|
||||||
|
|
||||||
|
本候选包含 2.24.0 之后的安全、依赖、编辑器、通知及环境变量修复。主 npm 包使用 `rc` 标签发布,正式 `latest` 保持 2.24.0。
|
||||||
|
|
||||||
|
## 变更
|
||||||
|
|
||||||
|
- 应用权限、首次通知配置与任务环境变量的控制边界更严格;旧数据库中的不安全环境变量标签会在启动前修复,受保护名称不会写入任务环境,服务端错误不会向客户端泄露内部细节。
|
||||||
|
- 依赖管理页面显示已保存状态,用户可以手动复查;配置与脚本编辑器可切换自动换行。
|
||||||
|
- 通知请求和线程等待设置上限,避免无响应的通知渠道拖住任务。
|
||||||
|
- Alpine 和 Debian 镜像的内置 Python 通知依赖放在数据卷之外,挂载空 `/ql/data` 后仍能发送通知。
|
||||||
|
- 独立远程 CLI 候选 `@whyour/qinglong-cli@0.1.5-rc.1` 改善跨平台配置和 OpenAPI 操作,并移除不安全的首次通知命令。
|
||||||
|
|
||||||
|
## 测试与安装
|
||||||
|
|
||||||
|
开发镜像 `whyour/qinglong:develop`、`whyour/qinglong:debian-dev` 与主包 `@whyour/qinglong@2.25.0-rc.0` 应对应同一候选源码。主包可用 `npm install @whyour/qinglong@rc` 安装;CLI 可用 `npm install @whyour/qinglong-cli@rc` 安装。
|
||||||
|
|
||||||
|
升级前备份 `/ql/data`,在目标设备验证任务、订阅、环境变量、依赖与通知。开发候选不建议覆盖正式环境。
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
# 青龙 2.25.0-rc.1 开发候选
|
||||||
|
|
||||||
|
自 2.25.0-rc.0 起的变更如下。
|
||||||
|
|
||||||
|
- 修复 Node 24 下的开发启动,并恢复旧版定时任务的注册。
|
||||||
|
- 定时任务现可查看触发记录、运行实例、每日结果与调度耗时。并发运行时可单独停止指定实例并检查对应日志。
|
||||||
|
- 改进重启后的调度恢复、队列诊断,以及脚本和日志目录的分页读取。
|
||||||
|
- 配套远程 CLI 增补调度、实例和文件浏览相关的 OpenAPI 操作。
|
||||||
+7
-3
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "@whyour/qinglong",
|
"name": "@whyour/qinglong",
|
||||||
"packageManager": "pnpm@8.3.1",
|
"packageManager": "pnpm@8.3.1",
|
||||||
"version": "2.24.0",
|
"version": "2.25.0-rc.1",
|
||||||
"description": "Timed task management platform supporting Python3, JavaScript, Shell, Typescript",
|
"description": "Timed task management platform supporting Python3, JavaScript, Shell, Typescript",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@@ -27,6 +27,7 @@
|
|||||||
"shell/api.sh",
|
"shell/api.sh",
|
||||||
"shell/bot.sh",
|
"shell/bot.sh",
|
||||||
"shell/check.sh",
|
"shell/check.sh",
|
||||||
|
"shell/cron-command.py",
|
||||||
"shell/env.sh",
|
"shell/env.sh",
|
||||||
"shell/lang/*.sh",
|
"shell/lang/*.sh",
|
||||||
"shell/node_path_cache.sh",
|
"shell/node_path_cache.sh",
|
||||||
@@ -36,11 +37,14 @@
|
|||||||
"shell/preload/esm-loader.mjs",
|
"shell/preload/esm-loader.mjs",
|
||||||
"shell/preload/notificationAudit.js",
|
"shell/preload/notificationAudit.js",
|
||||||
"shell/preload/sitecustomize.js",
|
"shell/preload/sitecustomize.js",
|
||||||
|
"shell/preload/script-start.cjs",
|
||||||
"shell/preload/sitecustomize.py",
|
"shell/preload/sitecustomize.py",
|
||||||
"shell/rmlog.sh",
|
"shell/rmlog.sh",
|
||||||
"shell/share.sh",
|
"shell/share.sh",
|
||||||
"shell/start-simplify.sh",
|
"shell/start-simplify.sh",
|
||||||
"shell/start.sh",
|
"shell/start.sh",
|
||||||
|
"shell/system-cron.sh",
|
||||||
|
"shell/subscription-files.py",
|
||||||
"shell/task.sh",
|
"shell/task.sh",
|
||||||
"shell/task-timeout.sh",
|
"shell/task-timeout.sh",
|
||||||
"shell/update.sh",
|
"shell/update.sh",
|
||||||
@@ -53,7 +57,7 @@
|
|||||||
"check:cli": "npm --prefix cli run check",
|
"check:cli": "npm --prefix cli run check",
|
||||||
"test:cli": "npm run build:cli && node cli/scripts/test.cjs",
|
"test:cli": "npm run build:cli && node cli/scripts/test.cjs",
|
||||||
"cli": "node cli/dist/index.js",
|
"cli": "node cli/dist/index.js",
|
||||||
"start": "concurrently -n w: npm:start:*",
|
"start": "concurrently -n back,front npm:start:back npm:start:front",
|
||||||
"start:back": "nodemon ./back/app.ts",
|
"start:back": "nodemon ./back/app.ts",
|
||||||
"start:front": "max dev",
|
"start:front": "max dev",
|
||||||
"build:front": "max build",
|
"build:front": "max build",
|
||||||
@@ -202,7 +206,7 @@
|
|||||||
"@types/uuid": "^8.3.4",
|
"@types/uuid": "^8.3.4",
|
||||||
"@uiw/codemirror-extensions-langs": "^4.21.9",
|
"@uiw/codemirror-extensions-langs": "^4.21.9",
|
||||||
"@uiw/react-codemirror": "^4.21.9",
|
"@uiw/react-codemirror": "^4.21.9",
|
||||||
"@umijs/max": "^4.4.4",
|
"@umijs/max": "^4.7.33",
|
||||||
"@umijs/ssr-darkreader": "^4.9.45",
|
"@umijs/ssr-darkreader": "^4.9.45",
|
||||||
"ahooks": "^3.7.8",
|
"ahooks": "^3.7.8",
|
||||||
"ansi-to-react": "^6.1.6",
|
"ansi-to-react": "^6.1.6",
|
||||||
|
|||||||
Generated
+2119
-983
File diff suppressed because it is too large
Load Diff
+78
-30
@@ -18,6 +18,11 @@ from email.utils import formataddr
|
|||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
|
# 单次 HTTP 请求的默认超时(秒),避免推送服务无响应时永久阻塞
|
||||||
|
REQUEST_TIMEOUT = 10
|
||||||
|
# 等待推送线程结束的最长时间(秒),超时后跳过并继续退出
|
||||||
|
NOTIFY_JOIN_TIMEOUT = 30
|
||||||
|
|
||||||
# 原先的 print 函数和主线程的锁
|
# 原先的 print 函数和主线程的锁
|
||||||
_print = print
|
_print = print
|
||||||
mutex = threading.Lock()
|
mutex = threading.Lock()
|
||||||
@@ -199,7 +204,7 @@ def bark(title: str, content: str) -> Optional[bool]:
|
|||||||
data[bark_params.get(pair[0])] = pair[1]
|
data[bark_params.get(pair[0])] = pair[1]
|
||||||
headers = {"Content-Type": "application/json;charset=utf-8"}
|
headers = {"Content-Type": "application/json;charset=utf-8"}
|
||||||
response = requests.post(
|
response = requests.post(
|
||||||
url=url, data=json.dumps(data), headers=headers, timeout=15
|
url=url, data=json.dumps(data), headers=headers, timeout=REQUEST_TIMEOUT
|
||||||
).json()
|
).json()
|
||||||
|
|
||||||
if response["code"] == 200:
|
if response["code"] == 200:
|
||||||
@@ -241,7 +246,7 @@ def dingding_bot(title: str, content: str) -> Optional[bool]:
|
|||||||
headers = {"Content-Type": "application/json;charset=utf-8"}
|
headers = {"Content-Type": "application/json;charset=utf-8"}
|
||||||
data = {"msgtype": "text", "text": {"content": f"{title}\n\n{content}"}}
|
data = {"msgtype": "text", "text": {"content": f"{title}\n\n{content}"}}
|
||||||
response = requests.post(
|
response = requests.post(
|
||||||
url=url, data=json.dumps(data), headers=headers, timeout=15
|
url=url, data=json.dumps(data), headers=headers, timeout=REQUEST_TIMEOUT
|
||||||
).json()
|
).json()
|
||||||
|
|
||||||
if not response["errcode"]:
|
if not response["errcode"]:
|
||||||
@@ -278,7 +283,9 @@ def feishu_bot(title: str, content: str) -> Optional[bool]:
|
|||||||
data["timestamp"] = timestamp
|
data["timestamp"] = timestamp
|
||||||
data["sign"] = sign
|
data["sign"] = sign
|
||||||
|
|
||||||
response = requests.post(url, data=json.dumps(data)).json()
|
response = requests.post(
|
||||||
|
url, data=json.dumps(data), timeout=REQUEST_TIMEOUT
|
||||||
|
).json()
|
||||||
|
|
||||||
if response.get("StatusCode") == 0 or response.get("code") == 0:
|
if response.get("StatusCode") == 0 or response.get("code") == 0:
|
||||||
audit_results.append(True)
|
audit_results.append(True)
|
||||||
@@ -299,7 +306,7 @@ def go_cqhttp(title: str, content: str) -> Optional[bool]:
|
|||||||
print("go-cqhttp 服务启动")
|
print("go-cqhttp 服务启动")
|
||||||
|
|
||||||
url = f'{push_config.get("GOBOT_URL")}?access_token={push_config.get("GOBOT_TOKEN")}&{push_config.get("GOBOT_QQ")}&message=标题:{title}\n内容:{content}'
|
url = f'{push_config.get("GOBOT_URL")}?access_token={push_config.get("GOBOT_TOKEN")}&{push_config.get("GOBOT_QQ")}&message=标题:{title}\n内容:{content}'
|
||||||
response = requests.get(url).json()
|
response = requests.get(url, timeout=REQUEST_TIMEOUT).json()
|
||||||
|
|
||||||
if response["status"] == "ok":
|
if response["status"] == "ok":
|
||||||
audit_results.append(True)
|
audit_results.append(True)
|
||||||
@@ -325,7 +332,7 @@ def gotify(title: str, content: str) -> Optional[bool]:
|
|||||||
"message": content,
|
"message": content,
|
||||||
"priority": push_config.get("GOTIFY_PRIORITY"),
|
"priority": push_config.get("GOTIFY_PRIORITY"),
|
||||||
}
|
}
|
||||||
response = requests.post(url, data=data).json()
|
response = requests.post(url, data=data, timeout=REQUEST_TIMEOUT).json()
|
||||||
|
|
||||||
if response.get("id"):
|
if response.get("id"):
|
||||||
audit_results.append(True)
|
audit_results.append(True)
|
||||||
@@ -348,7 +355,9 @@ def iGot(title: str, content: str) -> Optional[bool]:
|
|||||||
url = f'https://push.hellyw.com/{push_config.get("IGOT_PUSH_KEY")}'
|
url = f'https://push.hellyw.com/{push_config.get("IGOT_PUSH_KEY")}'
|
||||||
data = {"title": title, "content": content}
|
data = {"title": title, "content": content}
|
||||||
headers = {"Content-Type": "application/x-www-form-urlencoded"}
|
headers = {"Content-Type": "application/x-www-form-urlencoded"}
|
||||||
response = requests.post(url, data=data, headers=headers).json()
|
response = requests.post(
|
||||||
|
url, data=data, headers=headers, timeout=REQUEST_TIMEOUT
|
||||||
|
).json()
|
||||||
|
|
||||||
if response["ret"] == 0:
|
if response["ret"] == 0:
|
||||||
audit_results.append(True)
|
audit_results.append(True)
|
||||||
@@ -377,7 +386,7 @@ def serverJ(title: str, content: str) -> Optional[bool]:
|
|||||||
else:
|
else:
|
||||||
url = f'https://sctapi.ftqq.com/{push_config.get("PUSH_KEY")}.send'
|
url = f'https://sctapi.ftqq.com/{push_config.get("PUSH_KEY")}.send'
|
||||||
|
|
||||||
response = requests.post(url, data=data).json()
|
response = requests.post(url, data=data, timeout=REQUEST_TIMEOUT).json()
|
||||||
|
|
||||||
if response.get("errno") == 0 or response.get("code") == 0:
|
if response.get("errno") == 0 or response.get("code") == 0:
|
||||||
audit_results.append(True)
|
audit_results.append(True)
|
||||||
@@ -406,7 +415,7 @@ def pushdeer(title: str, content: str) -> Optional[bool]:
|
|||||||
if push_config.get("DEER_URL"):
|
if push_config.get("DEER_URL"):
|
||||||
url = push_config.get("DEER_URL")
|
url = push_config.get("DEER_URL")
|
||||||
|
|
||||||
response = requests.post(url, data=data).json()
|
response = requests.post(url, data=data, timeout=REQUEST_TIMEOUT).json()
|
||||||
|
|
||||||
if len(response.get("content").get("result")) > 0:
|
if len(response.get("content").get("result")) > 0:
|
||||||
audit_results.append(True)
|
audit_results.append(True)
|
||||||
@@ -427,7 +436,7 @@ def chat(title: str, content: str) -> Optional[bool]:
|
|||||||
print("chat 服务启动")
|
print("chat 服务启动")
|
||||||
data = "payload=" + json.dumps({"text": title + "\n" + content})
|
data = "payload=" + json.dumps({"text": title + "\n" + content})
|
||||||
url = push_config.get("CHAT_URL") + push_config.get("CHAT_TOKEN")
|
url = push_config.get("CHAT_URL") + push_config.get("CHAT_TOKEN")
|
||||||
response = requests.post(url, data=data)
|
response = requests.post(url, data=data, timeout=REQUEST_TIMEOUT)
|
||||||
|
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
audit_results.append(True)
|
audit_results.append(True)
|
||||||
@@ -461,7 +470,9 @@ def pushplus_bot(title: str, content: str) -> Optional[bool]:
|
|||||||
}
|
}
|
||||||
body = json.dumps(data).encode(encoding="utf-8")
|
body = json.dumps(data).encode(encoding="utf-8")
|
||||||
headers = {"Content-Type": "application/json"}
|
headers = {"Content-Type": "application/json"}
|
||||||
response = requests.post(url=url, data=body, headers=headers).json()
|
response = requests.post(
|
||||||
|
url=url, data=body, headers=headers, timeout=REQUEST_TIMEOUT
|
||||||
|
).json()
|
||||||
|
|
||||||
code = response["code"]
|
code = response["code"]
|
||||||
if code == 200:
|
if code == 200:
|
||||||
@@ -477,7 +488,9 @@ def pushplus_bot(title: str, content: str) -> Optional[bool]:
|
|||||||
else:
|
else:
|
||||||
url_old = "http://pushplus.hxtrip.com/send"
|
url_old = "http://pushplus.hxtrip.com/send"
|
||||||
headers["Accept"] = "application/json"
|
headers["Accept"] = "application/json"
|
||||||
response = requests.post(url=url_old, data=body, headers=headers).json()
|
response = requests.post(
|
||||||
|
url=url_old, data=body, headers=headers, timeout=REQUEST_TIMEOUT
|
||||||
|
).json()
|
||||||
|
|
||||||
if response["code"] == 200:
|
if response["code"] == 200:
|
||||||
audit_results.append(True)
|
audit_results.append(True)
|
||||||
@@ -513,7 +526,9 @@ def weplus_bot(title: str, content: str) -> Optional[bool]:
|
|||||||
}
|
}
|
||||||
body = json.dumps(data).encode(encoding="utf-8")
|
body = json.dumps(data).encode(encoding="utf-8")
|
||||||
headers = {"Content-Type": "application/json"}
|
headers = {"Content-Type": "application/json"}
|
||||||
response = requests.post(url=url, data=body, headers=headers).json()
|
response = requests.post(
|
||||||
|
url=url, data=body, headers=headers, timeout=REQUEST_TIMEOUT
|
||||||
|
).json()
|
||||||
|
|
||||||
if response["code"] == 200:
|
if response["code"] == 200:
|
||||||
audit_results.append(True)
|
audit_results.append(True)
|
||||||
@@ -535,7 +550,9 @@ def qmsg_bot(title: str, content: str) -> Optional[bool]:
|
|||||||
|
|
||||||
url = f'https://qmsg.zendee.cn/{push_config.get("QMSG_TYPE")}/{push_config.get("QMSG_KEY")}'
|
url = f'https://qmsg.zendee.cn/{push_config.get("QMSG_TYPE")}/{push_config.get("QMSG_KEY")}'
|
||||||
payload = {"msg": f'{title}\n\n{content.replace("----", "-")}'.encode("utf-8")}
|
payload = {"msg": f'{title}\n\n{content.replace("----", "-")}'.encode("utf-8")}
|
||||||
response = requests.post(url=url, params=payload).json()
|
response = requests.post(
|
||||||
|
url=url, params=payload, timeout=REQUEST_TIMEOUT
|
||||||
|
).json()
|
||||||
|
|
||||||
if response["code"] == 0:
|
if response["code"] == 0:
|
||||||
audit_results.append(True)
|
audit_results.append(True)
|
||||||
@@ -599,7 +616,7 @@ class WeCom:
|
|||||||
"corpid": self.CORPID,
|
"corpid": self.CORPID,
|
||||||
"corpsecret": self.CORPSECRET,
|
"corpsecret": self.CORPSECRET,
|
||||||
}
|
}
|
||||||
req = requests.post(url, params=values)
|
req = requests.post(url, params=values, timeout=REQUEST_TIMEOUT)
|
||||||
data = json.loads(req.text)
|
data = json.loads(req.text)
|
||||||
return data["access_token"]
|
return data["access_token"]
|
||||||
|
|
||||||
@@ -615,7 +632,9 @@ class WeCom:
|
|||||||
"safe": "0",
|
"safe": "0",
|
||||||
}
|
}
|
||||||
send_msges = bytes(json.dumps(send_values), "utf-8")
|
send_msges = bytes(json.dumps(send_values), "utf-8")
|
||||||
respone = requests.post(send_url, send_msges)
|
respone = requests.post(
|
||||||
|
send_url, send_msges, timeout=REQUEST_TIMEOUT
|
||||||
|
)
|
||||||
respone = respone.json()
|
respone = respone.json()
|
||||||
return respone["errmsg"]
|
return respone["errmsg"]
|
||||||
|
|
||||||
@@ -641,7 +660,9 @@ class WeCom:
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
send_msges = bytes(json.dumps(send_values), "utf-8")
|
send_msges = bytes(json.dumps(send_values), "utf-8")
|
||||||
respone = requests.post(send_url, send_msges)
|
respone = requests.post(
|
||||||
|
send_url, send_msges, timeout=REQUEST_TIMEOUT
|
||||||
|
)
|
||||||
respone = respone.json()
|
respone = respone.json()
|
||||||
return respone["errmsg"]
|
return respone["errmsg"]
|
||||||
|
|
||||||
@@ -663,7 +684,7 @@ def wecom_bot(title: str, content: str) -> Optional[bool]:
|
|||||||
headers = {"Content-Type": "application/json;charset=utf-8"}
|
headers = {"Content-Type": "application/json;charset=utf-8"}
|
||||||
data = {"msgtype": "text", "text": {"content": f"{title}\n\n{content}"}}
|
data = {"msgtype": "text", "text": {"content": f"{title}\n\n{content}"}}
|
||||||
response = requests.post(
|
response = requests.post(
|
||||||
url=url, data=json.dumps(data), headers=headers, timeout=15
|
url=url, data=json.dumps(data), headers=headers, timeout=REQUEST_TIMEOUT
|
||||||
).json()
|
).json()
|
||||||
|
|
||||||
if response["errcode"] == 0:
|
if response["errcode"] == 0:
|
||||||
@@ -711,7 +732,11 @@ def telegram_bot(title: str, content: str) -> Optional[bool]:
|
|||||||
)
|
)
|
||||||
proxies = {"http": proxyStr, "https": proxyStr}
|
proxies = {"http": proxyStr, "https": proxyStr}
|
||||||
response = requests.post(
|
response = requests.post(
|
||||||
url=url, headers=headers, params=payload, proxies=proxies
|
url=url,
|
||||||
|
headers=headers,
|
||||||
|
params=payload,
|
||||||
|
proxies=proxies,
|
||||||
|
timeout=REQUEST_TIMEOUT,
|
||||||
).json()
|
).json()
|
||||||
|
|
||||||
if response["ok"]:
|
if response["ok"]:
|
||||||
@@ -752,7 +777,9 @@ def aibotk(title: str, content: str) -> Optional[bool]:
|
|||||||
}
|
}
|
||||||
body = json.dumps(data).encode(encoding="utf-8")
|
body = json.dumps(data).encode(encoding="utf-8")
|
||||||
headers = {"Content-Type": "application/json"}
|
headers = {"Content-Type": "application/json"}
|
||||||
response = requests.post(url=url, data=body, headers=headers).json()
|
response = requests.post(
|
||||||
|
url=url, data=body, headers=headers, timeout=REQUEST_TIMEOUT
|
||||||
|
).json()
|
||||||
print(response)
|
print(response)
|
||||||
if response["code"] == 0:
|
if response["code"] == 0:
|
||||||
audit_results.append(True)
|
audit_results.append(True)
|
||||||
@@ -836,7 +863,7 @@ def pushme(title: str, content: str) -> Optional[bool]:
|
|||||||
"date": push_config.get("date") if push_config.get("date") else "",
|
"date": push_config.get("date") if push_config.get("date") else "",
|
||||||
"type": push_config.get("type") if push_config.get("type") else "",
|
"type": push_config.get("type") if push_config.get("type") else "",
|
||||||
}
|
}
|
||||||
response = requests.post(url, data=data)
|
response = requests.post(url, data=data, timeout=REQUEST_TIMEOUT)
|
||||||
|
|
||||||
if response.status_code == 200 and response.text == "success":
|
if response.status_code == 200 and response.text == "success":
|
||||||
audit_results.append(True)
|
audit_results.append(True)
|
||||||
@@ -883,7 +910,9 @@ def chronocat(title: str, content: str) -> Optional[bool]:
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
response = requests.post(url, headers=headers, data=json.dumps(data))
|
response = requests.post(
|
||||||
|
url, headers=headers, data=json.dumps(data), timeout=REQUEST_TIMEOUT
|
||||||
|
)
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
if chat_type == 1:
|
if chat_type == 1:
|
||||||
audit_results.append(True)
|
audit_results.append(True)
|
||||||
@@ -936,7 +965,9 @@ def ntfy(title: str, content: str) -> Optional[bool]:
|
|||||||
headers['Actions'] = encode_rfc2047(push_config.get("NTFY_ACTIONS"))
|
headers['Actions'] = encode_rfc2047(push_config.get("NTFY_ACTIONS"))
|
||||||
|
|
||||||
url = push_config.get("NTFY_URL") + "/" + push_config.get("NTFY_TOPIC")
|
url = push_config.get("NTFY_URL") + "/" + push_config.get("NTFY_TOPIC")
|
||||||
response = requests.post(url, data=data, headers=headers)
|
response = requests.post(
|
||||||
|
url, data=data, headers=headers, timeout=REQUEST_TIMEOUT
|
||||||
|
)
|
||||||
if response.status_code == 200: # 使用 response.status_code 进行检查
|
if response.status_code == 200: # 使用 response.status_code 进行检查
|
||||||
audit_results.append(True)
|
audit_results.append(True)
|
||||||
print("Ntfy 推送成功!")
|
print("Ntfy 推送成功!")
|
||||||
@@ -995,7 +1026,9 @@ def wxpusher_bot(title: str, content: str) -> Optional[bool]:
|
|||||||
}
|
}
|
||||||
|
|
||||||
headers = {"Content-Type": "application/json"}
|
headers = {"Content-Type": "application/json"}
|
||||||
response = requests.post(url=url, json=data, headers=headers).json()
|
response = requests.post(
|
||||||
|
url=url, json=data, headers=headers, timeout=REQUEST_TIMEOUT
|
||||||
|
).json()
|
||||||
|
|
||||||
if response.get("code") == 1000:
|
if response.get("code") == 1000:
|
||||||
audit_results.append(True)
|
audit_results.append(True)
|
||||||
@@ -1046,7 +1079,9 @@ def wxpusher_spt(title: str, content: str) -> Optional[bool]:
|
|||||||
data["sptList"] = spts
|
data["sptList"] = spts
|
||||||
|
|
||||||
headers = {"Content-Type": "application/json"}
|
headers = {"Content-Type": "application/json"}
|
||||||
response = requests.post(url=url, json=data, headers=headers).json()
|
response = requests.post(
|
||||||
|
url=url, json=data, headers=headers, timeout=REQUEST_TIMEOUT
|
||||||
|
).json()
|
||||||
|
|
||||||
if response.get("code") == 1000:
|
if response.get("code") == 1000:
|
||||||
audit_results.append(True)
|
audit_results.append(True)
|
||||||
@@ -1080,7 +1115,9 @@ def wpush(title: str, content: str) -> Optional[bool]:
|
|||||||
data["topic_code"] = push_config.get("WPUSH_TOPIC_CODE")
|
data["topic_code"] = push_config.get("WPUSH_TOPIC_CODE")
|
||||||
|
|
||||||
headers = {"Content-Type": "application/json"}
|
headers = {"Content-Type": "application/json"}
|
||||||
response = requests.post(url=url, json=data, headers=headers, timeout=15).json()
|
response = requests.post(
|
||||||
|
url=url, json=data, headers=headers, timeout=REQUEST_TIMEOUT
|
||||||
|
).json()
|
||||||
|
|
||||||
if response.get("code") == 0:
|
if response.get("code") == 0:
|
||||||
audit_results.append(True)
|
audit_results.append(True)
|
||||||
@@ -1121,7 +1158,9 @@ def openilink(title: str, content: str) -> Optional[bool]:
|
|||||||
if push_config.get("OPENILINK_CONTEXT_TOKEN"):
|
if push_config.get("OPENILINK_CONTEXT_TOKEN"):
|
||||||
data["context_token"] = push_config.get("OPENILINK_CONTEXT_TOKEN")
|
data["context_token"] = push_config.get("OPENILINK_CONTEXT_TOKEN")
|
||||||
|
|
||||||
response = requests.post(url=url, json=data, headers=headers).json()
|
response = requests.post(
|
||||||
|
url=url, json=data, headers=headers, timeout=REQUEST_TIMEOUT
|
||||||
|
).json()
|
||||||
|
|
||||||
if response.get("ok"):
|
if response.get("ok"):
|
||||||
audit_results.append(True)
|
audit_results.append(True)
|
||||||
@@ -1215,7 +1254,7 @@ def custom_notify(title: str, content: str) -> Optional[bool]:
|
|||||||
"$title", urllib.parse.quote_plus(title)
|
"$title", urllib.parse.quote_plus(title)
|
||||||
).replace("$content", urllib.parse.quote_plus(content))
|
).replace("$content", urllib.parse.quote_plus(content))
|
||||||
response = requests.request(
|
response = requests.request(
|
||||||
method=WEBHOOK_METHOD, url=formatted_url, headers=headers, timeout=15, data=body
|
method=WEBHOOK_METHOD, url=formatted_url, headers=headers, timeout=REQUEST_TIMEOUT, data=body
|
||||||
)
|
)
|
||||||
|
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
@@ -1233,7 +1272,7 @@ def one() -> str:
|
|||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
url = "https://v1.hitokoto.cn/"
|
url = "https://v1.hitokoto.cn/"
|
||||||
res = requests.get(url).json()
|
res = requests.get(url, timeout=REQUEST_TIMEOUT).json()
|
||||||
return res["hitokoto"] + " ----" + res["from"]
|
return res["hitokoto"] + " ----" + res["from"]
|
||||||
|
|
||||||
|
|
||||||
@@ -1374,11 +1413,20 @@ def send(title: str, content: str, ignore_default_config: bool = False, **kwargs
|
|||||||
notify_function = add_notify_function()
|
notify_function = add_notify_function()
|
||||||
results = []
|
results = []
|
||||||
ts = [
|
ts = [
|
||||||
threading.Thread(target=audited_notification, args=(mode, title, content, results), name=mode.__name__)
|
threading.Thread(
|
||||||
|
target=audited_notification,
|
||||||
|
args=(mode, title, content, results),
|
||||||
|
name=mode.__name__,
|
||||||
|
daemon=True,
|
||||||
|
)
|
||||||
for mode in notify_function
|
for mode in notify_function
|
||||||
]
|
]
|
||||||
[t.start() for t in ts]
|
[t.start() for t in ts]
|
||||||
[t.join() for t in ts]
|
for t in ts:
|
||||||
|
t.join(timeout=NOTIFY_JOIN_TIMEOUT)
|
||||||
|
if t.is_alive():
|
||||||
|
results.append((t.name, False))
|
||||||
|
print(f"{t.name} 推送超时(>{NOTIFY_JOIN_TIMEOUT}s),已跳过,任务继续结束")
|
||||||
report_notifications(results, title, content)
|
report_notifications(results, title, content)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,8 @@ const requiredFiles = [
|
|||||||
'.env.example', 'back/protos/api.proto', 'ecosystem.config.js', 'version.yaml',
|
'.env.example', 'back/protos/api.proto', 'ecosystem.config.js', 'version.yaml',
|
||||||
'shell/upgrade.cjs', 'shell/install-bundle.cjs', 'shell/install-package.cjs',
|
'shell/upgrade.cjs', 'shell/install-bundle.cjs', 'shell/install-package.cjs',
|
||||||
'shell/upgrade-lock.yaml', 'shell/node-runtime-probe.cjs', 'shell/start.sh', 'shell/start-simplify.sh',
|
'shell/upgrade-lock.yaml', 'shell/node-runtime-probe.cjs', 'shell/start.sh', 'shell/start-simplify.sh',
|
||||||
'shell/task.sh', 'shell/preload/client.js', 'shell/preload/client.py',
|
'shell/cron-command.py', 'static/build/shared/managedExit.cjs',
|
||||||
|
'shell/task.sh', 'shell/subscription-files.py', 'shell/preload/client.js', 'shell/preload/client.py',
|
||||||
'shell/preload/sitecustomize.js', 'shell/preload/sitecustomize.py',
|
'shell/preload/sitecustomize.js', 'shell/preload/sitecustomize.py',
|
||||||
'shell/preload/esm-loader.mjs', 'shell/preload/notificationAudit.js',
|
'shell/preload/esm-loader.mjs', 'shell/preload/notificationAudit.js',
|
||||||
'sample/config.sample.sh', 'sample/auth.sample.json',
|
'sample/config.sample.sh', 'sample/auth.sample.json',
|
||||||
|
|||||||
+58
-8
@@ -38,7 +38,7 @@ ql_api_request() {
|
|||||||
-H "Authorization: Bearer ${__ql_token__}" \
|
-H "Authorization: Bearer ${__ql_token__}" \
|
||||||
-H "Content-Type: application/json;charset=UTF-8" \
|
-H "Content-Type: application/json;charset=UTF-8" \
|
||||||
"$@" \
|
"$@" \
|
||||||
--compressed)
|
--compressed) || return $?
|
||||||
if [[ "$attempt" == 2 || "$api" == '{"code":200}' ]] ||
|
if [[ "$attempt" == 2 || "$api" == '{"code":200}' ]] ||
|
||||||
[[ "$(jq -r '.code' <<< "$api" 2>/dev/null)" != 401 ]]; then
|
[[ "$(jq -r '.code' <<< "$api" 2>/dev/null)" != 401 ]]; then
|
||||||
break
|
break
|
||||||
@@ -99,6 +99,7 @@ add_cron_api() {
|
|||||||
t '%s -> 添加成功' "$name"
|
t '%s -> 添加成功' "$name"
|
||||||
else
|
else
|
||||||
t '%s -> 添加失败(%s)' "$name" "$message"
|
t '%s -> 添加失败(%s)' "$name" "$message"
|
||||||
|
return 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,16 +166,43 @@ del_cron_api() {
|
|||||||
local api=$(ql_api_request \
|
local api=$(ql_api_request \
|
||||||
"http://localhost:${ql_port}/open/crons?t=$currentTimeStamp" \
|
"http://localhost:${ql_port}/open/crons?t=$currentTimeStamp" \
|
||||||
-X DELETE \
|
-X DELETE \
|
||||||
--data-raw "[$ids]")
|
--data-raw "${2:-[$ids]}")
|
||||||
code=$(echo "$api" | jq -r .code)
|
code=$(echo "$api" | jq -r .code)
|
||||||
message=$(echo "$api" | jq -r .message)
|
message=$(echo "$api" | jq -r .message)
|
||||||
if [[ $code == 200 ]]; then
|
if [[ $code == 200 ]]; then
|
||||||
t '成功'
|
t '成功'
|
||||||
else
|
else
|
||||||
t '失败(%s)' "$message"
|
t '失败(%s)' "$message"
|
||||||
|
return 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Initialize once in the wrapper, before any pipeline, signal handler or receipt.
|
||||||
|
init_execution_id() {
|
||||||
|
ql_execution_id=""
|
||||||
|
[[ -n ${QL_TRIGGER_ID:-} || ! ${ID:-} =~ ^[1-9][0-9]*$ ]] && return 0
|
||||||
|
local uuid
|
||||||
|
if [[ -r /proc/sys/kernel/random/uuid ]]; then
|
||||||
|
IFS= read -r uuid < /proc/sys/kernel/random/uuid || return 1
|
||||||
|
else
|
||||||
|
uuid=$(node -p "require('crypto').randomUUID()") || return 1
|
||||||
|
fi
|
||||||
|
ql_execution_id="client-$uuid"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Only an identified receipt can safely repeat a write after curl times out.
|
||||||
|
ql_receipt_request() {
|
||||||
|
if [[ -z ${QL_TRIGGER_ID:-}${ql_execution_id:-} ]]; then
|
||||||
|
ql_api_request "$@"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
local response status
|
||||||
|
response=$(ql_api_request --max-time 30 "$@") && { printf '%s' "$response"; return; }
|
||||||
|
status=$?
|
||||||
|
[[ $status == 28 ]] || return "$status"
|
||||||
|
ql_api_request --max-time 30 "$@"
|
||||||
|
}
|
||||||
|
|
||||||
update_cron() {
|
update_cron() {
|
||||||
local ids="$1"
|
local ids="$1"
|
||||||
local status="$2"
|
local status="$2"
|
||||||
@@ -183,15 +211,31 @@ update_cron() {
|
|||||||
local lastExecutingTime="${5:-0}"
|
local lastExecutingTime="${5:-0}"
|
||||||
local runningTime="${6:-0}"
|
local runningTime="${6:-0}"
|
||||||
local exitCode="${7:-}"
|
local exitCode="${7:-}"
|
||||||
|
local finish_header=()
|
||||||
|
if [[ ${8:-} =~ ^[0-9]{1,10}$ ]]; then finish_header=(-H "X-QL-Finished-At: ${8}000"); fi
|
||||||
|
local trigger_id=${QL_TRIGGER_ID:-}
|
||||||
local currentTimeStamp=${EPOCHSECONDS:-$(date +%s)}
|
local currentTimeStamp=${EPOCHSECONDS:-$(date +%s)}
|
||||||
local dataRaw="{\"ids\":[$ids],\"status\":\"$status\",\"pid\":\"$pid\",\"log_path\":\"$logPath\",\"last_execution_time\":$lastExecutingTime,\"last_running_time\":$runningTime"
|
# Preserve the no-fork fast path for generated names; legacy paths may need JSON escaping.
|
||||||
|
local jsonLog
|
||||||
|
if [[ $logPath =~ ^[a-zA-Z0-9_./-]+$ ]]; then
|
||||||
|
jsonLog="\"$logPath\""
|
||||||
|
else
|
||||||
|
jsonLog=$(jq -Rn --arg path "$logPath" '$path') || return 1
|
||||||
|
fi
|
||||||
|
local dataRaw="{\"ids\":[$ids],\"status\":\"$status\",\"pid\":\"$pid\",\"log_path\":$jsonLog,\"last_execution_time\":$lastExecutingTime,\"last_running_time\":$runningTime"
|
||||||
if [[ -n $exitCode ]]; then
|
if [[ -n $exitCode ]]; then
|
||||||
dataRaw="${dataRaw},\"exit_code\":$exitCode"
|
dataRaw="${dataRaw},\"exit_code\":$exitCode"
|
||||||
fi
|
fi
|
||||||
|
if [[ $trigger_id =~ ^[a-z0-9-]{1,120}$ ]]; then
|
||||||
|
dataRaw="${dataRaw},\"trigger_id\":\"$trigger_id\""
|
||||||
|
elif [[ -n ${ql_execution_id:-} ]]; then
|
||||||
|
dataRaw="${dataRaw},\"execution_id\":\"$ql_execution_id\""
|
||||||
|
fi
|
||||||
|
if [[ ${MANUAL:-} == true && -n $trigger_id${ql_execution_id:-} ]]; then dataRaw="${dataRaw},\"trigger_stopped\":true"; fi
|
||||||
dataRaw="${dataRaw}}"
|
dataRaw="${dataRaw}}"
|
||||||
local api=$(ql_api_request \
|
local api=$(ql_receipt_request \
|
||||||
"http://localhost:${ql_port}/open/crons/status?t=$currentTimeStamp" \
|
"http://localhost:${ql_port}/open/crons/status?t=$currentTimeStamp" \
|
||||||
-X PUT \
|
${finish_header[@]+"${finish_header[@]}"} -X PUT \
|
||||||
--data-raw "$dataRaw")
|
--data-raw "$dataRaw")
|
||||||
ql_parse_status_response "$api" || true
|
ql_parse_status_response "$api" || true
|
||||||
if [[ $code != 200 ]]; then
|
if [[ $code != 200 ]]; then
|
||||||
@@ -257,10 +301,16 @@ record_cron_stat() {
|
|||||||
local elapsed="${3:-0}"
|
local elapsed="${3:-0}"
|
||||||
[[ $ref_id ]] && [[ $ref_id -gt 0 ]] 2>/dev/null || return
|
[[ $ref_id ]] && [[ $ref_id -gt 0 ]] 2>/dev/null || return
|
||||||
|
|
||||||
local api=$(ql_api_request \
|
local finish_header=()
|
||||||
|
if [[ ${4:-} =~ ^[0-9]{1,10}$ ]]; then finish_header=(-H "X-QL-Finished-At: ${4}000"); fi
|
||||||
|
local trigger_id=${QL_TRIGGER_ID:-}
|
||||||
|
local extra=""
|
||||||
|
if [[ $trigger_id =~ ^[a-z0-9-]{1,120}$ ]]; then extra=",\"trigger_id\":\"$trigger_id\""
|
||||||
|
elif [[ -n ${ql_execution_id:-} ]]; then extra=",\"execution_id\":\"$ql_execution_id\""; fi
|
||||||
|
local api=$(ql_receipt_request \
|
||||||
"http://localhost:${ql_port:-5700}/open/dashboard/record" \
|
"http://localhost:${ql_port:-5700}/open/dashboard/record" \
|
||||||
-X POST \
|
${finish_header[@]+"${finish_header[@]}"} -X POST \
|
||||||
--data-raw "{\"ref_id\":$ref_id,\"code\":$exit_code,\"elapsed\":$elapsed}")
|
--data-raw "{\"ref_id\":$ref_id,\"code\":$exit_code,\"elapsed\":$elapsed$extra}")
|
||||||
ql_parse_status_response "$api" || true
|
ql_parse_status_response "$api" || true
|
||||||
if [[ $code != 200 ]]; then
|
if [[ $code != 200 ]]; then
|
||||||
if [[ ! $message ]]; then
|
if [[ ! $message ]]; then
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user