doujiao4705 2016-06-05 16:32
浏览 29

Pheanstalk没有履行职责

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);
}

}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
    • ¥15 可否在不同线程中调用封装数据库操作的类
    • ¥20 使用Photon PUN2解决游戏得分同步的问题
    • ¥15 微带串馈天线阵列每个阵元宽度计算
    • ¥15 keil的map文件中Image component sizes各项意思
    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM