feat(cli): allow limiting how many users are updated

This commit is contained in:
əlemi 2024-07-17 22:41:46 +02:00
parent 32b7870bf2
commit c811eb25bd
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 13 additions and 6 deletions

View file

@ -65,11 +65,15 @@ pub enum CliCommand {
replies: bool, replies: bool,
}, },
/// update remote users /// update remote actors
Update { Update {
#[arg(long, short, default_value_t = 7)] #[arg(long, short, default_value_t = 10)]
/// number of days after which users should get updated /// number of days after which actors should get updated
days: i64, days: i64,
#[arg(long)]
/// stop after updating this many actors
limit: Option<u64>,
}, },
/// register a new local user /// register a new local user
@ -141,8 +145,8 @@ pub async fn run(ctx: upub::Context, command: CliCommand) -> Result<(), Box<dyn
Ok(relay(ctx, action).await?), Ok(relay(ctx, action).await?),
CliCommand::Fix { likes, shares, replies } => CliCommand::Fix { likes, shares, replies } =>
Ok(fix(ctx, likes, shares, replies).await?), Ok(fix(ctx, likes, shares, replies).await?),
CliCommand::Update { days } => CliCommand::Update { days, limit } =>
Ok(update_users(ctx, days).await?), Ok(update_users(ctx, days, limit).await?),
CliCommand::Register { username, password, display_name, summary, avatar_url, banner_url } => CliCommand::Register { username, password, display_name, summary, avatar_url, banner_url } =>
Ok(register(ctx, username, password, display_name, summary, avatar_url, banner_url).await?), Ok(register(ctx, username, password, display_name, summary, avatar_url, banner_url).await?),
CliCommand::Nuke { for_real, delete_objects } => CliCommand::Nuke { for_real, delete_objects } =>

View file

@ -2,7 +2,7 @@ use futures::TryStreamExt;
use sea_orm::{ActiveModelTrait, ActiveValue::Set, ColumnTrait, EntityTrait, QueryFilter}; use sea_orm::{ActiveModelTrait, ActiveValue::Set, ColumnTrait, EntityTrait, QueryFilter};
use upub::traits::Fetcher; use upub::traits::Fetcher;
pub async fn update_users(ctx: upub::Context, days: i64) -> Result<(), sea_orm::DbErr> { pub async fn update_users(ctx: upub::Context, days: i64, limit: Option<u64>) -> Result<(), sea_orm::DbErr> {
let mut count = 0; let mut count = 0;
let mut stream = upub::model::actor::Entity::find() let mut stream = upub::model::actor::Entity::find()
.filter(upub::model::actor::Column::Updated.lt(chrono::Utc::now() - chrono::Duration::days(days))) .filter(upub::model::actor::Column::Updated.lt(chrono::Utc::now() - chrono::Duration::days(days)))
@ -12,6 +12,9 @@ pub async fn update_users(ctx: upub::Context, days: i64) -> Result<(), sea_orm::
while let Some(user) = stream.try_next().await? { while let Some(user) = stream.try_next().await? {
if ctx.is_local(&user.id) { continue } if ctx.is_local(&user.id) { continue }
if let Some(limit) = limit {
if count >= limit { break }
}
match ctx.pull(&user.id).await.map(|x| x.actor()) { match ctx.pull(&user.id).await.map(|x| x.actor()) {
Err(e) => tracing::warn!("could not update user {}: {e}", user.id), Err(e) => tracing::warn!("could not update user {}: {e}", user.id),
Ok(Err(e)) => tracing::warn!("could not update user {}: {e}", user.id), Ok(Err(e)) => tracing::warn!("could not update user {}: {e}", user.id),