forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunScriptTask.php
More file actions
99 lines (91 loc) · 3.79 KB
/
RunScriptTask.php
File metadata and controls
99 lines (91 loc) · 3.79 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
<?php
namespace ProcessMaker\Jobs;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Log;
use ProcessMaker\Exception\ScriptException;
use ProcessMaker\Facades\WorkflowManager;
use ProcessMaker\Models\Process as Definitions;
use ProcessMaker\Models\ProcessRequest;
use ProcessMaker\Models\ProcessRequestToken;
use ProcessMaker\Models\Script;
use ProcessMaker\Models\ScriptExecutor;
use ProcessMaker\Nayra\Contracts\Bpmn\ScriptTaskInterface;
use Throwable;
class RunScriptTask extends BpmnAction implements ShouldQueue
{
public $definitionsId;
public $instanceId;
public $tokenId;
public $data;
/**
* Create a new job instance.
*
* @param \ProcessMaker\Models\Process $definitions
* @param \ProcessMaker\Models\ProcessRequest $instance
* @param \ProcessMaker\Models\ProcessRequestToken $token
* @param array $data
*/
public function __construct(Definitions $definitions, ProcessRequest $instance, ProcessRequestToken $token, array $data)
{
$this->definitionsId = $definitions->getKey();
$this->instanceId = $instance->getKey();
$this->tokenId = $token->getKey();
$this->data = $data;
}
/**
* Execute the script task.
*
* @return void
*/
public function action(ProcessRequestToken $token, ScriptTaskInterface $element, ProcessRequest $instance)
{
$scriptRef = $element->getProperty('scriptRef');
Log::info('Script started: ' . $scriptRef);
$configuration = json_decode($element->getProperty('config'), true);
// Check to see if we've failed parsing. If so, let's convert to empty array.
if ($configuration === null) {
$configuration = [];
}
$dataStore = $token->getInstance()->getDataStore();
$data = $dataStore->getData();
try {
if (empty($scriptRef)) {
$code = $element->getScript();
if (empty($code)) {
throw new ScriptException(__('No code or script assigned to ":name"', ['name' => $element->getName()]));
}
$language = Script::scriptFormat2Language($element->getProperty('scriptFormat', 'application/x-php'));
$script = new Script([
'code' => $code,
'language' => $language,
'run_as_user_id' => Script::defaultRunAsUser()->id,
'script_executor_id' => ScriptExecutor::initialExecutor($language)->id,
]);
} else {
$script = Script::find($scriptRef);
}
$this->unlockInstance($instance->getKey());
$response = $script->runScript($data, $configuration);
$this->withUpdatedContext(function ($engine, $instance, $element, $processModel, $token) use ($response) {
$dataStore = $token->getInstance()->getDataStore();
// Update data
if (is_array($response['output'])) {
// Validate data
WorkflowManager::validateData($response['output'], $processModel, $element);
foreach ($response['output'] as $key => $value) {
$dataStore->putData($key, $value);
}
}
$element->complete($token);
$this->engine = $engine;
$this->instance = $instance;
});
Log::info('Script completed: ' . $scriptRef);
} catch (Throwable $exception) {
// Change to error status
$token->setStatus(ScriptTaskInterface::TOKEN_STATE_FAILING);
$token->getInstance()->logError($exception, $element);
Log::info('Script failed: ' . $scriptRef . ' - ' . $exception->getMessage());
}
}
}