如何在Zend Framework中使用守护程序访问模型 [英] How do I Access my Models using a Daemon in Zend Framework

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

问题描述

我正在一个使用 Phirehose 的项目中,以收集和使用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集成。我需要能够直接从命令行调用守护程序文件,或者使用 Upstart ,但是这样做不会加载Zend应用程序,这意味着我无权访问我的模型。

My issue is how to integrate it properly with Zend Framework. I need to be able to call the daemon file directly to start it from the command line or using a tool such as Upstart, but in doing so the Zend application doesn't load which means I don't have access to my models.

我可以创建一个控制器来启动它,但是我不想添加一个安全问题,即有人可以从Web界面控制守护程序。我也可以编写PDO以手动连接到数据库,但是出于扩展原因,我试图避免使用它。我希望所有数据库连接数据都驻留在application.ini中。

I could create a controller to start it, but I don't want to add the security issue of somebody being able to control the daemon from a web interface. I could also write the PDO to manually connect to the database, but I'm trying to avoid it for scaling reasons. I'd prefer all database connection data reside in application.ini.

在守护程序类中是否可以初始化Zend Framework应用程序,因此我可以使用楷模?

Is there a way, within my daemon class, to initialize my Zend Framework application so I can use the models?

推荐答案

此示例演示了如何使用Zend_Queue执行后台任务。
在这个特定示例中,我使用Zend_Queue和cronjob在后台生成发票,我的Zend_Queue已初始化并在引导程序中注册。

This example demonstrates how I perform background tasks using Zend_Queue. In this particular example I'm generating invoices in background using Zend_Queue and cronjob, my Zend_Queue is initialised and registered in bootstrap.

创建作业,My_Job的来源是此处

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中使用守护程序访问模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆