koishi-plugin-docker-control 0.1.6 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -70,21 +70,39 @@ class MonitorManager {
70
70
  logger_1.monitorLogger.debug(`[${node.name}] ${containerName} 收到 ${action},但已在等待停止确认中 (忽略)`);
71
71
  return;
72
72
  }
73
- logger_1.monitorLogger.debug(`[${node.name}] ${containerName} 已停止 (${action}),等待 ${debounceWait}ms...`);
74
- state.stopTimer = setTimeout(() => {
73
+ logger_1.monitorLogger.debug(`[${node.name}] ${containerName} 已停止 (${action}),等待 ${debounceWait}ms 后检查容器状态...`);
74
+ state.stopTimer = setTimeout(async () => {
75
75
  state.stopTimer = undefined;
76
+ // 检查是否有同名容器正在运行(容器升级场景)
77
+ const hasRunningContainer = await checkIfContainerRunning(node, containerName);
78
+ if (hasRunningContainer) {
79
+ // 有同名容器正在运行,说明是容器升级
80
+ logger_1.monitorLogger.info(`[${node.name}] ${containerName} 容器升级:检测到同名容器正在运行`);
81
+ this.emit({
82
+ eventType: 'container.upgraded',
83
+ action: 'upgraded',
84
+ nodeId: node.id,
85
+ nodeName: node.name,
86
+ containerId,
87
+ containerName,
88
+ timestamp: Date.now()
89
+ });
90
+ }
91
+ else {
92
+ // 没有同名容器运行,说明是真正的异常退出
93
+ logger_1.monitorLogger.info(`[${node.name}] ${containerName} 容器异常退出:未检测到同名容器运行`);
94
+ this.emit({
95
+ eventType: `container.die`, // 统一使用 die
96
+ action: 'die',
97
+ nodeId: node.id,
98
+ nodeName: node.name,
99
+ containerId,
100
+ containerName,
101
+ timestamp: Date.now()
102
+ });
103
+ }
76
104
  // 清理名称索引
77
105
  this.clearNameIndex(node.id, containerName);
78
- // 只有定时器真正走完了,才发送通知
79
- this.emit({
80
- eventType: `container.die`, // 统一使用 die
81
- action: 'die',
82
- nodeId: node.id,
83
- nodeName: node.name,
84
- containerId,
85
- containerName,
86
- timestamp: Date.now()
87
- });
88
106
  }, debounceWait);
89
107
  }
90
108
  else if (action === 'start' || action === 'restart') {
@@ -190,3 +208,21 @@ class MonitorManager {
190
208
  }
191
209
  }
192
210
  exports.MonitorManager = MonitorManager;
211
+ /**
212
+ * 检查节点上是否有指定名称的容器正在运行
213
+ * @param node Docker 节点
214
+ * @param containerName 容器名称
215
+ * @returns 是否有同名容器正在运行
216
+ */
217
+ async function checkIfContainerRunning(node, containerName) {
218
+ try {
219
+ // 只检查运行中的容器
220
+ const containers = await node.listContainers(false);
221
+ // 检查是否有同名容器(容器名称格式可能为 /name 或 name)
222
+ return containers.some(c => c.Names.some(n => n === `/${containerName}` || n === containerName));
223
+ }
224
+ catch (e) {
225
+ logger_1.monitorLogger.warn(`[${node.name}] 检查容器运行状态失败: ${e}`);
226
+ return false;
227
+ }
228
+ }
@@ -11,6 +11,7 @@ const EVENT_TEMPLATES = {
11
11
  'container.stop': '容器已停止',
12
12
  'container.restart': '容器已重启',
13
13
  'container.die': '容器已异常退出',
14
+ 'container.upgraded': '容器已升级',
14
15
  'container.create': '容器已创建',
15
16
  'container.destroy': '容器已销毁',
16
17
  // 健康检查
@@ -51,6 +52,7 @@ const EVENT_LEVEL = {
51
52
  'container.stop': 'info',
52
53
  'container.restart': 'info',
53
54
  'container.die': 'error',
55
+ 'container.upgraded': 'info',
54
56
  'container.health_status': 'warning',
55
57
  'node.online': 'info',
56
58
  'node.offline': 'warning',
package/lib/types.d.ts CHANGED
@@ -27,7 +27,7 @@ export interface NotificationConfig {
27
27
  targetGroups: string[];
28
28
  events: NotificationEventType[];
29
29
  }
30
- export type NotificationEventType = 'container.start' | 'container.stop' | 'container.restart' | 'container.die' | 'container.health_status' | 'node.online' | 'node.offline' | 'node.error';
30
+ export type NotificationEventType = 'container.start' | 'container.stop' | 'container.restart' | 'container.die' | 'container.upgraded' | 'container.health_status' | 'node.online' | 'node.offline' | 'node.error';
31
31
  export interface MonitorConfig {
32
32
  debounceWait: number;
33
33
  flappingWindow: number;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-docker-control",
3
3
  "description": "Koishi 插件 - 通过 SSH 控制 Docker 容器 (支持连接池、缓存、权限控制、审计日志)",
4
- "version": "0.1.6",
4
+ "version": "0.1.7",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [
@@ -30,13 +30,13 @@
30
30
  "koishi": "4.18.10"
31
31
  },
32
32
  "dependencies": {
33
- "ssh2": "^1.17.0",
33
+ "dockerode": "^4.0.2",
34
34
  "puppeteer": "^21.0.0",
35
- "dockerode": "^4.0.2"
35
+ "ssh2": "^1.17.0"
36
36
  },
37
37
  "devDependencies": {
38
38
  "@types/node": "^20.19.27",
39
39
  "@types/ssh2": "^1.15.5",
40
40
  "typescript": "^5.9.3"
41
41
  }
42
- }
42
+ }