mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-12 02:06:37 +08:00
[优化] 重构升级处理逻辑,添加备份二进制文件移除功能,更新相关测试以验证新行为
This commit is contained in:
@@ -11,30 +11,26 @@ import (
|
|||||||
|
|
||||||
func replaceAndRestart(execPath string, tmpPath string) error {
|
func replaceAndRestart(execPath string, tmpPath string) error {
|
||||||
backupPath := execPath + ".bak"
|
backupPath := execPath + ".bak"
|
||||||
err := os.Remove(backupPath)
|
if err := removeBackupBinary(backupPath); err != nil {
|
||||||
if err != nil {
|
|
||||||
slog.Error("remove backup binary failed", "path", backupPath, "error", err)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := os.Rename(execPath, backupPath); err != nil {
|
if err := os.Rename(execPath, backupPath); err != nil {
|
||||||
err := os.Remove(tmpPath)
|
renameErr := err
|
||||||
if err != nil {
|
if err := os.Remove(tmpPath); err != nil && !os.IsNotExist(err) {
|
||||||
slog.Error("remove tmp binary failed", "path", tmpPath, "error", err)
|
slog.Error("remove tmp binary failed", "path", tmpPath, "error", err)
|
||||||
return err
|
return fmt.Errorf("backup current binary: %w; remove tmp binary: %v", renameErr, err)
|
||||||
}
|
}
|
||||||
return fmt.Errorf("backup current binary: %w", err)
|
return fmt.Errorf("backup current binary: %w", renameErr)
|
||||||
}
|
}
|
||||||
if err := os.Rename(tmpPath, execPath); err != nil {
|
if err := os.Rename(tmpPath, execPath); err != nil {
|
||||||
err := os.Rename(backupPath, execPath)
|
replaceErr := err
|
||||||
if err != nil {
|
if err := os.Rename(backupPath, execPath); err != nil {
|
||||||
slog.Error("restore backup binary failed", "path", backupPath, "error", err)
|
slog.Error("restore backup binary failed", "path", backupPath, "error", err)
|
||||||
return err
|
return fmt.Errorf("replace binary: %w; restore backup binary: %v", replaceErr, err)
|
||||||
}
|
}
|
||||||
return fmt.Errorf("replace binary: %w", err)
|
return fmt.Errorf("replace binary: %w", replaceErr)
|
||||||
}
|
}
|
||||||
err = os.Remove(backupPath)
|
if err := removeBackupBinary(backupPath); err != nil {
|
||||||
if err != nil {
|
|
||||||
slog.Error("remove backup binary failed", "path", backupPath, "error", err)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := syscall.Exec(execPath, os.Args, os.Environ()); err != nil {
|
if err := syscall.Exec(execPath, os.Args, os.Environ()); err != nil {
|
||||||
@@ -42,3 +38,14 @@ func replaceAndRestart(execPath string, tmpPath string) error {
|
|||||||
}
|
}
|
||||||
return fmt.Errorf("unreachable after exec")
|
return fmt.Errorf("unreachable after exec")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func removeBackupBinary(path string) error {
|
||||||
|
if err := os.Remove(path); err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
slog.Error("remove backup binary failed", "path", path, "error", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
//go:build !windows
|
||||||
|
|
||||||
|
package updater
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRemoveBackupBinaryIgnoresMissingFile(t *testing.T) {
|
||||||
|
backupPath := filepath.Join(t.TempDir(), "openflare-agent.bak")
|
||||||
|
if err := removeBackupBinary(backupPath); err != nil {
|
||||||
|
t.Fatalf("expected missing backup cleanup to be ignored: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useMutation, useQuery } from '@tanstack/react-query';
|
import { useMutation, useQuery } from '@tanstack/react-query';
|
||||||
import { useEffect, useRef, useState } from 'react';
|
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
|
|
||||||
import { useAuth } from '@/components/providers/auth-provider';
|
import { useAuth } from '@/components/providers/auth-provider';
|
||||||
@@ -53,6 +53,9 @@ export function DashboardTopbar() {
|
|||||||
const [upgradeStream, setUpgradeStream] =
|
const [upgradeStream, setUpgradeStream] =
|
||||||
useState<UpgradeStreamSnapshot | null>(null);
|
useState<UpgradeStreamSnapshot | null>(null);
|
||||||
const menuRef = useRef<HTMLDivElement | null>(null);
|
const menuRef = useRef<HTMLDivElement | null>(null);
|
||||||
|
const upgradeRefreshPendingRef = useRef(false);
|
||||||
|
const upgradeReloadStartedRef = useRef(false);
|
||||||
|
const upgradeReloadTimerRef = useRef<number | null>(null);
|
||||||
const isRoot = (user?.role ?? 0) >= 100;
|
const isRoot = (user?.role ?? 0) >= 100;
|
||||||
const upgradeStatusPollInterval = 3000;
|
const upgradeStatusPollInterval = 3000;
|
||||||
|
|
||||||
@@ -90,6 +93,7 @@ export function DashboardTopbar() {
|
|||||||
const upgradeMutation = useMutation({
|
const upgradeMutation = useMutation({
|
||||||
mutationFn: (channel: ReleaseChannel) => upgradeServer(channel),
|
mutationFn: (channel: ReleaseChannel) => upgradeServer(channel),
|
||||||
onSuccess: (release) => {
|
onSuccess: (release) => {
|
||||||
|
upgradeRefreshPendingRef.current = true;
|
||||||
setUploadedBinary(null);
|
setUploadedBinary(null);
|
||||||
setManualUpgradeStatus(null);
|
setManualUpgradeStatus(null);
|
||||||
setManualUpgradeError(null);
|
setManualUpgradeError(null);
|
||||||
@@ -133,6 +137,7 @@ export function DashboardTopbar() {
|
|||||||
const confirmManualUpgradeMutation = useMutation({
|
const confirmManualUpgradeMutation = useMutation({
|
||||||
mutationFn: confirmManualServerUpgrade,
|
mutationFn: confirmManualServerUpgrade,
|
||||||
onSuccess: (candidate) => {
|
onSuccess: (candidate) => {
|
||||||
|
upgradeRefreshPendingRef.current = true;
|
||||||
setVersionFeedback(null);
|
setVersionFeedback(null);
|
||||||
setManualUpgradeError(null);
|
setManualUpgradeError(null);
|
||||||
setUploadedBinary(candidate);
|
setUploadedBinary(candidate);
|
||||||
@@ -152,6 +157,40 @@ export function DashboardTopbar() {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const scheduleUpgradePageReload = useCallback(() => {
|
||||||
|
if (upgradeReloadStartedRef.current) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
upgradeReloadStartedRef.current = true;
|
||||||
|
setVersionFeedback('服务升级已进入重启阶段,页面将在服务恢复后自动刷新。');
|
||||||
|
|
||||||
|
const reloadWhenServerReady = async () => {
|
||||||
|
try {
|
||||||
|
await getPublicStatus();
|
||||||
|
window.location.reload();
|
||||||
|
} catch {
|
||||||
|
upgradeReloadTimerRef.current = window.setTimeout(
|
||||||
|
reloadWhenServerReady,
|
||||||
|
1500,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
upgradeReloadTimerRef.current = window.setTimeout(
|
||||||
|
reloadWhenServerReady,
|
||||||
|
1200,
|
||||||
|
);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
return () => {
|
||||||
|
if (upgradeReloadTimerRef.current !== null) {
|
||||||
|
window.clearTimeout(upgradeReloadTimerRef.current);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isVersionModalOpen || !isRoot) {
|
if (!isVersionModalOpen || !isRoot) {
|
||||||
setUpgradeStream(null);
|
setUpgradeStream(null);
|
||||||
@@ -175,14 +214,28 @@ export function DashboardTopbar() {
|
|||||||
socket.onmessage = (event) => {
|
socket.onmessage = (event) => {
|
||||||
const snapshot = parseUpgradeStreamSnapshot(String(event.data));
|
const snapshot = parseUpgradeStreamSnapshot(String(event.data));
|
||||||
if (snapshot) {
|
if (snapshot) {
|
||||||
|
if (
|
||||||
|
snapshot.in_progress ||
|
||||||
|
snapshot.upgrade_status === 'succeeded'
|
||||||
|
) {
|
||||||
|
upgradeRefreshPendingRef.current = true;
|
||||||
|
}
|
||||||
|
if (snapshot.upgrade_status === 'failed') {
|
||||||
|
upgradeRefreshPendingRef.current = false;
|
||||||
|
}
|
||||||
setUpgradeStream(snapshot);
|
setUpgradeStream(snapshot);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
socket.onclose = () => {
|
socket.onclose = () => {
|
||||||
if (!closed) {
|
if (closed) {
|
||||||
reconnectTimer = window.setTimeout(connect, 1500);
|
return;
|
||||||
}
|
}
|
||||||
|
if (upgradeRefreshPendingRef.current) {
|
||||||
|
scheduleUpgradePageReload();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
reconnectTimer = window.setTimeout(connect, 1500);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -195,7 +248,7 @@ export function DashboardTopbar() {
|
|||||||
}
|
}
|
||||||
socket?.close();
|
socket?.close();
|
||||||
};
|
};
|
||||||
}, [isRoot, isVersionModalOpen]);
|
}, [isRoot, isVersionModalOpen, scheduleUpgradePageReload]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isUserMenuOpen) {
|
if (!isUserMenuOpen) {
|
||||||
|
|||||||
Reference in New Issue
Block a user