-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathActivityProxy.php
More file actions
136 lines (120 loc) · 4.68 KB
/
ActivityProxy.php
File metadata and controls
136 lines (120 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?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\Internal\Workflow;
use React\Promise\PromiseInterface;
use Temporal\Activity\ActivityMethod;
use Temporal\Activity\ActivityOptionsInterface;
use Temporal\Worker\FeatureFlags;
use Temporal\Interceptor\WorkflowOutboundCalls\ExecuteActivityInput;
use Temporal\Interceptor\WorkflowOutboundCalls\ExecuteLocalActivityInput;
use Temporal\Interceptor\WorkflowOutboundCallsInterceptor;
use Temporal\Internal\Declaration\Prototype\ActivityPrototype;
use Temporal\Internal\Interceptor\Pipeline;
use Temporal\Internal\Support\Reflection;
use Temporal\Internal\Transport\CompletableResultInterface;
use Temporal\Workflow\WorkflowContextInterface;
/**
* @template-covariant T of object
* @mixin T
*/
final class ActivityProxy extends Proxy
{
/**
* @var string
*/
private const ERROR_UNDEFINED_ACTIVITY_METHOD =
'The given stub class "%s" does not contain an activity method named "%s"';
/**
* @var array<ActivityPrototype>
*/
private array $activities;
private string $class;
private ActivityOptionsInterface $options;
private WorkflowContextInterface $ctx;
/**
* @param class-string<T> $class
* @param array<ActivityPrototype> $activities
* @param Pipeline<WorkflowOutboundCallsInterceptor, PromiseInterface> $callsInterceptor
*/
public function __construct(
string $class,
array $activities,
ActivityOptionsInterface $options,
WorkflowContextInterface $ctx,
private Pipeline $callsInterceptor,
) {
$this->activities = $activities;
$this->class = $class;
$this->options = $options;
$this->ctx = $ctx;
}
/**
* @return CompletableResultInterface
*/
public function __call(string $method, array $args = []): PromiseInterface
{
$prototype = $this->findPrototypeByHandlerNameOrFail($method);
$type = $prototype->getHandler()->getReturnType();
$options = $this->options->mergeWith($prototype->getMethodRetry());
$args = Reflection::orderArguments($prototype->getHandler(), $args);
if (FeatureFlags::$warnOnActivityMethodWithoutAttribute && !$prototype->getHandler()->getAttributes(ActivityMethod::class, \ReflectionAttribute::IS_INSTANCEOF)) {
\trigger_error(
\sprintf(
'Using implicit activity methods is deprecated. Explicitly mark activity method %s with #[%s] attribute instead.',
$prototype->getHandler()->getDeclaringClass()->getName() . '::' . $method,
ActivityMethod::class,
),
\E_USER_DEPRECATED,
);
}
return $prototype->isLocalActivity()
// Run local activity through an interceptor pipeline
? $this->callsInterceptor->with(
fn(ExecuteLocalActivityInput $input): PromiseInterface => $this->ctx
->newUntypedActivityStub($input->options)
->execute($input->type, $input->args, $input->returnType, true),
/** @see WorkflowOutboundCallsInterceptor::executeLocalActivity() */
'executeLocalActivity',
)(
new ExecuteLocalActivityInput(
$prototype->getID(),
$args,
$options,
$type,
$prototype->getHandler(),
)
)
// Run activity through an interceptor pipeline
: $this->callsInterceptor->with(
fn(ExecuteActivityInput $input): PromiseInterface => $this->ctx
->newUntypedActivityStub($input->options)
->execute($input->type, $input->args, $input->returnType),
/** @see WorkflowOutboundCallsInterceptor::executeActivity() */
'executeActivity',
)(
new ExecuteActivityInput(
$prototype->getID(),
$args,
$options,
$type,
$prototype->getHandler(),
)
);
}
private function findPrototypeByHandlerNameOrFail(string $name): ActivityPrototype
{
$prototype = $this->findPrototypeByHandlerName($this->activities, $name);
if ($prototype === null) {
throw new \BadMethodCallException(
\sprintf(self::ERROR_UNDEFINED_ACTIVITY_METHOD, $this->class, $name),
);
}
return $prototype;
}
}