mirror of
https://github.com/whyour/qinglong.git
synced 2026-12-01 09:45:36 +08:00
perf(cron): share global concurrency budget and streamline execution
Share durable permits across manual, scheduled and recovered runs, prioritize short ready tasks with aging, and record global queue occupancy. Reduce launch, log polling, dashboard and SQLite overhead while preserving transactional receipts, cancellation, recovery and script hook compatibility. Keep ordinary CRUD in ORM and bound SQL on measured execution hot paths. Fix supervisor descendant tracking, asynchronous log draining, concurrent log directory creation, Linux fallback launches and snapshot clock rollback. Include regression coverage, SQL audits and evolution/review documentation. Validation: five review rounds; local full suite 1478 passed, 28 platform skips; dev suites 415 passed, no failures/skips; backend types and formatting passed.
This commit is contained in:
1 parent
cddabbaf0e
commit
2f01f756d7
74 files changed
+6891
-837
No files matched your search
@@ -0,0 +1,51 @@
|
||||
import { executionModel } from '../../data/executionModels';
|
||||
import { Op } from 'sequelize';
|
||||
import type { ICron } from '../../protos/cron';
|
||||
import { CrontabModel } from '../../data/cron';
|
||||
import { slotId } from '../identity';
|
||||
import { runCron } from '../execution/runCron';
|
||||
|
||||
// Accepted work is distinct from catching up timers missed while offline.
|
||||
// Keep its identity and original queue deadline; never replay a bound command.
|
||||
export async function resumeAcceptedCrons(item: ICron, current: () => boolean) {
|
||||
const rows = await executionModel(
|
||||
CrontabModel.sequelize!,
|
||||
'CronTriggers',
|
||||
).findAll({
|
||||
attributes: ['id', 'scheduled_at', 'queued_at'],
|
||||
where: {
|
||||
cron_id: Number(item.id),
|
||||
source: { [Op.in]: ['node', 'recovery'] },
|
||||
state: 'skipped',
|
||||
reason: 'restart',
|
||||
resume_pending: 1,
|
||||
started_at: null,
|
||||
queued_at: { [Op.gte]: item.recovery_from || 0 },
|
||||
},
|
||||
order: [
|
||||
['queued_at', 'ASC'],
|
||||
['id', 'ASC'],
|
||||
],
|
||||
raw: true,
|
||||
});
|
||||
const rules = [
|
||||
item.schedule,
|
||||
...(item.extra_schedules || []).map((s) => s.schedule),
|
||||
];
|
||||
await Promise.all(
|
||||
rows
|
||||
.filter((row) =>
|
||||
rules.some(
|
||||
(rule, i) => slotId(item.id, i, rule, row.scheduled_at!) === row.id,
|
||||
),
|
||||
)
|
||||
.map((row) =>
|
||||
runCron(item.command, item, current, {
|
||||
id: row.id,
|
||||
time: row.scheduled_at!,
|
||||
queuedAt: row.queued_at!,
|
||||
source: 'recovery',
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import { executionModel } from '../../data/executionModels';
|
||||
import { AnomalyPage } from '../queries/scheduleAnomalies';
|
||||
import { QueryTypes, Sequelize, Transaction } from 'sequelize';
|
||||
import { AddCronRequest, DeleteCronRequest } from '../../protos/cron';
|
||||
@@ -61,14 +62,14 @@ export async function saveSchedulePlan(
|
||||
token: token ?? null,
|
||||
};
|
||||
if (token)
|
||||
await db.query(
|
||||
'INSERT INTO CronPlanSwitches(token,staged_at) VALUES($token,$now)',
|
||||
{ transaction, bind: { token, now } },
|
||||
await executionModel(db, 'CronPlanSwitches').create(
|
||||
{ token, staged_at: now },
|
||||
{ transaction, silent: true, hooks: false, validate: false },
|
||||
);
|
||||
if (replace)
|
||||
await db.query(
|
||||
'INSERT OR IGNORE INTO CronPlanCoverage(id,since) VALUES(1,$now)',
|
||||
{ transaction, bind: { now } },
|
||||
await executionModel(db, 'CronPlanCoverage').bulkCreate(
|
||||
[{ id: 1, since: now }],
|
||||
{ transaction, ignoreDuplicates: true, hooks: false },
|
||||
);
|
||||
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))`;
|
||||
@@ -105,10 +106,15 @@ export async function confirmSchedulePlan(
|
||||
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 } },
|
||||
await executionModel(db, 'CronPlanSwitches').update(
|
||||
{ first_at: first, last_at: last },
|
||||
{
|
||||
where: { token, last_at: null },
|
||||
transaction,
|
||||
silent: true,
|
||||
hooks: false,
|
||||
validate: false,
|
||||
},
|
||||
);
|
||||
if (replace) {
|
||||
// After a crash the former worker's switch time is unknowable. A complete
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { executionModel } from '../../data/executionModels';
|
||||
import { AnomalyPage } from '../queries/scheduleAnomalies';
|
||||
import { QueryTypes, Sequelize, Transaction } from 'sequelize';
|
||||
import { blockSize, blockSlots } from './scheduleAudit';
|
||||
@@ -35,20 +36,29 @@ export async function stageSystemPlan(
|
||||
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 },
|
||||
const [pending] = await executionModel(db, 'CronSystemPlans')
|
||||
.findAll({
|
||||
attributes: ['id', 'confirmed', 'zone', 'rules'],
|
||||
order: [['id', 'DESC']],
|
||||
limit: 1,
|
||||
transaction,
|
||||
type: QueryTypes.SELECT,
|
||||
},
|
||||
);
|
||||
raw: true,
|
||||
})
|
||||
.then((rows) =>
|
||||
rows.filter(
|
||||
(row) =>
|
||||
row.confirmed === null &&
|
||||
row.zone === bind.zone &&
|
||||
row.rules === bind.rules,
|
||||
),
|
||||
);
|
||||
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 },
|
||||
);
|
||||
const [id] = await executionModel(db, 'CronSystemPlans')
|
||||
.create(
|
||||
{ staged: bind.at, zone: bind.zone, rules: bind.rules },
|
||||
{ transaction, silent: true, hooks: false, validate: false },
|
||||
)
|
||||
.then((row) => [row.id] as const);
|
||||
return id;
|
||||
},
|
||||
);
|
||||
@@ -70,9 +80,15 @@ export async function confirmSystemPlan(
|
||||
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 } },
|
||||
await executionModel(db, 'CronSystemPlans').update(
|
||||
{ confirmed: at },
|
||||
{
|
||||
where: { id, confirmed: null },
|
||||
transaction,
|
||||
silent: true,
|
||||
hooks: false,
|
||||
validate: false,
|
||||
},
|
||||
);
|
||||
// Keep the effective snapshot preceding the retention window.
|
||||
await db.query(
|
||||
@@ -94,10 +110,14 @@ export async function systemScheduleAudit(
|
||||
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 [retention] = await executionModel(db, 'CronHistoryRetention')
|
||||
.findAll({
|
||||
attributes: ['cutoff'],
|
||||
where: { id: 1 },
|
||||
transaction,
|
||||
raw: true,
|
||||
})
|
||||
.then(([row]) => [{ since: Math.max(dayStart, row?.cutoff ?? 0) }]);
|
||||
const since = retention.since;
|
||||
const installs = await db.query<Install>(
|
||||
`SELECT * FROM CronSystemPlans WHERE id >= COALESCE((SELECT MAX(id) FROM CronSystemPlans
|
||||
|
||||
Reference in new issue
Block a user