SAAS-854 - celery-based SoundCloud backend uploader

This commit is contained in:
Duncan Sommerville 2015-06-09 14:02:29 -04:00
parent 80bfb7fd93
commit 49165e91d9
12 changed files with 672 additions and 12 deletions

View file

@ -27,7 +27,7 @@ require_once "ProvisioningHelper.php";
require_once "GoogleAnalytics.php";
require_once "Timezone.php";
require_once "Auth.php";
require_once __DIR__ . '/services/SoundCloudService.php';
require_once __DIR__.'/services/SoundCloudService.php';
require_once __DIR__.'/forms/helpers/ValidationTypes.php';
require_once __DIR__.'/forms/helpers/CustomDecorators.php';
require_once __DIR__.'/controllers/plugins/RabbitMqPlugin.php';

View file

@ -1,10 +1,13 @@
<?php
require_once 'php-amqplib/amqp.inc';
require_once 'massivescale/celery-php/celery.php';
class Application_Model_RabbitMq
{
public static $doPush = false;
const CELERY_TIMEOUT = 10;
/**
* Sets a flag to push the schedule at the end of the request.
*/
@ -42,6 +45,32 @@ class Application_Model_RabbitMq
$conn->close();
}
public static function sendCeleryMessage($task, $exchange, $data) {
$CC_CONFIG = Config::getConfig();
$c = new Celery($CC_CONFIG["rabbitmq"]["host"],
$CC_CONFIG["rabbitmq"]["user"],
$CC_CONFIG["rabbitmq"]["password"],
$CC_CONFIG["rabbitmq"]["vhost"],
$exchange=$exchange);
$result = $c->PostTask($task, $data);
$timeout = 0;
while(!$result->isReady()) {
sleep(1);
if($timeout++ >= self::CELERY_TIMEOUT) {
break;
}
}
if($result->isSuccess()) {
Logging::info($result);
return $result->getResult();
} else {
throw new CeleryTimeoutException("Celery task $task timed out!");
}
}
public static function SendMessageToPypo($event_type, $md)
{
$md["event_type"] = $event_type;
@ -146,5 +175,9 @@ class Application_Model_RabbitMq
public static function SendMessageToHaproxyConfigDaemon($md){
//XXX: This function has been deprecated and is no longer needed
}
}
public static function uploadToSoundCloud($data) {
return self::sendCeleryMessage("upload", "soundcloud-uploads", $data);
}
}

View file

@ -44,7 +44,6 @@ class SoundcloudService extends ThirdPartyService {
}
}
// TODO: upload functionality will be moved to python, this is just for testing
/**
* Upload the file with the given identifier to SoundCloud
*
@ -55,8 +54,13 @@ class SoundcloudService extends ThirdPartyService {
*/
public function upload($fileId) {
$file = Application_Model_StoredFile::RecallById($fileId);
$data = array(
'track_data' => $this->_buildTrackArray($file),
'token' => $this->_client->getAccessToken(),
'file_path' => $file->getFilePaths()[0]
);
try {
$track = json_decode($this->_client->post('tracks', $this->_buildTrackArray($file)));
$track = json_decode(Application_Model_RabbitMq::uploadToSoundCloud($data));
parent::_createTrackReference($fileId, $track);
} catch(Soundcloud\Exception\InvalidHttpResponseCodeException $e) {
Logging::info("Invalid request: " . $e->getMessage());
@ -74,15 +78,12 @@ class SoundcloudService extends ThirdPartyService {
*/
private function _buildTrackArray($file) {
$trackArray = array(
'track[title]' => $file->getName(),
// TODO: verify that S3 uploads work
'track[asset_data]' => '@'.$file->getFilePaths()[0]
'title' => $file->getName(),
);
foreach($this->_SOUNDCLOUD_PREF_FUNCTIONS as $func => $param) {
$val = Application_Model_Preference::$func();
if (!empty($val)) {
$trackArray["track[$param]"] = $val;
$trackArray[$param] = $val;
}
}