我正在使用Symfony框架3和LeezyPheanstalkBundle。这是我第一次使用Pheanstalk。我想创建一个启动创建新发电厂的活动。我创建了订阅者,工作经理,工作班。代码有效。我可以看到数据被发送到管,而不是像我在订户中设置的那样删除作业。我不明白为什么之后没有任何事情发生。在这行代码中,我告诉我要设置的命令:
$这 - > jobManager-> createJob(" power_plant:创建",$有效载荷);
如果我尝试使用缓存测试代码:clear或doctrine:generate:entity并且它什么都不做。所以我的问题是我在哪里声明我想要运行什么命令,如果我也必须创建命令,或者我可以使用服务或类巫婆我想在我的事件被激活时运行?我已经创建了具有参数电厂名称的自定义类,并为该新电厂创建了一些额外的表。我想用pheanstalk这样做,但这是我的问题,我不知道为什么这些命令没有像我预期的那样运行。而且这方面的文件很差。
Evet Subscriber:
<?php
namespace AppBundle\EventListener;
use AppBundle\Event\PowerPlantEvent;
use AppBundle\Manager\ManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use AppBundle\Event\Events;
class PowerPlantSubscriber implements EventSubscriberInterface
{
protected $jobManager;
public function __construct(ManagerInterface $jobManager)
{
$this->jobManager = $jobManager;
}
public static function getSubscribedEvents()
{
return [
Events::POWERPLANT_CREATE => ['onPowerPlantCreate', 0],
Events::POWERPLANT_DELETE => ['onPowerPlantDelete', 0],
];
}
public function onPowerPlantCreate(PowerPlantEvent $event)
{
$power_plant = $event->getPowerPlant();
$payload = [
"name" => $power_plant->getName(),
"tech_name" => $power_plant->getTechName(),
"system" => $power_plant->getSystem()
];
//$job = $this->jobManager->createJob("power_plant:create",$payload);
$job = $this->jobManager->createJob('doctrine:generate:entity --entity=AppBundle:Test --format=annotation --fields="title:string(255) body:text" --no-interaction',$payload);
$data = $this->jobManager->getData($job);
$this->jobManager->deleteJob($job);
}
public function onPowerPlantDelete(PowerPlantEvent $event)
{
$power_plant = $event->getPowerPlant();
$payload = [
"name" => $power_plant->getName(),
"tech_name" => $power_plant->getTechName(),
"system" => $power_plant->getSystem()
];
$job = $this->jobManager->createJob("power_plant:delete",$payload);
$data = $this->jobManager->getData($job);
$this->jobManager->deleteJob($job);
}
}
工作经理:
namespace AppBundle\Manager;
use Leezy\PheanstalkBundle\Proxy\PheanstalkProxy;
use AppBundle\Queue\Job;
/**
* Job manager class
*/
abstract class JobManager implements ManagerInterface
{
/**
* @var $pheanstalk
*/
protected $pheanstalk;
/**
* @var $name
*/
protected $name;
/**
*@var job;
*/
protected $job;
public function __construct(PheanstalkProxy $pheanstalk, Job $job)
{
$this->pheanstalk = $pheanstalk;
$this->job = $job;
}
/**
* Create job using pheanstalk
*
* @var $command
* @var $payload
*/
public function createJob($command, $payload)
{
$jobData = [
"command" => $command,
"meta" => $payload
];
$this->pheanstalk->useTube($this->tubeName);
return $this->job->enqueue($command, $jobData, $this->tubeName);
}
/**
* Get job data
*
* @var $job
*/
public function getData($job)
{
return $job->getData();
}
/**
* Delete job
*
* @var $job
*/
public function deleteJob($job)
{
return $this->job->deleteJob($job);
}
/**
* Set tube name
*
* @var $name
*/
public function setTubeName($name)
{
$this->tubeName = $name;
}
}
工作类:
namespace AppBundle\Queue;
use Leezy\PheanstalkBundle\Proxy\PheanstalkProxy;
class Job {
/**
* @var $pheanstalk
*/
protected $pheanstalk;
public function __construct(PheanstalkProxy $pheanstalk) {
$this->pheanstalk = $pheanstalk;
}
/**
* Preform job
*
* @var $job
* @var array $args
* @var string $tubeName
* @var integer $priority
* @var integer $delay
* @var integer $ttr
*/
public function enqueue($job, array $args, $tubeName, $priority = 65536, $delay = 0, $ttr = 120)
{
if (!preg_match('/[a-z0-9\.]+/i', $job)) {
throw new InvalidArgumentException("Invalid job name");
}
$this->pheanstalk->put(json_encode($args), $priority, $delay, $ttr);
return $this->pheanstalk->watch($tubeName)->ignore('default')->reserve();
}
/**
* Delete job
*
* @var $job
*/
public function deleteJob($job)
{
$this->pheanstalk->delete($job);
}
}