Skip to content

Commit 0bd5aa0

Browse files
committed
Merge pull request #1544 from CPAN-API/leo/purge_script
script to do purging and little cleanup of Fastly
2 parents 4174be8 + b427329 commit 0bd5aa0

File tree

3 files changed

+151
-29
lines changed

3 files changed

+151
-29
lines changed

bin/purge.pl

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env perl
2+
3+
use strict;
4+
5+
# Purge stuff
6+
7+
=head1 NAME
8+
9+
purge.pl
10+
11+
=head1 SYNOPSIS
12+
13+
purge.pl --all
14+
purge.pl --tag foo --tag bar
15+
purge.pl --url '/about/'
16+
17+
=head1 DESCRIPTION
18+
19+
Script to purge things from Fastly CDN.
20+
21+
=cut
22+
23+
use MetaCPAN::Web;
24+
use Getopt::Long::Descriptive;
25+
use List::MoreUtils qw(any);
26+
27+
my ( $opt, $usage ) = describe_options(
28+
'purge.pl %o <some-arg>',
29+
[ 'all=s', "purge all", ],
30+
[ 'tag|t=s@', "tag(s) to purge", ],
31+
[ 'url|t=s@', "url(s) to purge", ],
32+
[],
33+
[ 'help', "print usage message and exit" ],
34+
);
35+
36+
print( $usage->text ), exit if $opt->help;
37+
38+
my $c = MetaCPAN::Web->new();
39+
40+
if ( $opt->all ) {
41+
$c->cdn_purge_all();
42+
43+
}
44+
else {
45+
46+
my $tags = $opt->tag;
47+
my $urls = $opt->url;
48+
49+
$c->cdn_purge_now(
50+
{
51+
tags => $tags,
52+
urls => $urls
53+
}
54+
);
55+
56+
}

cpanfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ requires 'Encode', '2.51';
3131
requires 'Exporter';
3232
requires 'Format::Human::Bytes';
3333
requires 'File::Path';
34+
requires 'Getopt::Long::Descriptive';
3435
requires 'Gravatar::URL';
3536
requires 'HTML::Escape';
3637
requires 'HTML::Restrict', '2.2.2';

lib/MetaCPAN/Web/Role/Fastly.pm

Lines changed: 94 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,27 @@ use MetaCPAN::Web::Types qw( ArrayRef Str );
99
1010
MetaCPAN::Web::Role::Fastly - Methods for fastly intergration
1111
12+
=head1 METHODS
13+
14+
The following:
15+
16+
=head2 $c->add_surrogate_key('foo');
17+
18+
=head2 $c->purge_surrogate_key('bar');
19+
20+
=head2 $c->cdn_cache_ttl(3600);
21+
22+
Are applied when:
23+
24+
=head2 $c->fastly_magic()
25+
26+
is run in the L<end>, however if
27+
28+
=head2 $c->cdn_never_cache(1)
29+
30+
Is set fastly is forced to NOT cache, no matter
31+
what other options have been set
32+
1233
=cut
1334

1435
## Stuff for working with Fastly CDN
@@ -39,6 +60,21 @@ has '_surrogate_keys_to_purge' => (
3960
},
4061
);
4162

63+
# How long should the CDN cache, irrespective of
64+
# other cache headers
65+
has 'cdn_cache_ttl' => (
66+
is => 'rw',
67+
isa => 'Int',
68+
default => sub {0},
69+
);
70+
71+
# Make sure the CDN NEVER caches, ignore any other cdn_cache_ttl settings
72+
has 'cdn_never_cache' => (
73+
is => 'rw',
74+
isa => 'Bool',
75+
default => sub {0},
76+
);
77+
4278
sub _net_fastly {
4379
my $c = shift;
4480

@@ -55,28 +91,24 @@ sub _net_fastly {
5591
sub fastly_magic {
5692
my $c = shift;
5793

58-
# Surrogate key caching and purging
59-
if ( $c->has_surrogate_keys ) {
60-
61-
# See http://www.fastly.com/blog/surrogate-keys-part-1/
62-
$c->res->header( 'Surrogate-Key' => $c->join_surrogate_keys(' ') );
63-
}
64-
94+
# Some action must have triffered a purge
6595
if ( $c->has_surrogate_keys_to_purge ) {
6696

6797
# Something changed, means we need to purge some keys
98+
my @tags = $c->surrogate_keys_to_purge();
6899

69-
my $net_fastly = $c->_net_fastly();
70-
return unless $net_fastly;
71-
72-
my $fsi = $c->config->{fastly_service_id};
100+
$c->cdn_purge_now(
101+
{
102+
tags => \@tags,
103+
}
104+
);
105+
}
73106

74-
foreach my $purge_key ( $c->surrogate_keys_to_purge() ) {
75-
my $purge_string
76-
= "https://metacpan.org/${fsi}/purge/${purge_key}";
107+
# Surrogate key caching and purging
108+
if ( $c->has_surrogate_keys ) {
77109

78-
$net_fastly->purge($purge_string);
79-
}
110+
# See http://www.fastly.com/blog/surrogate-keys-part-1/
111+
$c->res->header( 'Surrogate-Key' => $c->join_surrogate_keys(' ') );
80112
}
81113

82114
# Set the caching at CDN, seperate to what the user's browser does
@@ -89,6 +121,7 @@ sub fastly_magic {
89121
}
90122
elsif ( my $ttl = $c->cdn_cache_ttl ) {
91123

124+
# TODO: https://www.fastly.com/blog/stale-while-revalidate/
92125
# Use this value
93126
$c->res->header( 'Surrogate-Control' => 'max-age=' . $ttl );
94127

@@ -101,19 +134,51 @@ sub fastly_magic {
101134
}
102135
}
103136

104-
# How long should the CDN cache, irrespective of
105-
# other cache headers
106-
has 'cdn_cache_ttl' => (
107-
is => 'rw',
108-
isa => 'Int',
109-
default => sub {0},
110-
);
137+
=head2 cdn_purge_now
111138
112-
# Make sure the CDN NEVER caches, ignore any other cdn_cache_ttl settings
113-
has 'cdn_never_cache' => (
114-
is => 'rw',
115-
isa => 'Bool',
116-
default => sub {0},
117-
);
139+
$c->cdn_purge_now({
140+
tags => [ 'foo', 'bar' ]
141+
urls => [ 'this', 'and/that' ],
142+
});
143+
144+
=cut
145+
146+
sub cdn_purge_now {
147+
my ( $c, $args ) = @_;
148+
149+
my $net_fastly = $c->_net_fastly();
150+
return unless $net_fastly;
151+
152+
my $fsi = $c->config->{fastly_service_id};
153+
154+
foreach my $tag ( @{ $args->{tags} || [] } ) {
155+
my $purge_string = "https://metacpan.org/${fsi}/purge/${tag}";
156+
$net_fastly->purge($purge_string);
157+
}
158+
159+
foreach my $url ( @{ $args->{urls} || [] } ) {
160+
my $purge_string = "https://metacpan.org/${url}";
161+
$net_fastly->purge($purge_string);
162+
}
163+
}
164+
165+
=head2 cdn_purge_all
166+
167+
$c->cdn_purge_all()
168+
169+
=cut
170+
171+
sub cdn_purge_all {
172+
my $c = shift;
173+
my $net_fastly = $c->_net_fastly();
174+
175+
die "No access" unless $net_fastly;
176+
177+
my $fsi = $c->config->{fastly_service_id};
178+
179+
my $purge_string = "/service/${fsi}/purge_all";
180+
181+
$net_fastly->purge($purge_string);
182+
}
118183

119184
1;

0 commit comments

Comments
 (0)