@@ -6,13 +6,26 @@ import * as fs from "fs";
66import * as path from "path" ;
77import * as crypto from "crypto" ;
88import * as os from "os" ;
9- import { spawn } from "child_process" ;
9+ import { spawn , type ChildProcess } from "child_process" ;
1010import { Low } from "lowdb" ;
1111import { JSONFile } from "lowdb/node" ;
1212import { getPrefixes } from "@utils/pluginManager" ;
1313
1414const prefixes = getPrefixes ( ) ;
1515const mainPrefix = prefixes [ 0 ] ;
16+ const activeChildProcesses = new Set < ChildProcess > ( ) ;
17+
18+ function trackChildProcess < T extends ChildProcess > ( child : T ) : T {
19+ const release = ( ) => {
20+ activeChildProcesses . delete ( child ) ;
21+ } ;
22+
23+ activeChildProcesses . add ( child ) ;
24+ child . once ( "close" , release ) ;
25+ child . once ( "exit" , release ) ;
26+ child . once ( "error" , release ) ;
27+ return child ;
28+ }
1629
1730// 时区设置
1831const CN_TIME_ZONE = "Asia/Shanghai" ;
@@ -128,6 +141,10 @@ class ConfigManager {
128141 await this . setTargets ( filtered ) ;
129142 return filtered ;
130143 }
144+
145+ static cleanup ( ) : void {
146+ this . db = null ;
147+ }
131148}
132149
133150// 工具函数
@@ -169,13 +186,13 @@ async function createBackup(dirs: string[], outputPath: string): Promise<void> {
169186
170187 // 创建tar.gz
171188 await new Promise < void > ( ( resolve , reject ) => {
172- const tar = spawn ( "tar" , [
189+ const tar = trackChildProcess ( spawn ( "tar" , [
173190 "-czf" ,
174191 outputPath ,
175192 "-C" ,
176193 tempDir ,
177194 "telebox_backup" ,
178- ] ) ;
195+ ] ) ) ;
179196
180197 tar . on ( "close" , ( code ) => {
181198 if ( code === 0 ) resolve ( ) ;
@@ -216,7 +233,7 @@ async function extractBackup(archivePath: string): Promise<string> {
216233 fs . mkdirSync ( extractDir , { recursive : true } ) ;
217234
218235 await new Promise < void > ( ( resolve , reject ) => {
219- const tar = spawn ( "tar" , [ "-xzf" , archivePath , "-C" , extractDir ] ) ;
236+ const tar = trackChildProcess ( spawn ( "tar" , [ "-xzf" , archivePath , "-C" , extractDir ] ) ) ;
220237
221238 tar . on ( "close" , ( code ) => {
222239 if ( code === 0 ) resolve ( ) ;
@@ -279,6 +296,13 @@ const help_text = `<code>${mainPrefix}bf</code> 备份 plugins + assets 目录
279296// 插件类
280297class BfPlugin extends Plugin {
281298 cleanup ( ) : void {
299+ for ( const child of activeChildProcesses ) {
300+ try {
301+ child . kill ( "SIGTERM" ) ;
302+ } catch { }
303+ }
304+ activeChildProcesses . clear ( ) ;
305+ ConfigManager . cleanup ( ) ;
282306 }
283307
284308 description = `\n📦 备份插件\n\n${ help_text }
@@ -404,7 +428,7 @@ class BfPlugin extends Plugin {
404428 const dirName = path . basename ( programDir ) ;
405429
406430 await new Promise < void > ( ( resolve , reject ) => {
407- const tar = spawn ( "tar" , [
431+ const tar = trackChildProcess ( spawn ( "tar" , [
408432 "-cf" ,
409433 "-" ,
410434 "-C" ,
@@ -415,9 +439,9 @@ class BfPlugin extends Plugin {
415439 "--exclude=temp" ,
416440 "--exclude=logs" ,
417441 dirName ,
418- ] , { stdio : [ "pipe" , "pipe" , "pipe" ] } ) ;
442+ ] , { stdio : [ "pipe" , "pipe" , "pipe" ] } ) ) ;
419443
420- const gzip = spawn ( "gzip" , [ "-1" ] , { stdio : [ "pipe" , "pipe" , "pipe" ] } ) ;
444+ const gzip = trackChildProcess ( spawn ( "gzip" , [ "-1" ] , { stdio : [ "pipe" , "pipe" , "pipe" ] } ) ) ;
421445
422446 const output = fs . createWriteStream ( backupPath ) ;
423447
0 commit comments