@@ -17,7 +17,8 @@ |
||
| 17 | 17 | * |
| 18 | 18 | * @see https://github.com/symplify/symplify/issues/2061 |
| 19 | 19 | */ |
| 20 | -register_shutdown_function(static function () { |
|
| 20 | +register_shutdown_function(static function () |
|
| 21 | +{ |
|
| 21 | 22 | $dest = json_decode(file_get_contents(__DIR__ . '/composer.json'), true); |
| 22 | 23 | |
| 23 | 24 | $result = [ |
@@ -26,7 +26,8 @@ discard block |
||
| 26 | 26 | { |
| 27 | 27 | $dotenvPath = $env->get('DOTENV_PATH', $dirs->get('root') . '.env'); |
| 28 | 28 | |
| 29 | - if (!file_exists($dotenvPath)) { |
|
| 29 | + if (!file_exists($dotenvPath)) |
|
| 30 | + { |
|
| 30 | 31 | return; |
| 31 | 32 | } |
| 32 | 33 | |
@@ -36,7 +37,8 @@ discard block |
||
| 36 | 37 | ) |
| 37 | 38 | ->load(); |
| 38 | 39 | |
| 39 | - foreach ($dotenv as $key => $value) { |
|
| 40 | + foreach ($dotenv as $key => $value) |
|
| 41 | + { |
|
| 40 | 42 | $env->set($key, $value); |
| 41 | 43 | } |
| 42 | 44 | } |
@@ -80,14 +80,20 @@ discard block |
||
| 80 | 80 | { |
| 81 | 81 | /** @var Http $http */ |
| 82 | 82 | $http = $this->container->get(Http::class); |
| 83 | - while ($request = $this->worker->waitRequest()) { |
|
| 84 | - try { |
|
| 83 | + while ($request = $this->worker->waitRequest()) |
|
| 84 | + { |
|
| 85 | + try |
|
| 86 | + { |
|
| 85 | 87 | $response = $http->handle($request); |
| 86 | 88 | |
| 87 | 89 | $this->worker->respond($response); |
| 88 | - } catch (\Throwable $e) { |
|
| 90 | + } |
|
| 91 | + catch (\Throwable $e) |
|
| 92 | + { |
|
| 89 | 93 | $this->worker->respond($this->errorToResponse($e)); |
| 90 | - } finally { |
|
| 94 | + } |
|
| 95 | + finally |
|
| 96 | + { |
|
| 91 | 97 | $this->finalizer->finalize(false); |
| 92 | 98 | } |
| 93 | 99 | } |
@@ -101,7 +107,8 @@ discard block |
||
| 101 | 107 | { |
| 102 | 108 | $handler = new HtmlHandler(); |
| 103 | 109 | |
| 104 | - try { |
|
| 110 | + try |
|
| 111 | + { |
|
| 105 | 112 | /** @var SnapshotInterface $snapshot */ |
| 106 | 113 | $snapshot = $this->container->get(SnapshotterInterface::class)->register($e); |
| 107 | 114 | \file_put_contents('php://stderr', $snapshot->getMessage()); |
@@ -109,10 +116,13 @@ discard block |
||
| 109 | 116 | // on demand |
| 110 | 117 | $state = $this->container->get(StateInterface::class); |
| 111 | 118 | |
| 112 | - if ($state !== null) { |
|
| 119 | + if ($state !== null) |
|
| 120 | + { |
|
| 113 | 121 | $handler = $handler->withState($state); |
| 114 | 122 | } |
| 115 | - } catch (\Throwable | ContainerExceptionInterface $se) { |
|
| 123 | + } |
|
| 124 | + catch (\Throwable | ContainerExceptionInterface $se) |
|
| 125 | + { |
|
| 116 | 126 | \file_put_contents('php://stderr', (string)$e); |
| 117 | 127 | } |
| 118 | 128 | |
@@ -53,13 +53,16 @@ discard block |
||
| 53 | 53 | */ |
| 54 | 54 | public function push(string $jobType, array $payload = [], Options $options = null): string |
| 55 | 55 | { |
| 56 | - try { |
|
| 56 | + try |
|
| 57 | + { |
|
| 57 | 58 | return $this->rpc->call(self::RR_SERVICE . '.Push', [ |
| 58 | 59 | 'job' => $this->jobName($jobType), |
| 59 | 60 | 'payload' => $this->serialize($jobType, $payload), |
| 60 | 61 | 'options' => $options ?? new Options(), |
| 61 | 62 | ]); |
| 62 | - } catch (RoadRunnerException | \Throwable $e) { |
|
| 63 | + } |
|
| 64 | + catch (RoadRunnerException | \Throwable $e) |
|
| 65 | + { |
|
| 63 | 66 | throw new JobException($e->getMessage(), $e->getCode(), $e); |
| 64 | 67 | } |
| 65 | 68 | } |
@@ -76,13 +79,16 @@ discard block |
||
| 76 | 79 | */ |
| 77 | 80 | public function pushAsync(string $jobType, array $payload = [], Options $options = null): bool |
| 78 | 81 | { |
| 79 | - try { |
|
| 82 | + try |
|
| 83 | + { |
|
| 80 | 84 | return $this->rpc->call(self::RR_SERVICE . '.PushAsync', [ |
| 81 | 85 | 'job' => $this->jobName($jobType), |
| 82 | 86 | 'payload' => $this->serialize($jobType, $payload), |
| 83 | 87 | 'options' => $options ?? new Options(), |
| 84 | 88 | ]); |
| 85 | - } catch (RoadRunnerException | \Throwable $e) { |
|
| 89 | + } |
|
| 90 | + catch (RoadRunnerException | \Throwable $e) |
|
| 91 | + { |
|
| 86 | 92 | throw new JobException($e->getMessage(), $e->getCode(), $e); |
| 87 | 93 | } |
| 88 | 94 | } |
@@ -94,7 +100,8 @@ discard block |
||
| 94 | 100 | private function jobName(string $job): string |
| 95 | 101 | { |
| 96 | 102 | $names = explode('\\', $job); |
| 97 | - $names = array_map(function (string $value) { |
|
| 103 | + $names = array_map(function (string $value) |
|
| 104 | + { |
|
| 98 | 105 | return $this->inflector->camelize($value); |
| 99 | 106 | }, $names); |
| 100 | 107 | |
@@ -61,11 +61,14 @@ discard block |
||
| 61 | 61 | /** @var ConsumerInterface $consumer */ |
| 62 | 62 | $consumer = $this->container->get(ConsumerInterface::class); |
| 63 | 63 | |
| 64 | - while ($task = $consumer->waitTask()) { |
|
| 65 | - try { |
|
| 64 | + while ($task = $consumer->waitTask()) |
|
| 65 | + { |
|
| 66 | + try |
|
| 67 | + { |
|
| 66 | 68 | $instance = $this->container->get($task->getName()); |
| 67 | 69 | |
| 68 | - if (!$instance instanceof HandlerInterface) { |
|
| 70 | + if (!$instance instanceof HandlerInterface) |
|
| 71 | + { |
|
| 69 | 72 | throw new \LogicException( |
| 70 | 73 | \sprintf('Job should be an instance %s', HandlerInterface::class) |
| 71 | 74 | ); |
@@ -73,7 +76,9 @@ discard block |
||
| 73 | 76 | |
| 74 | 77 | $instance->handle($task->getName(), $task->getId(), $task->getPayload()); |
| 75 | 78 | $task->complete(); |
| 76 | - } catch (\Throwable $e) { |
|
| 79 | + } |
|
| 80 | + catch (\Throwable $e) |
|
| 81 | + { |
|
| 77 | 82 | $this->handleException($e); |
| 78 | 83 | $task->fail($e); |
| 79 | 84 | } |
@@ -87,9 +92,12 @@ discard block |
||
| 87 | 92 | */ |
| 88 | 93 | protected function handleException(\Throwable $e): void |
| 89 | 94 | { |
| 90 | - try { |
|
| 95 | + try |
|
| 96 | + { |
|
| 91 | 97 | $this->container->get(SnapshotterInterface::class)->register($e); |
| 92 | - } catch (\Throwable | ContainerExceptionInterface $se) { |
|
| 98 | + } |
|
| 99 | + catch (\Throwable | ContainerExceptionInterface $se) |
|
| 100 | + { |
|
| 93 | 101 | // no need to notify when unable to register an exception |
| 94 | 102 | } |
| 95 | 103 | } |
@@ -128,7 +128,8 @@ |
||
| 128 | 128 | */ |
| 129 | 129 | private function getContext(?OptionsInterface $options): QueueInterface |
| 130 | 130 | { |
| 131 | - if ($options instanceof Options && $options->getPipeline() !== null) { |
|
| 131 | + if ($options instanceof Options && $options->getPipeline() !== null) |
|
| 132 | + { |
|
| 132 | 133 | $original = $this->jobs->connect($options->getPipeline()); |
| 133 | 134 | |
| 134 | 135 | return new self($this->jobs, $original); |
@@ -40,7 +40,8 @@ discard block |
||
| 40 | 40 | { |
| 41 | 41 | $kernel->addDispatcher($jobs); |
| 42 | 42 | |
| 43 | - if (!$container->has(SerializerInterface::class)) { |
|
| 43 | + if (!$container->has(SerializerInterface::class)) |
|
| 44 | + { |
|
| 44 | 45 | $this->registerJobsSerializer($container); |
| 45 | 46 | } |
| 46 | 47 | |
@@ -79,7 +80,8 @@ discard block |
||
| 79 | 80 | */ |
| 80 | 81 | private function registerJobsSerializer(Container $container): void |
| 81 | 82 | { |
| 82 | - $container->bindSingleton(SerializerInterface::class, static function () { |
|
| 83 | + $container->bindSingleton(SerializerInterface::class, static function () |
|
| 84 | + { |
|
| 83 | 85 | return new DefaultSerializer(); |
| 84 | 86 | }); |
| 85 | 87 | } |
@@ -134,7 +136,8 @@ discard block |
||
| 134 | 136 | private function registerQueue(Container $container): void |
| 135 | 137 | { |
| 136 | 138 | $container->bindSingleton(Queue::class, static function (JobsInterface $jobs): Queue { |
| 137 | - foreach ($jobs as $queue) { |
|
| 139 | + foreach ($jobs as $queue) |
|
| 140 | + { |
|
| 138 | 141 | return $queue; |
| 139 | 142 | } |
| 140 | 143 | |
@@ -40,10 +40,13 @@ |
||
| 40 | 40 | $method = new \ReflectionMethod($this, $this->getHandlerMethod()); |
| 41 | 41 | $method->setAccessible(true); |
| 42 | 42 | |
| 43 | - try { |
|
| 43 | + try |
|
| 44 | + { |
|
| 44 | 45 | $parameters = \array_merge(['payload' => $payload, 'id' => $id], $payload); |
| 45 | 46 | $method->invokeArgs($this, $this->resolver->resolveArguments($method, $parameters)); |
| 46 | - } catch (\Throwable $e) { |
|
| 47 | + } |
|
| 48 | + catch (\Throwable $e) |
|
| 49 | + { |
|
| 47 | 50 | $message = \sprintf('[%s] %s', \get_class($this), $e->getMessage()); |
| 48 | 51 | throw new JobException($message, (int)$e->getCode(), $e); |
| 49 | 52 | } |