diff options
Diffstat (limited to 'lib/Travelynx/Command/maintenance.pm')
-rw-r--r-- | lib/Travelynx/Command/maintenance.pm | 105 |
1 files changed, 73 insertions, 32 deletions
diff --git a/lib/Travelynx/Command/maintenance.pm b/lib/Travelynx/Command/maintenance.pm index 5cbf982..c9c7ed6 100644 --- a/lib/Travelynx/Command/maintenance.pm +++ b/lib/Travelynx/Command/maintenance.pm @@ -1,4 +1,8 @@ package Travelynx::Command::maintenance; + +# Copyright (C) 2020-2023 Birte Kristina Friesel +# +# SPDX-License-Identifier: AGPL-3.0-or-later use Mojo::Base 'Mojolicious::Command'; use DateTime; @@ -10,22 +14,15 @@ has usage => sub { shift->extract_usage }; sub run { my ( $self, $filename ) = @_; - my $now = DateTime->now( time_zone => 'Europe/Berlin' ); - my $checkin_deadline = $now->clone->subtract( hours => 48 ); - my $verification_deadline = $now->clone->subtract( hours => 48 ); - my $deletion_deadline = $now->clone->subtract( hours => 72 ); - my $old_deadline = $now->clone->subtract( years => 1 ); + my $now = DateTime->now( time_zone => 'Europe/Berlin' ); + my $verification_deadline = $now->clone->subtract( hours => 48 ); + my $deletion_deadline = $now->clone->subtract( hours => 72 ); + my $old_deadline = $now->clone->subtract( years => 1 ); + my $old_notification_deadline = $now->clone->subtract( weeks => 4 ); my $db = $self->app->pg->db; my $tx = $db->begin; - my $res = $db->delete( 'in_transit', - { checkin_time => { '<', $checkin_deadline } } ); - - if ( my $rows = $res->rows ) { - printf( "Removed %d incomplete checkins\n", $rows ); - } - my $unverified = $db->select( 'users', 'id, email, extract(epoch from registered_at) as registered_ts', @@ -72,7 +69,7 @@ sub run { printf( "Pruned unverified user %d\n", $user->{id} ); } - $res = $db->delete( 'pending_passwords', + my $res = $db->delete( 'pending_passwords', { requested_at => { '<', $verification_deadline } } ); if ( my $rows = $res->rows ) { @@ -86,12 +83,40 @@ sub run { printf( "Pruned %d pending mail change(s)\n", $rows ); } + my $to_notify = $db->select( + 'users', + [ 'id', 'name', 'email', 'last_seen' ], + { + last_seen => { '<', $old_deadline }, + deletion_notified => undef + } + ); + + for my $user ( $to_notify->hashes->each ) { + say "Sending account deletion notification to uid $user->{id}..."; + $self->app->sendmail->age_deletion_notification( + name => $user->{name}, + email => $user->{email}, + last_seen => $user->{last_seen}, + login_url => $self->app->base_url_for('login')->to_abs, + account_url => $self->app->base_url_for('account')->to_abs, + imprint_url => $self->app->base_url_for('impressum')->to_abs, + ); + $self->app->users->mark_deletion_notified( uid => $user->{id} ); + } + my $to_delete = $db->select( 'users', ['id'], { deletion_requested => { '<', $deletion_deadline } } ); my @uids_to_delete = $to_delete->arrays->map( sub { shift->[0] } )->each; - $to_delete - = $db->select( 'users', ['id'], { last_seen => { '<', $old_deadline } } ); + $to_delete = $db->select( + 'users', + ['id'], + { + last_seen => { '<', $old_deadline }, + deletion_notified => { '<', $old_notification_deadline } + } + ); push( @uids_to_delete, $to_delete->arrays->map( sub { shift->[0] } )->each ); @@ -101,33 +126,49 @@ sub run { "About to delete %d accounts, which is quite a lot.\n", scalar @uids_to_delete ); + for my $uid (@uids_to_delete) { + my $journeys_res = $db->select( + 'journeys', + 'count(*) as count', + { user_id => $uid } + )->hash; + printf STDERR ( + " - UID %5d (%4d journeys)\n", + $uid, $journeys_res->{count} + ); + } say STDERR 'Aborting maintenance. Please investigate.'; exit(1); } for my $uid (@uids_to_delete) { say "Deleting uid ${uid}..."; - my $tokens_res = $db->delete( 'tokens', { user_id => $uid } ); - my $stats_res = $db->delete( 'journey_stats', { user_id => $uid } ); - my $journeys_res = $db->delete( 'journeys', { user_id => $uid } ); - my $transit_res = $db->delete( 'in_transit', { user_id => $uid } ); - my $password_res - = $db->delete( 'pending_passwords', { user_id => $uid } ); - my $user_res = $db->delete( 'users', { id => $uid } ); - + my $count = $self->app->users->delete( + uid => $uid, + db => $db, + in_transaction => 1 + ); printf( " %d tokens, %d monthly stats, %d journeys\n", - $tokens_res->rows, $stats_res->rows, $journeys_res->rows ); - - if ( $user_res->rows != 1 ) { - printf STDERR ( - "Deleted %d rows from users, expected 1. Rollback and abort.\n", - $user_res->rows - ); - exit(1); - } + $count->{tokens}, $count->{stats}, $count->{journeys} ); } $tx->commit; + + # Computing stats may take a while, but we've got all time in the + # world here. This means users won't have to wait when loading their + # own journey log. + say 'Generating missing stats ...'; + for + my $user ( $db->select( 'users', ['id'], { status => 1 } )->hashes->each ) + { + $tx = $db->begin; + $self->app->journeys->generate_missing_stats( uid => $user->{id} ); + $self->app->journeys->get_stats( + uid => $user->{id}, + year => $now->year + ); + $tx->commit; + } } 1; |