-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathrunner.php
More file actions
136 lines (119 loc) · 4.46 KB
/
runner.php
File metadata and controls
136 lines (119 loc) · 4.46 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
declare(strict_types=1);
use Harness\Exception\SkipTest;
use Harness\Feature\WorkflowStubInjector;
use Harness\Runtime\Feature;
use Harness\Runtime\Runner;
use Harness\Runtime\State;
use Harness\RuntimeBuilder;
use Harness\Support;
use Psr\Container\ContainerInterface;
use Spiral\Core\Attribute\Proxy;
use Spiral\Core\Container;
use Spiral\Core\Scope;
use Spiral\Goridge\RPC\RPC;
use Spiral\Goridge\RPC\RPCInterface;
use Spiral\RoadRunner\KeyValue\Factory;
use Spiral\RoadRunner\KeyValue\StorageInterface;
use Temporal\Client\ClientOptions;
use Temporal\Client\GRPC\ServiceClient;
use Temporal\Client\GRPC\ServiceClientInterface;
use Temporal\Client\ScheduleClient;
use Temporal\Client\ScheduleClientInterface;
use Temporal\Client\WorkflowClient;
use Temporal\Client\WorkflowClientInterface;
use Temporal\Client\WorkflowStubInterface;
use Temporal\DataConverter\DataConverter;
use Temporal\DataConverter\DataConverterInterface;
require_once __DIR__ . '/src/RuntimeBuilder.php';
RuntimeBuilder::init();
$runtime = RuntimeBuilder::createState($argv, \getcwd());
$runner = new Runner($runtime);
// Run RoadRunner server if workflows or activities are defined
if (\iterator_to_array($runtime->workflows(), false) !== [] || \iterator_to_array($runtime->activities(), false) !== []) {
$runner->start();
}
// Prepare and run checks
// Prepare services to be injected
if ($runtime->command->tlsKey === null && $runtime->command->tlsCert === null) {
$serviceClient = ServiceClient::create($runtime->address);
} else {
$sslParams = [
$runtime->address,
'clientKey' => $runtime->command->tlsKey,
'clientPem' => $runtime->command->tlsCert,
];
if ($runtime->command->tlsServerName !== null) {
$sslParams['overrideServerName'] = $runtime->command->tlsServerName;
}
if ($runtime->command->tlsCaCert !== null) {
$sslParams['crt'] = $runtime->command->tlsCaCert;
}
$serviceClient = ServiceClient::createSSL(...$sslParams);
}
echo "Connecting to Temporal service at {$runtime->address}... ";
try {
$serviceClient->getConnection()->connect(5);
echo "\e[1;32mOK\e[0m\n";
} catch (\Throwable $e) {
echo "\e[1;31mFAILED\e[0m\n";
Support::echoException($e);
return;
}
// TODO if authKey is set
// $serviceClient->withAuthKey($authKey)
$converter = DataConverter::createDefault();
$workflowClient = WorkflowClient::create(
serviceClient: $serviceClient,
options: (new ClientOptions())->withNamespace($runtime->namespace),
converter: $converter,
)->withTimeout(10);
$scheduleClient = ScheduleClient::create(
serviceClient: $serviceClient,
options: (new ClientOptions())->withNamespace($runtime->namespace),
converter: $converter,
)->withTimeout(10);
$container = new Spiral\Core\Container();
$container->bindSingleton(State::class, $runtime);
$container->bindSingleton(Runner::class, $runner);
$container->bindSingleton(ServiceClientInterface::class, $serviceClient);
$container->bindSingleton(WorkflowClientInterface::class, $workflowClient);
$container->bindSingleton(ScheduleClientInterface::class, $scheduleClient);
$container->bindInjector(WorkflowStubInterface::class, WorkflowStubInjector::class);
$container->bindSingleton(DataConverterInterface::class, $converter);
$container->bind(RPCInterface::class, static fn() => RPC::create('tcp://127.0.0.1:6001'));
$container->bind(
StorageInterface::class,
fn (#[Proxy] ContainerInterface $c): StorageInterface => $c->get(Factory::class)->select('harness'),
);
// Run checks
$errors = 0;
foreach ($runtime->checks() as $feature => $definition) {
try {
$container->runScope(
new Scope(name: 'feature',bindings: [
Feature::class => $feature,
]),
static function (Container $container) use ($definition) {
// todo modify services based on feature requirements
[$class, $method] = $definition;
$container->bindSingleton($class, $class);
echo "Running check \e[1;36m{$class}::{$method}\e[0m ";
$container->invoke($definition);
echo "\e[1;32mSUCCESS\e[0m\n";
},
);
} catch (SkipTest $e) {
echo "\e[1;33mSKIPPED\e[0m\n";
echo "\e[35m{$e->reason}\e[0m\n";
} catch (\Throwable $e) {
echo "\e[1;31mFAILED\e[0m\n";
\trap($e);
++$errors;
Support::echoException($e);
echo "\n";
} finally {
$runner->start();
}
}
exit($errors === 0 ? 0 : 1);