From 4bb28fb8e64d193016cd09aa28aaba4b38e2ad1b Mon Sep 17 00:00:00 2001 From: Darin McBride Date: Thu, 12 Nov 2015 19:46:56 -0700 Subject: [PATCH] Modify the sitter auth cleanup. We don't delete expired auths, but, instead, only delete them when one of the two named empires are deleted. This means that if your sole sitter gets deleted, it won't be counted. Then, the empire cleanup code will look if there are any sitter auths that expired recently, or have not yet expired. If so, we just skip that empire altogether, it won't get cleaned up. This should extend an empire out to 20 days after the last sitter expires, or 20 days after its last login, whichever comes _later_. And then the regular essentia drain kicks in. While we'd like to change that as well, that change is not part of this update. --- bin/clean_up_empires.pl | 10 +++++++++- bin/clean_up_sitterauths.pl | 13 ++++--------- lib/Lacuna/DB/Result/Empire.pm | 3 +++ lib/Lacuna/DB/ResultSet/SitterAuths.pm | 15 ++++++--------- lib/Lacuna/RPC/Empire.pm | 6 +++++- 5 files changed, 27 insertions(+), 20 deletions(-) diff --git a/bin/clean_up_empires.pl b/bin/clean_up_empires.pl index 51b774a4..e4679adb 100644 --- a/bin/clean_up_empires.pl +++ b/bin/clean_up_empires.pl @@ -60,14 +60,22 @@ while (my $empire = $to_be_deleted->next) { out('Enabling Self Destruct For Inactivity'); my $abandons_tally; my $inactivity_time_out = Lacuna->config->get('self_destruct_after_inactive_days') || 20; +my $inactivity_time_out_formatted = $dtf->format_datetime(DateTime->now->subtract( days => $inactivity_time_out) ); my $inactives = $empires->search({ - last_login => { '<' => $dtf->format_datetime(DateTime->now->subtract( days => $inactivity_time_out) ) }, + last_login => { '<' => $inactivity_time_out_formatted }, self_destruct_active => 0, id => { '>' => 1}, is_admin => 0, # disable_self_destruct=> 0, }); while (my $empire = $inactives->next) { + # this checks if there were any sitters for this account that have not yet + # expired, or that have expired in the last 20 days. If so, then this + # account isn't ready to be checked for reaping yet. + my $recent_expires = $empire->sitterauths-> + search({ expiry => { '>' => $inactivity_time_out_formatted } })->count; + next if $recent_expires; + if ($empire->essentia >= 1) { unless (Lacuna->cache->get('empire_inactive',$empire->id)) { out('Preventing self-destruct by spending essentia.'); diff --git a/bin/clean_up_sitterauths.pl b/bin/clean_up_sitterauths.pl index 3fcae04f..ae92301e 100644 --- a/bin/clean_up_sitterauths.pl +++ b/bin/clean_up_sitterauths.pl @@ -16,18 +16,17 @@ my $rs = LD->resultset("SitterAuths"); # we do not warn about expired auths since we've already warned them for the # previous AUTH_WARNING_DAYS days. -out("Cleaning expired."); -$rs->clean_expired; - out("Checking for messages to send."); my $soon = DateTime->now()->add(days => $rs->AUTH_WARNING_DAYS); my $dtf = Lacuna->db->storage->datetime_parser; -# probably should join names in here. my $warnings = $rs->search( { - expiry => { '<' => $dtf->format_datetime($soon) } + expiry => { + '<' => $dtf->format_datetime($soon), + '>' => $dtf->format_datetime(DateTime->now), + }, }, { join => ['sitter', 'baby'], @@ -39,7 +38,6 @@ my $warnings = $rs->search( my %expiring_sitters; my %expiring_babies; my $now = DateTime->now; -#my $formatter = DateTime::Format::Duration->new(pattern => "%e days, %H:%M:%S"); while (my $auth = $warnings->next) { my $baby_name = $auth->get_column('baby_name'); @@ -66,8 +64,6 @@ for my $baby_name (sort keys %expiring_sitters) } sort keys %{$expiring_sitters{$baby_name}} ); - use Data::Dump; - dd \@table; LD->empire({name => $baby_name})->send_predefined_message( tags => ['Alert'], params => [ @table > 2 ? "Treaties" : "Treaty", @@ -93,7 +89,6 @@ for my $sitter_name (keys %expiring_babies) } sort keys %{$expiring_babies{$sitter_name}} ); - dd \@table; LD->empire({name => $sitter_name})->send_predefined_message( tags => ['Alert'], filename => 'expiring_babies.txt', diff --git a/lib/Lacuna/DB/Result/Empire.pm b/lib/Lacuna/DB/Result/Empire.pm index 0ae88120..2a336440 100644 --- a/lib/Lacuna/DB/Result/Empire.pm +++ b/lib/Lacuna/DB/Result/Empire.pm @@ -1153,6 +1153,9 @@ before delete => sub { $self->sent_messages->delete; $self->received_messages->delete; $self->medals->delete; + $self->babyauths->delete; + $self->sitterauths->delete; + my $planets = $self->planets; while ( my $planet = $planets->next ) { $planet->sanitize if ($planet->empire_id == $self->id); #In case of a cached space station diff --git a/lib/Lacuna/DB/ResultSet/SitterAuths.pm b/lib/Lacuna/DB/ResultSet/SitterAuths.pm index eedfe316..35f1243e 100644 --- a/lib/Lacuna/DB/ResultSet/SitterAuths.pm +++ b/lib/Lacuna/DB/ResultSet/SitterAuths.pm @@ -22,18 +22,15 @@ sub remove_auths_from_alliance } )->get_column('id')->all; - $self->search({ baby_id => { -in => \@ids }, sitter_id => $user->id })->delete; - $self->search({ sitter_id => { -in => \@ids }, baby_id => $user->id })->delete; + my $dtf = Lacuna->db->storage->datetime_parser; + my $now = $dtf->format_datetime(DateTime->now); + + # set the expiry to immediate. + $self->search({ baby_id => { -in => \@ids }, sitter_id => $user->id })->update({expiry => $now}); + $self->search({ sitter_id => { -in => \@ids }, baby_id => $user->id })->update({expiry => $now}); } sub new_auth_date { DateTime->now->add(days => VALID_AUTH_DAYS) } -sub clean_expired -{ - my ($self) = @_; - - $self->search({ expiry => { '<', \q[UTC_TIMESTAMP()] } })->delete; -} - no Moose; __PACKAGE__->meta->make_immutable(inline_constructor => 0); diff --git a/lib/Lacuna/RPC/Empire.pm b/lib/Lacuna/RPC/Empire.pm index cb3ff590..5c63df8c 100644 --- a/lib/Lacuna/RPC/Empire.pm +++ b/lib/Lacuna/RPC/Empire.pm @@ -1266,8 +1266,12 @@ sub deauthorize_sitters unless $opts->{empires} and ref $opts->{empires} eq 'ARRAY' and none { /\D/ } @{$opts->{empires}}; + my $dtf = Lacuna->db->storage->datetime_parser; + my $now = $dtf->format_datetime(DateTime->now); + + # set expiry to immediate $rs->search({baby_id => $baby_id, sitter_id => { in => $opts->{empires} }}) - ->delete(); + ->update({expiry => $now}); return $self->view_authorized_sitters($session); } -- 2.51.2