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 <dessalines@users.noreply.github.com>
This commit is contained in:
Nutomic 2024-01-19 15:40:12 +01:00 committed by GitHub
parent 1be7dbde33
commit b58da11fb7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 29 additions and 8 deletions

View file

@ -64,6 +64,18 @@ impl Instance {
e => e, e => e,
} }
} }
pub async fn update(
pool: &mut DbPool<'_>,
instance_id: InstanceId,
form: InstanceForm,
) -> Result<usize, Error> {
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<usize, Error> { pub async fn delete(pool: &mut DbPool<'_>, instance_id: InstanceId) -> Result<usize, Error> {
let conn = &mut get_conn(pool).await?; let conn = &mut get_conn(pool).await?;
diesel::delete(instance::table.find(instance_id)) diesel::delete(instance::table.find(instance_id))

View file

@ -11,7 +11,7 @@ use activitypub_federation::{
protocol::context::WithContext, protocol::context::WithContext,
}; };
use anyhow::{Context, Result}; 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_api_common::{context::LemmyContext, federate_retry_sleep_duration};
use lemmy_apub::{activity_lists::SharedInboxActivities, FEDERATION_CONTEXT}; use lemmy_apub::{activity_lists::SharedInboxActivities, FEDERATION_CONTEXT};
use lemmy_db_schema::{ use lemmy_db_schema::{
@ -19,17 +19,17 @@ use lemmy_db_schema::{
source::{ source::{
activity::SentActivity, activity::SentActivity,
federation_queue_state::FederationQueueState, federation_queue_state::FederationQueueState,
instance::Instance, instance::{Instance, InstanceForm},
site::Site, site::Site,
}, },
utils::DbPool, utils::{naive_now, DbPool},
}; };
use lemmy_db_views_actor::structs::CommunityFollowerView; use lemmy_db_views_actor::structs::CommunityFollowerView;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use reqwest::Url; use reqwest::Url;
use std::{ use std::{
collections::{HashMap, HashSet}, collections::{HashMap, HashSet},
ops::Deref, ops::{Add, Deref},
time::Duration, time::Duration,
}; };
use tokio::{sync::mpsc::UnboundedSender, time::sleep}; 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(()) Ok(())
} }

View file

@ -495,10 +495,7 @@ async fn update_instance_software(
} }
}; };
if let Some(form) = form { if let Some(form) = form {
diesel::update(instance::table.find(instance.id)) Instance::update(pool, instance.id, form).await?;
.set(form)
.execute(&mut conn)
.await?;
} }
} }
info!("Finished updating instances software and versions..."); info!("Finished updating instances software and versions...");