channel-worker 1.1.9 → 1.2.0
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 +20 -1
- package/lib/daemon.js +9 -1
- package/lib/updater.js +93 -0
- package/package.json +1 -1
package/bin/cli.js
CHANGED
|
@@ -128,6 +128,24 @@ if (cmd === 'pair') {
|
|
|
128
128
|
const daemon = new Daemon(config);
|
|
129
129
|
daemon.start();
|
|
130
130
|
|
|
131
|
+
} else if (cmd === 'update') {
|
|
132
|
+
const { checkAndUpdate, getLocalVersion } = require('../lib/updater');
|
|
133
|
+
console.log(`[channel-worker] Current version: ${getLocalVersion()}`);
|
|
134
|
+
(async () => {
|
|
135
|
+
try {
|
|
136
|
+
const result = await checkAndUpdate({ autoRestart: false });
|
|
137
|
+
if (result.updated) {
|
|
138
|
+
console.log(`[channel-worker] Updated: ${result.local} → ${result.latest}`);
|
|
139
|
+
console.log('[channel-worker] Restart the daemon to use the new version.');
|
|
140
|
+
} else {
|
|
141
|
+
console.log(`[channel-worker] Already up to date (${result.local})`);
|
|
142
|
+
}
|
|
143
|
+
} catch (err) {
|
|
144
|
+
console.error(`[channel-worker] Update failed: ${err.message}`);
|
|
145
|
+
process.exit(1);
|
|
146
|
+
}
|
|
147
|
+
})();
|
|
148
|
+
|
|
131
149
|
} else if (cmd === 'config') {
|
|
132
150
|
const config = loadConfig();
|
|
133
151
|
// Hide token in display
|
|
@@ -142,7 +160,8 @@ channel-worker — Channel Manager worker daemon
|
|
|
142
160
|
Commands:
|
|
143
161
|
pair Pair with dashboard using a one-time code (recommended)
|
|
144
162
|
init Configure worker manually
|
|
145
|
-
start Start the daemon
|
|
163
|
+
start Start the daemon (auto-checks for updates every 5min)
|
|
164
|
+
update Check and install updates manually
|
|
146
165
|
config Show current config
|
|
147
166
|
|
|
148
167
|
Pairing (recommended):
|
package/lib/daemon.js
CHANGED
|
@@ -2,6 +2,7 @@ const { ApiClient } = require('./api-client');
|
|
|
2
2
|
const { Heartbeat } = require('./heartbeat');
|
|
3
3
|
const { JobPoller } = require('./job-poller');
|
|
4
4
|
const { CommandPoller } = require('./command-poller');
|
|
5
|
+
const { UpdateChecker, getLocalVersion } = require('./updater');
|
|
5
6
|
|
|
6
7
|
class Daemon {
|
|
7
8
|
constructor(config) {
|
|
@@ -10,12 +11,14 @@ class Daemon {
|
|
|
10
11
|
this.heartbeat = new Heartbeat(this.api, config.worker_id);
|
|
11
12
|
this.poller = new JobPoller(this.api, config);
|
|
12
13
|
this.commandPoller = new CommandPoller(this.api, config);
|
|
14
|
+
this.updateChecker = new UpdateChecker(5 * 60 * 1000); // check every 5min
|
|
13
15
|
}
|
|
14
16
|
|
|
15
17
|
async start() {
|
|
18
|
+
const version = getLocalVersion();
|
|
16
19
|
console.log(`
|
|
17
20
|
╔══════════════════════════════════════╗
|
|
18
|
-
║ channel-worker
|
|
21
|
+
║ channel-worker v${version.padEnd(14)}║
|
|
19
22
|
╠══════════════════════════════════════╣
|
|
20
23
|
║ Worker ID: ${this.config.worker_id.padEnd(22)}║
|
|
21
24
|
║ API URL: ${this.config.api_url.padEnd(22)}║
|
|
@@ -49,6 +52,10 @@ class Daemon {
|
|
|
49
52
|
// Start command polling
|
|
50
53
|
this.commandPoller.start();
|
|
51
54
|
console.log('[daemon] Command poller started (every 3s)');
|
|
55
|
+
|
|
56
|
+
// Start auto-update checker
|
|
57
|
+
this.updateChecker.start();
|
|
58
|
+
console.log('[daemon] Auto-update checker started (every 5min)');
|
|
52
59
|
console.log('[daemon] Waiting for jobs & commands...\n');
|
|
53
60
|
|
|
54
61
|
// Graceful shutdown
|
|
@@ -57,6 +64,7 @@ class Daemon {
|
|
|
57
64
|
this.heartbeat.stop();
|
|
58
65
|
this.poller.stop();
|
|
59
66
|
this.commandPoller.stop();
|
|
67
|
+
this.updateChecker.stop();
|
|
60
68
|
|
|
61
69
|
// Mark offline
|
|
62
70
|
try {
|
package/lib/updater.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
const { execSync } = require('child_process');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
|
|
4
|
+
const PKG_NAME = 'channel-worker';
|
|
5
|
+
|
|
6
|
+
function getLocalVersion() {
|
|
7
|
+
const pkg = require(path.join(__dirname, '..', 'package.json'));
|
|
8
|
+
return pkg.version;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
async function getLatestVersion() {
|
|
12
|
+
const res = await fetch(`https://registry.npmjs.org/${PKG_NAME}/latest`);
|
|
13
|
+
if (!res.ok) throw new Error(`npm registry returned ${res.status}`);
|
|
14
|
+
const data = await res.json();
|
|
15
|
+
return data.version;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function isNewer(remote, local) {
|
|
19
|
+
const r = remote.split('.').map(Number);
|
|
20
|
+
const l = local.split('.').map(Number);
|
|
21
|
+
for (let i = 0; i < 3; i++) {
|
|
22
|
+
if (r[i] > l[i]) return true;
|
|
23
|
+
if (r[i] < l[i]) return false;
|
|
24
|
+
}
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function installUpdate(version) {
|
|
29
|
+
console.log(`[updater] Installing ${PKG_NAME}@${version}...`);
|
|
30
|
+
execSync(`npm install -g ${PKG_NAME}@${version}`, { stdio: 'inherit' });
|
|
31
|
+
console.log(`[updater] Installed ${PKG_NAME}@${version}`);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async function checkAndUpdate({ autoRestart = false } = {}) {
|
|
35
|
+
const local = getLocalVersion();
|
|
36
|
+
const latest = await getLatestVersion();
|
|
37
|
+
|
|
38
|
+
if (!isNewer(latest, local)) {
|
|
39
|
+
return { updated: false, local, latest };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
console.log(`[updater] Update available: ${local} → ${latest}`);
|
|
43
|
+
installUpdate(latest);
|
|
44
|
+
|
|
45
|
+
if (autoRestart) {
|
|
46
|
+
console.log('[updater] Restarting daemon...');
|
|
47
|
+
// Spawn detached process with same args, then exit current
|
|
48
|
+
const { spawn } = require('child_process');
|
|
49
|
+
const child = spawn(process.argv[0], process.argv.slice(1), {
|
|
50
|
+
detached: true,
|
|
51
|
+
stdio: 'ignore',
|
|
52
|
+
cwd: process.cwd(),
|
|
53
|
+
});
|
|
54
|
+
child.unref();
|
|
55
|
+
process.exit(0);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return { updated: true, local, latest };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
class UpdateChecker {
|
|
62
|
+
constructor(intervalMs = 5 * 60 * 1000) {
|
|
63
|
+
this.intervalMs = intervalMs;
|
|
64
|
+
this.timer = null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
start() {
|
|
68
|
+
// Check on startup after 10s delay
|
|
69
|
+
setTimeout(() => this.check(), 10000);
|
|
70
|
+
// Then periodically
|
|
71
|
+
this.timer = setInterval(() => this.check(), this.intervalMs);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
stop() {
|
|
75
|
+
if (this.timer) {
|
|
76
|
+
clearInterval(this.timer);
|
|
77
|
+
this.timer = null;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async check() {
|
|
82
|
+
try {
|
|
83
|
+
const result = await checkAndUpdate({ autoRestart: true });
|
|
84
|
+
if (!result.updated) {
|
|
85
|
+
// Silent — only log in verbose or when there's an update
|
|
86
|
+
}
|
|
87
|
+
} catch (err) {
|
|
88
|
+
console.warn(`[updater] Check failed: ${err.message}`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
module.exports = { checkAndUpdate, getLocalVersion, getLatestVersion, isNewer, UpdateChecker };
|