channel-worker 2.5.91 → 2.5.92
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.
- package/bin/cli.js +36 -0
- package/lib/updater.js +42 -0
- package/package.json +1 -1
package/bin/cli.js
CHANGED
|
@@ -201,6 +201,42 @@ if (cmd === 'pair') {
|
|
|
201
201
|
process.exit(0);
|
|
202
202
|
}
|
|
203
203
|
|
|
204
|
+
} else if (cmd === 'selftest') {
|
|
205
|
+
// Nạp thử MỌI module trong lib/ rồi thoát 0. Bộ tự-cập-nhật chạy lệnh này
|
|
206
|
+
// sau khi cài và chỉ khởi động lại khi nó xanh.
|
|
207
|
+
//
|
|
208
|
+
// Vì sao cần: 08/09/2026, một bản npm publish làm mọi daemon tự lao vào
|
|
209
|
+
// `npm install -g` ĐÈ LÊN CHÍNH MODULE ĐANG CHẠY. Trên Windows npm xoá cây
|
|
210
|
+
// cũ trước khi ghi cây mới, và lượt đó dừng giữa chừng: lib/daemon.js và
|
|
211
|
+
// lib/command-poller.js biến mất, package.json vẫn ghi version mới nên nhìn
|
|
212
|
+
// vào đó thấy "cài xong". Daemon thay thế chết ngay MODULE_NOT_FOUND, hai
|
|
213
|
+
// máy Windows tắt lịm ĐÚNG LÚC đang đăng 9 video — 9 lượt đăng thành "mập
|
|
214
|
+
// mờ" phải kiểm tay. Đọc version là không đủ; phải nạp thử thật.
|
|
215
|
+
const fs = require('fs');
|
|
216
|
+
const libDir = path.join(__dirname, '..', 'lib');
|
|
217
|
+
const bad = [];
|
|
218
|
+
let files = [];
|
|
219
|
+
try {
|
|
220
|
+
files = fs.readdirSync(libDir).filter((f) => f.endsWith('.js')).sort();
|
|
221
|
+
} catch (e) {
|
|
222
|
+
console.error(`[channel-worker] selftest FAILED — không đọc nổi ${libDir}: ${e.code || e.message}`);
|
|
223
|
+
process.exit(1);
|
|
224
|
+
}
|
|
225
|
+
if (files.length < 10) {
|
|
226
|
+
// Cây đủ có 15 file; ít hơn 10 là chắc chắn cụt, không cần require từng cái.
|
|
227
|
+
console.error(`[channel-worker] selftest FAILED — lib/ chỉ còn ${files.length} file (cây cài dở).`);
|
|
228
|
+
process.exit(1);
|
|
229
|
+
}
|
|
230
|
+
for (const f of files) {
|
|
231
|
+
try { require(path.join(libDir, f)); } catch (e) { bad.push(`${f}: ${e.code || e.message}`); }
|
|
232
|
+
}
|
|
233
|
+
if (bad.length) {
|
|
234
|
+
console.error(`[channel-worker] selftest FAILED — ${bad.length}/${files.length} module không nạp được:\n ${bad.join('\n ')}`);
|
|
235
|
+
process.exit(1);
|
|
236
|
+
}
|
|
237
|
+
console.log(`[channel-worker] selftest OK — v${require('../package.json').version}, ${files.length} module nạp được.`);
|
|
238
|
+
process.exit(0);
|
|
239
|
+
|
|
204
240
|
} else if (cmd === 'update') {
|
|
205
241
|
const { checkAndUpdate, getLocalVersion } = require('../lib/updater');
|
|
206
242
|
console.log(`[channel-worker] Current version: ${getLocalVersion()}`);
|
package/lib/updater.js
CHANGED
|
@@ -31,6 +31,28 @@ function installUpdate(version) {
|
|
|
31
31
|
console.log(`[updater] Installed ${PKG_NAME}@${version}`);
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
+
/**
|
|
35
|
+
* Cây vừa cài có nạp được không? `channel-worker selftest` require từng file
|
|
36
|
+
* trong lib/ và thoát 0 nếu đủ.
|
|
37
|
+
*
|
|
38
|
+
* npm cài ĐÈ LÊN module đang chạy, và trên Windows nó xoá cây cũ trước khi ghi
|
|
39
|
+
* cây mới. Lượt cài dừng giữa chừng để lại package.json version MỚI cạnh một
|
|
40
|
+
* lib/ THIẾU FILE — nhìn version thì thấy "cài xong". 08/09/2026 daemon thay
|
|
41
|
+
* thế chết ngay MODULE_NOT_FOUND (lib/daemon.js + lib/command-poller.js biến
|
|
42
|
+
* mất), hai máy Windows tắt giữa lúc đăng 9 video.
|
|
43
|
+
*/
|
|
44
|
+
function verifyInstall() {
|
|
45
|
+
const { spawnSync } = require('child_process');
|
|
46
|
+
const isWindows = process.platform === 'win32';
|
|
47
|
+
const bin = isWindows ? 'channel-worker.cmd' : 'channel-worker';
|
|
48
|
+
const out = spawnSync(bin, ['selftest'], {
|
|
49
|
+
shell: isWindows, windowsHide: true, encoding: 'utf8', timeout: 120000,
|
|
50
|
+
});
|
|
51
|
+
// status null = không chạy nổi (timeout, binary không thấy) → coi như hỏng.
|
|
52
|
+
const detail = `${out.stdout || ''}${out.stderr || ''}`.trim().split('\n').slice(-6).join(' | ');
|
|
53
|
+
return { ok: out.status === 0, detail: detail.slice(-600) };
|
|
54
|
+
}
|
|
55
|
+
|
|
34
56
|
async function checkAndUpdate({ autoRestart = false } = {}) {
|
|
35
57
|
const local = getLocalVersion();
|
|
36
58
|
const latest = await getLatestVersion();
|
|
@@ -42,6 +64,26 @@ async function checkAndUpdate({ autoRestart = false } = {}) {
|
|
|
42
64
|
console.log(`[updater] Update available: ${local} → ${latest}`);
|
|
43
65
|
installUpdate(latest);
|
|
44
66
|
|
|
67
|
+
// Cài xong CHƯA CHẮC chạy được — xem verifyInstall(). Hỏng thì thử cài lại
|
|
68
|
+
// đúng một lần, vẫn hỏng thì Ở YÊN: tiến trình này vẫn đang chạy code cũ đã
|
|
69
|
+
// nạp trong bộ nhớ, nên không restart là còn worker; restart vào cây cụt là
|
|
70
|
+
// mất worker. Bản mới sẽ vào ở lượt kiểm kế tiếp, hoặc do người cài tay.
|
|
71
|
+
let check = verifyInstall();
|
|
72
|
+
if (!check.ok) {
|
|
73
|
+
console.warn(`[updater] Bản vừa cài KHÔNG nạp được (${check.detail}) — cài lại một lần.`);
|
|
74
|
+
try {
|
|
75
|
+
installUpdate(latest);
|
|
76
|
+
check = verifyInstall();
|
|
77
|
+
} catch (e) {
|
|
78
|
+
check = { ok: false, detail: `cài lại lỗi: ${e.message}` };
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (!check.ok) {
|
|
82
|
+
console.error('[updater] BỎ khởi động lại: cây cài dở, daemon cũ chạy tiếp. '
|
|
83
|
+
+ `Cần cài tay: npm i -g ${PKG_NAME}@${latest} --prefer-online (dừng daemon trước). Chi tiết: ${check.detail}`);
|
|
84
|
+
return { updated: false, local, latest, verifyFailed: true, detail: check.detail };
|
|
85
|
+
}
|
|
86
|
+
|
|
45
87
|
if (autoRestart) {
|
|
46
88
|
console.log('[updater] Restarting daemon with new version...');
|
|
47
89
|
const { spawn } = require('child_process');
|