Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions bin/dmc-worktree-provider
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ $workspace = (string) ( $argv[2] ?? '' );
$value = (string) ( $argv[3] ?? '' );
$base_sha = (string) ( $argv[4] ?? '' );

if ( ! in_array($operation, array( 'capabilities', 'identity', 'task', 'safety', 'converge', 'plan' ), true) || ( 'capabilities' !== $operation && ( '' === $workspace || '' === $value ) ) || ( 'converge' === $operation && '' === $base_sha ) ) {
fwrite(STDERR, "Usage: dmc-worktree-provider capabilities | <identity|task|safety|converge|plan> <workspace-root> <handle|task-url|identity-token|plan-json> [base-sha]\n");
if ( ! in_array($operation, array( 'capabilities', 'identity', 'task', 'safety', 'converge', 'plan', 'primary-refresh' ), true) || ( 'capabilities' !== $operation && ( '' === $workspace || '' === $value ) ) || ( 'converge' === $operation && '' === $base_sha ) || ( 'primary-refresh' === $operation && '' !== $base_sha ) ) {
fwrite(STDERR, "Usage: dmc-worktree-provider capabilities | <identity|task|safety|converge|plan|primary-refresh> <workspace-root> <handle|task-url|identity-token|plan-json|repo> [base-sha]\n");
exit(2);
}

Expand All @@ -33,8 +33,10 @@ if ( 'plan' === $operation ) {
'task' => $provider->resolve_task($workspace, $value),
'safety' => $provider->attest_safety($workspace, $value),
'converge' => $provider->converge($workspace, $value, $base_sha),
'primary-refresh' => $provider->refresh_primary($workspace, $value),
};
}

fwrite(STDOUT, json_encode($result, JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR) . "\n");
exit('error' === ( $result['status'] ?? '' ) ? 1 : 0);
$failed = 'error' === ( $result['status'] ?? '' ) || ( 'primary-refresh' === $operation && 'refused' === ( $result['status'] ?? '' ) );
exit($failed ? 1 : 0);
36 changes: 36 additions & 0 deletions inc/Workspace/StandaloneFileLock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Bounded filesystem locking for database-independent workspace operations.
*
* @package DataMachineCode\Workspace
*/

namespace DataMachineCode\Workspace;

defined('ABSPATH') || defined('DATAMACHINE_CODE_STANDALONE') || exit;

final class StandaloneFileLock {

/** @return resource|null */
public static function acquire( string $path, float $timeout, int $sleep_microseconds ) {
$handle = @fopen($path, 'c');
if ( false === $handle ) {
return null;
}
$started = microtime(true);
do {
if ( flock($handle, LOCK_EX | LOCK_NB) ) {
return $handle;
}
usleep($sleep_microseconds);
} while ( microtime(true) - $started < $timeout );
fclose($handle);
return null;
}

/** @param resource $handle */
public static function release( $handle ): void {
flock($handle, LOCK_UN);
fclose($handle);
}
}
Loading
Loading