Don't stop even when caching local instance user

This commit is contained in:
Kitaiti Makoto 2021-11-26 09:51:25 +09:00
parent 1e67b3c13c
commit b9ea06a01a

View File

@ -11,6 +11,7 @@ use diesel::{self, result::Error::NotFound, ExpressionMethods, QueryDsl, RunQuer
use once_cell::sync::OnceCell; use once_cell::sync::OnceCell;
use plume_common::utils::md_to_html; use plume_common::utils::md_to_html;
use std::sync::RwLock; use std::sync::RwLock;
use tracing::error;
#[derive(Clone, Identifiable, Queryable)] #[derive(Clone, Identifiable, Queryable)]
pub struct Instance { pub struct Instance {
@ -81,7 +82,7 @@ impl Instance {
} }
pub fn create_local_instance_user(conn: &Connection) -> Result<User> { pub fn create_local_instance_user(conn: &Connection) -> Result<User> {
let instance = Instance::get_local().expect("Failed to get local instance"); let instance = Instance::get_local()?;
let email = format!("{}@{}", LOCAL_INSTANCE_USERNAME, &instance.public_domain); let email = format!("{}@{}", LOCAL_INSTANCE_USERNAME, &instance.public_domain);
NewUser::new_local( NewUser::new_local(
conn, conn,
@ -109,9 +110,13 @@ impl Instance {
} }
pub fn cache_local_instance_user(conn: &Connection) { pub fn cache_local_instance_user(conn: &Connection) {
let user = Self::get_local_instance_user_uncached(conn).unwrap_or_else(|_| { let res = Self::get_local_instance_user_uncached(conn)
Self::create_local_instance_user(conn).expect("Failed to create local instance user") .or_else(|_| Self::create_local_instance_user(conn));
}); if res.is_err() {
error!("Failed to cache local instance user: {:?}", res);
return;
}
let user = res.expect("Unreachable");
LOCAL_INSTANCE_USER LOCAL_INSTANCE_USER
.set(user) .set(user)
.expect("Failed to set local instance user"); .expect("Failed to set local instance user");