mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-11-16 05:36:50 +00:00
fix: Make unembed unsigned run in bg
This commit is contained in:
1 file changed
+34
-15
+34
-15
@@ -2,6 +2,8 @@ use std::{
|
|||||||
cmp,
|
cmp,
|
||||||
collections::{BTreeMap, HashMap},
|
collections::{BTreeMap, HashMap},
|
||||||
future::ready,
|
future::ready,
|
||||||
|
sync::Arc,
|
||||||
|
time::Instant,
|
||||||
};
|
};
|
||||||
|
|
||||||
use conduwuit::{
|
use conduwuit::{
|
||||||
@@ -15,7 +17,7 @@ use conduwuit::{
|
|||||||
},
|
},
|
||||||
warn,
|
warn,
|
||||||
};
|
};
|
||||||
use database::Json;
|
use database::{Database, Json};
|
||||||
use futures::{FutureExt, StreamExt, TryStreamExt};
|
use futures::{FutureExt, StreamExt, TryStreamExt};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use ruma::{
|
use ruma::{
|
||||||
@@ -30,7 +32,7 @@ use ruma::{
|
|||||||
};
|
};
|
||||||
use serde_json::value::to_raw_value;
|
use serde_json::value::to_raw_value;
|
||||||
|
|
||||||
use crate::{Services, media, rooms::short::ShortStateHash};
|
use crate::{Services, media, rooms, rooms::short::ShortStateHash};
|
||||||
|
|
||||||
/// The current schema version.
|
/// The current schema version.
|
||||||
/// - If database is opened at greater version we reject with error. The
|
/// - If database is opened at greater version we reject with error. The
|
||||||
@@ -125,7 +127,7 @@ async fn migrate(services: &Services) -> Result<()> {
|
|||||||
.map_err(|e| err!("Failed to run SHA256 media migration: {e}"))?;
|
.map_err(|e| err!("Failed to run SHA256 media migration: {e}"))?;
|
||||||
} else if config.media_startup_check {
|
} else if config.media_startup_check {
|
||||||
info!("Starting media startup integrity check.");
|
info!("Starting media startup integrity check.");
|
||||||
let now = std::time::Instant::now();
|
let now = Instant::now();
|
||||||
media::migrations::checkup_sha256_media(services)
|
media::migrations::checkup_sha256_media(services)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| err!("Failed to verify media integrity: {e}"))?;
|
.map_err(|e| err!("Failed to verify media integrity: {e}"))?;
|
||||||
@@ -263,9 +265,11 @@ async fn migrate(services: &Services) -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if db["global"].get(UNEMBED_UNSIGNED_INFO).await.is_not_found() {
|
if db["global"].get(UNEMBED_UNSIGNED_INFO).await.is_not_found() {
|
||||||
unembed_unsigned_info(services)
|
services.server.runtime().spawn(unembed_unsigned_info(
|
||||||
.await
|
services.db.clone(),
|
||||||
.map_err(|e| err!("Failed to run 'unembed_unsigned_info' migration: {e:?}"))?;
|
services.rooms.metadata.clone(),
|
||||||
|
services.rooms.timeline.clone(),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
@@ -916,12 +920,25 @@ async fn obliterate_roomsynctoken_shortstatehash_with_extreme_prejudice(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const UNEMBED_UNSIGNED_INFO: &str = "unembed_unsigned_info";
|
const UNEMBED_UNSIGNED_INFO: &str = "unembed_unsigned_info";
|
||||||
async fn unembed_unsigned_info(services: &Services) -> Result {
|
#[tracing::instrument(name = "unembed_unsigned_info", skip_all)]
|
||||||
|
async fn unembed_unsigned_info(
|
||||||
|
db: Arc<Database>,
|
||||||
|
metadata: Arc<rooms::metadata::Service>,
|
||||||
|
timeline: Arc<rooms::timeline::Service>,
|
||||||
|
) -> Result {
|
||||||
type Map = BTreeMap<String, Box<serde_json::value::RawValue>>;
|
type Map = BTreeMap<String, Box<serde_json::value::RawValue>>;
|
||||||
|
info!(
|
||||||
|
"Starting background migration. This may take a long time, and may cause your server to \
|
||||||
|
lag. Please avoid restarting until the migration is complete."
|
||||||
|
);
|
||||||
|
|
||||||
let mut all_rooms = services.rooms.metadata.iter_ids();
|
let start = Instant::now();
|
||||||
|
let mut total_migrated = 0_usize;
|
||||||
|
let mut all_rooms = metadata.iter_ids();
|
||||||
while let Some(room_id) = all_rooms.next().await {
|
while let Some(room_id) = all_rooms.next().await {
|
||||||
let mut pdus = std::pin::pin!(services.rooms.timeline.all_pdus(&room_id));
|
info!("Migrating PDUs in {room_id}");
|
||||||
|
let mut pdus = std::pin::pin!(timeline.all_pdus(&room_id));
|
||||||
|
let mut migrated = 0_usize;
|
||||||
while let Some((_, pdu)) = pdus.next().await {
|
while let Some((_, pdu)) = pdus.next().await {
|
||||||
let mut pdu = pdu.clone();
|
let mut pdu = pdu.clone();
|
||||||
let mut unsigned: Map = pdu
|
let mut unsigned: Map = pdu
|
||||||
@@ -951,20 +968,22 @@ async fn unembed_unsigned_info(services: &Services) -> Result {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
pdu.unsigned = Some(to_raw_value(&unsigned).expect("unsigned must be valid JSON"));
|
pdu.unsigned = Some(to_raw_value(&unsigned).expect("unsigned must be valid JSON"));
|
||||||
let pdu_id = services
|
let pdu_id = timeline
|
||||||
.rooms
|
|
||||||
.timeline
|
|
||||||
.get_pdu_id(pdu.event_id())
|
.get_pdu_id(pdu.event_id())
|
||||||
.await
|
.await
|
||||||
.expect("PDU must have an id");
|
.expect("PDU must have an id");
|
||||||
services
|
timeline
|
||||||
.rooms
|
|
||||||
.timeline
|
|
||||||
.replace_pdu(&pdu_id, &to_canonical_object(pdu).expect("PDU must be valid JSON"))
|
.replace_pdu(&pdu_id, &to_canonical_object(pdu).expect("PDU must be valid JSON"))
|
||||||
.await
|
.await
|
||||||
.expect("must be able to replace PDU after migrating unsigned data");
|
.expect("must be able to replace PDU after migrating unsigned data");
|
||||||
|
migrated = migrated.saturating_add(1);
|
||||||
}
|
}
|
||||||
|
total_migrated = total_migrated.saturating_add(migrated);
|
||||||
|
info!(elapsed=?start.elapsed(), "Migrated {migrated} PDUs in {room_id}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
db["global"].insert(UNEMBED_UNSIGNED_INFO, []);
|
||||||
|
info!(elapsed=?start.elapsed(), total_migrated_pdus=total_migrated, "Finished migration.");
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user