Skip to content

Commit 1b69860

Browse files
committed
Fix workspace lock lint findings
1 parent 61861a8 commit 1b69860

2 files changed

Lines changed: 28 additions & 30 deletions

File tree

inc/Cli/Commands/WorkspaceCommand.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2302,10 +2302,12 @@ public function worktree( array $args, array $assoc_args ): void {
23022302
}
23032303

23042304
if ( 'locks' === $operation ) {
2305-
$dry_run = ! empty($assoc_args['dry-run']) || empty($assoc_args['prune-stale']);
2306-
$result = ! empty($assoc_args['prune-stale'])
2307-
? WorkspaceMutationLock::prune_stale($this->workspace_path, $dry_run)
2308-
: WorkspaceMutationLock::status($this->workspace_path);
2305+
$workspace = new Workspace();
2306+
$workspace_path = $workspace->get_path();
2307+
$dry_run = ! empty($assoc_args['dry-run']) || empty($assoc_args['prune-stale']);
2308+
$result = ! empty($assoc_args['prune-stale'])
2309+
? WorkspaceMutationLock::prune_stale($workspace_path, $dry_run)
2310+
: WorkspaceMutationLock::status($workspace_path);
23092311
$this->render_workspace_lock_result($result, $assoc_args, ! empty($assoc_args['prune-stale']));
23102312
return;
23112313
}
@@ -2838,7 +2840,7 @@ static function ( array $lock ): array {
28382840
if ( ! empty($guidance) ) {
28392841
WP_CLI::log(sprintf('Status: %s', (string) ( $guidance['status_command'] ?? 'wp datamachine-code workspace worktree locks --format=json' )));
28402842
WP_CLI::log(sprintf('Prune: %s', (string) ( $guidance['dry_run_command'] ?? 'wp datamachine-code workspace worktree locks --prune-stale --dry-run --format=json' )));
2841-
WP_CLI::log((string) ( $guidance['safety'] ?? 'Active filesystem flocks are not pruned.' ));
2843+
WP_CLI::log( (string) ( $guidance['safety'] ?? 'Active filesystem flocks are not pruned.' ) );
28422844
}
28432845
}
28442846

inc/Workspace/WorkspaceMutationLock.php

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,9 @@ final class WorkspaceMutationLock {
2929

3030
private int $lock_id = 0;
3131

32-
private string $lock_path = '';
33-
34-
private function __construct( $handle, int $lock_id = 0, string $lock_path = '' ) {
35-
$this->handle = $handle;
36-
$this->lock_id = $lock_id;
37-
$this->lock_path = $lock_path;
32+
private function __construct( $handle, int $lock_id = 0 ) {
33+
$this->handle = $handle;
34+
$this->lock_id = $lock_id;
3835
}
3936

4037
/**
@@ -132,7 +129,7 @@ public static function acquire( string $workspace_path, string $repo, int $timeo
132129
return $lock_id;
133130
}
134131

135-
return new self($handle, (int) $lock_id, $lock_path);
132+
return new self($handle, (int) $lock_id);
136133
}
137134

138135
if ( 0 === $timeout || ( microtime(true) - $started ) >= $timeout ) {
@@ -162,9 +159,8 @@ public function release(): void {
162159
WorkspaceLockStore::release($this->lock_id);
163160
flock($this->handle, LOCK_UN); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_flock
164161
fclose($this->handle); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose
165-
$this->handle = null;
166-
$this->lock_id = 0;
167-
$this->lock_path = '';
162+
$this->handle = null;
163+
$this->lock_id = 0;
168164
}
169165

170166
/**
@@ -304,11 +300,11 @@ private static function busy_error_data( string $repo, string $lock_path ): arra
304300
*/
305301
private static function recovery_guidance(): array {
306302
return array(
307-
'status_command' => 'wp datamachine-code workspace worktree locks --format=json',
308-
'dry_run_command' => 'wp datamachine-code workspace worktree locks --prune-stale --dry-run --format=json',
309-
'apply_command' => 'wp datamachine-code workspace worktree locks --prune-stale --format=json',
310-
'safety' => 'Only expired DB rows and old unlocked filesystem lock files are pruned. Active filesystem flocks are never removed by this command.',
311-
'active_lock_note' => 'If a filesystem lock is active without DB owner evidence, another process still holds the OS file descriptor or crashed without releasing an operator-visible DB row. Inspect running DMC/WP-CLI processes before retrying.',
303+
'status_command' => 'wp datamachine-code workspace worktree locks --format=json',
304+
'dry_run_command' => 'wp datamachine-code workspace worktree locks --prune-stale --dry-run --format=json',
305+
'apply_command' => 'wp datamachine-code workspace worktree locks --prune-stale --format=json',
306+
'safety' => 'Only expired DB rows and old unlocked filesystem lock files are pruned. Active filesystem flocks are never removed by this command.',
307+
'active_lock_note' => 'If a filesystem lock is active without DB owner evidence, another process still holds the OS file descriptor or crashed without releasing an operator-visible DB row. Inspect running DMC/WP-CLI processes before retrying.',
312308
);
313309
}
314310

@@ -386,22 +382,22 @@ private static function filesystem_lock_entry( string $file ): array {
386382
}
387383

388384
if ( ! flock($handle, LOCK_EX | LOCK_NB) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_flock
389-
$entry['state'] = 'active';
390-
$entry['reason'] = 'filesystem_flock_held';
391-
$entry['owner_evidence'] = self::owner_evidence_for_lock($lock_key, $scope);
392-
$entry['recovery_command'] = 'wp datamachine-code workspace worktree locks --format=json';
393-
$entry['safe_to_prune'] = false;
394-
$entry['operator_guidance'] = 'An active OS flock cannot be safely pruned. Inspect the owner evidence or running DMC/WP-CLI processes and retry after the holder exits.';
385+
$entry['state'] = 'active';
386+
$entry['reason'] = 'filesystem_flock_held';
387+
$entry['owner_evidence'] = self::owner_evidence_for_lock($lock_key, $scope);
388+
$entry['recovery_command'] = 'wp datamachine-code workspace worktree locks --format=json';
389+
$entry['safe_to_prune'] = false;
390+
$entry['operator_guidance'] = 'An active OS flock cannot be safely pruned. Inspect the owner evidence or running DMC/WP-CLI processes and retry after the holder exits.';
395391
fclose($handle); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose
396392
return $entry;
397393
}
398394

399395
flock($handle, LOCK_UN); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_flock
400396
fclose($handle); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose
401-
$stale = false !== $mtime && $mtime < $cutoff;
402-
$entry['state'] = $stale ? 'stale' : 'recent';
403-
$entry['reason'] = $stale ? 'unlocked_stale_file' : 'unlocked_recent_file';
404-
$entry['safe_to_prune'] = $stale;
397+
$stale = false !== $mtime && $mtime < $cutoff;
398+
$entry['state'] = $stale ? 'stale' : 'recent';
399+
$entry['reason'] = $stale ? 'unlocked_stale_file' : 'unlocked_recent_file';
400+
$entry['safe_to_prune'] = $stale;
405401
$entry['recovery_command'] = $stale
406402
? 'wp datamachine-code workspace worktree locks --prune-stale --dry-run --format=json'
407403
: 'wp datamachine-code workspace worktree locks --format=json';

0 commit comments

Comments
 (0)