Skip to content
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

Expose WorkflowIdConflictPolicy #417

Merged
merged 4 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"psr/log": "^2.0 || ^3.0",
"ramsey/uuid": "^4.7",
"react/promise": "^2.9",
"roadrunner-php/roadrunner-api-dto": "^1.5.0",
"roadrunner-php/roadrunner-api-dto": "dev-update-dtos as 1.7.0",
"roadrunner-php/version-checker": "^1.0",
"spiral/attributes": "^3.1.4",
"spiral/roadrunner": "^2023.3.12 || ^2024.1",
Expand Down
3 changes: 3 additions & 0 deletions src/Client/Workflow/WorkflowExecutionDescription.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
final class WorkflowExecutionDescription
{
/**
* @internal
*/
public function __construct(
public readonly WorkflowExecutionInfo $info,
) {
Expand Down
31 changes: 27 additions & 4 deletions src/Client/WorkflowOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Temporal\Common\MethodRetry;
use Temporal\Common\RetryOptions;
use Temporal\Common\Uuid;
use Temporal\Common\WorkflowIdConflictPolicy;
use Temporal\DataConverter\DataConverterInterface;
use Temporal\Internal\Marshaller\Meta\Marshal;
use Temporal\Internal\Marshaller\Type\ArrayType;
Expand Down Expand Up @@ -98,12 +99,19 @@ final class WorkflowOptions extends Options
public \DateInterval $workflowTaskTimeout;

/**
* Whether server allow reuse of workflow ID, can be useful for deduplication logic.
* If set to {@see IdReusePolicy::POLICY_REJECT_DUPLICATE}.
* Whether server allow reuse of workflow ID.
*
* Can be useful for deduplication logic if set to {@see IdReusePolicy::POLICY_REJECT_DUPLICATE}.
*/
#[Marshal(name: 'WorkflowIDReusePolicy')]
public int $workflowIdReusePolicy = IdReusePolicy::POLICY_ALLOW_DUPLICATE_FAILED_ONLY;

/**
* Defines how to resolve an ID conflict with a *running* workflow.
*/
#[Marshal(name: 'WorkflowIdConflictPolicy')]
public WorkflowIdConflictPolicy $workflowIdConflictPolicy = WorkflowIdConflictPolicy::Unspecified;

/**
* Optional retry policy for workflow. If a retry policy is specified, in
* case of workflow failure server will start new workflow execution if
Expand Down Expand Up @@ -319,8 +327,8 @@ public function withWorkflowStartDelay($delay): self
}

/**
* Specifies server behavior if a completed workflow with the same id
* exists. Note that under no conditions Temporal allows two workflows
* Specifies server behavior if a *closed* workflow with the same id exists.
* Note that under no conditions Temporal allows two workflows
* with the same namespace and workflow id run simultaneously.
*
* - {@see IdReusePolicy::AllowDuplicateFailedOnly}: Is a default
Expand All @@ -347,6 +355,21 @@ public function withWorkflowIdReusePolicy(IdReusePolicy|int $policy): self
return $self;
}

/**
* Defines how to resolve an ID conflict with a *running* workflow.
*
* @psalm-suppress ImpureMethodCall
*
* @return $this
*/
#[Pure]
public function withWorkflowIdConflictPolicy(WorkflowIdConflictPolicy $policy): self
{
$self = clone $this;
$self->workflowIdConflictPolicy = $policy;
return $self;
}

/**
* RetryOptions that define how child workflow is retried in case of
* failure. Default is null which is no reties.
Expand Down
1 change: 1 addition & 0 deletions src/Common/WorkerVersionStamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ final class WorkerVersionStamp
{
public function __construct(
public string $buildId = '',
/** @deprecated that field was removed {@link https://github.com/temporalio/api/pull/393} */
public string $bundleId = '',
public bool $useVersioning = false,
) {
Expand Down
40 changes: 40 additions & 0 deletions src/Common/WorkflowIdConflictPolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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\Common;

/**
* Defines what to do when trying to start a workflow with the same workflow id as a *running* workflow.
* Note that it is *never* valid to have two actively running instances of the same workflow id.
*
* @see IdReusePolicy for handling workflow id duplication with a *closed* workflow.
* @see \Temporal\Api\Enums\V1\WorkflowIdConflictPolicy
*/
enum WorkflowIdConflictPolicy: int
{
case Unspecified = 0;

/**
* Don't start a new workflow; instead return `WorkflowExecutionAlreadyStartedFailure`.
*/
case Fail = 1;

/**
* Don't start a new workflow; instead return a workflow handle for the running workflow.
*/
case UseExisting = 2;

/**
* Terminate the running workflow before starting a new one.
*/
case TerminateExisting = 3;

}
1 change: 1 addition & 0 deletions src/Internal/Client/WorkflowStarter.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ private function configureExecutionRequest(
->setCronSchedule($options->cronSchedule ?? '')
->setRetryPolicy($options->retryOptions ? $options->retryOptions->toWorkflowRetryPolicy() : null)
->setWorkflowIdReusePolicy($options->workflowIdReusePolicy)
->setWorkflowIdConflictPolicy($options->workflowIdConflictPolicy->value)
->setWorkflowRunTimeout(DateInterval::toDuration($options->workflowRunTimeout))
->setWorkflowExecutionTimeout(DateInterval::toDuration($options->workflowExecutionTimeout))
->setWorkflowTaskTimeout(DateInterval::toDuration($options->workflowTaskTimeout))
Expand Down
1 change: 0 additions & 1 deletion src/Internal/Mapper/WorkflowExecutionInfoMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public function prepareWorkerVersionStamp(?WorkerVersionStamp $versionStamp): ?W
? null
: new WorkerVersionStampDto(
buildId: $versionStamp->getBuildId(),
bundleId: $versionStamp->getBundleId(),
useVersioning: $versionStamp->getUseVersioning(),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function testFromPayload(): void
'state_transition_count' => 1,
'history_size_bytes' => 1,
'most_recent_worker_version_stamp' => (new \Temporal\Api\Common\V1\WorkerVersionStamp())
->setBundleId('bundleId')
// ->setBundleId('bundleId')
->setBuildId('buildId')
->setUseVersioning(true),
]),
Expand Down Expand Up @@ -94,7 +94,6 @@ public function testFromPayload(): void
$this->assertTrue($info->autoResetPoints[0]->resettable);
$this->assertSame('binaryChecksum', $info->autoResetPoints[0]->binaryChecksum);
$this->assertSame(1, $info->historySizeBytes);
$this->assertSame('bundleId', $info->mostRecentWorkerVersionStamp->bundleId);
$this->assertSame('buildId', $info->mostRecentWorkerVersionStamp->buildId);
$this->assertTrue($info->mostRecentWorkerVersionStamp->useVersioning);
}
Expand Down
16 changes: 16 additions & 0 deletions tests/Unit/DTO/WorkflowOptionsTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Temporal\Common\IdReusePolicy;
use Temporal\Common\RetryOptions;
use Temporal\Common\Uuid;
use Temporal\Common\WorkflowIdConflictPolicy;
use Temporal\DataConverter\DataConverter;

class WorkflowOptionsTestCase extends AbstractDTOMarshalling
Expand All @@ -38,6 +39,10 @@ public function testMarshalling(): void
'WorkflowStartDelay' => 0,
'WorkflowTaskTimeout' => 0,
'WorkflowIDReusePolicy' => 2,
'WorkflowIdConflictPolicy' => [
'name' => 'Unspecified',
'value' => 0,
],
'RetryPolicy' => null,
'CronSchedule' => null,
'Memo' => null,
Expand Down Expand Up @@ -125,6 +130,17 @@ public function testWorkflowIdReusePolicyChangesNotMutateStateUsingEnum(): void
$this->assertSame(IdReusePolicy::AllowDuplicateFailedOnly->value, $dto->workflowIdReusePolicy);
}

public function testWorkflowIdConflictPolicy(): void
{
$dto = new WorkflowOptions();

$this->assertNotSame($dto, $newDto = $dto->withWorkflowIdConflictPolicy(
WorkflowIdConflictPolicy::Fail
));
$this->assertSame(WorkflowIdConflictPolicy::Unspecified, $dto->workflowIdConflictPolicy);
$this->assertSame(WorkflowIdConflictPolicy::Fail, $newDto->workflowIdConflictPolicy);
}

public function testRetryOptionsChangesNotMutateState(): void
{
$dto = new WorkflowOptions();
Expand Down
Loading