0
votes

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
why don't you just call nzend action from cron?Robert
I am not sure how to do it.. I am new to zend..DonOfDen
it has nothing to do with zend just open is as normal PHP site. Show your crontab.Robert
If you echo something after the $application->bootstrap(); line, do you get output then?Tim Fountain
@TomPHP have you tested my solution?Robert

1 Answers

1
votes

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