forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCatchSignalEvent.php
More file actions
100 lines (93 loc) · 3.45 KB
/
CatchSignalEvent.php
File metadata and controls
100 lines (93 loc) · 3.45 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
<?php
namespace ProcessMaker\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use ProcessMaker\Models\Process;
use ProcessMaker\Models\ProcessRequest;
use ProcessMaker\Nayra\Bpmn\Models\SignalEventDefinition;
use ProcessMaker\Nayra\Contracts\Bpmn\ThrowEventInterface;
use ProcessMaker\Nayra\Contracts\Bpmn\TokenInterface;
class CatchSignalEvent implements ShouldQueue
{
use Dispatchable,
InteractsWithQueue,
Queueable;
private const maxJobs = 10;
public $collaborationId;
public $eventDefinition;
public $payload;
public $requestId;
public $signalRef;
public $throwEvent;
public $tokenId;
public $processId;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(ThrowEventInterface $throwEvent, SignalEventDefinition $sourceEventDefinition, TokenInterface $token)
{
$this->collaborationId = $token->getInstance()->process_collaboration_id;
$this->eventDefinition = $sourceEventDefinition->getId();
$this->payload = $token->getInstance()->data;
$this->requestId = $token->getInstance()->getId();
$event = $sourceEventDefinition->getPayload();
$this->signalRef = $event ? $event->getId() : $sourceEventDefinition->getProperty('signalRef') ;
$this->throwEvent = $throwEvent->getId();
$this->tokenId = $token->getId();
$this->processId = $token->getInstance()->process_id;
}
public function handle()
{
$processes = Process::where('id', '!=', $this->processId)
->whereJsonContains('signal_events', $this->signalRef)
->pluck('id')
->toArray();
foreach ($processes as $process) {
CatchSignalEventProcess::dispatch(
$process,
$this->signalRef,
$this->payload,
$this->throwEvent,
$this->eventDefinition,
$this->tokenId,
$this->requestId
);
}
$count = ProcessRequest::where('status', 'ACTIVE')
->where('id', '!=', $this->requestId);
if ($this->collaborationId) {
$count = $count->where('process_collaboration_id', '!=', $this->collaborationId);
}
$count = $count->whereJsonContains('signal_events', $this->signalRef)
->count();
if ($count) {
$perJob = ceil($count / self::maxJobs);
$requests = ProcessRequest::select(['id'])
->whereJsonContains('signal_events', $this->signalRef)
->where('status', 'ACTIVE')
->where('id', '!=', $this->requestId);
if ($this->collaborationId) {
$requests = $requests->where('process_collaboration_id', '!=', $this->collaborationId);
}
$requests = $requests->orderBy('id')
->pluck('id')
->toArray();
$chuncks = array_chunk($requests, $perJob);
foreach ($chuncks as $chunck) {
CatchSignalEventRequest::dispatch(
$chunck,
$this->signalRef,
$this->payload,
$this->throwEvent,
$this->eventDefinition,
$this->tokenId,
$this->requestId
);
}
}
}
}