I am using Symfony framework 3 with LeezyPheanstalkBundle. This is my first time using Pheanstalk. I would like to create event that fire up on creating new power plant. I created subscriber, job manager, job class. The code works. I can see the data is send to tube, and than job delete itself like i set to do in subscriber. I dont undestand why nothing happens after that. in this line of code i tell what command i want to set:
$this->jobManager->createJob("power_plant:create",$payload);
if i try testing the code with cache:clear or doctrine:generate:entity and it do nothing. So my question is where I declare what command I would like to run and if I must create command too or I can use service or class witch I would like to run when my event is fired up? I already created my custom class that have parameter power plant name and than create some extra tables for that new power plant. I would like to do this using pheanstalk, but here is my problem, that I dont know why this commands do not runs like I would expected. And also documentation for this is very poor.
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);
}
}
Job manager:
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;
}
}
Job class:
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);
}
}