diff --git a/.github/workflows/scheduled_tasks.yml b/.github/workflows/scheduled_tasks.yml index fb863ecd..7bbd4450 100644 --- a/.github/workflows/scheduled_tasks.yml +++ b/.github/workflows/scheduled_tasks.yml @@ -2,8 +2,9 @@ name: Build scheduled tasks runner Docker image on: push: - branches: [main] + branches: [main, gateway-service] paths: + - .github/workflows/scheduled_tasks.yml - 'services/scheduled_tasks/**' jobs: diff --git a/go.work b/go.work index 6d7c1c12..fb60983f 100644 --- a/go.work +++ b/go.work @@ -2,5 +2,4 @@ go 1.19 use ( ./services/scheduled_tasks - ./services/web-proxy ) diff --git a/services/gateway/src/cache_api.rs b/services/gateway/src/cache_api.rs index 15066053..66d57ba2 100644 --- a/services/gateway/src/cache_api.rs +++ b/services/gateway/src/cache_api.rs @@ -5,12 +5,15 @@ use axum::{ routing::get, Router, }; -use serde_json::to_string; +use serde_json::{json, to_string}; use tracing::{error, info}; use twilight_model::guild::Permissions; use twilight_model::id::Id; -use crate::discord::cache::{dm_channel, DiscordCache, DM_PERMISSIONS}; +use crate::discord::{ + cache::{dm_channel, DiscordCache, DM_PERMISSIONS}, + gateway::cluster_config, +}; use std::sync::Arc; fn status_code(code: StatusCode, body: String) -> Response { @@ -156,6 +159,17 @@ pub async fn run_server(cache: Arc) -> anyhow::Result<()> { }) ) + .route("/stats", get(|State(cache): State>| async move { + let cluster = cluster_config(); + let has_been_up = cache.2.read().await.len() as u32 == if cluster.total_shards > 16 {16} else {cluster.total_shards}; + let stats = json!({ + "guild_count": cache.0.stats().guilds(), + "channel_count": cache.0.stats().channels(), + "up": has_been_up, + }); + status_code(StatusCode::FOUND, to_string(&stats).unwrap()) + })) + .layer(axum::middleware::from_fn(crate::logger::logger)) .with_state(cache); diff --git a/services/gateway/src/discord/cache.rs b/services/gateway/src/discord/cache.rs index 5ada74b8..38a43e40 100644 --- a/services/gateway/src/discord/cache.rs +++ b/services/gateway/src/discord/cache.rs @@ -1,6 +1,7 @@ use anyhow::format_err; use lazy_static::lazy_static; use std::sync::Arc; +use tokio::sync::RwLock; use twilight_cache_inmemory::{ model::CachedMember, permission::{MemberRoles, RootError}, @@ -110,10 +111,14 @@ pub fn new() -> DiscordCache { .build(), ); - DiscordCache(cache, client) + DiscordCache(cache, client, RwLock::new(Vec::new())) } -pub struct DiscordCache(pub Arc, pub Arc); +pub struct DiscordCache( + pub Arc, + pub Arc, + pub RwLock>, +); impl DiscordCache { pub async fn guild_permissions( diff --git a/services/gateway/src/discord/gateway.rs b/services/gateway/src/discord/gateway.rs index e8bfb26f..97711069 100644 --- a/services/gateway/src/discord/gateway.rs +++ b/services/gateway/src/discord/gateway.rs @@ -1,3 +1,4 @@ +use libpk::_config::ClusterSettings; use std::sync::{mpsc::Sender, Arc}; use tracing::{info, warn}; use twilight_gateway::{ @@ -13,6 +14,18 @@ use crate::discord::identify_queue::{self, RedisQueue}; use super::{cache::DiscordCache, shard_state::ShardStateManager}; +pub fn cluster_config() -> ClusterSettings { + libpk::config + .discord + .cluster + .clone() + .unwrap_or(libpk::_config::ClusterSettings { + node_id: 0, + total_shards: 1, + total_nodes: 1, + }) +} + pub fn create_shards(redis: fred::pool::RedisPool) -> anyhow::Result>> { let intents = Intents::GUILDS | Intents::DIRECT_MESSAGES @@ -23,16 +36,7 @@ pub fn create_shards(redis: fred::pool::RedisPool) -> anyhow::Result