mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-10-31 00:24:38 +00:00
The in-place update ran entirely inside c.Request.Context(). Browsers and reverse proxies commonly abort long-idle requests (axios global timeout 30s, nginx proxy_read_timeout 60s by default), which canceled the request context mid-download and killed every slow update with 'download failed: context canceled' while the version stayed unchanged. Users behind slow GitHub links saw the update button fail at a wall-clock ceiling (~60s) on every attempt (#4504). - Run PerformUpdate and RollbackToVersion on a context detached from the request (context.WithoutCancel) and bounded by a 15-minute deadline so the 10-minute GitHub download client owns its own timeout. A client disconnect no longer aborts the binary swap; retries then hit the system operation lock or report 'Already up to date'. - Raise the frontend timeout for the update/rollback calls from the global 30s axios default to 15 minutes so the browser can actually wait for the result. Fixes #4504
114 lines
2.8 KiB
TypeScript
114 lines
2.8 KiB
TypeScript
/**
|
|
* System API endpoints for admin operations
|
|
*/
|
|
|
|
import { apiClient } from '../client'
|
|
|
|
export interface ReleaseInfo {
|
|
name: string
|
|
body: string
|
|
published_at: string
|
|
html_url: string
|
|
}
|
|
|
|
export interface VersionInfo {
|
|
current_version: string
|
|
latest_version: string
|
|
has_update: boolean
|
|
release_info?: ReleaseInfo
|
|
cached: boolean
|
|
warning?: string
|
|
build_type: string // "source" for manual builds, "release" for CI builds
|
|
}
|
|
|
|
/**
|
|
* Get current version
|
|
*/
|
|
export async function getVersion(): Promise<{ version: string }> {
|
|
const { data } = await apiClient.get<{ version: string }>('/admin/system/version')
|
|
return data
|
|
}
|
|
|
|
/**
|
|
* Check for updates
|
|
* @param force - Force refresh from GitHub API
|
|
*/
|
|
export async function checkUpdates(force = false): Promise<VersionInfo> {
|
|
const { data } = await apiClient.get<VersionInfo>('/admin/system/check-updates', {
|
|
params: force ? { force: 'true' } : undefined
|
|
})
|
|
return data
|
|
}
|
|
|
|
export interface UpdateResult {
|
|
message: string
|
|
need_restart: boolean
|
|
}
|
|
|
|
export interface RollbackVersionInfo {
|
|
version: string
|
|
published_at: string
|
|
html_url: string
|
|
}
|
|
|
|
/**
|
|
* Get versions available for rollback (up to 3 versions older than current)
|
|
*/
|
|
export async function getRollbackVersions(): Promise<{ versions: RollbackVersionInfo[] }> {
|
|
const { data } = await apiClient.get<{ versions: RollbackVersionInfo[] }>(
|
|
'/admin/system/rollback-versions'
|
|
)
|
|
return data
|
|
}
|
|
|
|
/**
|
|
* In-place update/rollback downloads a full release binary from GitHub, which
|
|
* can take several minutes on slow links. The global 30s axios timeout would
|
|
* abort the request mid-download (#4504), so these calls wait as long as the
|
|
* backend allows (15 minutes server-side).
|
|
*/
|
|
const UPDATE_REQUEST_TIMEOUT_MS = 15 * 60 * 1000
|
|
|
|
/**
|
|
* Perform system update
|
|
* Downloads and applies the latest version
|
|
*/
|
|
export async function performUpdate(): Promise<UpdateResult> {
|
|
const { data } = await apiClient.post<UpdateResult>('/admin/system/update', undefined, {
|
|
timeout: UPDATE_REQUEST_TIMEOUT_MS
|
|
})
|
|
return data
|
|
}
|
|
|
|
/**
|
|
* Rollback to a previous version
|
|
* @param version - Target version (e.g. "0.1.146"); omit to restore the local backup binary
|
|
*/
|
|
export async function rollback(version?: string): Promise<UpdateResult> {
|
|
const { data } = await apiClient.post<UpdateResult>(
|
|
'/admin/system/rollback',
|
|
version ? { version } : undefined,
|
|
{ timeout: UPDATE_REQUEST_TIMEOUT_MS }
|
|
)
|
|
return data
|
|
}
|
|
|
|
/**
|
|
* Restart the service
|
|
*/
|
|
export async function restartService(): Promise<{ message: string }> {
|
|
const { data } = await apiClient.post<{ message: string }>('/admin/system/restart')
|
|
return data
|
|
}
|
|
|
|
export const systemAPI = {
|
|
getVersion,
|
|
checkUpdates,
|
|
performUpdate,
|
|
getRollbackVersions,
|
|
rollback,
|
|
restartService
|
|
}
|
|
|
|
export default systemAPI
|