Mepps has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/354511 )

Change subject: Create time limit based on user configuration Bug: T163412
......................................................................

Create time limit based on user configuration
Bug: T163412

Change-Id: I475881c52ad5c720a985bf4b0bb2822496ecf54e
---
M sites/all/modules/thank_you/thank_you.module
1 file changed, 59 insertions(+), 53 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/crm 
refs/changes/11/354511/1

diff --git a/sites/all/modules/thank_you/thank_you.module 
b/sites/all/modules/thank_you/thank_you.module
index 31b7c32..d1092a6 100644
--- a/sites/all/modules/thank_you/thank_you.module
+++ b/sites/all/modules/thank_you/thank_you.module
@@ -510,7 +510,7 @@
                        throw new WmfException( 'EMAIL_SYSTEM_FAILURE', $msg, 
$debug );
                }
 
-        return false;
+        return false; 
        } catch (Exception $e) {
                $debug = array_merge( $email, array( "html" => '', "plaintext" 
=> '' ) );
                watchdog('thank_you', 'Sending thank you message failed with 
generic exception for contribution: ' .
@@ -661,6 +661,13 @@
                        700 => 700,
                ),
        );
+  $form['thank_you_batch_time'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Job time limit (in seconds)'),
+    '#description' => t('Maximum elapsed duration of a thank you job, after 
which we will abort from the loop.  This can be used to set a reliable duty 
cycle for the job.  Either a time limit or batch size limit is required.'),
+    '#required' => TRUE,
+    '#default_value' => variable_get('thank_you_batch_time', 90),
+  );
 
        // this is still in use
   $form['thank_you_onestep_unsubscribe'] = array(
@@ -703,6 +710,9 @@
 }
 
 function thank_you_batch_process(){
+       $startTime = time();
+       $timeLimit = variable_get('thank_you_batch_time', 90); 
+
        civicrm_initialize();
        // the following variables should be set in the module settings, not 
here
        $days = variable_get( 'thank_you_days', false );
@@ -754,61 +764,57 @@
        $consecutiveFailures = 0;
        $failureThreshold = variable_get( 'thank_you_failure_threshold', 5 );
        $abort = false;
-
+       $endTime = $startTime + $timeLimit;
        foreach( $result as $contribution ) {
-               watchdog(
-                       "thank_you",
-                       "Attempting to send thank you mail for contribution ID 
[{$contribution->id}], trxn_id [{$contribution->trxn_id}]",
-                       array(),
-                       WATCHDOG_INFO
-               );
-               try {
-                       if ( thank_you_for_contribution( $contribution->id ) ) {
-                               $count++;
-                       }
-                       $consecutiveFailures = 0;
-               } catch ( WmfException $ex ) {
-                       $errName = $ex->getErrorName();
-                       $noThankYou = "failed: $errName";
-
-                       $logMessage = $ex->getMessage()
-                               . "<br/>Setting no_thank_you to '$noThankYou'";
-                       wmf_civicrm_set_no_thank_you( $contribution->id, 
$noThankYou );
-
-                       $consecutiveFailures++;
-                       $msg = "Disabling thank you job after 
$consecutiveFailures consecutive failures";
-                       if ( $consecutiveFailures > $failureThreshold ) {
-                               $abort = true;
-                               watchdog( 'thank_you', $msg, array(), 
WATCHDOG_ALERT );
-                               $logMessage .= "<br/>$msg";
-                       }
-
-                       // Always email if we're disabling the job
-                       if ( $ex->isNoEmail() && !$abort ) {
-                               watchdog( 'thank_you', $logMessage, array(), 
WATCHDOG_ERROR );
-                       } else {
-                               try {
-                                       // Make an exception for failmail with 
the updated message,
-                                       // but avoid doubling the type prefix.
-                                       $logMessage = substr( $logMessage, 
strlen( $errName ) + 1 );
-                                       $mailException = new WmfException( 
$errName, $logMessage );
-
-                                       // Failmail also logs a WATCHDOG_ERROR
-                                       wmf_common_failmail( 'thank_you', '', 
$mailException );
-                               } catch ( Exception $innerEx ) {
-                                       watchdog(
-                                               'thank_you',
-                                               'Can\'t even send failmail, 
disabling thank you job',
-                                               array(),
-                                               WATCHDOG_ALERT
-                                       );
-                                       $abort = true;
+               while ($timeOk = time() < $endTime) {
+                       try {
+                               if ( thank_you_for_contribution( 
$contribution->id ) ) {
+                                       $count++;
                                }
-                       }
+                               $consecutiveFailures = 0;
+                       } catch ( WmfException $ex ) {
+                               $errName = $ex->getErrorName();
+                               $noThankYou = "failed: $errName";
 
-                       if ( $abort ) {
-                               variable_set( 'thank_you_enabled', 'false' );
-                               break;
+                               $logMessage = $ex->getMessage()
+                                       . "<br/>Setting no_thank_you to 
'$noThankYou'";
+                               wmf_civicrm_set_no_thank_you( 
$contribution->id, $noThankYou );
+
+                               $consecutiveFailures++;
+                               $msg = "Disabling thank you job after 
$consecutiveFailures consecutive failures";
+                               if ( $consecutiveFailures > $failureThreshold ) 
{
+                                       $abort = true;
+                                       watchdog( 'thank_you', $msg, array(), 
WATCHDOG_ALERT );
+                                       $logMessage .= "<br/>$msg";
+                               }
+
+                               // Always email if we're disabling the job
+                               if ( $ex->isNoEmail() && !$abort ) {
+                                       watchdog( 'thank_you', $logMessage, 
array(), WATCHDOG_ERROR );
+                               } else {
+                                       try {
+                                               // Make an exception for 
failmail with the updated message,
+                                               // but avoid doubling the type 
prefix.
+                                               $logMessage = substr( 
$logMessage, strlen( $errName ) + 1 );
+                                               $mailException = new 
WmfException( $errName, $logMessage );
+
+                                               // Failmail also logs a 
WATCHDOG_ERROR
+                                               wmf_common_failmail( 
'thank_you', '', $mailException );
+                                       } catch ( Exception $innerEx ) {
+                                               watchdog(
+                                                       'thank_you',
+                                                       'Can\'t even send 
failmail, disabling thank you job',
+                                                       array(),
+                                                       WATCHDOG_ALERT
+                                               );
+                                               $abort = true;
+                                       }
+                               }
+
+                               if ( $abort ) {
+                                       variable_set( 'thank_you_enabled', 
'false' );
+                                       break;
+                               }
                        }
                }
        }

-- 
To view, visit https://gerrit.wikimedia.org/r/354511
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I475881c52ad5c720a985bf4b0bb2822496ecf54e
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/crm
Gerrit-Branch: master
Gerrit-Owner: Mepps <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to