From 0d7dbe6e804e52f9edac03a98799d2ac85e59221 Mon Sep 17 00:00:00 2001 From: Darin McBride Date: Mon, 17 Aug 2015 12:15:20 -0600 Subject: [PATCH] Add cleanup - deleting expired auths, emails for soon-to-expire auths. --- bin/clean_up_sitterauths.pl | 111 +++++++++++++++++++++++++ bin/run_daily.sh | 22 +++-- lib/L.pm | 3 +- lib/Lacuna/DB/ResultSet/SitterAuths.pm | 28 ++----- lib/Lacuna/RPC/Empire.pm | 10 ++- var/messages/expiring_babies.txt | 9 ++ var/messages/expiring_sitters.txt | 7 ++ 7 files changed, 156 insertions(+), 34 deletions(-) create mode 100644 bin/clean_up_sitterauths.pl create mode 100644 var/messages/expiring_babies.txt create mode 100644 var/messages/expiring_sitters.txt diff --git a/bin/clean_up_sitterauths.pl b/bin/clean_up_sitterauths.pl new file mode 100644 index 00000000..3fcae04f --- /dev/null +++ b/bin/clean_up_sitterauths.pl @@ -0,0 +1,111 @@ +use 5.10.0; +use strict; +use warnings; +use lib '/data/Lacuna-Server/lib'; +use Getopt::Long; + +use L; + +GetOptions( + 'quiet!' => \$quiet, +); + +out("Started."); + +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) } + }, + { + join => ['sitter', 'baby'], + '+select' => [ 'sitter.name', 'baby.name' ], + '+as' => [ 'sitter_name', 'baby_name' ], + }, + ); + +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'); + my $sitter_name = $auth->get_column('sitter_name'); + my %delta = $auth->expiry->subtract_datetime($now)->deltas(); + $delta{hours} = int($delta{minutes}/60);#/); + $delta{minutes} %= 60; + my $expiry = sprintf "%d days, %02d:%02d:%02d", @delta{qw(days hours minutes seconds)}; + + $expiring_sitters{$baby_name} {$sitter_name} = $expiry; + $expiring_babies {$sitter_name}{$baby_name} = $expiry; +} + +for my $baby_name (sort keys %expiring_sitters) +{ + out("Warning baby ", $baby_name); + + my @table = ( + [ 'Empire', 'Expiring in' ], + map { + [ + $_ => $expiring_sitters{$baby_name}{$_} + ] + } 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", + @table > 2 ? "multiple" : "a", + $baby_name ], + filename => 'expiring_sitters.txt', + attachments => { + table => \@table, + }, + ); +} + +for my $sitter_name (keys %expiring_babies) +{ + out("Warning sitter ", $sitter_name); + + my @table = ( + [ 'Empire', 'Expiring in' ], + map { + [ + $_ => $expiring_babies{$sitter_name}{$_} + ] + } sort keys %{$expiring_babies{$sitter_name}} + ); + + dd \@table; + LD->empire({name => $sitter_name})->send_predefined_message( + tags => ['Alert'], + filename => 'expiring_babies.txt', + params => [ @table > 2 ? "Treaties" : "Treaty", + @table > 2 ? "multiple" : "a", + $sitter_name ], + attachments => { + table => \@table, + }, + ); +} + + + +out("Done."); diff --git a/bin/run_daily.sh b/bin/run_daily.sh index 16a0aa92..bfdc95b3 100755 --- a/bin/run_daily.sh +++ b/bin/run_daily.sh @@ -2,11 +2,17 @@ export PATH=/data/apps/bin:$PATH touch /tmp/daily.log cd /data/Lacuna-Server/bin -perl trickle_essentia_veins.pl >> /tmp/daily.log 2>> /tmp/daily.log -perl determine_lottery_winner.pl >> /tmp/daily.log 2>> /tmp/daily.log -perl alert_inactive_users.pl >> /tmp/daily.log 2>> /tmp/daily.log -perl rotate_taxes_paid.pl >> /tmp/daily.log 2>> /tmp/daily.log -perl clean_up_mail.pl >> /tmp/daily.log 2>> /tmp/daily.log -perl record_rpc.pl >> /tmp/daily.log 2>> /tmp/daily.log -perl util/check_spy_count.pl --burn >> /tmp/daily.log 2>> /tmp/daily.log -perl check_starter_zones.pl >> /tmp/daily.log 2>> /tmp/daily.log + +# redirect everything. +exec >> /tmp/daily.log +exec 2>&1 + +perl trickle_essentia_veins.pl +perl determine_lottery_winner.pl +perl alert_inactive_users.pl +perl rotate_taxes_paid.pl +perl clean_up_mail.pl +perl record_rpc.pl +perl util/check_spy_count.pl --burn +perl check_starter_zones.pl +perl clean_up_sitterauths.pl diff --git a/lib/L.pm b/lib/L.pm index a521a093..2d627e83 100644 --- a/lib/L.pm +++ b/lib/L.pm @@ -21,9 +21,8 @@ binmode STDERR, ':encoding(UTF-8)'; our $quiet; sub out { - my $message = shift; unless ($quiet) { - say DateTime->now, " ", $message; + say DateTime->now, " ", @_; } } diff --git a/lib/Lacuna/DB/ResultSet/SitterAuths.pm b/lib/Lacuna/DB/ResultSet/SitterAuths.pm index 05665a14..eedfe316 100644 --- a/lib/Lacuna/DB/ResultSet/SitterAuths.pm +++ b/lib/Lacuna/DB/ResultSet/SitterAuths.pm @@ -5,6 +5,9 @@ use utf8; extends 'Lacuna::DB::ResultSet'; use DateTime; +use constant VALID_AUTH_DAYS => 60; +use constant AUTH_WARNING_DAYS => 7; + sub remove_auths_from_alliance { my ($self, $user) = @_; @@ -23,35 +26,14 @@ sub remove_auths_from_alliance $self->search({ sitter_id => { -in => \@ids }, baby_id => $user->id })->delete; } -sub new_auth_date { DateTime->now->add(days => 60) } - -sub hire_sitter -{ - my ($self, %opts) = @_; - die "Invalid hiring" unless ($opts{sitter} || $opts{sitter_id}) && ($opts{baby} || $opts{baby_id}); - - my $sitter = $opts{sitter} || $self->empire($opts{sitter_id}); - my $baby = $opts{baby} || $self->empire($opts{baby_id}); - - __PACKAGE__->new({ - sitter_id => $sitter->id, - baby_id => $baby->id, - expiry => new_auth_date(), - })->insert; -} +sub new_auth_date { DateTime->now->add(days => VALID_AUTH_DAYS) } sub clean_expired { my ($self) = @_; - my $db = Lacuna->db; - my $dtf = $db->storage->datetime_parser; - my $too_old = $dtf->format_datetime(DateTime->now); - - $self->search({ expiry => { '<', $too_old } })->delete; + $self->search({ expiry => { '<', \q[UTC_TIMESTAMP()] } })->delete; } -#sub - no Moose; __PACKAGE__->meta->make_immutable(inline_constructor => 0); diff --git a/lib/Lacuna/RPC/Empire.pm b/lib/Lacuna/RPC/Empire.pm index ccf5942a..6200a1e9 100644 --- a/lib/Lacuna/RPC/Empire.pm +++ b/lib/Lacuna/RPC/Empire.pm @@ -12,6 +12,7 @@ use Time::HiRes; use Text::CSV_XS; use Firebase::Auth; use Gravatar::URL; +use List::Util qw(none); sub find { my ($self, $session_id, $name) = @_; @@ -1259,9 +1260,16 @@ sub deauthorize_sitters my $baby = $self->get_empire_by_session($session); my $baby_id = $session->empire_id; - my $rs = $baby->sitters(); + my $rs = Lacuna->db->resultset('SitterAuths'); + confess [1009, "The 'empires' option must be an array of empire IDs"] + unless $opts->{empires} and ref $opts->{empires} eq 'ARRAY' and + none { /\D/ } @{$opts->{empires}}; + $rs->search({baby_id => $baby_id, sitter_id => { in => $opts->{empires} }}) + ->delete(); + + return $self->view_authorized_sitters($session); } __PACKAGE__->register_rpc_method_names( diff --git a/var/messages/expiring_babies.txt b/var/messages/expiring_babies.txt new file mode 100644 index 00000000..543efb34 --- /dev/null +++ b/var/messages/expiring_babies.txt @@ -0,0 +1,9 @@ +%s To Give Help Expiring +~~~ +We have %s "%1$s To Give Help" expiring. To continue to offer these other empires assistance administrate their empire during their far-too-regular vacations, they will need to renew the treaties in their profile settings. + +If they fail to do so prior to the expiry of the treaty, our access will be cut off with no further warning. + +Sincerely, + +Your diplomatic corps diff --git a/var/messages/expiring_sitters.txt b/var/messages/expiring_sitters.txt new file mode 100644 index 00000000..50d32fac --- /dev/null +++ b/var/messages/expiring_sitters.txt @@ -0,0 +1,7 @@ +%s To Accept Help Expiring +~~~ +We have %s "%1$s To Accept Help" expiring. If we wish to continue to allow these other empires to help administrate our empire during your well-deserved (and regular) vacations, you will need to renew the treaties in your profile settings. + +Sincerely, + +Your diplomatic corps -- 2.51.2