mirror of
https://github.com/whyour/qinglong.git
synced 2026-11-30 16:27:21 +08:00
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.
149 lines
5.1 KiB
TypeScript
149 lines
5.1 KiB
TypeScript
// A single, disposable backend process. Resolve startup dependencies, but never
|
|
// run appLoader: its initData/initTask hooks install dependencies and start user work.
|
|
import 'reflect-metadata';
|
|
import dns from 'dns';
|
|
import http from 'http';
|
|
|
|
// The first scheduler connection can outlive its short RPC deadline. Let the
|
|
// normal single-flight recovery finish within the supervised startup budget.
|
|
async function waitForUpgradeScheduler(
|
|
readiness: { recover(): Promise<boolean> },
|
|
timeoutMs: number,
|
|
retryMs = 250,
|
|
) {
|
|
const deadline = Date.now() + timeoutMs;
|
|
do {
|
|
if (await readiness.recover()) return;
|
|
const remaining = deadline - Date.now();
|
|
if (remaining <= 0) break;
|
|
await new Promise((resolve) =>
|
|
setTimeout(resolve, Math.min(retryMs, remaining)),
|
|
);
|
|
} while (Date.now() < deadline);
|
|
throw new Error('Upgrade scheduler readiness failed');
|
|
}
|
|
|
|
async function probeUpgrade() {
|
|
if (!process.send || process.env.QL_UPGRADE_PROBE !== '1')
|
|
throw new Error('Upgrade probe requires its isolated supervisor');
|
|
dns.setDefaultResultOrder('ipv4first');
|
|
const { default: config } = await import('../config');
|
|
const { sequelize } = await import('../data');
|
|
const { default: loadDatabase } = await import('../loaders/db');
|
|
const { Container } = await import('typedi');
|
|
const { GrpcServerService } = await import('../services/grpc');
|
|
const { HttpServerService } = await import('../services/http');
|
|
const { createHttpApplication } = await import('../loaders/httpApplication');
|
|
const { default: loadServer } = await import('../loaders/server');
|
|
const { default: loadExpress } = await import('../loaders/express');
|
|
await import('../loaders/app'); // Resolve normal startup hooks without running them.
|
|
const { default: inject } = await import('../loaders/depInjector');
|
|
const grpc = Container.get(GrpcServerService);
|
|
const server = Container.get(HttpServerService);
|
|
try {
|
|
await loadDatabase();
|
|
// Exercise an actual on-disk transaction as well as schema initialization.
|
|
const { DataTypes } = await import('sequelize');
|
|
const probe = sequelize.define<
|
|
import('sequelize').Model<{ value: number }>
|
|
>(
|
|
'UpgradeProbe',
|
|
{ value: DataTypes.INTEGER },
|
|
{ tableName: 'upgrade_probe', timestamps: false },
|
|
);
|
|
probe.removeAttribute('id');
|
|
await sequelize.transaction(async (transaction) => {
|
|
await sequelize
|
|
.getQueryInterface()
|
|
.createTable(
|
|
'upgrade_probe',
|
|
{ value: DataTypes.INTEGER },
|
|
{ transaction },
|
|
);
|
|
await probe.create({ value: 42 }, { transaction });
|
|
const row = await probe.findOne({ attributes: ['value'], transaction });
|
|
if (row?.get('value') !== 42)
|
|
throw new Error('Upgrade database readback failed');
|
|
});
|
|
inject();
|
|
config.grpcPort = await grpc.initialize();
|
|
const { default: cronClient } = await import('../schedule/client');
|
|
cronClient.readiness.configure(async () => {});
|
|
await waitForUpgradeScheduler(
|
|
cronClient.readiness,
|
|
process.arch === 'arm' &&
|
|
Number(
|
|
(process.config.variables as { arm_version?: string | number })
|
|
.arm_version,
|
|
) === 6
|
|
? 60000
|
|
: 10000,
|
|
);
|
|
const app = await createHttpApplication();
|
|
loadExpress({ app });
|
|
const listener = await server.initialize(app, 0);
|
|
await loadServer({ server: listener });
|
|
const address = listener.address();
|
|
if (!address || typeof address === 'string')
|
|
throw new Error('No probe HTTP port');
|
|
const health = await new Promise<any>((resolve, reject) => {
|
|
const request = http.get(
|
|
`http://127.0.0.1:${address.port}/api/health`,
|
|
(response) => {
|
|
let body = '';
|
|
response.on('data', (chunk) => {
|
|
body += chunk;
|
|
});
|
|
response.on('error', reject);
|
|
response.on('end', () => {
|
|
try {
|
|
const result = JSON.parse(body);
|
|
if (
|
|
response.statusCode !== 200 ||
|
|
result.code !== 200 ||
|
|
result.data?.status !== 'ok' ||
|
|
!result.data.services?.grpc ||
|
|
!result.data.services?.http
|
|
)
|
|
throw new Error('Upgrade HTTP/gRPC health check failed');
|
|
resolve(result.data);
|
|
} catch (error) {
|
|
reject(error);
|
|
}
|
|
});
|
|
},
|
|
);
|
|
request.setTimeout(10000, () =>
|
|
request.destroy(new Error('Upgrade health request timed out')),
|
|
);
|
|
request.on('error', reject);
|
|
});
|
|
process.send!({
|
|
type: 'upgrade-probe',
|
|
ok: true,
|
|
build: health.build,
|
|
memory: process.memoryUsage(),
|
|
pid: process.pid,
|
|
});
|
|
} finally {
|
|
await server.shutdown();
|
|
grpc.getServer().forceShutdown();
|
|
await sequelize.close();
|
|
}
|
|
}
|
|
|
|
process.once('disconnect', () => process.exit(1));
|
|
probeUpgrade()
|
|
.then(() => process.exit(0))
|
|
.catch((error) => {
|
|
process.send?.({
|
|
type: 'upgrade-probe',
|
|
ok: false,
|
|
code: error.code,
|
|
message: error.message,
|
|
stack: error.stack,
|
|
});
|
|
console.error(error);
|
|
process.exit(1);
|
|
});
|