Skip to content

Commit

Permalink
Remove conditional mutex; get rid of mutex name
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Sep 10, 2024
1 parent 9c06bb0 commit 7f74dca
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 77 deletions.
9 changes: 2 additions & 7 deletions src/Internal/Workflow/WorkflowContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -633,17 +633,12 @@ public function uuid7(?DateTimeInterface $dateTime = null): PromiseInterface
return $this->sideEffect(static fn(): UuidInterface => \Ramsey\Uuid\Uuid::uuid7($dateTime));
}

public function mutex(string $name): MutexInterface
public function mutex(): MutexInterface

Check failure on line 636 in src/Internal/Workflow/WorkflowContext.php

View workflow job for this annotation

GitHub Actions / Psalm Validation (PHP 8.3, OS ubuntu-latest)

InvalidReturnType

src/Internal/Workflow/WorkflowContext.php:636:30: InvalidReturnType: Not all code paths of Temporal\Internal\Workflow\WorkflowContext::mutex end in a return statement, return type Temporal\Workflow\MutexInterface expected (see https://psalm.dev/011)

Check failure on line 636 in src/Internal/Workflow/WorkflowContext.php

View workflow job for this annotation

GitHub Actions / Psalm Validation (PHP 8.3, OS ubuntu-latest)

InvalidReturnType

src/Internal/Workflow/WorkflowContext.php:636:30: InvalidReturnType: Not all code paths of Temporal\Internal\Workflow\WorkflowContext::mutex end in a return statement, return type Temporal\Workflow\MutexInterface expected (see https://psalm.dev/011)
{
// todo
}

public function conditionalMutex(string $name, PromiseInterface|callable ...$lockConditions): MutexInterface
{
// todo
}

public function runLocked(string|MutexInterface $name, callable $callable): PromiseInterface
public function runLocked(MutexInterface $name, callable $callable): PromiseInterface

Check failure on line 641 in src/Internal/Workflow/WorkflowContext.php

View workflow job for this annotation

GitHub Actions / Psalm Validation (PHP 8.3, OS ubuntu-latest)

InvalidReturnType

src/Internal/Workflow/WorkflowContext.php:641:74: InvalidReturnType: Not all code paths of Temporal\Internal\Workflow\WorkflowContext::runLocked end in a return statement, return type React\Promise\PromiseInterface expected (see https://psalm.dev/011)

Check failure on line 641 in src/Internal/Workflow/WorkflowContext.php

View workflow job for this annotation

GitHub Actions / Psalm Validation (PHP 8.3, OS ubuntu-latest)

InvalidReturnType

src/Internal/Workflow/WorkflowContext.php:641:74: InvalidReturnType: Not all code paths of Temporal\Internal\Workflow\WorkflowContext::runLocked end in a return statement, return type React\Promise\PromiseInterface expected (see https://psalm.dev/011)
{
// todo
}
Expand Down
59 changes: 9 additions & 50 deletions src/Workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -974,75 +974,34 @@ public static function uuid7(?DateTimeInterface $dateTime = null): PromiseInterf
}

/**
* Get a mutex by name or create a new one.
* Create a new mutex.
*
* If a mutex is yielded without calling `lock()`, the Workflow will continue
* only when the lock is released.
*
* ```php
* yield Workflow::mutex('my-mutex');
* ```
* $this->>mutex = Workflow::mutex();
*
* Now to continue only when the lock is acquired:
* // Continue only when the lock is released
* yield $this->mutex;
*
* ```php
* // Continue only when the lock is acquired
* yield Workflow::mutex('my-mutex')->lock();
* ```
*
* Note: in this case, if the lock is already acquired, the Workflow will be blocked until it's released
*
* @param non-empty-string $name The name of the mutex.
*/
public static function mutex(string $name): MutexInterface
{
/** @var WorkflowContextInterface $context */
$context = self::getCurrentContext();

return $context->mutex($name);
}

/**
* Create a conditional mutex that is locked when any of the conditions are met.
*
* @param non-empty-string $name
*
* Example:
*
* Monitor when the number of threads exceeds 10:
*
* ```php
* #[WorkflowMethod]
* function start() {
* // Register a conditional mutex that will be locked when the number of threads exceeds 10
* Workflow::conditionalMutex(
* 'limit',
* fn() => count($this->threads) > 10,
* );
* // ...
* }
*
* #[SignalMethod]
* function addTask(Task $task) {
* yield Workflow::runLocked('limit', function() {
* $key = array_key_last($this->threads) + 1;
* yield $this->threads[$key] = Workflow::executeChildWorkflow(...);
* unset($this->threads[$key]);
* });
* }
* ```
* Note: in the last case, if the lock is already acquired, the Workflow will be blocked until it's released.
*/
public function conditionalMutex(string $name, PromiseInterface|callable ...$lockConditions): MutexInterface
public static function mutex(): MutexInterface
{
/** @var WorkflowContextInterface $context */
$context = self::getCurrentContext();

return $context->conditionalMutex($name, ...$lockConditions);
return $context->mutex();
}

/**
* Run a function when the mutex is released.
* The mutex is locked for the duration of the function if it's not a conditional mutex.
* Conditional mutexes are locked only when all conditions are met.
* The mutex is locked for the duration of the function.
*
* @see Workflow::conditionalMutex()
*
Expand Down
10 changes: 6 additions & 4 deletions src/Workflow/MutexInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@

interface MutexInterface
{
public function lock(): PromiseInterface;

/**
* @return non-empty-string The name of the mutex.
* Try to lock the mutex.
*
* @return bool true if the mutex was successfully locked, false otherwise.
*/
public function getName(): string;

public function lock(): PromiseInterface;
public function tryLock(): bool;

public function unlock(): void;

Expand Down
20 changes: 4 additions & 16 deletions src/Workflow/WorkflowContextInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,25 +377,13 @@ public function uuid4(): PromiseInterface;
public function uuid7(?DateTimeInterface $dateTime = null): PromiseInterface;

/**
* Get a mutex by name or create a new one.
*
* @param non-empty-string $name The name of the mutex.
*/
public function mutex(string $name): MutexInterface;

/**
* Create a conditional mutex.
*
* @param non-empty-string $name
* Create a mutex.
*/
public function conditionalMutex(string $name, PromiseInterface|callable ...$lockConditions): MutexInterface;
public function mutex(): MutexInterface;

/**
* Run a function when the mutex is released.
* The mutex is locked for the duration of the function if it's not a conditional mutex.
* Conditional mutexes are locked only when all conditions are met.
*
* @param non-empty-string|MutexInterface $name Mutex name or instance.
* The mutex will be locked for the duration of the function.
*/
public function runLocked(string|MutexInterface $name, callable $callable): PromiseInterface;
public function runLocked(MutexInterface $name, callable $callable): PromiseInterface;
}

0 comments on commit 7f74dca

Please sign in to comment.