From b58da11fb7daaf986223509f81f6586d81f6da75 Mon Sep 17 00:00:00 2001 From: Nutomic Date: Fri, 19 Jan 2024 15:40:12 +0100 Subject: [PATCH] Mark instance as alive after successful activity send (fixes #4039) (#4377) * Mark instance as alive after successful activity send (fixes #4039) * clippy * Instance::update * domain --------- Co-authored-by: Dessalines --- crates/db_schema/src/impls/instance.rs | 12 ++++++++++++ crates/federate/src/worker.rs | 20 ++++++++++++++++---- src/scheduled_tasks.rs | 5 +---- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/crates/db_schema/src/impls/instance.rs b/crates/db_schema/src/impls/instance.rs index 7d63c9ffa..c329d8cc0 100644 --- a/crates/db_schema/src/impls/instance.rs +++ b/crates/db_schema/src/impls/instance.rs @@ -64,6 +64,18 @@ impl Instance { e => e, } } + pub async fn update( + pool: &mut DbPool<'_>, + instance_id: InstanceId, + form: InstanceForm, + ) -> Result { + let mut conn = get_conn(pool).await?; + diesel::update(instance::table.find(instance_id)) + .set(form) + .execute(&mut conn) + .await + } + pub async fn delete(pool: &mut DbPool<'_>, instance_id: InstanceId) -> Result { let conn = &mut get_conn(pool).await?; diesel::delete(instance::table.find(instance_id)) diff --git a/crates/federate/src/worker.rs b/crates/federate/src/worker.rs index e53a42bc3..b57c5e8ae 100644 --- a/crates/federate/src/worker.rs +++ b/crates/federate/src/worker.rs @@ -11,7 +11,7 @@ use activitypub_federation::{ protocol::context::WithContext, }; use anyhow::{Context, Result}; -use chrono::{DateTime, TimeZone, Utc}; +use chrono::{DateTime, Days, TimeZone, Utc}; use lemmy_api_common::{context::LemmyContext, federate_retry_sleep_duration}; use lemmy_apub::{activity_lists::SharedInboxActivities, FEDERATION_CONTEXT}; use lemmy_db_schema::{ @@ -19,17 +19,17 @@ use lemmy_db_schema::{ source::{ activity::SentActivity, federation_queue_state::FederationQueueState, - instance::Instance, + instance::{Instance, InstanceForm}, site::Site, }, - utils::DbPool, + utils::{naive_now, DbPool}, }; use lemmy_db_views_actor::structs::CommunityFollowerView; use once_cell::sync::Lazy; use reqwest::Url; use std::{ collections::{HashMap, HashSet}, - ops::Deref, + ops::{Add, Deref}, time::Duration, }; use tokio::{sync::mpsc::UnboundedSender, time::sleep}; @@ -257,6 +257,18 @@ impl InstanceWorker { } } } + + // Activity send successful, mark instance as alive if it hasn't been updated in a while. + let updated = self.instance.updated.unwrap_or(self.instance.published); + if updated.add(Days::new(1)) < Utc::now() { + self.instance.updated = Some(Utc::now()); + + let form = InstanceForm::builder() + .domain(self.instance.domain.clone()) + .updated(Some(naive_now())) + .build(); + Instance::update(pool, self.instance.id, form).await?; + } } Ok(()) } diff --git a/src/scheduled_tasks.rs b/src/scheduled_tasks.rs index 05ff4b9e2..7433c9638 100644 --- a/src/scheduled_tasks.rs +++ b/src/scheduled_tasks.rs @@ -495,10 +495,7 @@ async fn update_instance_software( } }; if let Some(form) = form { - diesel::update(instance::table.find(instance.id)) - .set(form) - .execute(&mut conn) - .await?; + Instance::update(pool, instance.id, form).await?; } } info!("Finished updating instances software and versions...");