SAAS-1071 - more work on celery backend

This commit is contained in:
Duncan Sommerville 2015-09-22 20:22:06 -04:00
parent ee1ceb9281
commit 55d5fc62a2
5 changed files with 111 additions and 17 deletions

View File

@ -121,6 +121,7 @@ define('CELERY_FAILED_STATUS', 'FAILED');
// Celery Services
define('SOUNDCLOUD_SERVICE_NAME', 'soundcloud');
define('PODCAST_SERVICE_NAME', 'podcast');
// Podcast Types
define('STATION_PODCAST', 0);

View File

@ -3,7 +3,7 @@
class CeleryServiceFactory {
/**
*
* Given an identifying string, get a ThirdPartyCeleryService object of that type
*
* @param $serviceName string the name of the service to create
*
@ -13,8 +13,11 @@ class CeleryServiceFactory {
switch($serviceName) {
case SOUNDCLOUD_SERVICE_NAME:
return new Application_Service_SoundcloudService();
case PODCAST_SERVICE_NAME:
return new Application_Service_PodcastService();
default:
return null;
}
return null;
}
}

View File

@ -1,7 +1,30 @@
<?php
class Application_Service_PodcastService
class Application_Service_PodcastService extends Application_Service_ThirdPartyCeleryService
{
/**
* Arbitrary constant identifiers for the internal tasks array
*/
const DOWNLOAD = 'download';
/**
* @var string service name to store in ThirdPartyTrackReferences database
*/
protected static $_SERVICE_NAME = PODCAST_SERVICE_NAME; // Service name constant from constants.php
/**
* @var string exchange name for Podcast tasks
*/
protected static $_CELERY_EXCHANGE_NAME = 'podcast';
/**
* @var array map of constant identifiers to Celery task names
*/
protected static $_CELERY_TASKS = [
self::DOWNLOAD => 'podcast-download' // TODO: rename this to ingest?
];
/**
* There is maximum of 50 podcasts allowed in the library - to limit
* resource consumption. This function returns true if the podcast
@ -39,4 +62,48 @@ class Application_Service_PodcastService
}
/**
* Given an array of track identifiers, download RSS feed tracks
*
* @param array $trackIds array of track identifiers to download
* TODO: do we need other parameters here...?
*/
public function download($trackIds) {
$CC_CONFIG = Config::getConfig();
$data = array(
// 'download_urls' => , TODO: get download urls to send to Celery
'callback_url' => Application_Common_HTTPHelper::getStationUrl() . '/rest/media',
'api_key' => $apiKey = $CC_CONFIG["apiKey"][0],
);
// FIXME
Logging::warn("FIXME: we can't create a task reference without a valid file ID");
$this->_executeTask(static::$_CELERY_TASKS[self::DOWNLOAD], $data, null);
}
/**
* Update a ThirdPartyTrackReferences object for a completed upload
*
* @param $task CeleryTasks the completed CeleryTasks object
* @param $trackId int ThirdPartyTrackReferences identifier
* @param $track object third-party service track object
* @param $status string Celery task status
*
* @return ThirdPartyTrackReferences the updated ThirdPartyTrackReferences object
*
* @throws Exception
* @throws PropelException
*/
function updateTrackReference($task, $trackId, $track, $status) {
$ref = parent::updateTrackReference($task, $trackId, $track, $status);
if ($status == CELERY_SUCCESS_STATUS) {
// TODO: handle successful download
// $ref->setDbForeignId();
// FIXME: we need the file ID here, but 'track' is too arbitrary...
$ref->setDbFileId($track->fileId);
}
$ref->save();
return $ref;
}
}

View File

@ -2,8 +2,17 @@
require_once "ThirdPartyCeleryService.php";
/**
* Service object for dealing with SoundCloud authorization and background tasks
*
* Class Application_Service_SoundcloudService
*/
class Application_Service_SoundcloudService extends Application_Service_ThirdPartyCeleryService implements OAuth2 {
/**
* Arbitrary constant identifiers for the internal tasks array
*/
const UPLOAD = 'upload';
const DOWNLOAD = 'download';
const DELETE = 'delete';
@ -23,12 +32,14 @@ class Application_Service_SoundcloudService extends Application_Service_ThirdPar
*/
protected static $_SERVICE_NAME = SOUNDCLOUD_SERVICE_NAME; // SoundCloud service name constant from constants.php
// TODO: Make these constants
/**
* @var string exchange name for SoundCloud tasks
*/
protected static $_CELERY_EXCHANGE_NAME = 'soundcloud';
/**
* @var array map of constant identifiers to Celery task names
*/
protected static $_CELERY_TASKS = [
self::UPLOAD => 'soundcloud-upload',
self::DOWNLOAD => 'soundcloud-download',
@ -174,32 +185,28 @@ class Application_Service_SoundcloudService extends Application_Service_ThirdPar
* @param $track object third-party service track object
* @param $status string Celery task status
*
* @return ThirdPartyTrackReferences the updated ThirdPartyTrackReferences object
* or null if the task was a DELETE
*
* @throws Exception
* @throws PropelException
*/
public function updateTrackReference($task, $trackId, $track, $status) {
parent::updateTask($task, $status);
$ref = ThirdPartyTrackReferencesQuery::create()
->findOneByDbId($trackId);
if (is_null($ref)) {
$ref = new ThirdPartyTrackReferences();
}
$ref->setDbService(static::$_SERVICE_NAME);
$ref = parent::updateTrackReference($task, $trackId, $track, $status);
// TODO: fetch any additional SoundCloud parameters we want to store
// Only set the SoundCloud fields if the task was successful
if ($status == CELERY_SUCCESS_STATUS) {
// If the task was to delete the file from SoundCloud, remove the reference
if ($task->getDbName() == static::$_CELERY_TASKS[DELETE]) {
if ($task->getDbName() == static::$_CELERY_TASKS[self::DELETE]) {
$this->removeTrackReference($ref->getDbFileId());
return;
return null;
}
$utc = new DateTimeZone("UTC");
$ref->setDbUploadTime(new DateTime("now", $utc));
// TODO: fetch any additional SoundCloud parameters we want to store
$ref->setDbForeignId($track->id); // SoundCloud identifier
}
// TODO: set SoundCloud upload status?
// $ref->setDbStatus($status);
$ref->save();
return $ref;
}
/**

View File

@ -80,14 +80,30 @@ abstract class Application_Service_ThirdPartyCeleryService extends Application_S
/**
* Update a ThirdPartyTrackReferences object for a completed upload
*
* Manipulation and use of the track object is left up to child implementations
*
* @param $task CeleryTasks the completed CeleryTasks object
* @param $trackId int ThirdPartyTrackReferences identifier
* @param $track object third-party service track object
* @param $status string Celery task status
*
* @return ThirdPartyTrackReferences the updated ThirdPartyTrackReferences object
*
* @throws Exception
* @throws PropelException
*/
abstract function updateTrackReference($task, $trackId, $track, $status);
public function updateTrackReference($task, $trackId, $track, $status) {
static::updateTask($task, $status);
$ref = ThirdPartyTrackReferencesQuery::create()
->findOneByDbId($trackId);
if (is_null($ref)) {
$ref = new ThirdPartyTrackReferences();
}
$ref->setDbService(static::$_SERVICE_NAME);
$utc = new DateTimeZone("UTC");
$ref->setDbUploadTime(new DateTime("now", $utc));
$ref->save();
return $ref;
}
}