QA fixes
[civicrm-core.git] / CRM / Core / Lock.php
index 134ba1ab0bb1a6b1444178c88f031a62d65fe291..87c267b01d92d1d21ecb474d2177fdd2be376d32 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.4                                                |
+ | CiviCRM version 4.5                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * $Id$
  *
  */
@@ -44,19 +44,18 @@ class CRM_Core_Lock {
   /**
    * Initialize the constants used during lock acquire / release
    *
-   * @param string  $name name of the lock. Please prefix with component / functionality
+   * @param string $name name of the lock. Please prefix with component / functionality
    *                      e.g. civimail.cronjob.JOB_ID
-   * @param int     $timeout the number of seconds to wait to get the lock. 1 if not set
+   * @param int $timeout the number of seconds to wait to get the lock. 1 if not set
    * @param boolean $serverWideLock should this lock be applicable across your entire mysql server
-   *                                this is useful if you have mutliple sites running on the same
+   *                                this is useful if you have multiple sites running on the same
    *                                mysql server and you want to limit the number of parallel cron
    *                                jobs - CRM-91XX
    *
-   * @return object the lock object
-   *
+   * @return \CRM_Core_Lock the lock object
    */
- function __construct($name, $timeout = NULL, $serverWideLock = FALSE) {
-    $config   = CRM_Core_Config::singleton();
 function __construct($name, $timeout = NULL, $serverWideLock = FALSE) {
+    $config = CRM_Core_Config::singleton();
     $dsnArray = DB::parseDSN($config->dsn);
     $database = $dsnArray['database'];
     $domainID = CRM_Core_Config::domainID();
@@ -66,15 +65,16 @@ class CRM_Core_Lock {
     else {
       $this->_name = $database . '.' . $domainID . '.' . $name;
     }
-    CRM_Core_Error::debug_log_message('trying to construct lock for ' . $this->_name);
+    if (defined('CIVICRM_LOCK_DEBUG')) {
+      CRM_Core_Error::debug_log_message('trying to construct lock for ' . $this->_name);
+    }
     static $jobLog = FALSE;
-    if($jobLog && CRM_Core_DAO::singleValueQuery("SELECT IS_USED_LOCK( '{$jobLog}')")) {
+    if ($jobLog && CRM_Core_DAO::singleValueQuery("SELECT IS_USED_LOCK( '{$jobLog}')")) {
       return $this->hackyHandleBrokenCode($jobLog);
     }
-    if(stristr($name, 'civimail.job.')) {
+    if (stristr($name, 'civimail.job.')) {
       $jobLog = $this->_name;
     }
-    //CRM_Core_Error::debug_var('backtrace', debug_backtrace());
     $this->_timeout = $timeout !== NULL ? $timeout : self::TIMEOUT;
 
     $this->acquire();
@@ -84,12 +84,17 @@ class CRM_Core_Lock {
     $this->release();
   }
 
+  /**
+   * @return bool
+   */
   function acquire() {
-
-    CRM_Core_Error::debug_log_message('aquire lock for ' . $this->_name);
+    if (defined('CIVICRM_LOCK_DEBUG')) {
+      CRM_Core_Error::debug_log_message('acquire lock for ' . $this->_name);
+    }
     if (!$this->_hasLock) {
       $query = "SELECT GET_LOCK( %1, %2 )";
-      $params = array(1 => array($this->_name, 'String'),
+      $params = array(
+        1 => array($this->_name, 'String'),
         2 => array($this->_timeout, 'Integer'),
       );
       $res = CRM_Core_DAO::singleValueQuery($query, $params);
@@ -100,6 +105,9 @@ class CRM_Core_Lock {
     return $this->_hasLock;
   }
 
+  /**
+   * @return null|string
+   */
   function release() {
     if ($this->_hasLock) {
       $this->_hasLock = FALSE;
@@ -110,12 +118,18 @@ class CRM_Core_Lock {
     }
   }
 
+  /**
+   * @return null|string
+   */
   function isFree() {
     $query = "SELECT IS_FREE_LOCK( %1 )";
     $params = array(1 => array($this->_name, 'String'));
     return CRM_Core_DAO::singleValueQuery($query, $params);
   }
 
+  /**
+   * @return bool
+   */
   function isAcquired() {
     return $this->_hasLock;
   }
@@ -134,10 +148,12 @@ class CRM_Core_Lock {
    * @return boolean
    */
   function hackyHandleBrokenCode($jobLog) {
-    if(stristr($this->_name, 'job')) {
+    if (stristr($this->_name, 'job')) {
       throw new CRM_Core_Exception('lock aquisition for ' . $this->_name . 'attempted when ' . $jobLog . 'is not released');
     }
-    CRM_Core_Error::debug_log_message('(CRM-12856) faking lock for ' . $this->_name);
+    if (defined('CIVICRM_LOCK_DEBUG')) {
+      CRM_Core_Error::debug_log_message('(CRM-12856) faking lock for ' . $this->_name);
+    }
     $this->_hasLock = TRUE;
     return TRUE;
   }