Skip to content

Commit 9039440

Browse files
committed
Add a cooldown for deleting stale S3 objects
Listing the bucket to find stale objects can be slow and costly on every deploy. Add a deleteStaleObjectsCooldown option, in hours and defaulting to 24, and skip the deletion if it last ran for the same bucket and prefix within that time. 0 runs it after every full deployment. The last run is kept in a WordPress option that uninstall.php deletes.
1 parent 27770ce commit 9039440

5 files changed

Lines changed: 93 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
- Add a "Delete Stale Objects" option to the S3 add-on. After a full
44
deployment or Direct Deploy, it deletes objects under the path prefix
5-
that aren't part of the site. It defaults to "off".
5+
that aren't part of the site. It defaults to "off". It runs at most
6+
once per "Delete Stale Objects Cooldown", which defaults to 24 hours.
67
- Fix paths that were detected before matching a Paths to Ignore pattern
78
still being crawled and deployed. They're now removed from the detected
89
and crawled files when crawling or running Direct Deploy.

‎src/S3/Deployer.php‎

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ class Deployer {
2222

2323
const DEFAULT_NAMESPACE = 'static-deploy-addon-s3/default';
2424

25+
/**
26+
* WordPress option recording when stale objects were last deleted,
27+
* and from which bucket and prefix
28+
*/
29+
const STALE_OBJECTS_LAST_RUN_OPTION = 'static_deploy_s3_deleteStaleObjectsLastRun';
30+
2531
private int $cf_max_paths;
2632

2733
/**
@@ -290,6 +296,9 @@ public function uploadFiles( string $processed_site_path ): void {
290296
* Delete objects under the bucket prefix that are not part of
291297
* the site, if the deleteStaleObjects option is set.
292298
*
299+
* Skipped if it already ran for the same bucket and prefix within
300+
* the deleteStaleObjectsCooldown period.
301+
*
293302
* Call this only after deploying the whole site. An object is part
294303
* of the site if its path was passed to uploadFilesIter by this
295304
* instance, or if it is in the crawled files. The crawled files
@@ -307,6 +316,23 @@ public function deleteStaleObjects(): void {
307316
return;
308317
}
309318

319+
$bucket = S3Options::getValue( 'bucketName' );
320+
$prefix = self::s3Prefix();
321+
$target = $bucket . '/' . $prefix;
322+
$cooldown_hours = intval( S3Options::getValue( 'deleteStaleObjectsCooldown' ) );
323+
$last_run = get_option( self::STALE_OBJECTS_LAST_RUN_OPTION );
324+
if ( $cooldown_hours > 0
325+
&& is_array( $last_run )
326+
&& ( $last_run['target'] ?? null ) === $target
327+
&& time() - intval( $last_run['time'] ?? 0 ) < $cooldown_hours * HOUR_IN_SECONDS ) {
328+
WsLog::l(
329+
'Not deleting stale S3 objects because they were deleted' .
330+
" less than {$cooldown_hours} hours ago."
331+
);
332+
return;
333+
}
334+
$started_at = time();
335+
310336
$current_keys = $this->current_keys;
311337
foreach ( CrawledFiles::getPathsIter() as $path_info ) {
312338
if ( $path_info->status !== 404 ) {
@@ -315,8 +341,6 @@ public function deleteStaleObjects(): void {
315341
}
316342

317343
$filtering = $mode === 'except-ignored' ? new FileFiltering() : null;
318-
$bucket = S3Options::getValue( 'bucketName' );
319-
$prefix = self::s3Prefix();
320344
$pages = $this->s3_client->getPaginator(
321345
'ListObjectsV2',
322346
[
@@ -343,6 +367,7 @@ public function deleteStaleObjects(): void {
343367

344368
if ( $stale_keys === [] ) {
345369
WsLog::l( 'No stale S3 objects to delete.' );
370+
$this->recordStaleObjectsDeleted( $target, $started_at );
346371
return;
347372
}
348373

@@ -390,9 +415,25 @@ public function deleteStaleObjects(): void {
390415

391416
WsLog::l( "Deleted $deleted_ct stale S3 objects, $error_ct failed." );
392417

418+
// Record time even if some deletions failed because
419+
// listing the bucket can be expensive. It's okay if
420+
// some files stick around for the next deletion pass.
421+
$this->recordStaleObjectsDeleted( $target, $started_at );
422+
393423
$this->invalidateStalePaths();
394424
}
395425

426+
private function recordStaleObjectsDeleted( string $target, int $time ): void {
427+
update_option(
428+
self::STALE_OBJECTS_LAST_RUN_OPTION,
429+
[
430+
'target' => $target,
431+
'time' => $time,
432+
],
433+
false
434+
);
435+
}
436+
396437
/**
397438
* Returns the deploy cache key for a path, which is the
398439
* path of the file it is deployed to.

‎src/S3/S3Options.php‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,17 @@ public static function getSpecs(): array {
176176
' This does not happen when deploying a single post.',
177177
allowed_values: [ 'off', 'all', 'except-ignored' ],
178178
),
179+
new OptionSpec(
180+
'integer',
181+
self::getName( 'deleteStaleObjectsCooldown' ),
182+
'24',
183+
'Delete Stale Objects Cooldown',
184+
'Minimum hours between deletions of stale objects,' .
185+
' since listing the bucket can be slow and costly.' .
186+
' Changing the bucket or path prefix starts a new cooldown.' .
187+
' 0 deletes stale objects after every full deployment.',
188+
min_value: 0,
189+
),
179190
new OptionSpec(
180191
'string',
181192
self::getName( 'headerCacheControl' ),
@@ -253,6 +264,7 @@ public static function getPageData(): array {
253264
self::getName( 'objectAcl' ),
254265
self::getName( 'concurrency' ),
255266
self::getName( 'deleteStaleObjects' ),
267+
self::getName( 'deleteStaleObjectsCooldown' ),
256268
],
257269
],
258270
[

‎tests/aws/S3SyncTest.php‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,17 @@ protected function setUp(): void {
4949
$this->setOptionValue( 's3_awsRegion', 'us-east-1' );
5050
$this->setOptionValue( 's3_awsSecretAccessKey', 'test' );
5151
$this->setOptionValue( 's3_bucketName', self::BUCKET );
52+
$this->setOptionValue( 's3_deleteStaleObjectsCooldown', '0' );
5253
$this->pluginCli( [ 'addons', 'enable', 'static-deploy-addon-s3' ] );
54+
$this->wpCli(
55+
[ 'eval', "delete_option( 'static_deploy_s3_deleteStaleObjectsLastRun' );" ]
56+
);
5357
}
5458

5559
protected function tearDown(): void {
5660
$this->setOptionValue( 's3_bucketPrefix', '' );
5761
$this->setOptionValue( 's3_deleteStaleObjects', 'off' );
62+
$this->setOptionValue( 's3_deleteStaleObjectsCooldown', '24' );
5863
}
5964

6065
/**
@@ -153,6 +158,35 @@ public function testObjectsOutsidePrefixAreKept(): void {
153158
);
154159
}
155160

161+
public function testStaleObjectsAreKeptDuringCooldown(): void {
162+
$this->setOptionValue( 's3_deleteStaleObjects', 'all' );
163+
$this->setOptionValue( 's3_deleteStaleObjectsCooldown', '24' );
164+
$this->putObject( 'stray.html' );
165+
$this->pluginCli( [ 'full-workflow' ] );
166+
$this->assertKeysAbsent( [ 'stray.html' ] );
167+
$this->putObject( 'stray.html' );
168+
169+
$this->pluginCli( [ 'full-workflow' ] );
170+
171+
$this->assertKeysPresent( [ 'stray.html' ] );
172+
}
173+
174+
/**
175+
* The cooldown applies to the bucket and prefix that were
176+
* cleaned up, not to a new deployment target.
177+
*/
178+
public function testCooldownIsResetByChangingPrefix(): void {
179+
$this->setOptionValue( 's3_deleteStaleObjects', 'all' );
180+
$this->setOptionValue( 's3_deleteStaleObjectsCooldown', '24' );
181+
$this->pluginCli( [ 'full-workflow' ] );
182+
$this->setOptionValue( 's3_bucketPrefix', 'site' );
183+
$this->putObject( 'site/stray.html' );
184+
185+
$this->pluginCli( [ 'full-workflow' ] );
186+
187+
$this->assertKeysAbsent( [ 'site/stray.html' ] );
188+
}
189+
156190
/**
157191
* A deploy that has nothing to upload must not empty the bucket.
158192
*/

‎uninstall.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@
2626
// phpcs:ignore WordPress.DB.DirectDatabaseQuery
2727
$wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $static_deploy_table_name ) );
2828
}
29+
30+
delete_option( 'static_deploy_s3_deleteStaleObjectsLastRun' );

0 commit comments

Comments
 (0)