本文介绍了如何在Zend Framework中使用守护程序访问模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个使用的项目中,以收集和使用Twitter Streaming API。 Phirehose库设计为可从命令行运行,最好是作为守护程序或cron作业运行。

I am working on a project that is using Phirehose to collect and consume the Twitter Streaming API. The Phirehose library is designed to be run from the command line, preferably as a daemon or cron job.

我创建了一个守护程序并将其放置在库文件夹中。 Bootstrap.php已更新,可以自动加载自定义库。因此,应用程序本身可以看到我的守护进程。

I created a daemon and placed it in the library folder. Bootstrap.php has been updated to autoload the custom library. So, the application itself has no problem seeing my daemon.

我的问题是如何将其正确地与Zend Framework集成。我需要能够直接从命令行调用守护程序文件,或者使用:

Creating a job, My_Job source is here:

class My_Job_SendInvoice extends My_Job
{
    protected $_invoiceId = null;

    public function __construct(Zend_Queue $queue, array $options = null)
    {
        if (is_array($options)) {
            $this->setOptions($options);
        }

        parent::__construct($queue);
    }

    public function job()
    {
        $filename = InvoiceTable::getInstance()
            ->generateInvoice($this->_invoiceId);

        return is_file($filename);
    }
}

在服务内部或模型:

$backgroundJob = new My_Job_SendInvoice(Zend_Registry::get('queue'), array(
    'invoiceId' => $invoiceId
));
$backgroundJob->execute();

创建背景脚本:

defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/..'));

// temp, environment should be specified prior execution
define('APPLICATION_ENV', 'development');

set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));


require_once 'Zend/Application.php';

$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);

$application->bootstrap();

/* @var $queue Zend_Queue */
$queue    = Zend_Registry::get('queue');
$messages = $queue->receive(5);

foreach ($messages as $i => $message) {
    /* @var $job My_Job */
    $job = unserialize($message->body);
    if ($job->job()) {
        $queue->deleteMessage($message);
    }
}

这篇关于如何在Zend Framework中使用守护程序访问模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 13:17