mirror of
https://github.com/roleypoly/roleypoly.git
synced 2025-06-17 01:59:08 +00:00
chore: add backend-y bits of heartbeats
This commit is contained in:
parent
f65779f925
commit
a34aebabe3
12 changed files with 174 additions and 2 deletions
23
packages/api/handlers/bot-heartbeat.ts
Normal file
23
packages/api/handlers/bot-heartbeat.ts
Normal file
|
@ -0,0 +1,23 @@
|
|||
import { BotHeartbeatData } from 'packages/types';
|
||||
import { getSharedKey, respond } from '../utils/api-tools';
|
||||
import { botHeartbeatToken } from '../utils/config';
|
||||
import { Infrastructure } from '../utils/kv';
|
||||
|
||||
const denied = () => respond({ error: 'robots only, human.' }, { status: 403 });
|
||||
|
||||
export const BotHeartbeat = async (request: Request): Promise<Response> => {
|
||||
const { id } = getSharedKey(request) || {};
|
||||
if (!id || id !== botHeartbeatToken) {
|
||||
return denied();
|
||||
}
|
||||
|
||||
const heartbeat: BotHeartbeatData = await request.json();
|
||||
|
||||
await Infrastructure.put<BotHeartbeatData>(`shard_${heartbeat.shard}`, {
|
||||
lastHeartbeat: Date.now(),
|
||||
state: heartbeat.state,
|
||||
shard: heartbeat.shard,
|
||||
});
|
||||
|
||||
return respond({ status: 'ok' });
|
||||
};
|
85
packages/api/handlers/get-infrastructure-status.ts
Normal file
85
packages/api/handlers/get-infrastructure-status.ts
Normal file
|
@ -0,0 +1,85 @@
|
|||
import { BotHeartbeatData } from 'packages/types';
|
||||
import { respond } from '../utils/api-tools';
|
||||
import { Infrastructure } from '../utils/kv';
|
||||
|
||||
export const GetInfrastructureStatus = async (request: Request): Promise<Response> => {
|
||||
const cachedStatus = await Infrastructure.get('cached_status');
|
||||
if (cachedStatus) {
|
||||
return respond({ status: cachedStatus });
|
||||
}
|
||||
|
||||
const [shardStatus, discord, cloudflare] = await Promise.all([
|
||||
getAllShardStatus(),
|
||||
getDiscordStatus(),
|
||||
getCloudflareStatus(),
|
||||
]);
|
||||
|
||||
const status = {
|
||||
shards: shardStatus,
|
||||
discord,
|
||||
cloudflare,
|
||||
};
|
||||
|
||||
await Infrastructure.put('cached_status', status, 60 * 5);
|
||||
|
||||
return respond({
|
||||
status,
|
||||
});
|
||||
};
|
||||
|
||||
const getAllShardStatus = async (): Promise<BotHeartbeatData[]> => {
|
||||
const shards: BotHeartbeatData[] = [];
|
||||
let currentShard = 0;
|
||||
|
||||
do {
|
||||
const thisShard = await getShardStatus(currentShard);
|
||||
if (!thisShard) {
|
||||
break;
|
||||
}
|
||||
|
||||
shards.push(thisShard);
|
||||
currentShard++;
|
||||
} while (currentShard < 64); // If we hit 64, there's bigger issues.
|
||||
|
||||
return shards;
|
||||
};
|
||||
|
||||
const getShardStatus = async (shardNumber: number): Promise<BotHeartbeatData | null> => {
|
||||
const shardHeartbeat = await Infrastructure.get<BotHeartbeatData>(
|
||||
`shard_${shardNumber}`
|
||||
);
|
||||
if (!shardHeartbeat) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return shardHeartbeat;
|
||||
};
|
||||
|
||||
const getDiscordStatus = async () => {
|
||||
const req = await fetch('https://srhpyqt94yxb.statuspage.io/api/v2/summary.json');
|
||||
const status = await req.json();
|
||||
|
||||
const api =
|
||||
status.components.find(
|
||||
(component: { id: string }) => component.id === 'rhznvxg4v7yh'
|
||||
)?.status !== 'operational';
|
||||
|
||||
return { api };
|
||||
};
|
||||
|
||||
const getCloudflareStatus = async () => {
|
||||
const req = await fetch('https://yh6f0r4529hb.statuspage.io/api/v2/summary.json');
|
||||
const status = await req.json();
|
||||
|
||||
const workers =
|
||||
status.components.find(
|
||||
(component: { id: string }) => component.id === '57srcl8zcn7c'
|
||||
)?.status !== 'operational';
|
||||
|
||||
const storage =
|
||||
status.components.find(
|
||||
(component: { id: string }) => component.id === 'tmh50tx2nprs'
|
||||
)?.status !== 'operational';
|
||||
|
||||
return { workers, storage };
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue