Skip to content

Commit

Permalink
added locks
Browse files Browse the repository at this point in the history
  • Loading branch information
bjendres committed Sep 18, 2014
1 parent 9cfbe1a commit 9aa8cd6
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
36 changes: 36 additions & 0 deletions extension/CRM/Banking/Helpers/Lock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/*-------------------------------------------------------+
| Project 60 - CiviBanking |
| Copyright (C) 2013-2014 SYSTOPIA |
| Author: B. Endres (endres -at- systopia.de) |
| http://www.systopia.de/ |
+--------------------------------------------------------+
| This program is released as free software under the |
| Affero GPL license. You can redistribute it and/or |
| modify it under the terms of this license which you |
| can read by viewing the included agpl.txt or online |
| at www.gnu.org/licenses/agpl.html. Removal of this |
| copyright header is strictly prohibited without |
| written permission from the original author(s). |
+--------------------------------------------------------*/

/**
* Get a batching lock
*
* the lock is needed so that only one relevant process can access the
* payment/statment data structures at a time
*
* @return lock object. check if it ->isAcquired() before use
*/
function banking_helper_getLock($type, $id) {
if ($type=='tx') {
$timeout = 7.0; // TODO: do we need a setting here?
return new CRM_Core_Lock('org.project60.banking.tx'.'-'.$id, $timeout);
} elseif ($type=='txbatch') {
$timeout = 600.0; // 10mins, TODO: do we need a setting here?
return new CRM_Core_Lock('org.project60.banking.txbatch'.'-'.$id, $timeout);
} else {
error_log("org.project60.banking - Lock of type '$type' not known.");
return NULL;
}
}
32 changes: 29 additions & 3 deletions extension/CRM/Banking/Matcher/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/

require_once 'CRM/Banking/Helpers/OptionValue.php';
require_once 'CRM/Banking/Helpers/Lock.php';

class CRM_Banking_Matcher_Engine {

Expand Down Expand Up @@ -90,12 +91,20 @@ private function initPlugins() {
* This will destroy all records of the execution!
*/
public function match( CRM_Banking_BAO_BankTransaction $btx, $override_processed = FALSE ) {
$lock = banking_helper_getLock('tx', $btx->id);
if (!$lock->isAcquired()) {
error_log("org.project60.banking - couldn't acquire lock. Timeout is ".$lock->_timeout);
return false;
}

error_log("matching ".$btx->id);
if (!$override_processed) {
// don't match already executed transactions...
$processed_status_id = banking_helper_optionvalueid_by_groupname_and_name('civicrm_banking.bank_tx_status', 'Processed');
$ignored_status_id = banking_helper_optionvalueid_by_groupname_and_name('civicrm_banking.bank_tx_status', 'Ignored');
if ($btx->status_id == $processed_status_id || $btx->status_id == $ignored_status_id) {
// will not match already executed transactions
$lock->release();
return true;
}
}
Expand All @@ -114,11 +123,17 @@ public function match( CRM_Banking_BAO_BankTransaction $btx, $override_processed
foreach ($plugins as $plugin) {
// run matchers to generate suggestions
$continue = $this->matchPlugin( $plugin, $context );
if (!$continue) return true;
if (!$continue) {
$lock->release();
return true;
}

// check if we can execute the suggestion right aways
$abort = $this->checkAutoExecute($plugin, $btx);
if ($abort) return false;
if ($abort) {
$lock->release();
return false;
}
}
}
}
Expand All @@ -129,6 +144,8 @@ public function match( CRM_Banking_BAO_BankTransaction $btx, $override_processed
$btx->status_id = $newStatus;
$btx->setStatus($newStatus);

error_log("done matching ".$btx->id);
$lock->release();
return false;
}

Expand All @@ -143,8 +160,17 @@ protected function checkAutoExecute($plugin, $btx) {
foreach ($suggestions as $suggestion) {
if ($suggestion->getPluginID()==$plugin->getPluginID()) {
if ($suggestion->getProbability() >= $plugin->autoExecute()) {
$lock = banking_helper_getLock('tx', $btx->id);
if (!$lock->isAcquired()) {
error_log("org.project60.banking - couldn't acquire lock. Timeout is ".$lock->_timeout);
continue;
}

$btx->saveSuggestions();
return $suggestion->execute( $btx, $plugin );
$result = $suggestion->execute( $btx, $plugin );

$lock->release();
return $result;
}
}
}
Expand Down

0 comments on commit 9aa8cd6

Please sign in to comment.