forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunServiceTask.php
More file actions
85 lines (78 loc) · 3.22 KB
/
RunServiceTask.php
File metadata and controls
85 lines (78 loc) · 3.22 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
<?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\Script;
use ProcessMaker\Nayra\Contracts\Bpmn\ServiceTaskInterface;
use ProcessMaker\Nayra\Contracts\Bpmn\TokenInterface;
use ProcessMaker\Nayra\Contracts\Engine\ExecutionInstanceInterface;
use Throwable;
class RunServiceTask extends BpmnAction implements ShouldQueue
{
public $definitionsId;
public $instanceId;
public $tokenId;
public $data;
/**
* Create a new job instance.
*
* @param \ProcessMaker\Models\Process $definitions
* @param \ProcessMaker\Nayra\Contracts\Engine\ExecutionInstanceInterface $instance
* @param \ProcessMaker\Nayra\Contracts\Bpmn\TokenInterface $token
* @param array $data
*/
public function __construct(Definitions $definitions, ExecutionInstanceInterface $instance, TokenInterface $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(TokenInterface $token, ServiceTaskInterface $element, Definitions $processModel)
{
$implementation = $element->getImplementation();
Log::info('Service task started: ' . $implementation);
$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($implementation)) {
Log::error('Service task implementation not defined');
throw new ScriptException('Service task implementation not defined');
} else {
$script = Script::where('key', $implementation)->first();
}
if (empty($script)) {
Log::error('Service task not implemented: ' . $implementation);
throw new ScriptException('Service task not implemented: ' . $implementation);
}
$response = $script->runScript($data, $configuration);
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);
Log::info('Service task completed: ' . $implementation);
} catch (Throwable $exception) {
// Change to error status
$token->setStatus(ServiceTaskInterface::TOKEN_STATE_FAILING);
$token->getInstance()->logError($exception, $element);
Log::info('Service task failed: ' . $implementation . ' - ' . $exception->getMessage());
}
}
}