public function removeInertLocks($transaction_id, $reason)
    {
        $query = array(_LOCKED_FOR_TRANS => $transaction_id);
        $docs = $this->getLocksCollection()->find($query);

        if($this->getLocksCollection()->count($query) == 0 ) {
            return false;
        }else{

            //1. Create audit entry with in_progress status
            $auditCollection  = $this->getAuditManualRollbacksCollection();
            $auditDocumentId = $this->generateIdForNewMongoDocument();
            try{
                $documents = array();
                foreach($docs as $doc)
                {
                    $documents[] = $doc[_ID_KEY][_ID_RESOURCE];
                }

                $result = $auditCollection->insertOne(
                    array(
                        _ID_KEY => $auditDocumentId,
                        'type' => AUDIT_TYPE_REMOVE_INERT_LOCKS,
                        'status' => AUDIT_STATUS_IN_PROGRESS,
                        'reason' => $reason,
                        'transaction_id' => $transaction_id,
                        'documents' => $documents,
                        _CREATED_TS=> $this->getMongoDate(),
                    )
                );
                if (!$result->isAcknowledged()) {
                    $error = $this->getLastDBError($this->getDatabase());
                    throw new \Exception("Failed to create audit entry with error message- " . $error['err']);
                }
            }
            catch(\Exception $e) { //simply send false as status as we are unable to create audit entry
                $this->errorLog(MONGO_LOCK,
                    array(
                        'description'=>'Driver::removeInertLocks - failed',
                        'transaction_id'=>$transaction_id,
                        'exception-message' => $e->getMessage()
                    )
                );
                throw $e;
            }

            //we can not try to combine this try-catch with try-catch above.
            //Catch below is supposed to update audit entry with error status but in above catch error can occur when creating audit entry.
            try{
                //2. Unlock documents linked to transaction
                $this->unlockAllDocuments($transaction_id);

                //3. Update audit entry to say it was completed

                $result = $auditCollection->updateOne(array(_ID_KEY => $auditDocumentId), array(MONGO_OPERATION_SET => array("status" => AUDIT_STATUS_COMPLETED, _UPDATED_TS => $this->getMongoDate())));

                if(!$result->isAcknowledged())
                {
                    $error = $this->getLastDBError($this->getDatabase());
                    throw new \Exception("Failed to update audit entry with error message- " . $error['err']);
                }
            }
            catch(\Exception $e) {
                $logInfo = array(
                    'description'=>'Driver::removeInertLocks - failed',
                    'transaction_id'=>$transaction_id,
                    'exception-message' => $e->getMessage()
                );

                //4. Update audit entry to say it was failed with error
                $result = $auditCollection->updateOne(array(_ID_KEY => $auditDocumentId), array(MONGO_OPERATION_SET => array("status" => AUDIT_STATUS_ERROR, _UPDATED_TS => $this->getMongoDate(), 'error' => $e->getMessage())));

                if(!$result->isAcknowledged())
                {
                    $error = $this->getLastDBError($this->getDatabase());
                    $logInfo['additional-error']=  "Failed to update audit entry with error message- " . $error['err'];
                }

                $this->errorLog(MONGO_LOCK, $logInfo);
                throw $e;
            }
        }
        return true;
    }
public function removeInertLocks($transaction_id, $reason)
    {
        $query = array(_LOCKED_FOR_TRANS => $transaction_id);
        $docs = $this->getLocksCollection()->find($query);

        if($this->getLocksCollection()->count($query) == 0 ) {
            return false;
        }else{

            //1. Create audit entry with in_progress status
            $auditCollection  = $this->getAuditManualRollbacksCollection();
            $auditDocumentId = $this->generateIdForNewMongoDocument();
            try{
                $documents = array();
                foreach($docs as $doc)
                {
                    $documents[] = $doc[_ID_KEY][_ID_RESOURCE];
                }

                $result = $auditCollection->insertOne(
                    array(
                        _ID_KEY => $auditDocumentId,
                        'type' => AUDIT_TYPE_REMOVE_INERT_LOCKS,
                        'status' => AUDIT_STATUS_IN_PROGRESS,
                        'reason' => $reason,
                        'transaction_id' => $transaction_id,
                        'documents' => $documents,
                        _CREATED_TS=> $this->getMongoDate(),
                    )
                );
                if (!$result->isAcknowledged()) {
                    $error = $this->getLastDBError($this->getDatabase());
                    throw new \Exception("Failed to create audit entry with error message- " . $error['err']);
                }
            }
            catch(\Exception $e) { //simply send false as status as we are unable to create audit entry
                $this->errorLog(MONGO_LOCK,
                    array(
                        'description'=>'Driver::removeInertLocks - failed',
                        'transaction_id'=>$transaction_id,
                        'exception-message' => $e->getMessage()
                    )
                );
                throw $e;
            }

            //we can not try to combine this try-catch with try-catch above.
            //Catch below is supposed to update audit entry with error status but in above catch error can occur when creating audit entry.
            try{
                //2. Unlock documents linked to transaction
                $this->unlockAllDocuments($transaction_id);

                //3. Update audit entry to say it was completed

                $result = $auditCollection->updateOne(array(_ID_KEY => $auditDocumentId), array(MONGO_OPERATION_SET => array("status" => AUDIT_STATUS_COMPLETED, _UPDATED_TS => $this->getMongoDate())));

                if(!$result->isAcknowledged())
                {
                    $error = $this->getLastDBError($this->getDatabase());
                    throw new \Exception("Failed to update audit entry with error message- " . $error['err']);
                }
            }
            catch(\Exception $e) {
                $logInfo = array(
                    'description'=>'Driver::removeInertLocks - failed',
                    'transaction_id'=>$transaction_id,
                    'exception-message' => $e->getMessage()
                );

                //4. Update audit entry to say it was failed with error
                $result = $auditCollection->updateOne(array(_ID_KEY => $auditDocumentId), array(MONGO_OPERATION_SET => array("status" => AUDIT_STATUS_ERROR, _UPDATED_TS => $this->getMongoDate(), 'error' => $e->getMessage())));

                if(!$result->isAcknowledged())
                {
                    $error = $this->getLastDBError($this->getDatabase());
                    $logInfo['additional-error']=  "Failed to update audit entry with error message- " . $error['err'];
                }

                $this->errorLog(MONGO_LOCK, $logInfo);
                throw $e;
            }
        }
        return true;
    }
public function removeInertLocks ( $ transaction_id , $ reason ) { $ query = array ( _LOCKED_FOR_TRANS => $ transaction_id ) ; $ docs = $ this -> getLocksCollection ( ) -> find ( $ query ) ; if ( $ this -> getLocksCollection ( ) -> count ( $ query ) == 0 ) { return false ; } else { //1. Create audit entry with in_progress status $ auditCollection = $ this -> getAuditManualRollbacksCollection ( ) ; $ auditDocumentId = $ this -> generateIdForNewMongoDocument ( ) ; try { $ documents = array ( ) ; foreach ( $ docs as $ doc ) { $ documents [ ] = $ doc [ _ID_KEY ] [ _ID_RESOURCE ] ; } $ result = $ auditCollection -> insertOne ( array ( _ID_KEY => $ auditDocumentId , 'type' => AUDIT_TYPE_REMOVE_INERT_LOCKS , 'status' => AUDIT_STATUS_IN_PROGRESS , 'reason' => $ reason , 'transaction_id' => $ transaction_id , 'documents' => $ documents , _CREATED_TS => $ this -> getMongoDate ( ) , ) ) ; if ( ! $ result -> isAcknowledged ( ) ) { $ error = $ this -> getLastDBError ( $ this -> getDatabase ( ) ) ; throw new \ Exception ( "Failed to create audit entry with error message- " . $ error [ 'err' ] ) ; } } catch ( \ Exception $ e ) { //simply send false as status as we are unable to create audit entry $ this -> errorLog ( MONGO_LOCK , array ( 'description' => 'Driver::removeInertLocks - failed' , 'transaction_id' => $ transaction_id , 'exception-message' => $ e -> getMessage ( ) ) ) ; throw $ e ; } //we can not try to combine this try-catch with try-catch above. //Catch below is supposed to update audit entry with error status but in above catch error can occur when creating audit entry. try { //2. Unlock documents linked to transaction $ this -> unlockAllDocuments ( $ transaction_id ) ; //3. Update audit entry to say it was completed $ result = $ auditCollection -> updateOne ( array ( _ID_KEY => $ auditDocumentId ) , array ( MONGO_OPERATION_SET => array ( "status" => AUDIT_STATUS_COMPLETED , _UPDATED_TS => $ this -> getMongoDate ( ) ) ) ) ; if ( ! $ result -> isAcknowledged ( ) ) { $ error = $ this -> getLastDBError ( $ this -> getDatabase ( ) ) ; throw new \ Exception ( "Failed to update audit entry with error message- " . $ error [ 'err' ] ) ; } } catch ( \ Exception $ e ) { $ logInfo = array ( 'description' => 'Driver::removeInertLocks - failed' , 'transaction_id' => $ transaction_id , 'exception-message' => $ e -> getMessage ( ) ) ; //4. Update audit entry to say it was failed with error $ result = $ auditCollection -> updateOne ( array ( _ID_KEY => $ auditDocumentId ) , array ( MONGO_OPERATION_SET => array ( "status" => AUDIT_STATUS_ERROR , _UPDATED_TS => $ this -> getMongoDate ( ) , 'error' => $ e -> getMessage ( ) ) ) ) ; if ( ! $ result -> isAcknowledged ( ) ) { $ error = $ this -> getLastDBError ( $ this -> getDatabase ( ) ) ; $ logInfo [ 'additional-error' ] = "Failed to update audit entry with error message- " . $ error [ 'err' ] ; } $ this -> errorLog ( MONGO_LOCK , $ logInfo ) ; throw $ e ; } } return true ; }
Remove locks that are there forever, creates a audit entry to keep track who and why removed these locks.
@param string $transaction_id
@param string $reason
@return bool
@throws \Exception, if something goes wrong when unlocking documents, or creating audit entries.
Remove locks that are there forever creates a audit entry to keep track who and why removed these locks .