Skip to content

Commit

Permalink
Add getter for ActivityPrototype::$factory
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Sep 9, 2024
1 parent 28538c1 commit 9c06bb0
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/Internal/Workflow/WorkflowContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
use Temporal\Workflow\ChildWorkflowStubInterface;
use Temporal\Workflow\ContinueAsNewOptions;
use Temporal\Workflow\ExternalWorkflowStubInterface;
use Temporal\Workflow\MutexInterface;
use Temporal\Workflow\WorkflowContextInterface;
use Temporal\Workflow\WorkflowExecution;
use Temporal\Workflow\WorkflowInfo;
Expand Down Expand Up @@ -309,7 +310,7 @@ public function panic(\Throwable $failure = null): PromiseInterface
public function continueAsNew(
string $type,
array $args = [],
ContinueAsNewOptions $options = null
ContinueAsNewOptions $options = null,
): PromiseInterface {
return $this->callsInterceptor->with(
function (ContinueAsNewInput $input): PromiseInterface {
Expand Down Expand Up @@ -632,6 +633,21 @@ public function uuid7(?DateTimeInterface $dateTime = null): PromiseInterface
return $this->sideEffect(static fn(): UuidInterface => \Ramsey\Uuid\Uuid::uuid7($dateTime));
}

public function mutex(string $name): 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:42: 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:42: 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

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:99: InvalidReturnType: Not all code paths of Temporal\Internal\Workflow\WorkflowContext::conditionalMutex end in a return statement, return type Temporal\Workflow\MutexInterface 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:99: InvalidReturnType: Not all code paths of Temporal\Internal\Workflow\WorkflowContext::conditionalMutex end in a return statement, return type Temporal\Workflow\MutexInterface expected (see https://psalm.dev/011)
{
// todo
}

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

Check failure on line 646 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:646:81: 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 646 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:646:81: 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
}

/**
* @internal
*/
Expand Down
85 changes: 85 additions & 0 deletions src/Workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
use Temporal\Workflow\ChildWorkflowStubInterface;
use Temporal\Workflow\ContinueAsNewOptions;
use Temporal\Workflow\ExternalWorkflowStubInterface;
use Temporal\Workflow\MutexInterface;
use Temporal\Workflow\ScopedContextInterface;
use Temporal\Workflow\UpdateContext;
use Temporal\Workflow\WorkflowContextInterface;
use Temporal\Workflow\WorkflowExecution;
use Temporal\Workflow\WorkflowInfo;
use Temporal\Internal\Support\DateInterval;
Expand Down Expand Up @@ -970,4 +972,87 @@ public static function uuid7(?DateTimeInterface $dateTime = null): PromiseInterf

return $context->uuid7($dateTime);
}

/**
* Get a mutex by name or create a new one.
*
* If a mutex is yielded without calling `lock()`, the Workflow will continue
* only when the lock is released.
*
* ```php
* yield Workflow::mutex('my-mutex');
* ```
*
* Now to continue only when the lock is acquired:
*
* ```php
* 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]);
* });
* }
* ```
*/
public function conditionalMutex(string $name, PromiseInterface|callable ...$lockConditions): MutexInterface
{
/** @var WorkflowContextInterface $context */
$context = self::getCurrentContext();

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

/**
* 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.
*
* @see Workflow::conditionalMutex()
*
* @param non-empty-string|MutexInterface $name Mutex name or instance.
*/
public function runLocked(string|MutexInterface $name, callable $callable): PromiseInterface
{
/** @var WorkflowContextInterface $context */
$context = self::getCurrentContext();

return $context->runLocked($name, $callable);
}
}
21 changes: 21 additions & 0 deletions src/Workflow/MutexInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Temporal\Workflow;

use React\Promise\PromiseInterface;

interface MutexInterface
{
/**
* @return non-empty-string The name of the mutex.
*/
public function getName(): string;

public function lock(): PromiseInterface;

public function unlock(): void;

public function isLocked(): bool;
}
23 changes: 23 additions & 0 deletions src/Workflow/WorkflowContextInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,4 +375,27 @@ public function uuid4(): PromiseInterface;
* @return PromiseInterface<UuidInterface>
*/
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
*/
public function conditionalMutex(string $name, PromiseInterface|callable ...$lockConditions): 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.
*/
public function runLocked(string|MutexInterface $name, callable $callable): PromiseInterface;
}

0 comments on commit 9c06bb0

Please sign in to comment.