簡體   English   中英

更新 Messenger Handler (Symfony) 中的實體

[英]Update an entity in Messenger Handler (Symfony)

我正試圖將我的沉重待遇轉移到Messenger

它有效,但我想向我的用戶顯示進度的百分比。 我創建了一個 DownloadTask 實體,並嘗試在此過程中更新它,但它不起作用。 我的實體未在數據庫中更新。

你有什么想法?

我還在學習,所以我希望我的代碼不會太傷你的眼睛。

<?php

namespace App\MessageHandler;

use App\Message\GetSellerList;
use App\Repository\DownloadTaskRepository;
use App\Service\EbayDL;
use Doctrine\DBAL\ConnectionException;
use Doctrine\ORM\EntityManagerInterface;
use DTS\eBaySDK\Trading\Types\GetSellerListResponseType;
use GuzzleHttp\Promise;
use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;

class GetSellerListHandler implements MessageHandlerInterface
{
    /**
     * @var EbayDL
     */
    private $ebayDL;
    /**
     * @var EntityManagerInterface
     */
    private $entityManager;
    /**
     * @var LoggerInterface
     */
    private $logger;
    /**
     * @var \App\Entity\DownloadTask
     */
    private $task;
    /**
     * @var DownloadTaskRepository
     */
    private $downloadTaskRepository;

    public function __construct(EbayDL $ebayDL, EntityManagerInterface $entityManager, DownloadTaskRepository $downloadTaskRepository, LoggerInterface $logger)
    {
        $this->ebayDL                 = $ebayDL;
        $this->entityManager          = $entityManager;
        $this->logger                 = $logger;
        $this->downloadTaskRepository = $downloadTaskRepository;
    }

    /**
     * @throws ConnectionException
     * @throws \Exception
     * @throws \Throwable
     */
    public function __invoke(GetSellerList $getSellerList): void
    {
        $task = $this->downloadTaskRepository->find($getSellerList->getDownloadTaskId());

        $this->clearDatabase();
        $listingInfos = $this->ebayDL->getInformation();

        $totalNumberOfPages = $listingInfos['totalNumberOfPages'];

        $promises = (function () use ($totalNumberOfPages) {
            for ($page = 1; $page <= $totalNumberOfPages; ++$page) {
                yield $this->ebayDL->getProductsByPageAsync($page);
            }
        })();

        $eachPromise = new Promise\EachPromise($promises, [
            'concurrency' => 6,
            'fulfilled'   => function (GetSellerListResponseType $response): void {
                $products = $this->ebayDL->parseSellerListResponse($response);
                foreach ($products as $product) {
                    try {
                        $this->entityManager->persist($product);
                        $this->entityManager->flush();
                    } catch (\Exception $e) {
                        $this->logger->error('Failed to store a product');
                        $this->logger->error($e->getMessage());
                        if (!$this->entityManager->isOpen()) {
                            // https://stackoverflow.com/questions/14258591/the-entitymanager-is-closed
                            $this->entityManager = $this->entityManager->create(
                                $this->entityManager->getConnection(),
                                $this->entityManager->getConfiguration()
                            );
                        }
                    }
                }
            },
        ]);

        $eachPromise->promise()->wait();

        $this->task
            ->setProgress(100)
            ->setCompletedAt(new \DateTime('NOW'));

        $this->entityManager->flush();
    }

    /**
     * @throws ConnectionException
     * @throws \Doctrine\DBAL\DBALException
     */
    private function clearDatabase(): void
    {
        $connection = $this->entityManager->getConnection();
        $connection->beginTransaction();

        try {
            $connection->query('SET FOREIGN_KEY_CHECKS=0');
            $connection->query('DELETE FROM product');
            $connection->query('SET FOREIGN_KEY_CHECKS=1');
            $connection->commit();
        } catch (\Exception $exception) {
            $connection->rollback();
        }
    }
}

應該

$task =  $this->downloadTaskRepository->find($getSellerList->getDownloadTaskId());

$this->task =  $this->downloadTaskRepository->find($getSellerList->getDownloadTaskId());

?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM