-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_health.js
More file actions
72 lines (63 loc) · 2.38 KB
/
check_health.js
File metadata and controls
72 lines (63 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const fs = require('fs');
const path = require('path');
// This script checks Feishu-specific health requirements.
// It is called by the main evolver via INTEGRATION_STATUS_CMD.
function check() {
const issues = [];
const MEMORY_DIR = process.env.MEMORY_DIR || path.resolve(__dirname, '../../memory');
// 1. Check App ID
if (!process.env.FEISHU_APP_ID) {
issues.push('Feishu App ID Missing');
}
// 2. Check Token Freshness
try {
const tokenPath = path.resolve(MEMORY_DIR, 'feishu_token.json');
if (fs.existsSync(tokenPath)) {
const tokenData = JSON.parse(fs.readFileSync(tokenPath, 'utf8'));
// expire is in seconds, Date.now() is ms
if (tokenData.expire < Date.now() / 1000) {
issues.push('Feishu Token Expired');
}
} else {
issues.push('Feishu Token Missing');
}
} catch (e) {
issues.push(`Feishu Token Check Error: ${e.message}`);
}
// 3. Check Temp Directory (Critical for Cards)
const TEMP_DIR = path.resolve(__dirname, '../../temp');
if (!fs.existsSync(TEMP_DIR)) {
try {
fs.mkdirSync(TEMP_DIR);
// Fixed silently, do not report unless it fails
} catch(e) {
issues.push('Temp Dir Missing & Cannot Create');
}
} else {
try { fs.accessSync(TEMP_DIR, fs.constants.W_OK); }
catch(e) { issues.push('Temp Dir Not Writable'); }
}
// 4. Log Hygiene (Auto-Cleanup Stale Error Logs)
const { resolveEvolverDir } = require('./utils/resolve-evolver');
const evolverDir = resolveEvolverDir();
let errorLogPath = evolverDir ? path.resolve(evolverDir, 'evolution_error.log') : null;
if (errorLogPath && !fs.existsSync(errorLogPath)) errorLogPath = null;
if (errorLogPath) {
try {
const stats = fs.statSync(errorLogPath);
const now = Date.now();
const ageHours = (now - stats.mtimeMs) / (1000 * 60 * 60);
// If error log is > 24 hours old, delete it to avoid confusion in future alerts
if (ageHours > 24) {
fs.unlinkSync(errorLogPath);
}
} catch (e) {
// Ignore cleanup errors
}
}
// Output issues to stdout (will be captured by evolver)
if (issues.length > 0) {
console.log(issues.join(', '));
}
}
check();