-
Notifications
You must be signed in to change notification settings - Fork 305
/
Copy pathWorker.php
265 lines (220 loc) · 8.1 KB
/
Worker.php
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
namespace Laravel\Octane;
use Closure;
use Illuminate\Container\Container;
use Illuminate\Foundation\Application;
use Illuminate\Http\Request;
use Laravel\Octane\Contracts\Client;
use Laravel\Octane\Contracts\ServesStaticFiles;
use Laravel\Octane\Contracts\Worker as WorkerContract;
use Laravel\Octane\Events\TaskReceived;
use Laravel\Octane\Events\TaskTerminated;
use Laravel\Octane\Events\TickReceived;
use Laravel\Octane\Events\TickTerminated;
use Laravel\Octane\Events\WorkerErrorOccurred;
use Laravel\Octane\Events\WorkerStarting;
use Laravel\Octane\Events\WorkerStopping;
use Laravel\Octane\Exceptions\TaskExceptionResult;
use Laravel\Octane\Swoole\TaskResult;
use RuntimeException;
use Throwable;
class Worker implements WorkerContract
{
use DispatchesEvents;
protected $requestHandledCallbacks = [];
/**
* The root application instance.
*
* @var \Illuminate\Foundation\Application
*/
protected $app;
public function __construct(
protected ApplicationFactory $appFactory,
protected Client $client
) {
}
/**
* Boot / initialize the Octane worker.
*
* @param array $initialInstances
* @return void
*/
public function boot(array $initialInstances = []): void
{
// First we will create an instance of the Laravel application that can serve as
// the base container instance we will clone from on every request. This will
// also perform the initial bootstrapping that's required by the framework.
$this->app = $app = $this->appFactory->createApplication(
array_merge(
$initialInstances,
[Client::class => $this->client],
)
);
$this->dispatchEvent($app, new WorkerStarting($app));
}
/**
* Handle an incoming request and send the response to the client.
*
* @param \Illuminate\Http\Request $request
* @param \Laravel\Octane\RequestContext $context
* @return void
*/
public function handle(Request $request, RequestContext $context): void
{
if ($this->client instanceof ServesStaticFiles &&
$this->client->canServeRequestAsStaticFile($request, $context)) {
$this->client->serveStaticFile($request, $context);
return;
}
// We will clone the application instance so that we have a clean copy to switch
// back to once the request has been handled. This allows us to easily delete
// certain instances that got resolved / mutated during a previous request.
CurrentApplication::set($sandbox = clone $this->app);
$gateway = new ApplicationGateway($this->app, $sandbox);
try {
$responded = false;
ob_start();
$response = $gateway->handle($request);
$output = ob_get_contents();
ob_end_clean();
// Here we will actually hand the incoming request to the Laravel application so
// it can generate a response. We'll send this response back to the client so
// it can be returned to a browser. This gateway will also dispatch events.
$this->client->respond(
$context,
$octaneResponse = new OctaneResponse($response, $output),
);
$responded = true;
$this->invokeRequestHandledCallbacks($request, $response, $sandbox);
$gateway->terminate($request, $response);
} catch (Throwable $e) {
$this->handleWorkerError($e, $sandbox, $request, $context, $responded);
} finally {
$sandbox->flush();
$this->app->make('view.engine.resolver')->forget('blade');
$this->app->make('view.engine.resolver')->forget('php');
// After the request handling process has completed we will unset some variables
// plus reset the current application state back to its original state before
// it was cloned. Then we will be ready for the next worker iteration loop.
unset($gateway, $sandbox, $request, $response, $octaneResponse, $output);
CurrentApplication::set($this->app);
}
}
/**
* Handle an incoming task.
*
* @param mixed $data
* @return mixed
*/
public function handleTask($data)
{
$result = false;
// We will clone the application instance so that we have a clean copy to switch
// back to once the request has been handled. This allows us to easily delete
// certain instances that got resolved / mutated during a previous request.
CurrentApplication::set($sandbox = clone $this->app);
try {
$this->dispatchEvent($sandbox, new TaskReceived($this->app, $sandbox, $data));
$result = $data();
$this->dispatchEvent($sandbox, new TaskTerminated($this->app, $sandbox, $data, $result));
} catch (Throwable $e) {
$this->dispatchEvent($sandbox, new WorkerErrorOccurred($e, $sandbox));
return TaskExceptionResult::from($e);
} finally {
$sandbox->flush();
// After the request handling process has completed we will unset some variables
// plus reset the current application state back to its original state before
// it was cloned. Then we will be ready for the next worker iteration loop.
unset($sandbox);
CurrentApplication::set($this->app);
}
return new TaskResult($result);
}
/**
* Handle an incoming tick.
*
* @return void
*/
public function handleTick(): void
{
CurrentApplication::set($sandbox = clone $this->app);
try {
$this->dispatchEvent($sandbox, new TickReceived($this->app, $sandbox));
$this->dispatchEvent($sandbox, new TickTerminated($this->app, $sandbox));
} catch (Throwable $e) {
$this->dispatchEvent($sandbox, new WorkerErrorOccurred($e, $sandbox));
} finally {
$sandbox->flush();
unset($sandbox);
CurrentApplication::set($this->app);
}
}
/**
* Handle an uncaught exception from the worker.
*
* @param \Throwable $e
* @param \Illuminate\Foundation\Application $app
* @param \Illuminate\Http\Request $request
* @param \Laravel\Octane\RequestContext $context
* @param bool $hasResponded
* @return void
*/
protected function handleWorkerError(
Throwable $e,
Application $app,
Request $request,
RequestContext $context,
bool $hasResponded
): void {
if (! $hasResponded) {
$this->client->error($e, $app, $request, $context);
}
$this->dispatchEvent($app, new WorkerErrorOccurred($e, $app));
}
/**
* Invoke the request handled callbacks.
*
* @param \Illuminate\Http\Request $request
* @param \Symfony\Component\HttpFoundation\Response $response
* @param \Illuminate\Foundation\Application $sandbox
* @return void
*/
protected function invokeRequestHandledCallbacks($request, $response, $sandbox): void
{
foreach ($this->requestHandledCallbacks as $callback) {
$callback($request, $response, $sandbox);
}
}
/**
* Register a closure to be invoked when requests are handled.
*
* @param \Closure $callback
* @return $this
*/
public function onRequestHandled(Closure $callback)
{
$this->requestHandledCallbacks[] = $callback;
return $this;
}
/**
* Get the application instance being used by the worker.
*
* @return \Illuminate\Foundation\Application
*/
public function application(): Application
{
if (! $this->app) {
throw new RuntimeException('Worker has not booted. Unable to access application.');
}
return $this->app;
}
/**
* Terminate the worker.
*
* @return void
*/
public function terminate(): void
{
$this->dispatchEvent($this->app, new WorkerStopping($this->app));
}
}