birthdayGiftConsumer.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace common\components\rabbitmq;
  3. use bizHd\birthday\classes\BirthdayGiftClass;
  4. use common\components\noticeUtil;
  5. use mikemadisonweb\rabbitmq\components\ConsumerInterface;
  6. use PhpAmqpLib\Message\AMQPMessage;
  7. use Yii;
  8. class birthdayGiftConsumer extends baseConsumer
  9. {
  10. public function execute(AMQPMessage $msg)
  11. {
  12. try {
  13. $this->ensureDbConnection();
  14. $data = unserialize($msg->body);
  15. if (!is_array($data)) {
  16. noticeUtil::push("生日赠礼消费者报错:Invalid message format");
  17. return ConsumerInterface::MSG_REJECT;
  18. }
  19. $type = $data['type'] ?? null;
  20. $result = false;
  21. if ($type == 'birthday_gift_expire') {
  22. $giftId = intval($data['giftId'] ?? 0);
  23. $result = $this->runWithDbReconnect(function () use ($giftId) {
  24. return BirthdayGiftClass::expireById($giftId);
  25. });
  26. } else {
  27. noticeUtil::push("生日赠礼消费者未知 type: {$type}");
  28. $result = true;
  29. }
  30. return $result ? ConsumerInterface::MSG_ACK : ConsumerInterface::MSG_ACK;
  31. } catch (\Exception $e) {
  32. noticeUtil::push("生日赠礼消费者报错:" . $e->getMessage());
  33. return ConsumerInterface::MSG_ACK;
  34. }
  35. }
  36. }