Compare commits

..

No commits in common. "b845ad21d0bd1512d132d664e8485340a715e6ec" and "f62d8a42d4c9327033405e07775bef3ee1531bfb" have entirely different histories.

3 changed files with 103 additions and 75 deletions

View file

@ -96,23 +96,32 @@ pub(crate) enum RoomTargetOption {
} }
#[admin_command] #[admin_command]
pub(super) async fn purge_all_sync_tokens( pub(super) async fn purge_empty_room_tokens(
&self, &self,
yes: bool,
target_option: Option<RoomTargetOption>, target_option: Option<RoomTargetOption>,
execute: bool, dry_run: bool,
) -> Result { ) -> Result {
use conduwuit::{debug, info}; use conduwuit::{debug, info};
let mode = if !execute { "Simulating" } else { "Starting" }; if !yes && !dry_run {
return Err!(
"Please confirm this operation with --yes as it may delete tokens from many rooms, \
or use --dry-run to simulate"
);
}
let mode = if dry_run { "Simulating" } else { "Starting" };
// strictly, we should check if these reach the max value after the loop and // strictly, we should check if these reach the max value after the loop and
// warn the user that the count is too large // warn the user that the count is too large
let mut total_rooms_checked: usize = 0; let mut total_rooms_processed: usize = 0;
let mut empty_rooms_processed: u32 = 0;
let mut total_tokens_deleted: usize = 0; let mut total_tokens_deleted: usize = 0;
let mut error_count: u32 = 0; let mut error_count: u32 = 0;
let mut skipped_rooms: usize = 0; let mut skipped_rooms: u32 = 0;
info!("{} purge of sync tokens", mode); info!("{} purge of sync tokens for rooms with no local users", mode);
// Get all rooms in the server // Get all rooms in the server
let all_rooms = self let all_rooms = self
@ -160,31 +169,33 @@ pub(super) async fn purge_all_sync_tokens(
// Process each room // Process each room
for room_id in rooms { for room_id in rooms {
total_rooms_checked = total_rooms_checked.saturating_add(1); total_rooms_processed = total_rooms_processed.saturating_add(1);
// Log progress periodically // Count local users in this room
if total_rooms_checked % 100 == 0 || total_rooms_checked == total_rooms { let local_users_count = self
info!( .services
"Progress: {}/{} rooms checked, {} tokens {}", .rooms
total_rooms_checked, .state_cache
total_rooms, .local_users_in_room(room_id)
total_tokens_deleted, .count()
if !execute { "would be deleted" } else { "deleted" } .await;
);
} // Only process rooms with no local users
if local_users_count == 0 {
empty_rooms_processed = empty_rooms_processed.saturating_add(1);
// In dry run mode, just count what would be deleted, don't actually delete // In dry run mode, just count what would be deleted, don't actually delete
debug!( debug!(
"Room {} has no local users, {}", "Room {} has no local users, {}",
room_id, room_id,
if !execute { if dry_run {
"would purge sync tokens" "would purge sync tokens"
} else { } else {
"purging sync tokens" "purging sync tokens"
} }
); );
if !execute { if dry_run {
// For dry run mode, count tokens without deleting // For dry run mode, count tokens without deleting
match self.services.rooms.user.count_room_tokens(room_id).await { match self.services.rooms.user.count_room_tokens(room_id).await {
| Ok(count) => | Ok(count) =>
@ -215,31 +226,45 @@ pub(super) async fn purge_all_sync_tokens(
}, },
} }
} }
} else {
debug!("Room {} has {} local users, skipping", room_id, local_users_count);
} }
let action = if !execute { "would be deleted" } else { "deleted" }; // Log progress periodically
if total_rooms_processed % 100 == 0 || total_rooms_processed == total_rooms {
info!( info!(
"Finished {}: checked {} rooms out of {} total, {} tokens {}, errors: {}", "Progress: {}/{} rooms processed, {} empty rooms found, {} tokens {}",
if !execute { total_rooms_processed,
total_rooms,
empty_rooms_processed,
total_tokens_deleted,
if dry_run { "would be deleted" } else { "deleted" }
);
}
}
let action = if dry_run { "would be deleted" } else { "deleted" };
info!(
"Finished {}: processed {} empty rooms out of {} total, {} tokens {}, errors: {}",
if dry_run {
"purge simulation" "purge simulation"
} else { } else {
"purging sync tokens" "purging sync tokens"
}, },
total_rooms_checked, empty_rooms_processed,
total_rooms, total_rooms,
total_tokens_deleted, total_tokens_deleted,
action, action,
error_count error_count
); );
let mode_msg = if dry_run { "DRY RUN: " } else { "" };
self.write_str(&format!( self.write_str(&format!(
"Finished {}: checked {} rooms out of {} total, {} tokens {}, errors: {}", "{}Successfully processed {empty_rooms_processed} empty rooms (out of {total_rooms} \
if !execute { "simulation" } else { "purging sync tokens" }, total rooms), {total_tokens_deleted} tokens {}. Skipped {skipped_rooms} rooms based on \
total_rooms_checked, filters. Failed for {error_count} rooms.",
total_rooms, mode_msg,
total_tokens_deleted, if dry_run { "would be deleted" } else { "deleted" }
action,
error_count
)) ))
.await .await
} }

View file

@ -68,14 +68,17 @@ pub(super) enum RoomCommand {
/// - Delete sync tokens for all rooms that have no local users /// - Delete sync tokens for all rooms that have no local users
/// ///
/// By default, processes all empty rooms. /// By default, processes all empty rooms.
PurgeAllSyncTokens { PurgeEmptyRoomTokens {
/// Confirm you want to delete tokens from potentially many rooms
#[arg(long)]
yes: bool,
/// Target specific room types /// Target specific room types
#[arg(long, value_enum)] #[arg(long, value_enum)]
target_option: Option<RoomTargetOption>, target_option: Option<RoomTargetOption>,
/// Execute token deletions. Otherwise, /// Perform a dry run without actually deleting any tokens
/// Performs a dry run without actually deleting any tokens
#[arg(long)] #[arg(long)]
execute: bool, dry_run: bool,
}, },
} }

View file

@ -79,12 +79,12 @@ fn main() {
// --- Rerun Triggers --- // --- Rerun Triggers ---
// TODO: The git rerun triggers seem to always run // TODO: The git rerun triggers seem to always run
// // Rerun if the git HEAD changes // Rerun if the git HEAD changes
// println!("cargo:rerun-if-changed=.git/HEAD"); println!("cargo:rerun-if-changed=.git/HEAD");
// // Rerun if the ref pointed to by HEAD changes (e.g., new commit on branch) // Rerun if the ref pointed to by HEAD changes (e.g., new commit on branch)
// if let Some(ref_path) = run_git_command(&["symbolic-ref", "--quiet", "HEAD"]) if let Some(ref_path) = run_git_command(&["symbolic-ref", "--quiet", "HEAD"]) {
// { println!("cargo:rerun-if-changed=.git/{ref_path}"); println!("cargo:rerun-if-changed=.git/{ref_path}");
// } }
println!("cargo:rerun-if-env-changed=GIT_COMMIT_HASH"); println!("cargo:rerun-if-env-changed=GIT_COMMIT_HASH");
println!("cargo:rerun-if-env-changed=GIT_COMMIT_HASH_SHORT"); println!("cargo:rerun-if-env-changed=GIT_COMMIT_HASH_SHORT");