如何重新启动Symfony 4.2的doctrine实体管理器?

问题描述 投票:0回答:2

我使用的是Symfony版本4.2。我使用下面写的其他包。

"php": "^7.1.3",
"ext-ctype": "*",
"ext-iconv": "*",
"enqueue/enqueue-bundle": "^0.9.7",
"enqueue/pheanstalk": "^0.9.7",
"friendsofsymfony/elastica-bundle": "^5.0",
"nelmio/cors-bundle": "^1.5",
"nesbot/carbon": "^2.10",
"symfony/console": "*",
"symfony/flex": "^1.1",
"symfony/framework-bundle": "*",
"symfony/orm-pack": "^1.0",
"symfony/serializer-pack": "^1.0",
"symfony/yaml": "*"

我正在使用主管运行cosume命令。我在下面写了我的主管设置。

[program:devlog-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/project-directory/bin/console enqueue:consume --setup-broker
autostart=true
autorestart=true
user=nginx
numprocs=1
redirect_stderr=true
stdout_logfile=/var/www/project-directory/worker.log

实体经理在工作一段时间后关闭。我想我需要重新开始。但我无法在代码中执行此操作。但是当主管重新启动时,一切都开始工作了。我怎么能解决这个问题,因为我总是从一开始就重新启动主管。

我正在编写下面的示例流程。

<?php

namespace App\Processor;

use App\Entity\Main\Event;
use Doctrine\ORM\EntityManagerInterface;
use Interop\Queue\Message;
use Interop\Queue\Context;
use Interop\Queue\Processor;
use Enqueue\Client\TopicSubscriberInterface;

class FooProcessor implements Processor, TopicSubscriberInterface
{
    protected $entityManager;

    public function __construct(EntityManagerInterface $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    public function process(Message $message, Context $session)
    {
        try {
            $event = new Event();
            $event->setAction('example');

            if (!$this->entityManager->isOpen()) {
                echo "Entity Manger is closed...\n";
                // here i need to restart the entity manager or another solution
            }

            $this->entityManager->persist($event);
            $this->entityManager->flush();
            $this->entityManager->clear();

            echo "Success\n";
            return self::ACK;

        } catch (\Exception $e){
            echo ($e->getMessage())." \n";
            return self::REJECT;
        }
    }

    public static function getSubscribedTopics()
    {
        return ['aFooTopic'];
    }
}
doctrine-orm supervisor symfony-4.2 enqueue
2个回答
1
投票

如果您将查看Doctrine源代码 - 您会看到(12EntityManager如果在事务上下文中抛出某个异常,则会被关闭。

这意味着如果你的EntityManager被关闭 - 应用程序或数据库可能出现问题(例如,数据库的连接丢失或存在一些数据不一致等等)。从Doctrine来源,您还可以看到(12)在EntityManager关闭的情况下抛出异常,查看您的来源,您应该看到此异常在关闭后立即回显。

当然,您应该首先检查这些异常,以检查EntityManager关闭的原因,并在可能的情况下消除它们的原因。这是推荐的方式。

没有内置的方法可以重新打开封闭的EntityManager,但由于“封闭”状态只是Doctrine中的一个标志 - 你可以通过Reflection清除它:

$reflection = new \ReflectionObject($this->entityManager);
$prop = $reflection->getProperty('closed');
$prop->setAccessible(true);
$prop->setValue($this->entityManager, false);
$prop->setAccessible(false);

但这是一种“hacky”方式,在绝对必要之前我不会推荐它。


1
投票

通过更改管理员设置解决了该问题。我错过了设置。

新的主管设置

[program:devlog-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/project-directory/bin/console enqueue:consume --setup-broker --env=prod --no-debug --time-limit="now + 5 minutes"
autostart=true
autorestart=true
user=nginx
numprocs=1
redirect_stderr=true
stdout_logfile=/var/www/project-directory/worker.log

谢谢

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.