dqingn8836 2013-06-21 12:30
浏览 62
已采纳

用于FIFO队列的Zend Framework Cron

I am trying to run a cron job in my application my set up is like this: My zend application Version 1.12

inside my public/index.php

function Mylib_init_settings($settings, $environment)
{
    if (getenv('LOCAL_ENV') && file_exists($serverConfigFile = __DIR__ . '/../application/configs/' . getenv('LOCAL_ENV') . '.ini')) {
        $settings->addOverrideFile($serverConfigFile);
    }
}

define('MYLIB_APPLICATION_ENV', 'production');
require __DIR__ . '/../library/Mylib/Application/start.php';

Inside Start.php

<?php
use Mylib\Config;
use Mylib\Config\Loader\SecondGeneration;
function mylib_trigger_hook($hook, $params = array())
{
    $func = 'mylib_init_' . strtolower(trim($hook));
    if (function_exists($func)) {
        call_user_func_array($func, $params);
    }
}
// ---------------------------------------------------------------------------------------------------------------------
// setup application constants
if (getenv('SELENIUM')) {
    defined('MYLIB_APPLICATION_ENV')
        ?: define('MYLIB_APPLICATION_ENV', 'testing');
}
// should the application be bootstrapped?
defined('MYLIB_APPLICATION_BOOTSTRAP')
    ?: define('MYLIB_APPLICATION_BOOTSTRAP', true);
// should the application run?
defined('MYLIB_APPLICATION_CREATE')
    ?: define('MYLIB_APPLICATION_CREATE', true);
// should the application run?
defined('MYLIB_APPLICATION_RUN')
    ?: define('MYLIB_APPLICATION_RUN', true);
// maximum execution time
defined('MYLIB_APPLICATION_TIME_LIMIT')
    ?: define('MYLIB_APPLICATION_TIME_LIMIT', 0);
// path to application rooth
defined('MYLIB_APPLICATION_PATH_ROOT')
    ?: define('MYLIB_APPLICATION_PATH_ROOT', realpath(__DIR__ . '/../../../'));
// path to library
defined('MYLIB_APPLICATION_PATH_LIBRARY')
    ?: define('MYLIB_APPLICATION_PATH_LIBRARY', realpath(__DIR__ . '/../../'));
mylib_trigger_hook('constants');
// ---------------------------------------------------------------------------------------------------------------------
// limits the maximum execution time
set_time_limit(MYLIB_APPLICATION_TIME_LIMIT);
// ---------------------------------------------------------------------------------------------------------------------
// determine which configuration section, and overrides to load
$configSection  = defined('MYLIB_APPLICATION_ENV') ?MYLIB_APPLICATION_ENV : null;
$configOverride = null;
$environmentFilename = MYLIB_APPLICATION_PATH_ROOT . '/environment';
if (file_exists($environmentFilename)) {
    $ini = parse_ini_file($environmentFilename);
    if ($ini === false) {
        throw new \RuntimeException('Failed to parse enviroment file: ' . $environmentFilename);
    }
    if (!defined('MYLIB_APPLICATION_ENV')) {
        // should have at least a config.section variable
        if (!isset($ini['config.section'])) {
            throw new \RuntimeException('\'config.section\' setting is missing in environment file');
        }

        $configSection = $ini['config.section'];
    }
    if (isset($ini['config.override'])) {
        $configOverrideFilename = MYLIB_APPLICATION_PATH_ROOT . '/application/configs/' . $ini['config.override'] . '.ini';
        if (!is_readable($configOverrideFilename)) {
            throw new \RuntimeException(
                sprintf('You have provided a config override file (%s), but it is not readable', $configOverrideFilename)
            );
        } else {
            $configOverride = $configOverrideFilename;
        }
    }
}
defined('MYLIB_APPLICATION_ENV')
    ?: define('MYLIB_APPLICATION_ENV', $configSection);
static $allowedEnvironmnets = array(
    'production',
    'staging',
    'testing',
    'development',
);
if (!in_array(MYLIB_APPLICATION_ENV, $allowedEnvironmnets)) {
    throw new \RuntimeException(
        sprintf('Invalid environment %s provided. Must be either of: %s', MYLIB_APPLICATION_ENV, implode(', ', $allowedEnvironmnets))
    );
}
macq_trigger_hook('environment', array(MYLIB_APPLICATION_ENV));
// ---------------------------------------------------------------------------------------------------------------------
// set the include path
set_include_path(MYLIB_APPLICATION_PATH_LIBRARY . PATH_SEPARATOR . get_include_path());
mylib_trigger_hook('includepath', array(get_include_path()));
// ---------------------------------------------------------------------------------------------------------------------
// enable PSR-0 autoloading
require_once MYLIB_APPLICATION_PATH_LIBRARY . '/Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance()
    ->setFallbackAutoloader(true);
// ---------------------------------------------------------------------------------------------------------------------
// load configuration settings, and if an override is specified, merge it
$settings = new SecondGeneration(
    MYLIB_APPLICATION_PATH_ROOT,
    MYLIB_APPLICATION_ENV,
    MYLIB_APPLICATION_PATH_LIBRARY . '/MyLib/Application/configuration.ini'
);
if ($configOverride) {
    $settings->addOverrideFile($configOverride);
}
// set up config file caching, this is a seperate cache then any application caches created!
if (isset($ini['config.cache.enabled']) && $ini['config.cache.enabled']) {
    if (isset($ini['config.cache.dir']) && is_writable($ini['config.cache.dir'])) {
        $configCache = new Zend_Cache_Core(array('automatic_serialization'=>true));
        $backend = new Zend_Cache_Backend_File(array(
            'cache_dir' => $ini['config.cache.dir'],
        ));
        $configCache->setBackend($backend);
        $settings->setCache($configCache);
        unset($configCache, $backend);
    } else {
        throw new \RuntimeException(
            sprintf('Configuration cache is enabled, but no correct cache dir is specified, or the specified directory is not writable')
        );
    }
}
// load configuration settings
Config::load($settings);
mylib_trigger_hook('settings', array($settings, MYLIB_APPLICATION_ENV));
// ---------------------------------------------------------------------------------------------------------------------
// create application and bootstrap
if (MYLIB_APPLICATION_CREATE) {

    $application = new Zend_Application(Config::environment(), Config::config());

    macq_trigger_hook('application', array($application));

    if (MYLIB_APPLICATION_BOOTSTRAP) {
        $application->bootstrap();

        macq_trigger_hook('bootstrap', array($application));
    }
    // ---------------------------------------------------------------------------------------------------------------------
    // run application?
    if (MYLIB_APPLICATION_RUN) {
        $application->run();
        macq_trigger_hook('run', array($application));
    }

}

What I did is :

I followed the following link: http://www.magentozend.com/blog/2012/02/03/setting-up-cronjobs-for-zend-framework-envoirment/

what I did is create a "cron" folder at the level in which my application folders are present.

inside the folder created init.php file inside that I added my index.php code and start.php code.

and my controller file is as like this:

application/modules/myproject/controller/cronjop.php

inside the cron job file I just called init.php by

require_once('/../../../../cron/init.php');

but the cron is not working can some one help me..

thanks in advance..

  • 写回答

1条回答 默认 最新

  • dqf42223 2013-06-21 13:00
    关注

    I see you miss the point of using Cron and Zend as well. Zend site is just normal site so you can use for example lynx browser to run the site.

     */10 * * * * lynx -dump http://www.myzendsite.com/mycontroller/mycronaction
    

    just create My Controller add mycron Action and put in this method what you want cron to do. Lynx will open it as normal user would do. Cron will run lynx after some time.

    The line */10 means every 10 minutes. You can fit it to your needs.

    There are other ways to run php script for example via php parser or curl.

    Check this tutorial

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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