Repository navigation
Expand file tree
/
Copy pathProfiledQueueWorker.php
More file actions
123 lines (101 loc) · 2.81 KB
/
Copy pathProfiledQueueWorker.php
File metadata and controls
123 lines (101 loc) · 2.81 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
namespace Perfbase\Drupal\Queue;
use Drupal\Core\Queue\QueueWorkerInterface;
use Perfbase\Drupal\Config\ConfigResolver;
use Perfbase\Drupal\Lifecycle\QueueItemLifecycle;
use Perfbase\Drupal\Runtime\PerfbaseFactoryInterface;
use Perfbase\Drupal\Support\ErrorHandler;
/**
*
*/
class ProfiledQueueWorker implements QueueWorkerInterface {
private QueueWorkerInterface $inner;
private string $pluginId;
private PerfbaseFactoryInterface $factory;
private ConfigResolver $configResolver;
private ErrorHandler $errorHandler;
public function __construct(
QueueWorkerInterface $inner,
string $pluginId,
PerfbaseFactoryInterface $factory,
ConfigResolver $configResolver,
ErrorHandler $errorHandler,
) {
$this->inner = $inner;
$this->pluginId = $pluginId;
$this->factory = $factory;
$this->configResolver = $configResolver;
$this->errorHandler = $errorHandler;
}
/**
* @param mixed $data
*/
public function processItem($data): void {
$lifecycle = new QueueItemLifecycle(
$this->pluginId,
$this->factory,
$this->configResolver,
$this->errorHandler
);
$lifecycle->startProfiling();
try {
$this->inner->processItem($data);
}
catch (\Throwable $exception) {
$this->finishAfterFailure($lifecycle, $exception);
throw $exception;
}
$lifecycle->stopProfiling();
}
/**
*
*/
public function getPluginId(): string {
return (string) $this->inner->getPluginId();
}
/**
* @return array<string, mixed>
*/
public function getPluginDefinition(): array {
$definition = $this->inner->getPluginDefinition();
return is_array($definition) ? $definition : [];
}
/**
*
*/
public function getBaseId(): string {
$parts = explode(':', $this->pluginId, 2);
return $parts[0];
}
/**
*
*/
public function getDerivativeId(): ?string {
$parts = explode(':', $this->pluginId, 2);
return $parts[1] ?? NULL;
}
/**
* @param array<int, mixed> $arguments
* @return mixed
*/
public function __call(string $name, array $arguments) {
return $this->inner->{$name}(...$arguments);
}
/**
* Records the failure and finishes the trace without masking the exception.
*
* The original exception must reach the caller unchanged: Drupal's queue
* runner relies on SuspendQueueException, RequeueException and
* DelayedRequeueException. A profiling error (rethrown in debug mode) must
* never replace it.
*/
private function finishAfterFailure(QueueItemLifecycle $lifecycle, \Throwable $exception): void {
try {
$lifecycle->setException($exception->getMessage());
$lifecycle->stopProfiling();
}
catch (\Throwable $profilingException) {
// Ignored: the application's exception takes precedence.
}
}
}