【问题标题】:How can I restart the doctrine entity manager for Symfony 4.2?如何重新启动 Symfony 4.2 的理论实体管理器?
【发布时间】:2023-03-18 16:42:02
【问题描述】:

我正在使用 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 supervisord symfony-4.2 enqueue


    【解决方案1】:

    如果您查看 Doctrine 来源 - 您会看到 (1, 2) 如果在事务上下文中抛出一些异常,EntityManager 将被关闭。

    这意味着如果您的EntityManager 关闭 - 应用程序或数据库可能有问题(例如,与数据库的连接丢失或存在一些数据不一致等)。从 Doctrine 来源中,您还可以看到 (1, 2) 在 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”方式,除非绝对必要,否则我不会推荐它。

    【讨论】:

      【解决方案2】:

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

      新的主管设置

      [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
      

      谢谢

      【讨论】:

        【解决方案3】:

        我相信您正在寻找的是 Enqueue 库的 DoctrinePingConnectionExtension。

        这是启用此功能所需的 services.yaml 配置:

        services:
            app.enqueue.doctrine_ping_connection_extension:
                class: 'Enqueue\Bundle\Consumption\Extension\DoctrinePingConnectionExtension'
                tags:
                    - { name: 'enqueue.consumption.extension', priority: 10, client: 'all' }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-10-30
          • 2018-09-15
          • 1970-01-01
          • 1970-01-01
          • 2012-12-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多