Skip to content

Commit

Permalink
Add Workflow::upsertMemo() implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Feb 6, 2025
1 parent e70ae3b commit 3e63886
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Interceptor/WorkflowOutboundCallsInterceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Temporal\Interceptor\WorkflowOutboundCalls\SideEffectInput;
use Temporal\Interceptor\WorkflowOutboundCalls\SignalExternalWorkflowInput;
use Temporal\Interceptor\WorkflowOutboundCalls\TimerInput;
use Temporal\Interceptor\WorkflowOutboundCalls\UpsertMemoInput;
use Temporal\Interceptor\WorkflowOutboundCalls\UpsertSearchAttributesInput;
use Temporal\Interceptor\WorkflowOutboundCalls\UpsertTypedSearchAttributesInput;
use Temporal\Internal\Interceptor\Interceptor;
Expand Down Expand Up @@ -113,6 +114,11 @@ public function continueAsNew(ContinueAsNewInput $input, callable $next): Promis
*/
public function getVersion(GetVersionInput $input, callable $next): PromiseInterface;

/**
* @param callable(UpsertMemoInput): PromiseInterface $next

Check failure on line 118 in src/Interceptor/WorkflowOutboundCallsInterceptor.php

View workflow job for this annotation

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

UndefinedDocblockClass

src/Interceptor/WorkflowOutboundCallsInterceptor.php:118:15: UndefinedDocblockClass: Docblock-defined class, interface or enum named Temporal\Interceptor\WorkflowOutboundCalls\UpsertMemoInput does not exist (see https://psalm.dev/200)
*/
public function upsertMemo(UpsertMemoInput $input, callable $next): PromiseInterface;

Check failure on line 120 in src/Interceptor/WorkflowOutboundCallsInterceptor.php

View workflow job for this annotation

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

UndefinedClass

src/Interceptor/WorkflowOutboundCallsInterceptor.php:120:32: UndefinedClass: Class, interface or enum named Temporal\Interceptor\WorkflowOutboundCalls\UpsertMemoInput does not exist (see https://psalm.dev/019)

/**
* @param callable(UpsertSearchAttributesInput): PromiseInterface $next
*/
Expand Down
27 changes: 27 additions & 0 deletions src/Internal/Workflow/WorkflowContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use Temporal\Interceptor\WorkflowOutboundCalls\PanicInput;
use Temporal\Interceptor\WorkflowOutboundCalls\SideEffectInput;
use Temporal\Interceptor\WorkflowOutboundCalls\TimerInput;
use Temporal\Interceptor\WorkflowOutboundCalls\UpsertMemoInput;
use Temporal\Interceptor\WorkflowOutboundCalls\UpsertSearchAttributesInput;
use Temporal\Interceptor\WorkflowOutboundCalls\UpsertTypedSearchAttributesInput;
use Temporal\Interceptor\WorkflowOutboundCallsInterceptor;
Expand All @@ -55,6 +56,7 @@
use Temporal\Internal\Transport\Request\NewTimer;
use Temporal\Internal\Transport\Request\Panic;
use Temporal\Internal\Transport\Request\SideEffect;
use Temporal\Internal\Transport\Request\UpsertMemo;
use Temporal\Internal\Transport\Request\UpsertSearchAttributes;
use Temporal\Internal\Transport\Request\UpsertTypedSearchAttributes;
use Temporal\Internal\Workflow\Process\HandlerState;
Expand Down Expand Up @@ -448,6 +450,31 @@ public function allHandlersFinished(): bool
return !$this->handlers->hasRunningHandlers();
}

public function upsertMemo(array $values): void
{
$this->callsInterceptor->with(
function (UpsertMemoInput $input): PromiseInterface {

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

View workflow job for this annotation

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

UndefinedClass

src/Internal/Workflow/WorkflowContext.php:456:23: UndefinedClass: Class, interface or enum named Temporal\Interceptor\WorkflowOutboundCalls\UpsertMemoInput does not exist (see https://psalm.dev/019)
$result = $this->request(new UpsertMemo($input->memo), false);

/** @psalm-suppress UnsupportedPropertyReferenceUsage $memo */
$memo = &$this->input->info->memo;
$memo ??= [];
foreach ($input->memo as $name => $value) {
if ($value === null) {
unset($memo[$name]);
continue;
}

$memo[$name] = $value;
}

return $result;
},
/** @see WorkflowOutboundCallsInterceptor::upsertMemo() */
'upsertMemo',
)(new UpsertMemoInput($values));

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

View workflow job for this annotation

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

UndefinedClass

src/Internal/Workflow/WorkflowContext.php:475:15: UndefinedClass: Class, interface or enum named Temporal\Interceptor\WorkflowOutboundCalls\UpsertMemoInput does not exist (see https://psalm.dev/019)
}

public function upsertSearchAttributes(array $searchAttributes): void
{
$this->callsInterceptor->with(
Expand Down
10 changes: 10 additions & 0 deletions src/Workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,16 @@ public static function allHandlersFinished(): bool
return $context->allHandlersFinished();
}

/**
* Upsert Memo
*
* @param array<string, mixed> $values
*/
public static function upsertMemo(array $values): void
{
self::getCurrentContext()->upsertMemo($values);

Check failure on line 913 in src/Workflow.php

View workflow job for this annotation

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

ArgumentTypeCoercion

src/Workflow.php:913:47: ArgumentTypeCoercion: Argument 1 of Temporal\Internal\Workflow\ScopeContext::upsertMemo expects array<non-empty-string, mixed>, but parent type array<string, mixed> provided (see https://psalm.dev/193)
}

/**
* Upsert search attributes
*
Expand Down
37 changes: 37 additions & 0 deletions src/Workflow/WorkflowContextInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,43 @@ public function getStackTrace(): string;
*/
public function allHandlersFinished(): bool;

/**
* Updates this Workflow's Memos by merging the provided memo with existing Memos.
*
* New Memo is merged by replacing properties of the same name at the first level only.
* Setting a property to {@see null} clears that key from the Memo.
*
* For example:
*
* ```php
* Workflow::upsertMemo([
* 'key1' => 'value',
* 'key3' => ['subkey1' => 'value']
* 'key4' => 'value',
* });
*
* Workflow::upsertMemo([
* 'key2' => 'value',
* 'key3' => ['subkey2' => 'value']
* 'key4' => null,
* ]);
* ```
*
* would result in the Workflow having these Memo:
*
* ```php
* [
* 'key1' => 'value',
* 'key2' => 'value',
* 'key3' => ['subkey2' => 'value'], // Note this object was completely replaced
* // Note that 'key4' was completely removed
* ]
* ```
*
* @param array<non-empty-string, mixed> $values
*/
public function upsertMemo(array $values): void;

/**
* Upsert search attributes
*
Expand Down

0 comments on commit 3e63886

Please sign in to comment.