(
info: ServerStatusInfo,
requestContext: ServerStatusRequestContext = {}
)
| 95 | }); |
| 96 | |
| 97 | export async function recordServerStatus( |
| 98 | info: ServerStatusInfo, |
| 99 | requestContext: ServerStatusRequestContext = {} |
| 100 | ) { |
| 101 | const { workspaceId, name, hostname, timeout, payload } = info; |
| 102 | |
| 103 | if (!workspaceId || !name || !hostname) { |
| 104 | console.warn( |
| 105 | '[ServerStatus] lost some necessary params, request will be ignore', |
| 106 | info |
| 107 | ); |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | // Get current server map from cache |
| 112 | const serverMap = await getServerMapFromCache(workspaceId); |
| 113 | |
| 114 | // Update current server status |
| 115 | const serverKey = name || hostname; |
| 116 | serverMap[serverKey] = { |
| 117 | workspaceId, |
| 118 | name, |
| 119 | hostname, |
| 120 | timeout, |
| 121 | updatedAt: Date.now(), |
| 122 | payload: { |
| 123 | ...requestContext, |
| 124 | ...payload, |
| 125 | }, |
| 126 | }; |
| 127 | |
| 128 | // Save updated server map to cache |
| 129 | await saveServerMapToCache(workspaceId, serverMap); |
| 130 | |
| 131 | // Update server history using cache |
| 132 | const history = await getServerHistoryFromCache(workspaceId, serverKey); |
| 133 | history.push(serverMap[serverKey]); |
| 134 | |
| 135 | // Keep only the last 20 records |
| 136 | if (history.length > 20) { |
| 137 | history.shift(); |
| 138 | } |
| 139 | |
| 140 | saveServerHistoryToCache(workspaceId, serverKey, history).catch((err) => { |
| 141 | logger.error('[ServerStatus] Error saving history to cache:', err); |
| 142 | }); |
| 143 | |
| 144 | promServerCounter.set( |
| 145 | { |
| 146 | workspaceId, |
| 147 | }, |
| 148 | Object.keys(serverMap).length |
| 149 | ); |
| 150 | |
| 151 | subscribeEventBus.emit('onServerStatusUpdate', workspaceId, serverMap); |
| 152 | } |
| 153 | |
| 154 | export async function clearOfflineServerStatus(workspaceId: string) { |
no test coverage detected