2011-03-22 14:55:33 +01:00
|
|
|
<?php
|
|
|
|
require_once 'php-amqplib/amqp.inc';
|
2015-06-09 20:02:29 +02:00
|
|
|
require_once 'massivescale/celery-php/celery.php';
|
2011-03-22 14:55:33 +01:00
|
|
|
|
2011-09-26 21:32:56 +02:00
|
|
|
class Application_Model_RabbitMq
|
2011-03-22 14:55:33 +01:00
|
|
|
{
|
2012-09-15 00:20:46 +02:00
|
|
|
public static $doPush = false;
|
2011-03-24 04:24:06 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a flag to push the schedule at the end of the request.
|
|
|
|
*/
|
2012-07-16 03:17:13 +02:00
|
|
|
public static function PushSchedule()
|
|
|
|
{
|
2012-09-15 00:20:46 +02:00
|
|
|
self::$doPush = true;
|
2011-03-24 04:24:06 +01:00
|
|
|
}
|
2011-03-22 14:55:33 +01:00
|
|
|
|
2014-03-13 18:35:48 +01:00
|
|
|
private static function sendMessage($exchange, $exchangeType, $autoDeleteExchange, $data, $queue="")
|
2011-08-12 21:36:00 +02:00
|
|
|
{
|
2013-01-14 22:16:14 +01:00
|
|
|
$CC_CONFIG = Config::getConfig();
|
2011-08-12 21:36:00 +02:00
|
|
|
|
|
|
|
$conn = new AMQPConnection($CC_CONFIG["rabbitmq"]["host"],
|
|
|
|
$CC_CONFIG["rabbitmq"]["port"],
|
|
|
|
$CC_CONFIG["rabbitmq"]["user"],
|
2011-12-19 06:11:45 +01:00
|
|
|
$CC_CONFIG["rabbitmq"]["password"],
|
|
|
|
$CC_CONFIG["rabbitmq"]["vhost"]);
|
2013-03-04 21:22:09 +01:00
|
|
|
|
|
|
|
if (!isset($conn)) {
|
|
|
|
throw new Exception("Cannot connect to RabbitMQ server");
|
|
|
|
}
|
|
|
|
|
2011-08-12 21:36:00 +02:00
|
|
|
$channel = $conn->channel();
|
2012-09-04 22:52:22 +02:00
|
|
|
$channel->access_request($CC_CONFIG["rabbitmq"]["vhost"], false, false,
|
|
|
|
true, true);
|
2011-08-12 21:36:00 +02:00
|
|
|
|
2014-03-13 18:35:48 +01:00
|
|
|
//I'm pretty sure we DON'T want to autodelete ANY exchanges but I'm keeping the code
|
|
|
|
//the way it is just so I don't accidentally break anything when I add the Analyzer code in. -- Albert, March 13, 2014
|
|
|
|
$channel->exchange_declare($exchange, $exchangeType, false, true, $autoDeleteExchange);
|
2011-08-12 21:36:00 +02:00
|
|
|
|
|
|
|
$msg = new AMQPMessage($data, array('content_type' => 'text/plain'));
|
|
|
|
|
2012-09-11 17:20:42 +02:00
|
|
|
$channel->basic_publish($msg, $exchange);
|
2011-08-12 21:36:00 +02:00
|
|
|
$channel->close();
|
|
|
|
$conn->close();
|
2011-03-22 14:55:33 +01:00
|
|
|
}
|
|
|
|
|
2012-09-11 17:12:58 +02:00
|
|
|
public static function SendMessageToPypo($event_type, $md)
|
2011-05-05 16:55:14 +02:00
|
|
|
{
|
2011-06-17 17:54:36 +02:00
|
|
|
$md["event_type"] = $event_type;
|
|
|
|
|
2012-09-11 17:12:58 +02:00
|
|
|
$exchange = 'airtime-pypo';
|
|
|
|
$data = json_encode($md, JSON_FORCE_OBJECT);
|
2014-03-13 18:35:48 +01:00
|
|
|
self::sendMessage($exchange, 'direct', true, $data);
|
2012-09-11 17:12:58 +02:00
|
|
|
}
|
2011-05-05 16:55:14 +02:00
|
|
|
|
2012-09-11 17:12:58 +02:00
|
|
|
public static function SendMessageToMediaMonitor($event_type, $md)
|
|
|
|
{
|
|
|
|
$md["event_type"] = $event_type;
|
2011-05-05 16:55:14 +02:00
|
|
|
|
2012-09-11 17:12:58 +02:00
|
|
|
$exchange = 'airtime-media-monitor';
|
2011-05-05 16:55:14 +02:00
|
|
|
$data = json_encode($md);
|
2014-03-13 18:35:48 +01:00
|
|
|
self::sendMessage($exchange, 'direct', true, $data);
|
2011-05-05 16:55:14 +02:00
|
|
|
}
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2011-07-23 18:30:36 +02:00
|
|
|
public static function SendMessageToShowRecorder($event_type)
|
|
|
|
{
|
2012-09-11 17:12:58 +02:00
|
|
|
$exchange = 'airtime-pypo';
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2011-11-11 20:54:08 +01:00
|
|
|
$now = new DateTime("@".time()); //in UTC timezone
|
|
|
|
$end_timestamp = new DateTime("@".(time() + 3600*2)); //in UTC timezone
|
2011-08-16 22:36:31 +02:00
|
|
|
|
2012-09-11 17:12:58 +02:00
|
|
|
$temp = array();
|
2011-07-25 22:24:00 +02:00
|
|
|
$temp['event_type'] = $event_type;
|
2011-10-05 23:27:00 +02:00
|
|
|
$temp['server_timezone'] = Application_Model_Preference::GetTimezone();
|
2012-07-16 03:17:13 +02:00
|
|
|
if ($event_type == "update_recorder_schedule") {
|
2012-09-04 22:52:22 +02:00
|
|
|
$temp['shows'] = Application_Model_Show::getShows($now,
|
2012-09-11 17:12:58 +02:00
|
|
|
$end_timestamp, $onlyRecord=true);
|
2011-07-25 22:24:00 +02:00
|
|
|
}
|
|
|
|
$data = json_encode($temp);
|
2012-07-11 00:51:32 +02:00
|
|
|
|
2014-03-13 18:35:48 +01:00
|
|
|
self::sendMessage($exchange, 'direct', true, $data);
|
2011-07-23 18:30:36 +02:00
|
|
|
}
|
2014-03-17 15:19:39 +01:00
|
|
|
|
2015-06-16 21:10:08 +02:00
|
|
|
public static function getRmqConfigPath() {
|
2014-04-01 18:07:29 +02:00
|
|
|
//Hack for Airtime Pro. The RabbitMQ settings for communicating with airtime_analyzer are global
|
|
|
|
//and shared between all instances on Airtime Pro.
|
2015-06-12 18:31:55 +02:00
|
|
|
$CC_CONFIG = Config::getConfig();
|
2014-04-22 17:13:15 +02:00
|
|
|
$devEnv = "production"; //Default
|
2014-04-22 18:28:20 +02:00
|
|
|
if (array_key_exists("dev_env", $CC_CONFIG)) {
|
|
|
|
$devEnv = $CC_CONFIG["dev_env"];
|
2014-04-22 17:13:15 +02:00
|
|
|
}
|
2015-02-05 20:31:20 +01:00
|
|
|
$rmq_config_path = "/etc/airtime-saas/".$devEnv."/rabbitmq-analyzer.ini";
|
|
|
|
if (!file_exists($rmq_config_path)) {
|
|
|
|
// If the dev env specific rabbitmq-analyzer.ini doesn't exist default
|
|
|
|
// to the production rabbitmq-analyzer.ini
|
|
|
|
$rmq_config_path = "/etc/airtime-saas/production/rabbitmq-analyzer.ini";
|
|
|
|
}
|
2015-06-12 18:31:55 +02:00
|
|
|
return $rmq_config_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function SendMessageToAnalyzer($tmpFilePath, $importedStorageDirectory, $originalFilename,
|
|
|
|
$callbackUrl, $apiKey, $storageBackend, $filePrefix)
|
|
|
|
{
|
2015-06-16 21:10:08 +02:00
|
|
|
$config = parse_ini_file(self::getRmqConfigPath(), true);
|
2014-04-01 18:07:29 +02:00
|
|
|
$conn = new AMQPConnection($config["rabbitmq"]["host"],
|
|
|
|
$config["rabbitmq"]["port"],
|
|
|
|
$config["rabbitmq"]["user"],
|
|
|
|
$config["rabbitmq"]["password"],
|
|
|
|
$config["rabbitmq"]["vhost"]);
|
|
|
|
|
2014-03-13 16:14:30 +01:00
|
|
|
$exchange = 'airtime-uploads';
|
2014-04-01 18:07:29 +02:00
|
|
|
$exchangeType = 'topic';
|
|
|
|
$queue = 'airtime-uploads';
|
|
|
|
$autoDeleteExchange = false;
|
2014-03-13 18:35:48 +01:00
|
|
|
$data['tmp_file_path'] = $tmpFilePath;
|
2015-02-21 00:21:49 +01:00
|
|
|
$data['storage_backend'] = $storageBackend;
|
2014-03-17 15:19:39 +01:00
|
|
|
$data['import_directory'] = $importedStorageDirectory;
|
|
|
|
$data['original_filename'] = $originalFilename;
|
2014-03-13 18:35:48 +01:00
|
|
|
$data['callback_url'] = $callbackUrl;
|
|
|
|
$data['api_key'] = $apiKey;
|
2015-05-19 22:20:51 +02:00
|
|
|
|
2015-01-12 20:50:42 +01:00
|
|
|
// We add a prefix to the resource name so files are not all placed
|
|
|
|
// under the root folder. We do this in case we need to restore a
|
|
|
|
// customer's file/s; File restoration is done via the S3 Browser
|
|
|
|
// client. The client will hang if there are too many files under the
|
|
|
|
// same folder.
|
2015-01-26 19:10:10 +01:00
|
|
|
$data['file_prefix'] = $filePrefix;
|
2014-03-13 18:35:48 +01:00
|
|
|
|
|
|
|
$jsonData = json_encode($data);
|
2014-04-01 18:07:29 +02:00
|
|
|
//self::sendMessage($exchange, 'topic', false, $jsonData, 'airtime-uploads');
|
|
|
|
|
|
|
|
if (!isset($conn)) {
|
|
|
|
throw new Exception("Cannot connect to RabbitMQ server");
|
|
|
|
}
|
|
|
|
|
|
|
|
$channel = $conn->channel();
|
|
|
|
$channel->access_request($config["rabbitmq"]["vhost"], false, false,
|
|
|
|
true, true);
|
|
|
|
|
|
|
|
//I'm pretty sure we DON'T want to autodelete ANY exchanges but I'm keeping the code
|
|
|
|
//the way it is just so I don't accidentally break anything when I add the Analyzer code in. -- Albert, March 13, 2014
|
|
|
|
$channel->exchange_declare($exchange, $exchangeType, false, true, $autoDeleteExchange);
|
|
|
|
|
2014-04-02 23:27:10 +02:00
|
|
|
$msg = new AMQPMessage($jsonData, array('content_type' => 'text/plain'));
|
2014-04-01 18:07:29 +02:00
|
|
|
|
|
|
|
$channel->basic_publish($msg, $exchange);
|
2014-04-02 23:27:10 +02:00
|
|
|
$channel->close();
|
|
|
|
$conn->close();
|
2014-04-01 18:07:29 +02:00
|
|
|
|
2011-07-23 18:30:36 +02:00
|
|
|
}
|
2012-10-24 00:47:15 +02:00
|
|
|
|
2014-09-13 00:05:24 +02:00
|
|
|
|
2014-09-17 00:28:39 +02:00
|
|
|
public static function SendMessageToHaproxyConfigDaemon($md){
|
2015-05-11 20:34:21 +02:00
|
|
|
//XXX: This function has been deprecated and is no longer needed
|
2015-06-09 20:02:29 +02:00
|
|
|
}
|
|
|
|
|
2011-03-22 14:55:33 +01:00
|
|
|
}
|