Skip to content

Commit

Permalink
Adds user session purging.
Browse files Browse the repository at this point in the history
  • Loading branch information
oalders committed Oct 21, 2014
1 parent 52ff156 commit 97f504f
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions lib/MetaCPAN/Script/Session.pm
@@ -0,0 +1,59 @@
package MetaCPAN::Script::Session;

use strict;
use warnings;

use DateTime;
use Moose;

with 'MetaCPAN::Role::Common', 'MooseX::Getopt';

sub run {
my $self = shift;

my $scroll = $self->es()->scrolled_search(
size => 10_000,
scroll => '1m',
index => 'user',
type => 'session',
query => { filtered => { query => { match_all => {} }, }, },
);

my @delete;

my $cutoff = DateTime->now->subtract( months => 1 )->epoch;
while ( my $search = $scroll->next ) {
if ( $search->{_source}->{__updated} < $cutoff ) {
push @delete, $search->{_id};
}

if ( scalar @delete >= 10_000 ) {
$self->delete(@delete);
@delete = ();
}

}
$self->delete(@delete) if @delete;
}

sub delete {
my $self = shift;
my @delete = @_;

$self->es->bulk(
index => 'user',
type => 'session',
actions => [ map { +{ delete => { id => $_ } } } @delete ],
);
}

__PACKAGE__->meta->make_immutable;
1;

=pod
Purges user sessions. The timestamp field doesn't appear to get populated in
the session type, so we just iterate over the sessions for the time being and
perform bulk delete.
=cut

0 comments on commit 97f504f

Please sign in to comment.