producerName) { throw new InvalidArgumentException('Field producerName is required!'); } if (!$this->msgBody) { throw new InvalidArgumentException('Field msgBody is required!'); } if (!$this->exchangeName) { throw new InvalidArgumentException('Field exchangeName is required!'); } if (!$this->errorMsg) { throw new InvalidArgumentException('Field errorMsg is required!'); } $this->message = $this->msgBody; $options = [ 'exchangeName' => $this->exchangeName, 'producerName' => $this->producerName, 'routingKey' => $this->routingKey, 'additionalProperties' => $this->additionalProperties, 'headers' => $this->headers ]; $this->options = json_encode($options); $this->error = $this->errorMsg; $this->counter = 1; if (!$this->save()) { print_r($this->errors); throw new DomainException(); } } public function rePublish() { $cache = Yii::$app->cache; $key = 'rabbit_publish_error'; if ($cache->exists($key)) { return; } $cache->set($key, true); foreach (self::find()->each() as $model) { /** @var self $model */ try { $options = json_decode($model->options, true); /** @var Producer $producer */ $producer = Yii::$app->rabbitmq->getProducer($options['producerName']); $producer->publish( $model->message, $options['exchangeName'], $options['routingKey'], $options['additionalProperties'], $options['headers'] ); $model->delete(); } catch (Exception $e) { $model->error = $e->getMessage(); $model->updateCounters(['counter' => 1]); $model->save(); } } $cache->delete($key); } }