birthdayGiftConsumer.php 1.3 KB

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