<commit_before><?php
/**
 * MessagingGroupsConsumer
 */

namespace DoSomething\MessagingGroupsConsumer;

use \SimpleXMLElement;
use \Exception;

use DoSomething\MB_Toolbox\MB_Toolbox_BaseConsumer;
use DoSomething\StatHat\Client as StatHat;

class MessagingGroupsConsumer extends MB_Toolbox_BaseConsumer
{

  /**
   * The amount of time for the application to sleep / wait when an exception is
   * encountered.
   */
  const RETRY_SECONDS = 60;

  /**
   * Exception code signal to retry.
   */
  const RETRY_SIGNAL = 42042;

  /**
   * Retry count for retry code signal.
   */
  const RETRY_SIGNAL_ATTEMPTS = 10;

  /**
   * Gambit campaigns cache.
   *
   * @var array
   */
  private $gambitCampaignsCache = [];

  /**
   * Gambit campaign.
   *
   * @var boolean|string
   */
  private $gambitCampaign = false;

  /**
   * Constructor compatible with MBC_BaseConsumer.
   */
  public function __construct($targetMBconfig = 'messageBroker') {
    parent::__construct($targetMBconfig);

    // Cache gambit campaigns,
    $gambit = $this->mbConfig->getProperty('gambit');
    $gambitCampaigns = $gambit->getAllCampaigns();

    foreach ($gambitCampaigns as $campaign) {
      if ($campaign->campaignbot === true) {
        $this->gambitCampaignsCache[$campaign->id] = $campaign;
      }
    }

    if (count($this->gambitCampaignsCache) < 1) {
      // Basically, die.
      throw new Exception('No gambit connection.');
    }
  }

  /**
   * Initial method triggered by blocked call in mbc-registration-mobile.php.
   *
   * @param array $payload
   *   The contents of the queue entry message being processed.
   */
  public function consumeMessagingGroupsQueue($payload) {
    echo '------ messaging-groups-consumer - MessagingGroupsConsumer->consumeRegistrationMobileQueue() - ' . date('j D M Y G:i:s T') . ' START ------', PHP_EOL . PHP_EOL;
    $this->gambitCampaign = false;

    parent::consumeQueue($payload);

    try {

      if ($this->canProcess($this->message)) {

        $params = $this->setter($this->message);
        $this->process($params);

        $this->statHat->ezCount('messaging-groups-consumer: MessagingGroupsConsumer: process', 1);
        // Ack in Service process() due to nested try/catch
      }
      else {
        echo '- canProcess() is not passed, removing from queue.', PHP_EOL;
        $this->statHat->ezCount('messaging-groups-consumer: MessagingGroupsConsumer: skipping', 1);
        $this->messageBroker->sendAck($this->message['payload']);
      }
    }
    catch(Exception $e) {
      /**
       * The following code block is just awful.
       * It's legacy and we'll get rid of it soon.
       * | | |
       * V V V
       */

      if (!(strpos($e->getMessage(), 'Connection timed out') === false)) {
        echo '** Connection timed out... waiting before retrying: ' . date('j D M Y G:i:s T') . ' - getMessage(): ' . $e->getMessage(), PHP_EOL;
        $this->statHat->ezCount('messaging-groups-consumer: MessagingGroupsConsumer: Exception: Connection timed out', 1);
        sleep(self::RETRY_SECONDS);
        $this->messageBroker->sendNack($this->message['payload']);
        echo '- Nack sent to requeue message: ' . date('j D M Y G:i:s T'), PHP_EOL . PHP_EOL;
      }
      elseif (!(strpos($e->getMessage(), 'Operation timed out') === false)) {
        echo '** Operation timed out... waiting before retrying: ' . date('j D M Y G:i:s T') . ' - getMessage(): ' . $e->getMessage(), PHP_EOL;
        $this->statHat->ezCount('messaging-groups-consumer: MessagingGroupsConsumer: Exception: Operation timed out', 1);
        sleep(self::RETRY_SECONDS);
        $this->messageBroker->sendNack($this->message['payload']);
        echo '- Nack sent to requeue message: ' . date('j D M Y G:i:s T'), PHP_EOL . PHP_EOL;
      }
      elseif (!(strpos($e->getMessage(), 'Failed to connect') === false)) {
        echo '** Failed to connect... waiting before retrying: ' . date('j D M Y G:i:s T') . ' - getMessage(): ' . $e->getMessage(), PHP_EOL;
        sleep(self::RETRY_SECONDS);
        $this->messageBroker->sendNack($this->message['payload']);
        echo '- Nack sent to requeue message: ' . date('j D M Y G:i:s T'), PHP_EOL . PHP_EOL;
        $this->statHat->ezCount('messaging-groups-consumer: MessagingGroupsConsumer: Exception: Failed to connect', 1);
      }
      elseif (!(strpos($e->getMessage(), 'Bad response - HTTP Code:500') === false)) {
        echo '** Connection error, http code 500... waiting before retrying: ' . date('j D M Y G:i:s T') . ' - getMessage(): ' . $e->getMessage(), PHP_EOL;
        sleep(self::RETRY_SECONDS);
        $this->messageBroker->sendNack($this->message['payload']);
        echo '- Nack sent to requeue message: ' . date('j D M Y G:i:s T'), PHP_EOL . PHP_EOL;
        $this->statHat->ezCount('messaging-groups-consumer: MessagingGroupsConsumer: Exception: Bad response - HTTP Code:500', 1);
      }
      elseif ($e->getCode() === self::RETRY_SIGNAL) {
        echo '** Retry signal caught, waiting before retrying: ' . date('j D M Y G:i:s T') . ' - getMessage(): '
          . PHP_EOL . $e->getMessage() . PHP_EOL;
        sleep(self::RETRY_SECONDS);
        if ($this->message['payload']->get('delivery_tag') <= self::RETRY_SIGNAL_ATTEMPTS) {
          $this->messageBroker->sendNack($this->message['payload']);
          echo '- Nack sent to requeue message: ' . date('j D M Y G:i:s T')
            . '. Attempt ' . $this->message['payload']->get('delivery_tag') . PHP_EOL;
          $this->statHat->ezCount('messaging-groups-consumer: MessagingGroupsConsumer: Exception: Retry signal', 1);
        } else {
          // Max attempt has reached, saving message to deadLetterQueue.
          echo '- Exception: Retry signal: Max attempt reached. Saving message to deadLetterQueue ' . date('j D M Y G:i:s T') . PHP_EOL;
          parent::deadLetter($this->message, 'MessagingGroupsConsumer->consumeRegistrationMobileQueue() Error', $e);

          // Nack without requeueing.
          $this->messageBroker->sendNack($this->message['payload'], false, false);
          $this->statHat->ezCount('messaging-groups-consumer: MessagingGroupsConsumer: Exception: Retry signal: Max attempt reached', 1);
        }
      }
      else {
        echo '- Not timeout or connection error, message to deadLetterQueue: ' . date('j D M Y G:i:s T'), PHP_EOL;
        echo '- Error message: ' . $e->getMessage(), PHP_EOL;

        // Unknown exception, save the message to deadLetter queue.
        $this->statHat->ezCount('messaging-groups-consumer: MessagingGroupsConsumer: Exception: deadLetter', 1);
        parent::deadLetter($this->message, 'MessagingGroupsConsumer->consumeRegistrationMobileQueue() Error', $e);

        // Send Negative Acknowledgment, don't requeue the message.
        $this->messageBroker->sendNack($this->message['payload'], false, false);
      }
    }

    // @todo: Throttle the number of consumers running. Based on the number of messages
    // waiting to be processed start / stop consumers. Make "reactive"!
    $queueStatus = parent::queueStatus('messagingGroupsQueue');

    echo  PHP_EOL . '------ messaging-groups-consumer - MessagingGroupsConsumer->consumeRegistrationMobileQueue() - ' . date('j D M Y G:i:s T') . ' END ------', PHP_EOL . PHP_EOL;
  }

  /**
   * Method to determine if message can / should be processed. Conditions based on business
   * logic for submitted mobile numbers and related message values.
   *
   * @param array $message Values to determine if message can be processed.
   *
   * @retun boolean
   */
  protected function canProcess($message) {
    // Check mobile number presence.
    if (empty($message['mobile'])) {
      echo '** canProcess(): mobile number was not defined, skipping.' . PHP_EOL;

      return false;
    }

    // Check application id.
    if (empty($message['application_id'])) {
      echo '** canProcess(): application_id not set.' . PHP_EOL;

      return false;
    }

    // Check that application id is allowed.
    $supportedApps = ['US', 'MUI'];<commit_msg>Add more logging improvements<commit_after><?php
/**
 * MessagingGroupsConsumer
 */

namespace DoSomething\MessagingGroupsConsumer;

use \SimpleXMLElement;
use \Exception;

use DoSomething\MB_Toolbox\MB_Toolbox_BaseConsumer;
use DoSomething\StatHat\Client as StatHat;

class MessagingGroupsConsumer extends MB_Toolbox_BaseConsumer
{

  /**
   * The amount of time for the application to sleep / wait when an exception is
   * encountered.
   */
  const RETRY_SECONDS = 60;

  /**
   * Exception code signal to retry.
   */
  const RETRY_SIGNAL = 42042;

  /**
   * Retry count for retry code signal.
   */
  const RETRY_SIGNAL_ATTEMPTS = 10;

  /**
   * Gambit campaigns cache.
   *
   * @var array
   */
  private $gambitCampaignsCache = [];

  /**
   * Gambit campaign.
   *
   * @var boolean|string
   */
  private $gambitCampaign = false;

  /**
   * Constructor compatible with MBC_BaseConsumer.
   */
  public function __construct($targetMBconfig = 'messageBroker') {
    parent::__construct($targetMBconfig);

    // Cache gambit campaigns,
    $gambit = $this->mbConfig->getProperty('gambit');
    $gambitCampaigns = $gambit->getAllCampaigns();

    foreach ($gambitCampaigns as $campaign) {
      if ($campaign->campaignbot === true) {
        $this->gambitCampaignsCache[$campaign->id] = $campaign;
      }
    }

    if (count($this->gambitCampaignsCache) < 1) {
      // Basically, die.
      throw new Exception('No gambit connection.');
    }
  }

  /**
   * Initial method triggered by blocked call in mbc-registration-mobile.php.
   *
   * @param array $payload
   *   The contents of the queue entry message being processed.
   */
  public function consumeMessagingGroupsQueue($payload) {
    echo '------ ' . date(DATE_ISO8601) . ' messaging-groups-consumer - MessagingGroupsConsumer->consumeRegistrationMobileQueue() START ------' . PHP_EOL . PHP_EOL;
    $this->gambitCampaign = false;

    parent::consumeQueue($payload);

    try {

      if ($this->canProcess($this->message)) {

        $params = $this->setter($this->message);
        $this->process($params);

        $this->statHat->ezCount('messaging-groups-consumer: MessagingGroupsConsumer: process', 1);
        // Ack in Service process() due to nested try/catch
      }
      else {
        echo '- canProcess() is not passed, removing from queue.' . PHP_EOL;
        $this->statHat->ezCount('messaging-groups-consumer: MessagingGroupsConsumer: skipping', 1);
        $this->messageBroker->sendAck($this->message['payload']);
      }
    }
    catch(Exception $e) {
      /**
       * The following code block is just awful.
       * It's legacy and we'll get rid of it soon.
       * | | |
       * V V V
       */

      if (!(strpos($e->getMessage(), 'Connection timed out') === false)) {
        echo '** ' . date(DATE_ISO8601) . ' Connection timed out... waiting before retrying: - getMessage(): ' . $e->getMessage() . PHP_EOL;
        $this->statHat->ezCount('messaging-groups-consumer: MessagingGroupsConsumer: Exception: Connection timed out', 1);
        sleep(self::RETRY_SECONDS);
        $this->messageBroker->sendNack($this->message['payload']);
        echo '- ' . date(DATE_ISO8601) . ' Nack sent to requeue message: ' . PHP_EOL . PHP_EOL;
      }
      elseif (!(strpos($e->getMessage(), 'Operation timed out') === false)) {
        echo '** ' . date(DATE_ISO8601) . ' Operation timed out... waiting before retrying: - getMessage(): ' . $e->getMessage() . PHP_EOL;
        $this->statHat->ezCount('messaging-groups-consumer: MessagingGroupsConsumer: Exception: Operation timed out', 1);
        sleep(self::RETRY_SECONDS);
        $this->messageBroker->sendNack($this->message['payload']);
        echo '- ' . date(DATE_ISO8601) . ' Nack sent to requeue message: ' . PHP_EOL . PHP_EOL;
      }
      elseif (!(strpos($e->getMessage(), 'Failed to connect') === false)) {
        echo '** ' . date(DATE_ISO8601) . ' Failed to connect... waiting before retrying: - getMessage(): ' . $e->getMessage() . PHP_EOL;
        sleep(self::RETRY_SECONDS);
        $this->messageBroker->sendNack($this->message['payload']);
        echo '- ' . date(DATE_ISO8601) . ' Nack sent to requeue message: ' . PHP_EOL . PHP_EOL;
        $this->statHat->ezCount('messaging-groups-consumer: MessagingGroupsConsumer: Exception: Failed to connect', 1);
      }
      elseif (!(strpos($e->getMessage(), 'Bad response - HTTP Code:500') === false)) {
        echo '** ' . date(DATE_ISO8601) . ' Connection error, http code 500... waiting before retrying: - getMessage(): ' . $e->getMessage() . PHP_EOL;
        sleep(self::RETRY_SECONDS);
        $this->messageBroker->sendNack($this->message['payload']);
        echo '- ' . date(DATE_ISO8601) . ' Nack sent to requeue message: ' . PHP_EOL . PHP_EOL;
        $this->statHat->ezCount('messaging-groups-consumer: MessagingGroupsConsumer: Exception: Bad response - HTTP Code:500', 1);
      }
      elseif ($e->getCode() === self::RETRY_SIGNAL) {
        echo '** ' . date(DATE_ISO8601) . ' Retry signal caught, waiting before retrying: - getMessage(): '
          . PHP_EOL . $e->getMessage() . PHP_EOL;
        sleep(self::RETRY_SECONDS);
        if ($this->message['payload']->get('delivery_tag') <= self::RETRY_SIGNAL_ATTEMPTS) {
          $this->messageBroker->sendNack($this->message['payload']);
          echo '- ' . date(DATE_ISO8601) . ' Nack sent to requeue message. Attempt ' . $this->message['payload']->get('delivery_tag') . PHP_EOL;
          $this->statHat->ezCount('messaging-groups-consumer: MessagingGroupsConsumer: Exception: Retry signal', 1);
        } else {
          // Max attempt has reached, saving message to deadLetterQueue.
          echo '- ' . date(DATE_ISO8601) . ' Exception: Retry signal: Max attempt reached. Saving message to deadLetterQueue ' . PHP_EOL;
          parent::deadLetter($this->message, 'MessagingGroupsConsumer->consumeRegistrationMobileQueue() Error', $e);

          // Nack without requeueing.
          $this->messageBroker->sendNack($this->message['payload'], false, false);
          $this->statHat->ezCount('messaging-groups-consumer: MessagingGroupsConsumer: Exception: Retry signal: Max attempt reached', 1);
        }
      }
      else {
        echo '- ' . date(DATE_ISO8601) . ' Not timeout or connection error, message to deadLetterQueue: ' . PHP_EOL;
        echo '- Error message: ' . $e->getMessage() . PHP_EOL;

        // Unknown exception, save the message to deadLetter queue.
        $this->statHat->ezCount('messaging-groups-consumer: MessagingGroupsConsumer: Exception: deadLetter', 1);
        parent::deadLetter($this->message, 'MessagingGroupsConsumer->consumeRegistrationMobileQueue() Error', $e);

        // Send Negative Acknowledgment, don't requeue the message.
        $this->messageBroker->sendNack($this->message['payload'], false, false);
      }
    }

    // @todo: Throttle the number of consumers running. Based on the number of messages
    // waiting to be processed start / stop consumers. Make "reactive"!
    $queueStatus = parent::queueStatus('messagingGroupsQueue');

    echo  PHP_EOL . '------ ' . date(DATE_ISO8601) . ' messaging-groups-consumer - MessagingGroupsConsumer->consumeRegistrationMobileQueue() END ------' . PHP_EOL . PHP_EOL;
  }

  /**
   * Method to determine if message can / should be processed. Conditions based on business
   * logic for submitted mobile numbers and related message values.
   *
   * @param array $message Values to determine if message can be processed.
   *
   * @retun boolean
   */
  protected function canProcess($message) {
    // Check mobile number presence.
    if (empty($message['mobile'])) {
      echo '** canProcess(): mobile number was not defined, skipping.' . PHP_EOL;

      return false;
    }

    // Check application id.
    if (empty($message['application_id'])) {
      echo '** canProcess(): application_id not set.' . PHP_EOL;

      return false;
    }

    // Check that application id is allowed.
    $supportedApps = ['US', 'MUI'];
{'fraction_non_alphanumeric': 0.1462822269273882, 'fraction_numerical': 0.006974716652136007, 'mean_word_length': 2.994776119402985, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 155, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5448526', 'n_tokens_mistral': 5019, 'n_tokens_neox': 4800, 'n_words': 1198}