如何在Zend Framework 2中运行定时任务(Cron Job)

13

我有一个使用Zend Framework 2构建的应用程序。我想为更新我的产品设置cron job。我知道这样的脚本应该从公共文件夹外运行,但不幸的是我的cron中的脚本需要使用框架文件。
我该怎么做?
我想到的唯一方法是从公共文件夹外运行脚本,然后添加一些哈希或密码,并重定向到

www.domain.com/cron/test

所以我将拥有全部的框架功能。
它会安全吗?也许有其他方式?

2个回答

25
我强烈建议使用CLI来满足此需求。
  1. Create a ConsoleController with an updateAction() inside the application module.
  2. Add a console route to your application module's module.config.php:

    array(
        'router' => array(
            'routes' => array(
            ...
            )
        ),
    
    'console' => array(
        'router' => array(
            'routes' => array(
                'cronroute' => array(
                    'options' => array(
                        'route'    => 'updateproducts',
                        'defaults' => array(
                            'controller' => 'Application\Controller\Console',
                            'action' => 'update'
                        )
                    )
                )
            )
        )
    )
    );
    
  3. Now open the terminal and

    $ cd /path/to/your/project
    $ php public/index.php updateproducts
    

就这些。希望能帮到你。


谢谢,我会尝试一下。只有一个问题,你的方法安全吗?它不会被HTTP访问到吧? - user1409508
1
是的,它不会被HTTP访问。根据文档:“当应用程序在控制台(终端)窗口内运行时,仅处理控制台路由。它们对Web(http)请求没有影响,并将被忽略。可以定义仅HTTP路由(仅Web应用程序)或仅控制台路由(这意味着我们想要一个仅限控制台的应用程序,它将拒绝在浏览器中运行)。请查看:http://framework.zend.com/manual/2.2/en/modules/zend.console.routes.html。” - edigu
谢谢,我想确认一下。我会在检查后接受你的答案。可能明天才能完成。感谢你的帮助! - user1409508

2

我在Collabnet(现已关闭)找到了解决方案。

我将解决方案复制到这里,因为ColabEdit有时会删除帖子:

<?php
/*
Cron directory setup:

Cron
    config
        module.config.php
    src
        Cron
            Controller
                IndexController.php
    autoload_classmap.php
    Module.php                

NOTES: Remember to include the Cron module in the main config file (trunk/config/application.config.php)

Once you have the route in place, write your cron and call it from your webhost cron manager.

*/

// Cron/config/module.config.php
return array(
    // Placeholder for console routes
    'controllers' => array(
        'invokables' => array(
            'Cron\Controller\IndexController' => 'Cron\Controller\IndexController'
        ),
    ),
    'console' => array(
        'router' => array(
            'routes' => array(
                //CRON RESULTS SCRAPER
                'my-first-route' => array(
                    'type'    => 'simple',       // <- simple route is created by default, we can skip that
                    'options' => array(
                    'route'    => 'hello',
                    'defaults' => array(
                        'controller' => 'Cron\Controller\IndexController',
                        'action'     => 'index'
                        )
                    )
                )

            ),
        ),
    ),


);

<?php
// Cron/src/Cron/Controller/IndexController.php
namespace Cron\Controller;

use Zend\Mvc\Controller\AbstractActionController;

class IndexController extends AbstractActionControlle
{
    public function indexAction()
    {
        echo "hello";
        echo "\r\n";
    }
}

From the console navigate to trunk (or public_html) (the directory before public) and run:

path/to/trunk>php public/index.php hello

hello
path/to/trunk>

以上链接已失效! - Marcel Djaman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接