Add TaskManager task to fail pending imports over an hour old

This commit is contained in:
Duncan Sommerville 2015-10-29 11:03:28 -04:00
parent 8978c3692d
commit ada7212823
2 changed files with 72 additions and 0 deletions

View File

@ -175,6 +175,8 @@ interface AirtimeTask {
/** /**
* Class UpgradeTask * Class UpgradeTask
*
* Checks the current Airtime version and runs any outstanding upgrades
*/ */
class UpgradeTask implements AirtimeTask { class UpgradeTask implements AirtimeTask {
@ -198,6 +200,8 @@ class UpgradeTask implements AirtimeTask {
/** /**
* Class CeleryTask * Class CeleryTask
*
* Checks the Celery broker task queue and runs callbacks for completed tasks
*/ */
class CeleryTask implements AirtimeTask { class CeleryTask implements AirtimeTask {
@ -221,6 +225,9 @@ class CeleryTask implements AirtimeTask {
/** /**
* Class PodcastTask * Class PodcastTask
*
* Checks podcasts marked for automatic ingest and downloads any new episodes
* since the task was last run
*/ */
class PodcastTask implements AirtimeTask { class PodcastTask implements AirtimeTask {
@ -242,8 +249,36 @@ class PodcastTask implements AirtimeTask {
} }
/**
* Class ImportTask
*/
class ImportCleanupTask implements AirtimeTask {
/**
* Check if there are any files that have been stuck
* in Pending status for over an hour
*
* @return bool true if there are any files stuck pending,
* otherwise false
*/
public function shouldBeRun() {
return Application_Service_MediaService::areFilesStuckInPending();
}
/**
* Clean up stuck imports by changing their import status to Failed
*/
public function run() {
Application_Service_MediaService::clearStuckPendingImports();
}
}
/** /**
* Class StationPodcastTask * Class StationPodcastTask
*
* Checks the Station podcast rollover timer and resets monthly allotted
* downloads if enough time has passed (default: 1 month)
*/ */
class StationPodcastTask implements AirtimeTask { class StationPodcastTask implements AirtimeTask {
@ -282,6 +317,7 @@ class TaskFactory {
const UPGRADE = "upgrade"; const UPGRADE = "upgrade";
const CELERY = "celery"; const CELERY = "celery";
const PODCAST = "podcast"; const PODCAST = "podcast";
const IMPORT = "import";
const STATION_PODCAST = "station-podcast"; const STATION_PODCAST = "station-podcast";
/** /**
@ -291,6 +327,7 @@ class TaskFactory {
"upgrade" => "UpgradeTask", "upgrade" => "UpgradeTask",
"celery" => "CeleryTask", "celery" => "CeleryTask",
"podcast" => "PodcastTask", "podcast" => "PodcastTask",
"import" => "ImportCleanupTask",
"station-podcast" => "StationPodcastTask", "station-podcast" => "StationPodcastTask",
); );

View File

@ -5,6 +5,15 @@ require_once("FileIO.php");
class Application_Service_MediaService class Application_Service_MediaService
{ {
const PENDING_FILE_TIMEOUT_SECONDS = 3600;
/**
* @var array store an internal array of the pending files so we don't have
* to go to the database twice
*/
private static $_pendingFiles;
/** Move (or copy) a file to the stor/organize directory and send it off to the /** Move (or copy) a file to the stor/organize directory and send it off to the
analyzer to be processed. analyzer to be processed.
* @param $callbackUrl * @param $callbackUrl
@ -111,5 +120,31 @@ class Application_Service_MediaService
} }
} }
/**
* Check if there are any files that have been stuck
* in Pending status for over an hour
*
* @return bool true if there are any files stuck pending,
* otherwise false
*/
public static function areFilesStuckInPending() {
$oneHourAgo = gmdate(DEFAULT_TIMESTAMP_FORMAT, (microtime(true) - self::PENDING_FILE_TIMEOUT_SECONDS));
self::$_pendingFiles = CcFilesQuery::create()
->filterByDbImportStatus(CcFiles::IMPORT_STATUS_PENDING)
->filterByDbUtime($oneHourAgo, Criteria::LESS_EQUAL)
->find();
return !empty(self::$_pendingFiles);
}
/**
* Clean up stuck imports by changing their import status to Failed
*/
public static function clearStuckPendingImports() {
foreach(self::$_pendingFiles as $file) {
/** @var $file CcFiles */
$file->setDbImportStatus(CcFiles::IMPORT_STATUS_FAILED)->save();
}
}
} }