Skip to content

Commit 6d928c0

Browse files
authored
Merge pull request #19 from john-shaffer/14-selective-cloudfront-invalidations
Selective CloudFront Invalidations
2 parents 09a80b4 + 68d46d8 commit 6d928c0

3 files changed

Lines changed: 79 additions & 26 deletions

File tree

src/Controller.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@ public function run() : void {
2020
1
2121
);
2222

23-
add_action(
24-
'wp2static_post_deploy_trigger',
25-
[ 'WP2StaticS3\Deployer', 'cloudfront_invalidate_all_items' ],
26-
15,
27-
1
28-
);
29-
3023
if ( defined( 'WP_CLI' ) ) {
3124
\WP_CLI::add_command(
3225
'wp2static s3',
@@ -185,6 +178,16 @@ public static function seedOptions() : void {
185178
);
186179

187180
$wpdb->query( $query );
181+
182+
$query = $wpdb->prepare(
183+
$query_string,
184+
'cfMaxPathsToInvalidate',
185+
'',
186+
'Maximum number of paths to invalidate before triggering a full invalidation',
187+
''
188+
);
189+
190+
$wpdb->query( $query );
188191
}
189192

190193
/**
@@ -410,6 +413,12 @@ public static function saveOptionsFromUI() : void {
410413
[ 'name' => 's3CacheControl' ]
411414
);
412415

416+
$wpdb->update(
417+
$table_name,
418+
[ 'value' => sanitize_text_field( $_POST['cfMaxPathsToInvalidate'] ) ],
419+
[ 'name' => 'cfMaxPathsToInvalidate' ]
420+
);
421+
413422
wp_safe_redirect( admin_url( 'admin.php?page=wp2static-s3' ) );
414423
exit;
415424
}

src/Deployer.php

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ public function upload_files( string $processed_site_path ) : void {
4545
$put_data['CacheControl'] = $cache_control;
4646
}
4747

48+
$cf_max_paths = Controller::getValue( 'cfMaxPathsToInvalidate' );
49+
$cf_max_paths = $cf_max_paths ? intval( $cf_max_paths ) : 0;
50+
$cf_stale_paths = [];
51+
4852
foreach ( $iterator as $filename => $file_object ) {
4953
$base_name = basename( $filename );
5054
if ( $base_name != '.' && $base_name != '..' ) {
@@ -87,12 +91,32 @@ public function upload_files( string $processed_site_path ) : void {
8791

8892
if ( $result['@metadata']['statusCode'] === 200 ) {
8993
\WP2Static\DeployCache::addFile( $cache_key );
94+
95+
if ( $cf_max_paths >= count( $cf_stale_paths ) ) {
96+
$cf_key = $cache_key;
97+
if ( 0 === substr_compare( $cf_key, '/index.html', -11) ) {
98+
$cf_key = substr( $cf_key, 0, -10 );
99+
}
100+
array_push( $cf_stale_paths, $cf_key );
101+
}
90102
}
91103
}
92104
}
105+
106+
$distribution_id = Controller::getValue( 'cfDistributionID' );
107+
$num_stale = count ( $cf_stale_paths );
108+
if ( $distribution_id && $num_stale > 0 ) {
109+
if ( $num_stale > $cf_max_paths ) {
110+
self::cloudfront_invalidate_all_items();
111+
} else {
112+
$path_text = ( $num_stale === 1 ) ? 'path' : 'paths';
113+
\WP2Static\WsLog::l( "Invalidating $num_stale CloudFront $path_text" );
114+
self::create_invalidation( $distribution_id, $cf_stale_paths );
115+
}
116+
}
93117
}
94118

95-
public function s3_client() : \Aws\S3\S3Client {
119+
public static function s3_client() : \Aws\S3\S3Client {
96120
$client_options = [
97121
'version' => 'latest',
98122
'region' => Controller::getValue( 's3Region' ),
@@ -124,7 +148,7 @@ public function s3_client() : \Aws\S3\S3Client {
124148
return new \Aws\S3\S3Client( $client_options );
125149
}
126150

127-
public function cloudfront_client() : \Aws\CloudFront\CloudFrontClient {
151+
public static function cloudfront_client() : \Aws\CloudFront\CloudFrontClient {
128152
/*
129153
If no credentials option, SDK attempts to load credentials from
130154
your environment in the following order:
@@ -173,29 +197,34 @@ public function cloudfront_client() : \Aws\CloudFront\CloudFrontClient {
173197
return $client;
174198
}
175199

176-
public function cloudfront_invalidate_all_items() : void {
200+
public static function create_invalidation( string $distribution_id, array $items )
201+
: string {
202+
$client = self::cloudfront_client();
203+
204+
return $client->createInvalidation(
205+
[
206+
'DistributionId' => $distribution_id,
207+
'InvalidationBatch' => [
208+
'CallerReference' => 'WP2Static S3 Add-on ' . time(),
209+
'Paths' => [
210+
'Items' => $items,
211+
'Quantity' => count( $items ),
212+
],
213+
],
214+
]
215+
);
216+
}
217+
218+
public static function cloudfront_invalidate_all_items() : void {
177219
if ( ! Controller::getValue( 'cfDistributionID' ) ) {
178220
return;
179221
}
180222

181-
\WP2Static\WsLog::l( 'Invalidating all CloudFront items' );
182-
183-
$client = self::cloudfront_client();
223+
\WP2Static\WsLog::l( 'Invalidating all CloudFront paths' );
184224

185225
try {
186-
$result = $client->createInvalidation(
187-
[
188-
'DistributionId' => Controller::getValue( 'cfDistributionID' ),
189-
'InvalidationBatch' => [
190-
'CallerReference' => 'WP2Static S3 Add-on ' . time(),
191-
'Paths' => [
192-
'Items' => [ '/*' ],
193-
'Quantity' => 1,
194-
],
195-
],
196-
]
197-
);
198-
226+
self::create_invalidation( Controller::getValue( 'cfDistributionID' ),
227+
[ '/*' ]);
199228
} catch ( AwsException $e ) {
200229
// output error message if fails
201230
error_log( $e->getMessage() );

views/s3-page.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,21 @@
217217
</td>
218218
</tr>
219219

220+
<tr>
221+
<td style="width:50%;">
222+
<label
223+
for="<?php echo $view['options']['cfMaxPathsToInvalidate']->name; ?>"
224+
><?php echo $view['options']['cfMaxPathsToInvalidate']->label; ?></label>
225+
</td>
226+
<td>
227+
<input
228+
id="<?php echo $view['options']['cfMaxPathsToInvalidate']->name; ?>"
229+
name="<?php echo $view['options']['cfMaxPathsToInvalidate']->name; ?>"
230+
type="text"
231+
value="<?php echo $view['options']['cfMaxPathsToInvalidate']->value !== '' ? $view['options']['cfMaxPathsToInvalidate']->value : ''; ?>"
232+
/>
233+
</td>
234+
</tr>
220235

221236
</tbody>
222237
</table>

0 commit comments

Comments
 (0)