Skip to content

Commit 58b7988

Browse files
committed
Remove conditional mutex; get rid of mutex name
1 parent 9c06bb0 commit 58b7988

4 files changed

Lines changed: 25 additions & 81 deletions

File tree

src/Internal/Workflow/WorkflowContext.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -633,17 +633,12 @@ public function uuid7(?DateTimeInterface $dateTime = null): PromiseInterface
633633
return $this->sideEffect(static fn(): UuidInterface => \Ramsey\Uuid\Uuid::uuid7($dateTime));
634634
}
635635

636-
public function mutex(string $name): MutexInterface
636+
public function mutex(): MutexInterface
637637
{
638638
// todo
639639
}
640640

641-
public function conditionalMutex(string $name, PromiseInterface|callable ...$lockConditions): MutexInterface
642-
{
643-
// todo
644-
}
645-
646-
public function runLocked(string|MutexInterface $name, callable $callable): PromiseInterface
641+
public function runLocked(MutexInterface $mutex, callable $callable): PromiseInterface
647642
{
648643
// todo
649644
}

src/Workflow.php

Lines changed: 13 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -974,85 +974,44 @@ public static function uuid7(?DateTimeInterface $dateTime = null): PromiseInterf
974974
}
975975

976976
/**
977-
* Get a mutex by name or create a new one.
977+
* Create a new mutex.
978978
*
979979
* If a mutex is yielded without calling `lock()`, the Workflow will continue
980980
* only when the lock is released.
981981
*
982982
* ```php
983-
* yield Workflow::mutex('my-mutex');
984-
* ```
983+
* $this->>mutex = Workflow::mutex();
985984
*
986-
* Now to continue only when the lock is acquired:
985+
* // Continue only when the lock is released
986+
* yield $this->mutex;
987987
*
988-
* ```php
988+
* // Continue only when the lock is acquired
989989
* yield Workflow::mutex('my-mutex')->lock();
990990
* ```
991991
*
992-
* Note: in this case, if the lock is already acquired, the Workflow will be blocked until it's released
993-
*
994-
* @param non-empty-string $name The name of the mutex.
995-
*/
996-
public static function mutex(string $name): MutexInterface
997-
{
998-
/** @var WorkflowContextInterface $context */
999-
$context = self::getCurrentContext();
1000-
1001-
return $context->mutex($name);
1002-
}
1003-
1004-
/**
1005-
* Create a conditional mutex that is locked when any of the conditions are met.
1006-
*
1007-
* @param non-empty-string $name
1008-
*
1009-
* Example:
1010-
*
1011-
* Monitor when the number of threads exceeds 10:
1012-
*
1013-
* ```php
1014-
* #[WorkflowMethod]
1015-
* function start() {
1016-
* // Register a conditional mutex that will be locked when the number of threads exceeds 10
1017-
* Workflow::conditionalMutex(
1018-
* 'limit',
1019-
* fn() => count($this->threads) > 10,
1020-
* );
1021-
* // ...
1022-
* }
1023-
*
1024-
* #[SignalMethod]
1025-
* function addTask(Task $task) {
1026-
* yield Workflow::runLocked('limit', function() {
1027-
* $key = array_key_last($this->threads) + 1;
1028-
* yield $this->threads[$key] = Workflow::executeChildWorkflow(...);
1029-
* unset($this->threads[$key]);
1030-
* });
1031-
* }
1032-
* ```
992+
* Note: in the last case, if the lock is already acquired, the Workflow will be blocked until it's released.
1033993
*/
1034-
public function conditionalMutex(string $name, PromiseInterface|callable ...$lockConditions): MutexInterface
994+
public static function mutex(): MutexInterface
1035995
{
1036996
/** @var WorkflowContextInterface $context */
1037997
$context = self::getCurrentContext();
1038998

1039-
return $context->conditionalMutex($name, ...$lockConditions);
999+
return $context->mutex();
10401000
}
10411001

10421002
/**
10431003
* Run a function when the mutex is released.
1044-
* The mutex is locked for the duration of the function if it's not a conditional mutex.
1045-
* Conditional mutexes are locked only when all conditions are met.
1004+
* The mutex is locked for the duration of the function.
10461005
*
1047-
* @see Workflow::conditionalMutex()
1006+
* @param non-empty-string|MutexInterface $mutex Mutex name or instance.
1007+
*@see Workflow::conditionalMutex()
10481008
*
1049-
* @param non-empty-string|MutexInterface $name Mutex name or instance.
10501009
*/
1051-
public function runLocked(string|MutexInterface $name, callable $callable): PromiseInterface
1010+
public function runLocked(string|MutexInterface $mutex, callable $callable): PromiseInterface
10521011
{
10531012
/** @var WorkflowContextInterface $context */
10541013
$context = self::getCurrentContext();
10551014

1056-
return $context->runLocked($name, $callable);
1015+
return $context->runLocked($mutex, $callable);
10571016
}
10581017
}

src/Workflow/MutexInterface.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88

99
interface MutexInterface
1010
{
11+
public function lock(): PromiseInterface;
12+
1113
/**
12-
* @return non-empty-string The name of the mutex.
14+
* Try to lock the mutex.
15+
*
16+
* @return bool true if the mutex was successfully locked, false otherwise.
1317
*/
14-
public function getName(): string;
15-
16-
public function lock(): PromiseInterface;
18+
public function tryLock(): bool;
1719

1820
public function unlock(): void;
1921

src/Workflow/WorkflowContextInterface.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -377,25 +377,13 @@ public function uuid4(): PromiseInterface;
377377
public function uuid7(?DateTimeInterface $dateTime = null): PromiseInterface;
378378

379379
/**
380-
* Get a mutex by name or create a new one.
381-
*
382-
* @param non-empty-string $name The name of the mutex.
383-
*/
384-
public function mutex(string $name): MutexInterface;
385-
386-
/**
387-
* Create a conditional mutex.
388-
*
389-
* @param non-empty-string $name
380+
* Create a mutex.
390381
*/
391-
public function conditionalMutex(string $name, PromiseInterface|callable ...$lockConditions): MutexInterface;
382+
public function mutex(): MutexInterface;
392383

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

0 commit comments

Comments
 (0)