Skip to content

Commit dac2f2f

Browse files
committed
Add ScriptTimeoutException
1 parent 325371f commit dac2f2f

2 files changed

Lines changed: 26 additions & 19 deletions

File tree

‎ProcessMaker/Models/ScriptDockerBindingFilesTrait.php‎

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
namespace ProcessMaker\Models;
44

55
use Log;
6-
use RuntimeException;
6+
use ProcessMaker\Exception\ScriptTimeoutException;
77

88
/**
99
* Execute a docker container binding files to interchange information.
1010
*
1111
*/
1212
trait ScriptDockerBindingFilesTrait
1313
{
14-
1514
private $temporalFiles = [];
1615
private $outputFiles = [];
1716

@@ -32,8 +31,13 @@ protected function executeBinding(array $options)
3231
foreach ($options['outputs'] as $name => $guestFile) {
3332
$bindings .= $this->bindOutput($guestFile, $name);
3433
}
35-
$response = $this->runContainer($options['image'], $options['command'],
36-
$options['parameters'], $bindings, $options['timeout']);
34+
$response = $this->runContainer(
35+
$options['image'],
36+
$options['command'],
37+
$options['parameters'],
38+
$bindings,
39+
$options['timeout']
40+
);
3741
return $response;
3842
}
3943

@@ -47,18 +51,23 @@ protected function executeBinding(array $options)
4751
* @param integer $timeout
4852
*
4953
* @return array
50-
* @throws RuntimeException
54+
* @throws ScriptTimeoutException
5155
*/
5256
private function runContainer($image, $command, $parameters, $bindings, $timeout)
5357
{
5458
$cmd = '';
55-
59+
5660
if ($timeout > 0) {
5761
$cmd .= "timeout -s 9 $timeout ";
5862
}
59-
60-
$cmd .= config('app.bpm_scripts_docker') . sprintf(' run %s %s %s %s 2>&1',
61-
$parameters, $bindings, $image, $command);
63+
64+
$cmd .= config('app.bpm_scripts_docker') . sprintf(
65+
' run %s %s %s %s 2>&1',
66+
$parameters,
67+
$bindings,
68+
$image,
69+
$command
70+
);
6271

6372
Log::debug('Running Docker container', [
6473
'timeout' => $timeout,
@@ -67,14 +76,13 @@ private function runContainer($image, $command, $parameters, $bindings, $timeout
6776

6877
$line = exec($cmd, $output, $returnCode);
6978
if ($returnCode) {
70-
7179
if ($returnCode == 137) {
7280
Log::error('Script timed out');
7381
} else {
7482
Log::error('Script threw return code ' . $returnCode);
7583
}
76-
77-
throw new RuntimeException(implode("\n", $output));
84+
85+
throw new ScriptTimeoutException(implode("\n", $output));
7886
}
7987
$outputs = $this->getOutputFilesContent();
8088
$this->removeTemporalFiles();
@@ -91,7 +99,7 @@ private function runContainer($image, $command, $parameters, $bindings, $timeout
9199
*/
92100
private function bindFile($hostFile, $guestFile)
93101
{
94-
return sprintf(" -v %s:%s", $hostFile, $guestFile);
102+
return sprintf(' -v %s:%s', $hostFile, $guestFile);
95103
}
96104

97105
/**

‎ProcessMaker/Models/ScriptDockerCopyingFilesTrait.php‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
use Log;
66
use RuntimeException;
7+
use ProcessMaker\Exception\ScriptTimeoutException;
78

89
/**
910
* Execute a docker container copying files to interchange information.
1011
*
1112
*/
1213
trait ScriptDockerCopyingFilesTrait
1314
{
14-
1515
/**
1616
* Run a command in a docker container.
1717
*
@@ -115,11 +115,11 @@ private function getFromContainer($container, $path)
115115
private function startContainer($container, $timeout)
116116
{
117117
$cmd = '';
118-
118+
119119
if ($timeout > 0) {
120120
$cmd .= "timeout -s 9 $timeout ";
121121
}
122-
122+
123123
$cmd .= config('app.bpm_scripts_docker') . sprintf(' start %s -a 2>&1', $container);
124124

125125
Log::debug('Running Docker container', [
@@ -129,14 +129,13 @@ private function startContainer($container, $timeout)
129129

130130
$line = exec($cmd, $output, $returnCode);
131131
if ($returnCode) {
132-
133132
if ($returnCode == 137) {
134133
Log::error('Script timed out');
135134
} else {
136135
Log::error('Script threw return code ' . $returnCode);
137136
}
138-
139-
throw new RuntimeException(implode("\n", $output));
137+
138+
throw new ScriptTimeoutException(implode("\n", $output));
140139
}
141140
return compact('line', 'output', 'returnCode');
142141
}

0 commit comments

Comments
 (0)