|
8 | 8 | use Aws\CloudFront\CloudFrontClient; |
9 | 9 | use Aws\Exception\AwsException; |
10 | 10 | use Aws\Credentials\Credentials; |
| 11 | +use WP2Static\WsLog; |
11 | 12 |
|
12 | 13 | class Deployer { |
13 | 14 |
|
@@ -173,11 +174,12 @@ public function upload_files( string $processed_site_path ) : void { |
173 | 174 | $num_stale = count( $cf_stale_paths ); |
174 | 175 | if ( $distribution_id && $num_stale > 0 ) { |
175 | 176 | if ( $num_stale > $cf_max_paths ) { |
176 | | - self::cloudfront_invalidate_all_items(); |
| 177 | + WsLog::l( 'Invalidating all CloudFront paths' ); |
| 178 | + self::invalidate_items( $distribution_id, [ '/*' ] ); |
177 | 179 | } else { |
178 | 180 | $path_text = ( $num_stale === 1 ) ? 'path' : 'paths'; |
179 | | - \WP2Static\WsLog::l( "Invalidating $num_stale CloudFront $path_text" ); |
180 | | - self::create_invalidation( $distribution_id, $cf_stale_paths ); |
| 181 | + WsLog::l( "Invalidating $num_stale CloudFront $path_text" ); |
| 182 | + self::invalidate_items( $distribution_id, $cf_stale_paths ); |
181 | 183 | } |
182 | 184 | } |
183 | 185 | } |
@@ -285,22 +287,13 @@ public static function create_invalidation( string $distribution_id, array $item |
285 | 287 | ); |
286 | 288 | } |
287 | 289 |
|
288 | | - public static function cloudfront_invalidate_all_items() : void { |
289 | | - if ( ! Controller::getValue( 'cfDistributionID' ) ) { |
290 | | - return; |
291 | | - } |
292 | | - |
293 | | - \WP2Static\WsLog::l( 'Invalidating all CloudFront paths' ); |
294 | | - |
| 290 | + public static function invalidate_items( string $distribution_id, array $items ) : ?string { |
295 | 291 | try { |
296 | | - self::create_invalidation( |
297 | | - Controller::getValue( 'cfDistributionID' ), |
298 | | - [ '/*' ] |
299 | | - ); |
300 | | - } catch ( AwsException $e ) { |
301 | | - // output error message if fails |
302 | | - error_log( $e->getMessage() ); |
| 292 | + return self::create_invalidation( $distribution_id, $items ); |
| 293 | + } catch (AwsException $e) { |
| 294 | + WsLog::l( 'Error creating CloudFront invalidation: ' . $e->getMessage() ); |
| 295 | + return null; |
303 | 296 | } |
304 | 297 | } |
305 | | -} |
306 | 298 |
|
| 299 | +} |
0 commit comments