/**
  * @param integer $limit
  * @throws \yii\console\Exception
  */
 public function actionSend($limit = 10)
 {
     $this->stdout(sprintf('Looking for the next %d letters to send... ', $limit));
     $Messages = PostmanMessageModel::find()->forMailQueue()->limit($limit)->all();
     if (empty($Messages)) {
         $this->stdout('messages not found.' . PHP_EOL);
     } else {
         $this->stdout(sprintf('fond %d.', count($Messages)) . PHP_EOL);
         foreach ($Messages as $Message) {
             $this->stdout(sprintf('    > attempt to send message %d...', $Message->id));
             $result = $Message->sendImmediately();
             if ($Message->hasErrors()) {
                 $errors = $Message->getErrors();
                 $this->stderr('an error occurred.' . PHP_EOL);
                 $this->stderr(print_r($errors, 1) . PHP_EOL);
                 $Message->error = Json::encode($errors);
                 $Message->validate() && $Message->save();
             } elseif (is_bool($result) && $result === true) {
                 $this->stdout('send.' . PHP_EOL);
             } elseif (is_bool($result) && $result === false) {
                 $Postman = PostmanMessageModel::getPostman();
                 $Message->repeatAfter($Postman->resentTry, $Postman->resentOffset);
                 $this->stdout('NOT send.' . PHP_EOL);
             } else {
                 throw new \yii\console\Exception('Emergency response');
             }
         }
     }
 }
Example #2
0
 /**
  * @return array|bool
  * @throws \yii\base\ErrorException
  */
 public function run()
 {
     $id = $this->postmanMessageId;
     if (empty($id)) {
         throw new \yii\base\ErrorException(\Yii::t('cookyii.postman', 'Unable to determine the id of the message'));
     }
     $Message = PostmanMessageModel::find()->byId($id)->one();
     if (empty($Message)) {
         throw new \yii\base\ErrorException(\Yii::t('cookyii.postman', 'Failed to find message'));
     }
     $result = $Message->sendImmediately();
     if ($Message->hasErrors()) {
         $errors = $Message->getErrors();
         $Message->error = Json::encode($errors);
         $Message->validate() && $Message->save();
     } elseif (is_bool($result) && $result === true) {
     } elseif (is_bool($result) && $result === false) {
         $Postman = PostmanMessageModel::getPostman();
         $Message->repeatAfter($Postman->resentTry, $Postman->resentOffset);
     } else {
         throw new \yii\base\ErrorException(\Yii::t('cookyii.postman', 'Emergency response'));
     }
     return $result;
 }