From ada7212823dd053128d3530b669c7f5eaf41b4b5 Mon Sep 17 00:00:00 2001 From: Duncan Sommerville Date: Thu, 29 Oct 2015 11:03:28 -0400 Subject: [PATCH] Add TaskManager task to fail pending imports over an hour old --- .../application/common/TaskManager.php | 37 +++++++++++++++++++ .../application/services/MediaService.php | 35 ++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/airtime_mvc/application/common/TaskManager.php b/airtime_mvc/application/common/TaskManager.php index 180c11932..0425f340c 100644 --- a/airtime_mvc/application/common/TaskManager.php +++ b/airtime_mvc/application/common/TaskManager.php @@ -175,6 +175,8 @@ interface AirtimeTask { /** * Class UpgradeTask + * + * Checks the current Airtime version and runs any outstanding upgrades */ class UpgradeTask implements AirtimeTask { @@ -198,6 +200,8 @@ class UpgradeTask implements AirtimeTask { /** * Class CeleryTask + * + * Checks the Celery broker task queue and runs callbacks for completed tasks */ class CeleryTask implements AirtimeTask { @@ -221,6 +225,9 @@ class CeleryTask implements AirtimeTask { /** * Class PodcastTask + * + * Checks podcasts marked for automatic ingest and downloads any new episodes + * since the task was last run */ 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 + * + * Checks the Station podcast rollover timer and resets monthly allotted + * downloads if enough time has passed (default: 1 month) */ class StationPodcastTask implements AirtimeTask { @@ -282,6 +317,7 @@ class TaskFactory { const UPGRADE = "upgrade"; const CELERY = "celery"; const PODCAST = "podcast"; + const IMPORT = "import"; const STATION_PODCAST = "station-podcast"; /** @@ -291,6 +327,7 @@ class TaskFactory { "upgrade" => "UpgradeTask", "celery" => "CeleryTask", "podcast" => "PodcastTask", + "import" => "ImportCleanupTask", "station-podcast" => "StationPodcastTask", ); diff --git a/airtime_mvc/application/services/MediaService.php b/airtime_mvc/application/services/MediaService.php index 1bd3e6772..e1712c92d 100644 --- a/airtime_mvc/application/services/MediaService.php +++ b/airtime_mvc/application/services/MediaService.php @@ -5,6 +5,15 @@ require_once("FileIO.php"); 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 analyzer to be processed. * @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(); + } + } + }