<commit_before><?php
/**
 * Main process for Cron tasks.
 *
 * @category  ZendExt
 * @package   ZendExt_Cron
 * @copyright 2010 Monits
 * @license   Copyright (C) 2010. All rights reserved.
 * @version   Release: 1.0.0
 * @link      http://www.zendext.com/
 * @since     1.0.0
 */

/**
 * Main process for Cron tasks.
 *
 * @category  ZendExt
 * @package   ZendExt_Cron
 * @author    jpcivile <<EMAIL>>
 * @copyright 2010 Monits
 * @license   Copyright 2010. All rights reserved.
 * @version   Release: 1.0.0
 * @link      http://www.zendext.com/
 * @since     1.0.0
 */
abstract class ZendExt_Cron_Process
{
    private static $_defaultOptions = array(
                                          'configDir' => 'config/strategies',
                                          'outputFile' => 'php://stdout',
                                          'pid' => array(
                                              'path' => 'pid/'
                                          ),
                                          'strategy' => array(
                                              'stub' => 'bar'
                                          )
                                      );

    /**
     * The config being used.
     *
     * @var Zend_Config
     */
    protected $_config;

    /**
     * The log being used.
     *
     * @var Zend_Log
     */
    protected $_logger;

    /**
     * @var ZendExt_Cron_Persistance
     */
    protected $_persistance;

    private $_pidFile = false;

    protected $_allowProgress = false;

    protected $_hasRun = false;

    /**
     * @var ZendExt_Cron_Manager
     */
    protected $_manager;

    /**
     * Creates a new offline process.
     *
     * @param ZendExt_Cron_Manager $manager The manager instance.
     * @param Zend_Config          $config  The config data to use.
     * @param array                $extra   Override config.
     *
     * @throws ZendExt_Cron_ErrorException
     */
    public final function __construct(ZendExt_Cron_Manager $manager,
        Zend_Config $config, array $extra = array())
    {
        $this->_manager = $manager;
        $this->_loadConfig($config, $extra);
        $this->_setupLogger();

        $this->_lock();

        $this->_logger->info('Initializing process...');
        $this->_bootstrap();

        if ($this->_config->data->dir) {

            $dataDir = $this->_config->data->dir.'/';
            $this->_persistance = new ZendExt_Cron_Persistance(
                get_class($this), $dataDir
            );
        }

        try {

            $this->_init();
        } catch (Exception $e) {

            $this->_logger->crit('An error ocurred during init!');
            $this->_logger->crit($e->__toString());

            $this->_unlock();
            throw new ZendExt_Cron_ErrorException(
                'An error ocurred during init. Execution aborted.'
            );
        }
    }

    /**
     * Get the path to the pid file.
     *
     * If this returns a falsy value, locking will be disabled.
     *
     * @return string
     */
    protected function _getPidFileName()
    {
        $configPath = $this->_config->pid->path;
        return $configPath.'/'.$this->_config->pid->file;
    }

    /**
     * Load and set the config for the process.
     *
     * @param Zend_Config $config The config data to use.
     * @param array       $extra  Extra config to override defaults.
     *
     * @return void
     */
    private function _loadConfig($config, array $extra = array())
    {
        $this->_config = new Zend_Config(self::$_defaultOptions, true);
        $this->_config->merge($config);

        $configFile = $this->_config->configDir.'/'
            .$this->_configFileName;

        $this->_config->merge(new Zend_Config_Xml($configFile, 'process'));
        $this->_config->merge(new Zend_Config($extra));
    }


    /**
     * Lock the process so that no other instance can run.
     *
     * @return void
     */
    private function _lock()
    {
        $this->_pidFile = $this->_getPidFileName();

        $this->_checkLock();
        if ($this->_pidFile) {

            $pidPath = dirname($this->_pidFile);
            if (!file_exists($pidPath)) {

                mkdir($pidPath, 0744, true);
            }

            file_put_contents($this->_pidFile, getmypid());
        }
    }

    /**
     * Bootstrap whatever the strategy needs.
     *
     * @return Zend_Application_Bootstrap_BootstrapAbstract|NULL
     *
     * @throws ZendExt_Cron_ErrorException
     */
    private function _bootstrap()
    {
        $this->_logger->info('Bootstraping...');
        $appConfig = $this->_config->app;
        if ($appConfig) {

            try {
                $app = new Zend_Application(
                    'cron',
                    $appConfig
                );

                if ($appConfig->resources) {

                    $resources = array_keys($appConfig->resources->toArray());
                } else {

                    $resources = null;
                }
                $app->bootstrap($resources);
            } catch (Exception $e) {

                $this->_logger->crit($e->__toString());
                throw new ZendExt_Cron_ErrorException(<commit_msg>When initializing the bootstrap, the bootsrap should actually exist and be accesible.<commit_after><?php
/**
 * Main process for Cron tasks.
 *
 * @category  ZendExt
 * @package   ZendExt_Cron
 * @copyright 2010 Monits
 * @license   Copyright (C) 2010. All rights reserved.
 * @version   Release: 1.0.0
 * @link      http://www.zendext.com/
 * @since     1.0.0
 */

/**
 * Main process for Cron tasks.
 *
 * @category  ZendExt
 * @package   ZendExt_Cron
 * @author    jpcivile <<EMAIL>>
 * @copyright 2010 Monits
 * @license   Copyright 2010. All rights reserved.
 * @version   Release: 1.0.0
 * @link      http://www.zendext.com/
 * @since     1.0.0
 */
abstract class ZendExt_Cron_Process
{
    private static $_defaultOptions = array(
                                          'configDir' => 'config/strategies',
                                          'outputFile' => 'php://stdout',
                                          'pid' => array(
                                              'path' => 'pid/'
                                          ),
                                          'strategy' => array(
                                              'stub' => 'bar'
                                          )
                                      );

    /**
     * The config being used.
     *
     * @var Zend_Config
     */
    protected $_config;

    /**
     * The log being used.
     *
     * @var Zend_Log
     */
    protected $_logger;

    /**
     * @var ZendExt_Cron_Persistance
     */
    protected $_persistance;

    /**
     * @var Zend_Application_Bootstrap_Bootstrap
     */
    protected $_bootstrap;

    private $_pidFile = false;

    protected $_allowProgress = false;

    protected $_hasRun = false;

    /**
     * @var ZendExt_Cron_Manager
     */
    protected $_manager;

    /**
     * Creates a new offline process.
     *
     * @param ZendExt_Cron_Manager $manager The manager instance.
     * @param Zend_Config          $config  The config data to use.
     * @param array                $extra   Override config.
     *
     * @throws ZendExt_Cron_ErrorException
     */
    public final function __construct(ZendExt_Cron_Manager $manager,
        Zend_Config $config, array $extra = array())
    {
        $this->_manager = $manager;
        $this->_loadConfig($config, $extra);
        $this->_setupLogger();

        $this->_lock();

        $this->_logger->info('Initializing process...');
        $this->_bootstrap();

        if ($this->_config->data->dir) {

            $dataDir = $this->_config->data->dir.'/';
            $this->_persistance = new ZendExt_Cron_Persistance(
                get_class($this), $dataDir
            );
        }

        try {

            $this->_init();
        } catch (Exception $e) {

            $this->_logger->crit('An error ocurred during init!');
            $this->_logger->crit($e->__toString());

            $this->_unlock();
            throw new ZendExt_Cron_ErrorException(
                'An error ocurred during init. Execution aborted.'
            );
        }
    }

    /**
     * Get the path to the pid file.
     *
     * If this returns a falsy value, locking will be disabled.
     *
     * @return string
     */
    protected function _getPidFileName()
    {
        $configPath = $this->_config->pid->path;
        return $configPath.'/'.$this->_config->pid->file;
    }

    /**
     * Load and set the config for the process.
     *
     * @param Zend_Config $config The config data to use.
     * @param array       $extra  Extra config to override defaults.
     *
     * @return void
     */
    private function _loadConfig($config, array $extra = array())
    {
        $this->_config = new Zend_Config(self::$_defaultOptions, true);
        $this->_config->merge($config);

        $configFile = $this->_config->configDir.'/'
            .$this->_configFileName;

        $this->_config->merge(new Zend_Config_Xml($configFile, 'process'));
        $this->_config->merge(new Zend_Config($extra));
    }


    /**
     * Lock the process so that no other instance can run.
     *
     * @return void
     */
    private function _lock()
    {
        $this->_pidFile = $this->_getPidFileName();

        $this->_checkLock();
        if ($this->_pidFile) {

            $pidPath = dirname($this->_pidFile);
            if (!file_exists($pidPath)) {

                mkdir($pidPath, 0744, true);
            }

            file_put_contents($this->_pidFile, getmypid());
        }
    }

    /**
     * Bootstrap whatever the strategy needs.
     *
     * @return void
     *
     * @throws ZendExt_Cron_ErrorException
     */
    private function _bootstrap()
    {
        $this->_logger->info('Bootstraping...');
        $appConfig = $this->_config->app;
        if ($appConfig) {

            try {
                $app = new Zend_Application(
                    'cron',
                    $appConfig
                );

                if ($appConfig->resources) {

                    $resources = array_keys($appConfig->resources->toArray());
                } else {

                    $resources = null;
                }
                $app->bootstrap($resources);
            } catch (Exception $e) {

                $this->_logger->crit($e->__toString());
                throw new ZendExt_Cron_ErrorException(
{'fraction_non_alphanumeric': 0.12433962264150944, 'fraction_numerical': 0.0060377358490566035, 'mean_word_length': 1.5162591977213387, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 127, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '509557', 'n_tokens_mistral': 3004, 'n_tokens_neox': 2812, 'n_words': 656}