RabbitPublishError.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace mikemadisonweb\rabbitmq\models;
  3. use DomainException;
  4. use Exception;
  5. use InvalidArgumentException;
  6. use mikemadisonweb\rabbitmq\components\Producer;
  7. use Yii;
  8. use yii\behaviors\TimestampBehavior;
  9. use yii\db\ActiveRecord;
  10. /**
  11. * This is the model class for table "rabbit_publish_error".
  12. *
  13. * @property int $id
  14. * @property string $message
  15. * @property int|null $created_at
  16. * @property int|null $updated_at
  17. * @property string $options
  18. * @property string|null $error
  19. * @property int|null $counter
  20. */
  21. class RabbitPublishError extends ActiveRecord
  22. {
  23. /** @var string */
  24. public $msgBody;
  25. /** @var string */
  26. public $exchangeName;
  27. /** @var string */
  28. public $routingKey = '';
  29. /** @var array */
  30. public $additionalProperties = [];
  31. /** @var array|null */
  32. public $headers = null;
  33. /** @var string */
  34. public $producerName;
  35. /** @var string */
  36. public $errorMsg;
  37. public static function tableName()
  38. {
  39. return 'rabbit_publish_error';
  40. }
  41. public function behaviors()
  42. {
  43. return [
  44. TimestampBehavior::class,
  45. ];
  46. }
  47. public function rules()
  48. {
  49. return [
  50. [['message', 'options'], 'required'],
  51. [['message', 'error'], 'string'],
  52. [['created_at', 'updated_at', 'counter'], 'integer'],
  53. [['options'], 'safe'],
  54. ];
  55. }
  56. public function saveItem()
  57. {
  58. if (!$this->producerName) {
  59. throw new InvalidArgumentException('Field producerName is required!');
  60. }
  61. if (!$this->msgBody) {
  62. throw new InvalidArgumentException('Field msgBody is required!');
  63. }
  64. if (!$this->exchangeName) {
  65. throw new InvalidArgumentException('Field exchangeName is required!');
  66. }
  67. if (!$this->errorMsg) {
  68. throw new InvalidArgumentException('Field errorMsg is required!');
  69. }
  70. $this->message = $this->msgBody;
  71. $options = [
  72. 'exchangeName' => $this->exchangeName,
  73. 'producerName' => $this->producerName,
  74. 'routingKey' => $this->routingKey,
  75. 'additionalProperties' => $this->additionalProperties,
  76. 'headers' => $this->headers
  77. ];
  78. $this->options = json_encode($options);
  79. $this->error = $this->errorMsg;
  80. $this->counter = 1;
  81. if (!$this->save()) {
  82. print_r($this->errors);
  83. throw new DomainException();
  84. }
  85. }
  86. public function rePublish()
  87. {
  88. $cache = Yii::$app->cache;
  89. $key = 'rabbit_publish_error';
  90. if ($cache->exists($key)) {
  91. return;
  92. }
  93. $cache->set($key, true);
  94. foreach (self::find()->each() as $model) {
  95. /** @var self $model */
  96. try {
  97. $options = json_decode($model->options, true);
  98. /** @var Producer $producer */
  99. $producer = Yii::$app->rabbitmq->getProducer($options['producerName']);
  100. $producer->publish(
  101. $model->message,
  102. $options['exchangeName'],
  103. $options['routingKey'],
  104. $options['additionalProperties'],
  105. $options['headers']
  106. );
  107. $model->delete();
  108. } catch (Exception $e) {
  109. $model->error = $e->getMessage();
  110. $model->updateCounters(['counter' => 1]);
  111. $model->save();
  112. }
  113. }
  114. $cache->delete($key);
  115. }
  116. }