Skip to content

Commit 053189b

Browse files
committed
Add errorHandler to scripts and data sources
1 parent 881b961 commit 053189b

7 files changed

Lines changed: 77 additions & 7 deletions

File tree

‎ProcessMaker/Jobs/RunScriptTask.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function action(ProcessRequestToken $token = null, ScriptTaskInterface $e
8686
$this->unlock();
8787
$dataManager = new DataManager();
8888
$data = $dataManager->getData($token);
89-
$response = $script->runScript($data, $configuration, $token->getId());
89+
$response = $script->runScript($data, $configuration, $token->getId(), $errorHandling);
9090

9191
$this->withUpdatedContext(function ($engine, $instance, $element, $processModel, $token) use ($response) {
9292
// Exit if the task was completed or closed

‎ProcessMaker/Jobs/RunServiceTask.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function action(ProcessRequestToken $token = null, ServiceTaskInterface $
9393
'output' => WorkflowManager::runServiceImplementation($implementation, $data, $configuration, $token->getId(), $errorHandling),
9494
];
9595
} else {
96-
$response = $script->runScript($data, $configuration, $token->getId());
96+
$response = $script->runScript($data, $configuration, $token->getId(), $errorHandling);
9797
}
9898
$this->withUpdatedContext(function ($engine, $instance, $element, $processModel, $token) use ($response) {
9999
// Exit if the task was completed or closed

‎ProcessMaker/Models/ScriptVersion.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ public function parent()
5050
* @param array $data
5151
* @param array $config
5252
*/
53-
public function runScript(array $data, array $config, $tokenId = '')
53+
public function runScript(array $data, array $config, $tokenId = '', array $errorHandling = [])
5454
{
5555
$script = $this->parent->replicate();
5656
$except = $script->getGuarded();
5757
foreach (collect($script->getAttributes())->except($except)->keys() as $prop) {
5858
$script->$prop = $this->$prop;
5959
}
6060

61-
return $script->runScript($data, $config, $tokenId);
61+
return $script->runScript($data, $config, $tokenId, $errorHandling);
6262
}
6363

6464
/**

‎ProcessMaker/ScriptRunners/MockRunner.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class MockRunner
1010
{
11-
public function __construct($_executor)
11+
public function __construct($scriptExecutor)
1212
{
1313
}
1414

‎ProcessMaker/ScriptRunners/ScriptRunner.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private function getScriptRunner(ScriptExecutor $executor)
5353
} else {
5454
$class = "ProcessMaker\\ScriptRunners\\{$runner}";
5555

56-
return new $class($executor);
56+
return app()->make($class, ['scriptExecutor' => $executor]);
5757
}
5858
}
5959

‎ProcessMaker/Traits/MakeHttpRequests.php‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ trait MakeHttpRequests
3434

3535
private $mustache = null;
3636

37+
private $timeout = 0;
38+
3739
private function getMustache()
3840
{
3941
if ($this->mustache === null) {
@@ -98,6 +100,23 @@ private function prepareRequest(array &$data, array &$config)
98100
return $request;
99101
}
100102

103+
/**
104+
* Set timeout from endpoint config or from BPMN errorHandling
105+
*
106+
* @param array $endpoint
107+
* @param array $config
108+
*
109+
* @return void
110+
*/
111+
private function setTimeout(array $endpoint, array $config)
112+
{
113+
$this->timeout = (int) Arr::get($endpoint, 'timeout', 0);
114+
$bpmnTimeout = Arr::get($config, 'errorHandling.timeout', null);
115+
if ($bpmnTimeout) {
116+
$this->timeout = (int) $bpmnTimeout;
117+
}
118+
}
119+
101120
/**
102121
* Prepare data to be used in body (mustache)
103122
*
@@ -202,6 +221,8 @@ private function prepareRequestWithOutboundConfig(array $requestData, array &$co
202221
$request = [$method, $url, $headers, $body, $bodyType];
203222
$request = $this->addAuthorizationHeaders(...$request);
204223

224+
$this->setTimeout($endpoint, $config);
225+
205226
return $request;
206227
}
207228

@@ -427,7 +448,13 @@ private function responseWithHeaderData($response, array $data = [], array $conf
427448
*/
428449
private function call($method, $url, array $headers, $body, $bodyType)
429450
{
430-
$client = $this->client ?? new Client(['verify' => $this->verifySsl]);
451+
$client = $this->client ?? app()->make(Client::class, [
452+
'config' => [
453+
'verify' => $this->verifySsl,
454+
'timeout' => $this->timeout,
455+
],
456+
]);
457+
431458
$options = [];
432459
if ($bodyType === 'form-data') {
433460
$options['form_params'] = json_decode($body, true);

‎tests/Model/ScriptTest.php‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Tests\Model;
4+
5+
use Mockery;
6+
use ProcessMaker\Models\Script;
7+
use ProcessMaker\ScriptRunners\MockRunner;
8+
use Tests\TestCase;
9+
10+
class ScriptTest extends TestCase
11+
{
12+
public function testUseTimeoutFromModeler()
13+
{
14+
$this->mockWithExpectedTimeout(123);
15+
16+
$script = Script::factory()->create([
17+
'code' => 'foo',
18+
'timeout' => 5,
19+
]);
20+
$script->runScript([], [], '', ['timeout' => 123]);
21+
}
22+
23+
public function testUseTimeoutFromSetting()
24+
{
25+
$this->mockWithExpectedTimeout(5);
26+
27+
$script = Script::factory()->create([
28+
'code' => 'foo',
29+
'timeout' => 5,
30+
]);
31+
$script->runScript([], [], '', ['timeout' => '']);
32+
}
33+
34+
private function mockWithExpectedTimeout($timeout)
35+
{
36+
$mock = Mockery::mock(MockRunner::class);
37+
$mock->shouldReceive('run')->once()->with('foo', [], [], $timeout, Mockery::any());
38+
$mock->shouldReceive('setTokenId')->once();
39+
app()->bind(MockRunner::class, function () use ($mock) {
40+
return $mock;
41+
});
42+
}
43+
}

0 commit comments

Comments
 (0)