RabbitMq.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. require_once 'php-amqplib/amqp.inc';
  3. class Application_Model_RabbitMq
  4. {
  5. public static $doPush = false;
  6. /**
  7. * Sets a flag to push the schedule at the end of the request.
  8. */
  9. public static function PushSchedule()
  10. {
  11. self::$doPush = true;
  12. }
  13. private static function sendMessage($exchange, $data)
  14. {
  15. $CC_CONFIG = Config::getConfig();
  16. $conn = new AMQPConnection($CC_CONFIG["rabbitmq"]["host"],
  17. $CC_CONFIG["rabbitmq"]["port"],
  18. $CC_CONFIG["rabbitmq"]["user"],
  19. $CC_CONFIG["rabbitmq"]["password"],
  20. $CC_CONFIG["rabbitmq"]["vhost"]);
  21. if (!isset($conn)) {
  22. throw new Exception("Cannot connect to RabbitMQ server");
  23. }
  24. $channel = $conn->channel();
  25. $channel->access_request($CC_CONFIG["rabbitmq"]["vhost"], false, false,
  26. true, true);
  27. $channel->exchange_declare($exchange, 'direct', false, true);
  28. $msg = new AMQPMessage($data, array('content_type' => 'text/plain'));
  29. $channel->basic_publish($msg, $exchange);
  30. $channel->close();
  31. $conn->close();
  32. }
  33. public static function SendMessageToPypo($event_type, $md)
  34. {
  35. $md["event_type"] = $event_type;
  36. $exchange = 'airtime-pypo';
  37. $data = json_encode($md, JSON_FORCE_OBJECT);
  38. self::sendMessage($exchange, $data);
  39. }
  40. public static function SendMessageToMediaMonitor($event_type, $md)
  41. {
  42. $md["event_type"] = $event_type;
  43. $exchange = 'airtime-media-monitor';
  44. $data = json_encode($md);
  45. self::sendMessage($exchange, $data);
  46. }
  47. public static function SendMessageToShowRecorder($event_type)
  48. {
  49. $exchange = 'airtime-pypo';
  50. $now = new DateTime("@".time()); //in UTC timezone
  51. $end_timestamp = new DateTime("@".(time() + 3600*2)); //in UTC timezone
  52. $temp = array();
  53. $temp['event_type'] = $event_type;
  54. $temp['server_timezone'] = Application_Model_Preference::GetTimezone();
  55. if ($event_type == "update_recorder_schedule") {
  56. $temp['shows'] = Application_Model_Show::getShows($now,
  57. $end_timestamp, $onlyRecord=true);
  58. }
  59. $data = json_encode($temp);
  60. self::sendMessage($exchange, $data);
  61. }
  62. }