Skip to content

Commit 510795a

Browse files
committed
machine-setup: allow heartbeat force save
1 parent 0437c75 commit 510795a

File tree

1 file changed

+63
-25
lines changed

1 file changed

+63
-25
lines changed

packages/machine-setup/frontend/src/components/HeartbeatInfo.vue

Lines changed: 63 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,23 @@
22
<n-card bordered shadow="always">
33
<n-grid x-gap="12" :cols="2">
44
<n-gi>
5-
<p>XCPC-TOOLS 心跳上报状态:<n-tag :type="onHeartbeat ? 'success' : 'error'">{{ onHeartbeat ? '已开启' : '未开启' }}</n-tag></p>
6-
<p>当前上报中心:<n-tag :type="!heartbeaturl ? 'error' : 'success'">{{ heartbeaturl }}</n-tag></p>
5+
<p>上报中心:<n-tag :type="!nowHeartbeat ? 'error' : 'success'">{{ nowHeartbeat || 'no center' }}</n-tag></p>
6+
<n-space>
7+
<n-tag :type="onHeartbeat ? 'success' : 'error'">{{ onHeartbeat ? '已开启上报' : '未开启上报' }}</n-tag>
8+
<n-button type="warning" size="small" @click="">获取中心状态</n-button>
9+
</n-space>
710
</n-gi>
811
<n-gi>
9-
<n-input placeholder="请输入心跳上报URL" v-model:value="heartbeaturl" size="large" style="width: 100%; margin-bottom: .5em;" />
10-
<n-grid x-gap="12" :cols="2" style="width: 100%;">
12+
<n-input placeholder="IP/HOST/URL" v-model:value="editHeartbeat" size="large" style="width: 100%; margin-bottom: .5em;" />
13+
<n-grid x-gap="12" :cols="3" style="width: 100%;">
1114
<n-gi>
12-
<n-button type="primary" @click="saveHeartbeat" style="width: 100%;">保存</n-button>
15+
<n-button type="primary" @click="saveHeartbeat(false)" style="width: 100%;">保存</n-button>
1316
</n-gi>
1417
<n-gi>
15-
<n-button type="info" @click="runHeartbeat" style="width: 100%;">测试</n-button>
18+
<n-button type="info" @click="testHeartbeat" style="width: 100%;">测试</n-button>
19+
</n-gi>
20+
<n-gi>
21+
<n-button type="warning" @click="saveHeartbeat(true)" style="width: 100%;">强制保存</n-button>
1622
</n-gi>
1723
</n-grid>
1824
</n-gi>
@@ -25,44 +31,76 @@ import { filesystem, os } from '@neutralinojs/lib';
2531
import { NCard, NGrid, NGi, NButton, NInput } from 'naive-ui';
2632
import { onMounted, ref } from 'vue';
2733
28-
const heartbeaturl = ref<string>('');
34+
const editHeartbeat = ref<string>('');
35+
const nowHeartbeat = ref<string>('');
2936
const onHeartbeat = ref<boolean>(false);
3037
31-
const saveHeartbeat = async () => {
38+
const getRealHeartbeatUrl = async () => {
39+
const iporHost = editHeartbeat.value.trim();
40+
if (iporHost.includes('http')) return iporHost;
41+
if (!iporHost.includes(':')) return `http://${iporHost}:5283/report`;
42+
return `http://${iporHost}/report`;
43+
};
44+
45+
const getHeartbeatVersion = async (url: string) => {
3246
try {
33-
console.log('save heartbeat', heartbeaturl.value);
34-
const res = await os.execCommand(`HEARTBEATURL=${heartbeaturl.value} /usr/sbin/icpc-heartbeat`);
35-
if (res.stdErr || res.exitCode) throw new Error(res.stdErr);
36-
console.log('run heartbeat on save', res);
37-
await filesystem.writeFile('/etc/default/icpc-heartbeat', `HEARTBEATURL=${heartbeaturl.value}`);
38-
const res2 = await os.execCommand('systemctl enable heartbeat.timer');
39-
console.log('run enable heartbeat on save', res2);
47+
const version = await fetch(url.replace('/report', '/version')).then(res => res.json());
48+
if (!version) throw new Error('无法获取上报中心版本');
49+
window.$notification.success({ title: '连接上报中心成功', content: `上报中心版本:${version.version}`, duration: 3000 });
4050
} catch (error) {
41-
console.error(`save heartbeat error: ${error}`);
42-
window.$notification.error({ title: '保存心跳上报URL失败', content: (error as any).message });
51+
console.error(`get heartbeat error: ${error}`);
52+
window.$notification.error({ title: '获取中心版本失败', content: (error as any).message, duration: 3000 });
4353
}
4454
};
4555
46-
const runHeartbeat = async () => {
56+
const runHeartbeat = async (url: string) => {
4757
try {
48-
const version = await fetch(heartbeaturl.value.replace('/report', '/version')).then(res => res.json());
49-
if (!version) throw new Error('无法获取上报中心版本');
50-
window.$notification.success({ title: '连接上报中心成功', content: `上报中心版本:${version.version}` });
51-
const res = await os.execCommand(`HEARTBEATURL=${heartbeaturl.value} /usr/sbin/icpc-heartbeat`);
58+
const res = await os.execCommand(`HEARTBEATURL=${url} /usr/sbin/icpc-heartbeat`);
5259
if (res.stdErr || res.exitCode) throw new Error(res.stdErr);
5360
console.log('run heartbeat on test', res);
5461
} catch (error) {
5562
console.error(`run heartbeat error: ${error}`);
56-
window.$notification.error({ title: '测试心跳上报URL失败', content: (error as any).message });
63+
window.$notification.error({ title: '测试心跳上报URL失败', content: (error as any).message, duration: 3000 });
64+
}
65+
};
66+
67+
const testHeartbeat = async () => {
68+
const url = await getRealHeartbeatUrl();
69+
try {
70+
await getHeartbeatVersion(url);
71+
await runHeartbeat(url);
72+
} catch (error) {
73+
console.error(`test heartbeat error: ${error}`);
74+
window.$notification.error({ title: '测试心跳上报URL失败', content: (error as any).message, duration: 3000 });
5775
}
5876
};
5977
78+
const saveHeartbeat = async (force = false) => {
79+
const url = await getRealHeartbeatUrl();
80+
try {
81+
if (!force) {
82+
await getHeartbeatVersion(url);
83+
await runHeartbeat(url);
84+
}
85+
console.log('save heartbeat', url);
86+
await filesystem.writeFile('/etc/default/icpc-heartbeat', `HEARTBEATURL=${url}`);
87+
const res2 = await os.execCommand('systemctl enable heartbeat.timer');
88+
console.log('run enable heartbeat on save', res2);
89+
nowHeartbeat.value = url;
90+
} catch (error) {
91+
console.error(`save heartbeat error: ${error}`);
92+
window.$notification.error({ title: '保存心跳上报URL失败', content: (error as any).message, duration: 3000 });
93+
}
94+
};
95+
96+
97+
6098
onMounted(async () => {
6199
try {
62100
const res = await filesystem.readFile('/etc/default/icpc-heartbeat');
63101
console.log('icpc-heartbeat', res);
64-
heartbeaturl.value = res.split('=')[1].trim();
65-
if (!heartbeaturl.value) {
102+
nowHeartbeat.value = res.split('=')[1].trim();
103+
if (!nowHeartbeat.value) {
66104
const res = await os.execCommand('systemctl disable heartbeat.timer');
67105
console.log('disable heartbeat.timer', res);
68106
onHeartbeat.value = false;

0 commit comments

Comments
 (0)