Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |