【发布时间】:2016-03-05 03:11:42
【问题描述】:
我有一个具有以下配置的 ZF2 项目。它使用 Doctrine ORM 和 SlmQueue。由于 SlmQueue 不支持我们的命名约定和 oracle 数据库,我们自定义了 SlmQueueDoctrine。
我怀疑我的作业没有使用 ClearObjectManagerStrategy,并且在执行单个作业之前没有清除 ObjectManager。
它不反映队列启动后的数据库修改。但如果我杀死队列守护进程并重新开始,它会选择新值。
如何在执行单个作业之前实现 ClearObjectManagerStrategy 和清除 ObjectManager?
我已经尝试了很多没有运气的方法。
composer.json
{
"repositories": [
{
"url": "https://github.com/pradeep-sanjaya/doctrine-extensions.git",
"type": "git"
}
],
"require": {
"php": ">=5.3.3",
"zendframework/zendframework": "2.3.3",
"doctrine/doctrine-orm-module": "0.7.*",
"pradeep-sanjaya/doctrine-extensions": "dev-master",
"spoonx/sxmail": "1.4.*",
"slm/locale": "dev-master",
"imagine/Imagine": "0.6.*",
"tecnick.com/tcpdf": "dev-master",
"slm/queue": "0.4.*",
"slm/queue-doctrine": "0.4.*"
}
}
config/autoload/slm_queue.local.php
<?php
return array(
'slm_queue' => array(
'queue_manager' => array(
'factories' => array(
'doctrineQueue' => 'SlmQueueDoctrine\Factory\DoctrineQueueFactory'
),
),
'job_manager' => array(
'factories' => array(
'Report\Job\Rank' => 'Report\Job\RankFactory',
),
'shared' => array(
'Report\Job\Rank' => false
),
),
'queues' => array(
'doctrineQueue' => array(
'table_name' => 'IOQUEUE'
)
)
)
);
?>
module/Report/src/Report/Job/Rank.php
<?php
namespace Report\Job;
use DoctrineModule\Persistence\ObjectManagerAwareInterface;
use DoctrineModule\Persistence\ProvidesObjectManager as ProvidesObjectManagerTrait;
use SlmQueue\Job\AbstractJob;
use Application\Entity\Report;
use Application\Log\LoggerAwareInterface;
use Application\Log\LoggerAwareTrait;
use Application\Service\ReportService;
class Rank extends AbstractJob implements ObjectManagerAwareInterface, LoggerAwareInterface
{
use LoggerAwareTrait;
use ProvidesObjectManagerTrait;
/**
* @var ReportService
*/
protected $reportService;
/**
* @var array
*/
protected $reportId = array();
public function setReportService(ReportService $reportService)
{
$this->reportService = $reportService;
}
/**
* Execute the job
*
* @return void
*/
public function execute()
{
//clear object manager does not work
//$om = $this->getObjectManager();
//$om->clear();
$content = $this->getContent();
$this->setReportId($content['reportId']);
if (!empty($this->reportId)) {
try {
if (is_array($this->reportId)) {
foreach ($this->reportId as $reportId) {
$this->updateRank($reportId);
}
unset($reportId);
} else {
$this->updateRank($this->reportId);
}
} catch (\Exception $exception) {
echo "Exception message is {$exception->getMessage()} \n";
}
}
}
private function updateRank($reportId)
{
/* @var $report Report */
$report = $this->reportService->getReport($reportId);
$this->logInfo(print_r($report, true)); // this always return older db values, the values before it start queue deamon
if (!$report instanceof Report) {
return;
}
if (empty($rankData)) {
return;
}
//more codes, application related logics
$this->reportService->updateReportEntity($report);
}
private function setReportId($reportId)
{
if (is_numeric($reportId)) {
$this->reportId = array($reportId);
} elseif (is_array($reportId)) {
$this->reportId = $reportId;
} else {
throw new \Exception('Expects reportId as int or array');
}
}
}
【问题讨论】:
-
“清除对象管理器不起作用”是什么意思?你有错误吗?错误是什么?
ObjectManager没有设置吗?你在哪里注入你的ObjectManager?我没有看到构造函数... -
实现ObjectManagerAwareInterface并使用ProvidesObjectManager,可以从工厂注入ObjectManager
-
带有“清除对象管理器不起作用”的注释行意味着它不会刷新对象管理器和当前事务
标签: php oracle zend-framework2 doctrine queue