Skip to content
This repository was archived by the owner on Mar 14, 2025. It is now read-only.

Commit 04e3613

Browse files
author
ZeroWolf233
committed
feat: 超大更新:
src/cluster.ts: 1.跳过GC文件回收 2.在获取云端文件列表后跳过同步,保留正常统计 建议删除在获取云端文件列表前跳过同步 没有云端文件统计且 可能存在未知问题; src/bootstrap 1.跳过GC文件回收 2.同步间隔修改 storage/webdav.storage 1.修复了423 locked解决方案遗漏的配置 src/keepalive.ts 1.汉化 2.修正变量名 README.md 1.新增变量: FORCE_SKIP_SYNC(原SKIP_SYNC) SKIP_SYNC(智能跳过检查和同步) SKIP_GC(跳过垃圾回收) THREADS(同步线程)
1 parent b45eb02 commit 04e3613

5 files changed

Lines changed: 62 additions & 40 deletions

File tree

README.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ OpenMCIM是对外开放的,所有需要 Minecraft Mod 资源的启动器均可
1111

1212
[OpenMCIM 文件分发相关](https://github.com/mcmod-info-mirror/mcim/issues/91)
1313

14-
## 配置
14+
## 变量
1515

1616
| 环境变量 |必填 | 默认值 | 说明 |
1717
|---------------------|-----|--------------|--------------------------------------------------------------------------------------------------------|
@@ -28,7 +28,10 @@ OpenMCIM是对外开放的,所有需要 Minecraft Mod 资源的启动器均可
2828
| CLUSTER_STORAGE || files | 使用其他存储源的类型(默认为本地) |
2929
| CLUSTER_STORAGE_OPTIONS ||| 挂载其他存储源的配置项 |
3030
| SKIP_FILE_SHA_CHECK || false | 防止主控SHA爆炸,强制忽略SHA问题上线 |
31-
| SKIP_SYNC || false | 测试用,跳过同步 |
31+
| SKIP_SYNC || false | 智能跳过同步(由 **千时雨** 提供) |
32+
| FORCE_SKIP_SYNC || false | 强制跳过所有同步(不推荐) |
33+
| SKIP_GC || false | 跳过GC垃圾自动回收(由 **千时雨** 提供) |
34+
| THREADS || 由主控分配 | 同步线程(改太高会被banban) |
3235

3336
### 如果你在源码中发现了其他环境变量, 那么它们是为了方便开发而存在的, 可能会随时修改, 不要在生产环境中使用!
3437

@@ -120,17 +123,12 @@ node dist/index.js
120123
```env
121124
CLUSTER_ID=你的节点ID
122125
CLUSTER_SECRET=你的节点密钥
123-
CLUSTER_PUBLIC_PORT=你的对外开放端口(用户请求时访问)
124-
CLUSTER_PORT=你的本地开放端口
125-
CLUSTER_STORAGE=存储类型
126-
CLUSTER_STORAGE_OPTIONS=存储配置项(请参考上方Alist配置)
127-
SKIP_FILE_SHA_CHECK=true或false(参考上方ENV配置项)
126+
CLUSTER_PORT=你的开放端口
127+
# 更多变量请看上方变量的详细解释
128128
```
129129

130130
如果配置无误的话, 运行程序, 就会开始拉取文件, 拉取完成后就会开始等待服务器分发请求了!
131131

132-
133-
134132
## 致谢
135133

136134
- [**bangbang93**](https://github.com/bangbang93) 基本全是照着bangbang93的源码改的,总之谢谢93!

src/bootstrap.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,20 @@ export async function bootstrap(version: string): Promise<void> {
1818
logger.info(colors.green(`正在启动 OpenMCIM ${version}`))
1919
const tokenManager = new TokenManager(config.clusterId, config.clusterSecret, version)
2020
await tokenManager.getToken()
21-
const skipsync = config.skipsync ?? false;// 获取跳过同步布尔值,默认false
2221
const skipfileshacheck = config.skipfileshacheck ?? false;// 获取跳过文件校验布尔值,默认false
23-
const cluster = new Cluster(config.clusterSecret, version, tokenManager, skipfileshacheck, skipsync)
22+
const cluster = new Cluster(config.clusterSecret, version, tokenManager, skipfileshacheck)
2423
await cluster.init()
2524

2625
const storageReady = await cluster.storage.check()
26+
if (process.env.FORCE_SKIP_SYNC) {
27+
logger.info('已强制跳过存储检查')
28+
}
2729
if (!storageReady) {
28-
throw new Error('存储异常')
30+
if(process.env.SKIP_SYNC){
31+
logger.info('存储异常,已跳过存储检查');
32+
}else{
33+
throw new Error('存储异常');
34+
}
2935
}
3036

3137
const configuration = await cluster.getConfiguration()
@@ -40,7 +46,11 @@ export async function bootstrap(version: string): Promise<void> {
4046
throw e
4147
}
4248
logger.info('回收文件')
43-
cluster.gcBackground(files)
49+
if (process.env.SKIP_GC) {
50+
logger.info('已跳过文件回收')
51+
}else{
52+
cluster.gcBackground(files)
53+
}
4454

4555
cluster.connect()
4656
const proto = config.byoc ? 'http' : 'https'
@@ -52,7 +62,7 @@ export async function bootstrap(version: string): Promise<void> {
5262
if (typeof cluster.port === 'number') {
5363
await cluster.setupNginx(join(__dirname, '..'), cluster.port, proto)
5464
} else {
55-
throw new Error('cluster.port is not a number')
65+
throw new Error('端口号不是一个数字')
5666
}
5767
}
5868
const server = cluster.setupExpress(proto === 'https' && !config.enableNginx)
@@ -62,7 +72,7 @@ export async function bootstrap(version: string): Promise<void> {
6272
await cluster.listen()
6373
await cluster.enable()
6474

65-
logger.info(colors.rainbow(`完成, 正在提供 ${files.files.length} 个文件`))
75+
logger.info(colors.rainbow(`启动完成, 正在提供 ${files.files.length} 个文件`))
6676
if (nodeCluster.isWorker && typeof process.send === 'function') {
6777
process.send('ready')
6878
}
@@ -72,7 +82,7 @@ export async function bootstrap(version: string): Promise<void> {
7282
console.error('检查文件错误')
7383
console.error(e)
7484
})
75-
}, ms('10m'))
85+
}, ms('60m'))
7686
} catch (e) {
7787
logger.fatal(e)
7888
if (process.env.NODE_ENV === 'development') {
@@ -100,7 +110,7 @@ export async function bootstrap(version: string): Promise<void> {
100110
console.error('检查文件错误')
101111
console.error(e)
102112
})
103-
}, ms('10m'))
113+
}, ms('60m'))
104114
}
105115
}
106116

src/cluster.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ export class Cluster {
7777
private readonly version: string,
7878
private readonly tokenManager: TokenManager,
7979
private readonly skipfileshacheck: boolean,
80-
private readonly skipsync: boolean,
8180
) {
8281
this.host = config.clusterIp
8382
this._port = config.port
@@ -169,13 +168,17 @@ export class Cluster {
169168
public async syncFiles(fileList: IFileList, syncConfig: OpenbmclapiAgentConfiguration['sync']): Promise<void> {
170169
const storageReady = await this.storage.check()
171170
if (!storageReady) {
171+
if (process.env.SKIP_SYNC) {
172+
logger.info('存储异常,已跳过存储检查')
173+
}else{
172174
throw new Error('存储异常')
175+
}
173176
}
174-
if (this.skipsync) {
175-
logger.info('已跳过文件同步,在保活时您将无法看到保活的文件大小')
177+
if (process.env.FORCE_SKIP_SYNC) {
178+
logger.info('已强制跳过文件同步,在保活时您将无法看到保活的文件大小')
176179
return
177180
}
178-
logger.info('正在检查缺失文件')
181+
logger.info('正在检查缺失文件')
179182
const missingFiles = await this.storage.getMissingFiles(fileList.files)
180183
if (missingFiles.length === 0) {
181184
return
@@ -188,7 +191,8 @@ export class Cluster {
188191
notTTYSchedule: ms('10s'),
189192
})
190193
const totalBar = multibar.create(missingFiles.length, 0, {filename: '总文件数'})
191-
const parallel = syncConfig.concurrency
194+
const parallel = Number(process.env.THREADS) ?? syncConfig.concurrency
195+
logger.info('您当前的下载线程:' + {parallel})
192196
let hasError = false
193197
await pMap(
194198
missingFiles,
@@ -534,18 +538,23 @@ export class Cluster {
534538
}
535539

536540
public gcBackground(files: IFileList): void {
537-
this.storage
538-
.gc(files.files)
539-
.then((res) => {
540-
if (res.count === 0) {
541-
logger.info('没有过期文件')
542-
} else {
543-
logger.info(`文件回收完成,共删除${res.count}个文件,释放空间${prettyBytes(res.size)}, that's ♂ good`)
544-
}
545-
})
546-
.catch((e: unknown) => {
547-
logger.error({err: e}, 'gc错误')
548-
})
541+
if (process.env.SKIP_GC) {
542+
logger.info('已跳过文件回收(谨防硬盘挤爆)');
543+
return;
544+
}else{
545+
this.storage
546+
.gc(files.files)
547+
.then((res) => {
548+
if (res.count === 0) {
549+
logger.info('没有过期文件')
550+
} else {
551+
logger.info(`文件回收完成,共删除${res.count}个文件,释放空间${prettyBytes(res.size)}, that's ♂ good`)
552+
}
553+
})
554+
.catch((e: unknown) => {
555+
logger.error({err: e}, 'gc错误')
556+
})
557+
}
549558
}
550559

551560
private async _enable(): Promise<void> {

src/keepalive.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class Keepalive {
5151
this.keepAliveError = 0
5252
} catch (e) {
5353
this.keepAliveError++
54-
logger.error(e, 'keep alive error')
54+
logger.error(e, '保活错误')
5555
if (this.keepAliveError >= 3) {
5656
await this.restart()
5757
}
@@ -74,9 +74,9 @@ export class Keepalive {
7474
...counters,
7575
})) as [object, unknown]
7676

77-
if (err) throw new Error('keep alive error', {cause: err})
77+
if (err) throw new Error('保活错误', {cause: err})
7878
const bytes = prettyBytes(counters.bytes, {binary: true})
79-
if (process.env.SKIP_SYNC) {
79+
if (process.env.FORCE_SKIP_SYNC) {
8080
logger.info(`保活成功,上传了 ${counters.hits} 个文件`)
8181
} else {
8282
logger.info(`保活成功,上传了 ${counters.hits} 个文件,总共${bytes}`)
@@ -92,7 +92,7 @@ export class Keepalive {
9292
this.cluster.connect()
9393
await this.cluster.enable()
9494
})
95-
.timeout(ms('10m'), 'restart timeout')
95+
.timeout(ms('10m'), '重启超时')
9696
.catch((e) => {
9797
logger.error(e, '重启失败')
9898
this.cluster.exit(1)

src/storage/webdav.storage.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,13 @@ export class WebdavStorage implements IStorage {
6363
await this.client.putFileContents(join(this.basePath, '.check'), Buffer.from(Date.now().toString()))
6464
return true
6565
} catch (e) {
66-
logger.error(e, '存储检查异常')
67-
return false
66+
if (process.env.SKIP_SYNC){
67+
logger.info('跳过存储可用性检查')
68+
return true;
69+
}else{
70+
logger.error(e, '存储检查异常')
71+
return false
72+
}
6873
} finally {
6974
try {
7075
await this.client.deleteFile(join(this.basePath, '.check'))

0 commit comments

Comments
 (0)