How to prevent the cron job execution, if it is already running

前端 未结 9 2373
花落未央
花落未央 2020-11-29 01:57

I have one php script, and I am executing this script via cron every 10 minutes on CentOS.

The problem is that if the cron job will take more than 10 minutes, then a

相关标签:
9条回答
  • 2020-11-29 02:15

    Maybe it is better to not write code if you can configure it:

    https://serverfault.com/questions/82857/prevent-duplicate-cron-jobs-running

    0 讨论(0)
  • 2020-11-29 02:17

    Another alternative:

    <?php
    
    /**
    * Lock manager to ensure our cron doesn't run twice at the same time.
    *
    * Inspired by the lock mechanism in Mage_Index_Model_Process
    *
    * Usage:
    * 
    * $lock = Mage::getModel('stcore/cron_lock');
    *
    * if (!$lock->isLocked()) {
    *      $lock->lock();
    *      // Do your stuff
    *      $lock->unlock();
    * }
    */
    class ST_Core_Model_Cron_Lock extends Varien_Object
    {
        /**
         * Process lock properties
         */
        protected $_isLocked = null;
        protected $_lockFile = null;
    
        /**
         * Get lock file resource
         *
         * @return resource
         */
        protected function _getLockFile()
        {
            if ($this->_lockFile === null) {
                $varDir = Mage::getConfig()->getVarDir('locks');
                $file = $varDir . DS . 'stcore_cron.lock';
                if (is_file($file)) {
                    $this->_lockFile = fopen($file, 'w');
                } else {
                    $this->_lockFile = fopen($file, 'x');
                }
                fwrite($this->_lockFile, date('r'));
            }
            return $this->_lockFile;
        }
    
        /**
         * Lock process without blocking.
         * This method allow protect multiple process runing and fast lock validation.
         *
         * @return Mage_Index_Model_Process
         */
        public function lock()
        {
            $this->_isLocked = true;
            flock($this->_getLockFile(), LOCK_EX | LOCK_NB);
            return $this;
        }
    
        /**
         * Lock and block process.
         * If new instance of the process will try validate locking state
         * script will wait until process will be unlocked
         *
         * @return Mage_Index_Model_Process
         */
        public function lockAndBlock()
        {
            $this->_isLocked = true;
            flock($this->_getLockFile(), LOCK_EX);
            return $this;
        }
    
        /**
         * Unlock process
         *
         * @return Mage_Index_Model_Process
         */
        public function unlock()
        {
            $this->_isLocked = false;
            flock($this->_getLockFile(), LOCK_UN);
            return $this;
        }
    
        /**
         * Check if process is locked
         *
         * @return bool
         */
        public function isLocked()
        {
            if ($this->_isLocked !== null) {
                return $this->_isLocked;
            } else {
                $fp = $this->_getLockFile();
                if (flock($fp, LOCK_EX | LOCK_NB)) {
                    flock($fp, LOCK_UN);
                    return false;
                }
                return true;
            }
        }
    
        /**
         * Close file resource if it was opened
         */
        public function __destruct()
        {
            if ($this->_lockFile) {
                fclose($this->_lockFile);
            }
        }
    }
    

    Source: https://gist.github.com/wcurtis/9539178

    0 讨论(0)
  • 2020-11-29 02:20

    flock will not work in php 5.3.3 as The automatic unlocking when the file's resource handle is closed was removed. Unlocking now always has to be done manually.

    0 讨论(0)
提交回复
热议问题