Skip to content

fix: incorrect updateWithStart signature #594

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions src/Client/WorkflowClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,19 +250,19 @@ public function updateWithStart(
);
$workflowStub->hasExecution() and throw new InvalidArgumentException(self::ERROR_WORKFLOW_START_DUPLICATION);

[$execution, $handle] = $this->getStarter()->updateWithStart(
$output = $this->getStarter()->updateWithStart(
$workflowType,
$workflowStub->getOptions() ?? WorkflowOptions::new(),
$update,
$updateArgs,
$startArgs,
);

$workflowStub->setExecution($execution);
$workflowStub->setExecution($output->execution);

return $handle instanceof \Throwable
? throw $handle
: $handle;
return $output->handle instanceof UpdateHandle
? $output->handle
: throw $output->handle;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Temporal\Interceptor\Trait;

use Temporal\Client\Update\UpdateHandle;
use Temporal\Client\Workflow\WorkflowExecutionDescription;
use Temporal\DataConverter\EncodedValues;
use Temporal\Interceptor\WorkflowClient\CancelInput;
Expand All @@ -25,6 +24,7 @@
use Temporal\Interceptor\WorkflowClient\TerminateInput;
use Temporal\Interceptor\WorkflowClient\UpdateInput;
use Temporal\Interceptor\WorkflowClient\UpdateWithStartInput;
use Temporal\Interceptor\WorkflowClient\UpdateWithStartOutput;
use Temporal\Interceptor\WorkflowClientCallsInterceptor;
use Temporal\Workflow\WorkflowExecution;

Expand Down Expand Up @@ -80,7 +80,7 @@ public function signalWithStart(SignalWithStartInput $input, callable $next): Wo
*
* @see WorkflowClientCallsInterceptor::updateWithStart()
*/
public function updateWithStart(UpdateWithStartInput $input, callable $next): UpdateHandle
public function updateWithStart(UpdateWithStartInput $input, callable $next): UpdateWithStartOutput
{
return $next($input);
}
Expand Down
23 changes: 23 additions & 0 deletions src/Interceptor/WorkflowClient/UpdateWithStartOutput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* This file is part of Temporal package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Temporal\Interceptor\WorkflowClient;

use Temporal\Client\Update\UpdateHandle;
use Temporal\Workflow\WorkflowExecution;

final class UpdateWithStartOutput
{
public function __construct(
public readonly WorkflowExecution $execution,
public readonly UpdateHandle|\Throwable $handle,
) {}
}
6 changes: 2 additions & 4 deletions src/Interceptor/WorkflowClientCallsInterceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Temporal\Interceptor;

use Temporal\Client\Update\UpdateHandle;
use Temporal\Client\Workflow\WorkflowExecutionDescription;
use Temporal\DataConverter\ValuesInterface;
use Temporal\Interceptor\Trait\WorkflowClientCallsInterceptorTrait;
Expand All @@ -26,6 +25,7 @@
use Temporal\Interceptor\WorkflowClient\TerminateInput;
use Temporal\Interceptor\WorkflowClient\UpdateInput;
use Temporal\Interceptor\WorkflowClient\UpdateWithStartInput;
use Temporal\Interceptor\WorkflowClient\UpdateWithStartOutput;
use Temporal\Internal\Interceptor\Interceptor;
use Temporal\Workflow\WorkflowExecution;

Expand Down Expand Up @@ -74,10 +74,8 @@ public function signalWithStart(SignalWithStartInput $input, callable $next): Wo
/**
* @param UpdateWithStartInput $input
* @param callable(UpdateWithStartInput): WorkflowExecution $next
*
* @return UpdateHandle
*/
public function updateWithStart(UpdateWithStartInput $input, callable $next): UpdateHandle;
public function updateWithStart(UpdateWithStartInput $input, callable $next): UpdateWithStartOutput;

/**
* @param callable(GetResultInput): ?ValuesInterface $next
Expand Down
34 changes: 18 additions & 16 deletions src/Internal/Client/WorkflowStarter.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use Temporal\Interceptor\WorkflowClient\StartInput;
use Temporal\Interceptor\WorkflowClient\UpdateInput;
use Temporal\Interceptor\WorkflowClient\UpdateWithStartInput;
use Temporal\Interceptor\WorkflowClient\UpdateWithStartOutput;
use Temporal\Interceptor\WorkflowClientCallsInterceptor;
use Temporal\Internal\Interceptor\Pipeline;
use Temporal\Internal\Support\DateInterval;
Expand Down Expand Up @@ -128,21 +129,19 @@ function (SignalWithStartInput $input): WorkflowExecution {

/**
* @param non-empty-string $workflowType
*
* @return array{WorkflowExecution, UpdateHandle|\Throwable}
*/
public function updateWithStart(
string $workflowType,
WorkflowOptions $options,
UpdateOptions $update,
array $updateArgs = [],
array $startArgs = [],
): array {
): UpdateWithStartOutput {
$arguments = EncodedValues::fromValues($startArgs, $this->converter);
$updateArguments = EncodedValues::fromValues($updateArgs, $this->converter);

return $this->interceptors->with(
function (UpdateWithStartInput $input): array {
function (UpdateWithStartInput $input): UpdateWithStartOutput {
$startRequest = $this->configureExecutionRequest(
new StartWorkflowExecutionRequest(),
$input->workflowStartInput,
Expand Down Expand Up @@ -234,20 +233,23 @@ function (UpdateWithStartInput $input): array {
workflowExecution: $execution,
);
} catch (\RuntimeException $e) {
return [$execution, $e];
return new UpdateWithStartOutput($execution, $e);
}

return [$execution, new UpdateHandle(
client: $this->serviceClient,
clientOptions: $this->clientOptions,
converter: $this->converter,
execution: $updateResult->getReference()->workflowExecution,
workflowType: $input->updateInput->workflowType,
updateName: $input->updateInput->updateName,
resultType: $input->updateInput->resultType,
updateId: $updateResult->getReference()->updateId,
result: $updateResult->getResult(),
)];
return new UpdateWithStartOutput(
$execution,
new UpdateHandle(
client: $this->serviceClient,
clientOptions: $this->clientOptions,
converter: $this->converter,
execution: $updateResult->getReference()->workflowExecution,
workflowType: $input->updateInput->workflowType,
updateName: $input->updateInput->updateName,
resultType: $input->updateInput->resultType,
updateId: $updateResult->getReference()->updateId,
result: $updateResult->getResult(),
),
);
},
/** @see WorkflowClientCallsInterceptor::updateWithStart() */
'updateWithStart',
Expand Down
14 changes: 14 additions & 0 deletions tests/Fixtures/src/Interceptor/InterceptorCallsCounter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
use Temporal\Interceptor\Trait\WorkflowOutboundRequestInterceptorTrait;
use Temporal\Interceptor\WorkflowClient\SignalWithStartInput;
use Temporal\Interceptor\WorkflowClient\StartInput;
use Temporal\Interceptor\WorkflowClient\UpdateWithStartInput;
use Temporal\Interceptor\WorkflowClient\UpdateWithStartOutput;
use Temporal\Interceptor\WorkflowClientCallsInterceptor;
use Temporal\Interceptor\WorkflowInbound\SignalInput;
use Temporal\Interceptor\WorkflowInbound\UpdateInput;
use Temporal\Interceptor\WorkflowInbound\WorkflowInput;
use Temporal\Interceptor\WorkflowInboundCallsInterceptor;
use Temporal\Interceptor\WorkflowOutboundRequestInterceptor;
Expand Down Expand Up @@ -92,4 +95,15 @@ public function signalWithStart(SignalWithStartInput $input, callable $next): Wo
),
);
}

public function updateWithStart(UpdateWithStartInput $input, callable $next): UpdateWithStartOutput
{
return $next(
$input->with(
workflowStartInput: $input->workflowStartInput->with(
header: $this->increment($input->workflowStartInput->header, __FUNCTION__),
),
),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* This file is part of Temporal package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Temporal\Tests\Workflow\Interceptor;

use Temporal\Workflow;
use Temporal\Workflow\WorkflowMethod;

#[Workflow\WorkflowInterface]
class UpdateHeadersWorkflow
{
private ?array $headers = null;
private bool $updated = false;

#[WorkflowMethod(name: 'InterceptorUpdateHeadersWorkflow')]
public function handler(): mixed
{
yield Workflow::await(fn() => $this->updated);

$this->headers = \iterator_to_array(Workflow::getCurrentContext()->getHeader());

return $this->headers;
}

#[Workflow\UpdateMethod]
public function update(): void
{
$this->updated = true;
}

#[Workflow\QueryMethod]
public function headers(): mixed
{
return $this->headers;
}
}
27 changes: 27 additions & 0 deletions tests/Functional/Interceptor/InterceptorsTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
namespace Temporal\Tests\Functional\Interceptor;

use Carbon\CarbonInterval;
use Temporal\Client\Update\LifecycleStage;
use Temporal\Client\Update\UpdateOptions;
use Temporal\Client\WorkflowOptions;
use Temporal\Testing\WithoutTimeSkipping;
use Temporal\Tests\Workflow\Interceptor\HeadersWorkflow;
use Temporal\Tests\Workflow\Interceptor\QueryHeadersWorkflow;
use Temporal\Tests\Workflow\Interceptor\SignalHeadersWorkflow;
use Temporal\Tests\Workflow\Interceptor\UpdateHeadersWorkflow;

/**
* @group client
Expand Down Expand Up @@ -111,6 +114,30 @@ public function testSignalWithStartMethod(): void
], (array)$run->getResult());
}

public function testUpdateWithStartMethod(): void
{
$client = $this->createClient();
$workflow = $client->newWorkflowStub(
UpdateHeadersWorkflow::class,
WorkflowOptions::new()
->withWorkflowExecutionTimeout(CarbonInterval::seconds(5)),
);

$run = $client->updateWithStart($workflow, 'update');

// Workflow header
$this->assertEquals([
/** @see \Temporal\Tests\Interceptor\InterceptorCallsCounter::updateWithStart() */
'updateWithStart' => '1',
/**
* Inherited from handler run
*
* @see \Temporal\Tests\Interceptor\InterceptorCallsCounter::execute()
*/
'execute' => '1',
], (array) $workflow->headers());
}

// todo: rewrite tests because there is no header in query call
// todo: add test about dynamic query
// public function testQueryMethod(): void
Expand Down
Loading