Skip to content

Commit bd95a63

Browse files
committed
feat: Expose RetryPolicy in Workflow context
1 parent 82ff905 commit bd95a63

File tree

5 files changed

+97
-61
lines changed

5 files changed

+97
-61
lines changed

src/Common/RetryOptions.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Temporal\Api\Common\V1\RetryPolicy;
1818
use Temporal\Internal\Assert;
1919
use Temporal\Internal\Marshaller\Meta\Marshal;
20+
use Temporal\Internal\Marshaller\Type\DateIntervalType;
2021
use Temporal\Internal\Marshaller\Type\DurationJsonType;
2122
use Temporal\Internal\Support\DateInterval;
2223
use Temporal\Internal\Support\Options;
@@ -68,6 +69,7 @@ class RetryOptions extends Options
6869
* is 1.0 then it is used for all retries.
6970
*/
7071
#[Marshal(name: 'initial_interval', type: DurationJsonType::class, nullable: true)]
72+
#[Marshal(name: 'InitialInterval', type: DurationJsonType::class, nullable: true)]
7173
public ?\DateInterval $initialInterval = self::DEFAULT_INITIAL_INTERVAL;
7274

7375
/**
@@ -77,6 +79,7 @@ class RetryOptions extends Options
7779
* Note: Must be greater than 1.0
7880
*/
7981
#[Marshal(name: 'backoff_coefficient')]
82+
#[Marshal(name: 'BackoffCoefficient')]
8083
public float $backoffCoefficient = self::DEFAULT_BACKOFF_COEFFICIENT;
8184

8285
/**
@@ -86,6 +89,7 @@ class RetryOptions extends Options
8689
* Default is 100x of {@see $initialInterval}.
8790
*/
8891
#[Marshal(name: 'maximum_interval', type: DurationJsonType::class, nullable: true)]
92+
#[Marshal(name: 'MaximumInterval', type: DurationJsonType::class, nullable: true)]
8993
public ?\DateInterval $maximumInterval = self::DEFAULT_MAXIMUM_INTERVAL;
9094

9195
/**
@@ -96,6 +100,7 @@ class RetryOptions extends Options
96100
* @var int<0, max>
97101
*/
98102
#[Marshal(name: 'maximum_attempts')]
103+
#[Marshal(name: 'MaximumAttempts')]
99104
public int $maximumAttempts = self::DEFAULT_MAXIMUM_ATTEMPTS;
100105

101106
/**
@@ -105,6 +110,7 @@ class RetryOptions extends Options
105110
* @var ExceptionsList
106111
*/
107112
#[Marshal(name: 'non_retryable_error_types')]
113+
#[Marshal(name: 'NonRetryableErrorTypes')]
108114
public array $nonRetryableExceptions = self::DEFAULT_NON_RETRYABLE_EXCEPTIONS;
109115

110116
public function mergeWith(?MethodRetry $retry = null): self

src/Internal/Marshaller/MarshallingRule.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313

1414
use Temporal\Internal\Marshaller\Type\TypeInterface;
1515

16-
/**
17-
* @internal
18-
*/
1916
class MarshallingRule
2017
{
2118
/**

src/Workflow/WorkflowInfo.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Temporal\Client\ClientOptions;
1717
use Temporal\Common\CronSchedule;
1818
use Temporal\Common\Priority;
19+
use Temporal\Common\RetryOptions;
1920
use Temporal\Common\TypedSearchAttributes;
2021
use Temporal\Internal\Marshaller\Meta\Marshal;
2122
use Temporal\Internal\Marshaller\Type\ArrayType;
@@ -143,6 +144,12 @@ final class WorkflowInfo
143144
#[Marshal(name: 'Priority')]
144145
public Priority $priority;
145146

147+
/**
148+
* @since SDK 2.16.0
149+
*/
150+
#[Marshal(name: 'RetryPolicy')]
151+
public ?RetryOptions $retryPolicy = null;
152+
146153
/**
147154
* WorkflowInfo constructor.
148155
*/

tests/Acceptance/Extra/Workflow/RootWorkflowExecutionTest.php

Lines changed: 0 additions & 58 deletions
This file was deleted.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Temporal\Tests\Acceptance\Extra\Workflow\WorkflowInfo;
6+
7+
use PHPUnit\Framework\Attributes\Test;
8+
use Temporal\Client\WorkflowStubInterface;
9+
use Temporal\Tests\Acceptance\App\Attribute\RetryOptions;
10+
use Temporal\Tests\Acceptance\App\Attribute\Stub;
11+
use Temporal\Tests\Acceptance\App\TestCase;
12+
use Temporal\Workflow;
13+
use Temporal\Workflow\WorkflowInterface;
14+
use Temporal\Workflow\WorkflowMethod;
15+
16+
class WorkflowInfoTest extends TestCase
17+
{
18+
#[Test]
19+
public static function rootWorkflowExecution(
20+
#[Stub('Extra_Workflow_WorkflowInfo', args: [MainWorkflow::ARG_ROOT_EXECUTION])]
21+
WorkflowStubInterface $stub,
22+
): void {
23+
$result = $stub->getResult(type: 'array');
24+
self::assertSame([
25+
'ID' => $stub->getExecution()->getID(),
26+
'RunID' => $stub->getExecution()->getRunID(),
27+
], $result);
28+
}
29+
30+
#[Test]
31+
public static function retryPolicy(
32+
#[Stub('Extra_Workflow_WorkflowInfo', args: [MainWorkflow::ARG_RETRY_POLICY], retryOptions: new RetryOptions(
33+
backoffCoefficient: 3.0,
34+
maximumInterval: '2 minutes',
35+
maximumAttempts: 10,
36+
))]
37+
WorkflowStubInterface $stub,
38+
): void {
39+
$result = $stub->getResult(type: 'array');
40+
self::assertEquals([
41+
"initial_interval" => ['seconds' => 1, 'nanos' => 0],
42+
"backoff_coefficient" => 3,
43+
"maximum_interval" => ['seconds' => 120, 'nanos' => 0],
44+
"maximum_attempts" => 10,
45+
"non_retryable_error_types" => [],
46+
], $result);
47+
}
48+
}
49+
50+
#[WorkflowInterface]
51+
class MainWorkflow
52+
{
53+
public const ARG_RETRY_POLICY = 'retryPolicy';
54+
public const ARG_ROOT_EXECUTION = 'rootExecution';
55+
56+
#[WorkflowMethod('Extra_Workflow_WorkflowInfo')]
57+
public function run($arg)
58+
{
59+
return yield match ($arg) {
60+
self::ARG_ROOT_EXECUTION => Workflow::newChildWorkflowStub(ChildWorkflow::class)->run(),
61+
self::ARG_RETRY_POLICY => Workflow::getCurrentContext()->getInfo()->retryPolicy,
62+
};
63+
}
64+
}
65+
66+
#[WorkflowInterface]
67+
class ChildWorkflow
68+
{
69+
#[WorkflowMethod('Extra_Workflow_WorkflowInfo_Child')]
70+
public function run()
71+
{
72+
return yield Workflow::newChildWorkflowStub(ChildWorkflow2::class)->run();
73+
}
74+
}
75+
76+
#[WorkflowInterface]
77+
class ChildWorkflow2
78+
{
79+
#[WorkflowMethod('Extra_Workflow_WorkflowInfo_Child2')]
80+
public function run()
81+
{
82+
return Workflow::getCurrentContext()->getInfo()->rootExecution;
83+
}
84+
}

0 commit comments

Comments
 (0)