mirror of
https://github.com/whyour/qinglong.git
synced 2026-08-13 04:02:56 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c32a53c40b | ||
|
|
8ae7a85bf8 | ||
|
|
a56f2393a4 | ||
|
|
1124040eeb | ||
|
|
f043aa62e4 | ||
|
|
d64dc64df7 |
@@ -397,6 +397,21 @@ export function psTree(pid: number): Promise<number[]> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function isPidRunning(pid: number): boolean {
|
||||||
|
// Validate PID: must be a positive integer
|
||||||
|
if (pid == null || pid <= 0 || !Number.isInteger(pid)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Signal 0 doesn't kill the process, just checks if it exists
|
||||||
|
process.kill(pid, 0);
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function killTask(pid: number) {
|
export async function killTask(pid: number) {
|
||||||
const pids = await psTree(pid);
|
const pids = await psTree(pid);
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import { AuthDataType, SystemModel } from '../data/system';
|
|||||||
import SystemService from '../services/system';
|
import SystemService from '../services/system';
|
||||||
import UserService from '../services/user';
|
import UserService from '../services/user';
|
||||||
import { writeFile, readFile } from 'fs/promises';
|
import { writeFile, readFile } from 'fs/promises';
|
||||||
import { createRandomString, fileExist, safeJSONParse } from '../config/util';
|
import { createRandomString, fileExist, safeJSONParse, isPidRunning } from '../config/util';
|
||||||
import OpenService from '../services/open';
|
import OpenService from '../services/open';
|
||||||
import { shareStore } from '../shared/store';
|
import { shareStore } from '../shared/store';
|
||||||
import Logger from './logger';
|
import Logger from './logger';
|
||||||
@@ -132,7 +132,32 @@ export default async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// 初始化更新所有任务状态为空闲
|
// 初始化更新所有任务状态为空闲
|
||||||
await CrontabModel.update({ status: CrontabStatus.idle }, { where: {} });
|
// 但保留仍在运行的任务的状态
|
||||||
|
const allCrons = await CrontabModel.findAll({
|
||||||
|
attributes: ['id', 'pid'],
|
||||||
|
raw: true,
|
||||||
|
});
|
||||||
|
const idsToReset: number[] = [];
|
||||||
|
|
||||||
|
for (const cron of allCrons) {
|
||||||
|
// 如果任务有 PID 且进程仍在运行,则保持其状态
|
||||||
|
if (cron.pid != null && isPidRunning(cron.pid)) {
|
||||||
|
// 保留当前状态(running 或 queued)
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// 收集需要重置的任务 ID
|
||||||
|
if (cron.id) {
|
||||||
|
idsToReset.push(cron.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 批量更新所有需要重置的任务
|
||||||
|
if (idsToReset.length > 0) {
|
||||||
|
await CrontabModel.update(
|
||||||
|
{ status: CrontabStatus.idle, pid: undefined },
|
||||||
|
{ where: { id: { [Op.in]: idsToReset } } }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// 初始化时执行一次所有的 ql repo 任务
|
// 初始化时执行一次所有的 ql repo 任务
|
||||||
CrontabModel.findAll({
|
CrontabModel.findAll({
|
||||||
|
|||||||
+17
-97
@@ -218,108 +218,28 @@ export default class SystemService {
|
|||||||
...oDoc,
|
...oDoc,
|
||||||
info: { ...oDoc.info, ...info },
|
info: { ...oDoc.info, ...info },
|
||||||
});
|
});
|
||||||
|
let defaultDomain = 'https://dl-cdn.alpinelinux.org';
|
||||||
|
let targetDomain = 'https://dl-cdn.alpinelinux.org';
|
||||||
if (os.platform() !== 'linux') {
|
if (os.platform() !== 'linux') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const content = await fs.promises.readFile('/etc/apk/repositories', {
|
||||||
let command = '';
|
encoding: 'utf-8',
|
||||||
|
});
|
||||||
// Check if this is a Debian-based system (including Armbian)
|
const domainMatch = content.match(/(http.*)\/alpine\/.*/);
|
||||||
// Check for both sources.list and debian_version for more reliable detection
|
if (domainMatch) {
|
||||||
const hasAptSourcesList = await fs.promises.access('/etc/apt/sources.list')
|
defaultDomain = domainMatch[1];
|
||||||
.then(() => true)
|
|
||||||
.catch(() => false);
|
|
||||||
const hasDebianVersion = await fs.promises.access('/etc/debian_version')
|
|
||||||
.then(() => true)
|
|
||||||
.catch(() => false);
|
|
||||||
const isDebianBased = hasAptSourcesList || hasDebianVersion;
|
|
||||||
|
|
||||||
if (isDebianBased) {
|
|
||||||
// Handle Debian/Ubuntu/Armbian systems
|
|
||||||
let defaultDomain = '';
|
|
||||||
let targetDomain = info.linuxMirror || '';
|
|
||||||
|
|
||||||
try {
|
|
||||||
// Read the current sources.list
|
|
||||||
const content = await fs.promises.readFile('/etc/apt/sources.list', {
|
|
||||||
encoding: 'utf-8',
|
|
||||||
});
|
|
||||||
|
|
||||||
// Match the first deb line to extract the current mirror
|
|
||||||
// Note: This assumes all mirrors in sources.list use the same base URL
|
|
||||||
// If multiple different mirrors are configured, only the first one will be replaced
|
|
||||||
const debMatch = content.match(/^deb\s+(https?:\/\/[^\s]+)/m);
|
|
||||||
if (debMatch) {
|
|
||||||
defaultDomain = debMatch[1];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (defaultDomain && targetDomain) {
|
|
||||||
// Sanitize and escape special characters for sed
|
|
||||||
// Escape backslashes first, then other special characters
|
|
||||||
const escapedDefault = defaultDomain
|
|
||||||
.replace(/\\/g, '\\\\') // Escape backslashes first
|
|
||||||
.replace(/\//g, '\\/') // Escape forward slashes
|
|
||||||
.replace(/\./g, '\\.'); // Escape dots
|
|
||||||
const escapedTarget = targetDomain
|
|
||||||
.replace(/\\/g, '\\\\') // Escape backslashes first
|
|
||||||
.replace(/\//g, '\\/'); // Escape forward slashes
|
|
||||||
|
|
||||||
// Replace mirror URL in main sources.list
|
|
||||||
command = `sed -i 's/${escapedDefault}/${escapedTarget}/g' /etc/apt/sources.list`;
|
|
||||||
|
|
||||||
// Also update sources.list.d if it exists
|
|
||||||
command += ` && if [ -d /etc/apt/sources.list.d ]; then find /etc/apt/sources.list.d -type f \\( -name "*.list" -o -name "*.sources" \\) -exec sed -i 's/${escapedDefault}/${escapedTarget}/g' {} \\;; fi`;
|
|
||||||
|
|
||||||
// Update package lists
|
|
||||||
command += ` && apt-get update`;
|
|
||||||
} else if (!defaultDomain && targetDomain) {
|
|
||||||
// Cannot detect current mirror, log warning
|
|
||||||
this.logger.warn('Unable to detect current mirror from /etc/apt/sources.list. Mirror update skipped.');
|
|
||||||
this.sockService.sendMessage({
|
|
||||||
type: 'updateLinuxMirror',
|
|
||||||
message: 'Warning: Unable to detect current mirror. Please manually configure /etc/apt/sources.list',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
this.logger.error('Failed to read /etc/apt/sources.list', error);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Handle Alpine Linux systems
|
|
||||||
let defaultDomain = 'https://dl-cdn.alpinelinux.org';
|
|
||||||
let targetDomain = 'https://dl-cdn.alpinelinux.org';
|
|
||||||
|
|
||||||
try {
|
|
||||||
const content = await fs.promises.readFile('/etc/apk/repositories', {
|
|
||||||
encoding: 'utf-8',
|
|
||||||
});
|
|
||||||
const domainMatch = content.match(/(http.*)\/alpine\/.*/);
|
|
||||||
if (domainMatch) {
|
|
||||||
defaultDomain = domainMatch[1];
|
|
||||||
}
|
|
||||||
if (info.linuxMirror) {
|
|
||||||
targetDomain = info.linuxMirror;
|
|
||||||
}
|
|
||||||
// Sanitize and escape special characters for sed
|
|
||||||
// Escape backslashes first, then other special characters
|
|
||||||
command = `sed -i 's/${defaultDomain
|
|
||||||
.replace(/\\/g, '\\\\') // Escape backslashes first
|
|
||||||
.replace(/\//g, '\\/') // Escape forward slashes
|
|
||||||
.replace(/\./g, '\\.')}/${targetDomain
|
|
||||||
.replace(/\\/g, '\\\\') // Escape backslashes first
|
|
||||||
.replace(/\//g, '\\/')}/g' /etc/apk/repositories && apk update -f`;
|
|
||||||
} catch (error) {
|
|
||||||
this.logger.error('Failed to read /etc/apk/repositories', error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
if (info.linuxMirror) {
|
||||||
if (!command) {
|
targetDomain = info.linuxMirror;
|
||||||
this.sockService.sendMessage({
|
|
||||||
type: 'updateLinuxMirror',
|
|
||||||
message: 'No supported package manager found or mirror not configured',
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
const command = `sed -i 's/${defaultDomain.replace(
|
||||||
|
/\//g,
|
||||||
|
'\\/',
|
||||||
|
)}/${targetDomain.replace(
|
||||||
|
/\//g,
|
||||||
|
'\\/',
|
||||||
|
)}/g' /etc/apk/repositories && apk update -f`;
|
||||||
|
|
||||||
this.scheduleService.runTask(
|
this.scheduleService.runTask(
|
||||||
command,
|
command,
|
||||||
|
|||||||
Reference in New Issue
Block a user