示例#1
0
 /**
  * @param mixed $subscriber_id
  * @return NfyDbSubscription[]
  */
 public function getSubscriptions($subscriber_id = null)
 {
     NfyDbSubscription::model()->current()->withQueue($this->id)->with(array('categories'));
     $dbSubscriptions = $subscriber_id === null ? NfyDbSubscription::model()->findAll() : NfyDbSubscription::model()->findByAttributes(array('subscriber_id' => $subscriber_id));
     return NfyDbSubscription::createSubscriptions($dbSubscriptions);
 }
示例#2
0
    public function send($message, $category = null, $class_name = null, $object_pk = null)
    {
        $queueMessage = $this->createMessage($message, $class_name, $object_pk);

        if ($this->beforeSend($queueMessage) !== true) {
            Yii::log(Yii::t('NfyModule.app', "Not sending message '{msg}' to queue {queue_label}.", array('{msg}' => $queueMessage->body, '{queue_label}' => $this->label)), CLogger::LEVEL_INFO, 'nfy');
            return;
        }

        $success = true;

        $subscriptions = NfyDbSubscription::model()->current()->withQueue($this->id)->matchingCategory($category)->findAll();


        $trx = $queueMessage->getDbConnection()->getCurrentTransaction() !== null ? null : $queueMessage->getDbConnection()->beginTransaction();

        // empty($subscriptions) &&
        if (!$queueMessage->save()) {
            Yii::log(Yii::t('NfyModule.app', "Failed to save message '{msg}' in queue {queue_label}.", array('{msg}' => $queueMessage->body, '{queue_label}' => $this->label)), CLogger::LEVEL_ERROR, 'nfy');
            return false;
        }

        foreach ($subscriptions as $subscription) {
            $subscriptionMessage = clone $queueMessage;
            $subscriptionMessage->subscription_id = $subscription->id;
            $subscriptionMessage->message_id = $queueMessage->id;
            if ($this->beforeSendSubscription($subscriptionMessage, $subscription->subscriber_id) !== true) {
                continue;
            }
            if(!$this->sendMessage($queueMessage)){
               Yii::log(Yii::t('NfyModule.app', "Failed to send message '{msg}' by email in queue {queue_label} for the subscription {subscription_id}.", array(
                    '{msg}' => $queueMessage->body,
                    '{queue_label}' => $this->label,
                    '{subscription_id}' => $subscription->id,
                )), CLogger::LEVEL_ERROR, 'nfy');
                $success = false;  
            }
            if($success){
                if (!$subscriptionMessage->save()) {
                    Yii::log(Yii::t('NfyModule.app', "Failed to save message '{msg}' in queue {queue_label} for the subscription {subscription_id}.", array(
                        '{msg}' => $queueMessage->body,
                        '{queue_label}' => $this->label,
                        '{subscription_id}' => $subscription->id,
                    )), CLogger::LEVEL_ERROR, 'nfy');
                    $success = false;
                }
            }

            $this->afterSendSubscription($subscriptionMessage, $subscription->subscriber_id);
        }

        $this->afterSend($queueMessage);

        if ($trx !== null) {
            $trx->commit();
        }

        Yii::log(Yii::t('NfyModule.app', "Sent message '{msg}' to queue {queue_label}.", array('{msg}' => $queueMessage->body, '{queue_label}' => $this->label)), CLogger::LEVEL_INFO, 'nfy');

        return $success;
    }