2015-06-15 21:12:37 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Class TaskManager.
|
2015-06-30 17:46:58 +02:00
|
|
|
*
|
2015-11-19 22:08:25 +01:00
|
|
|
* Background class for 'asynchronous' task management for Airtime stations
|
2015-06-15 21:12:37 +02:00
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
final class TaskManager
|
|
|
|
{
|
2015-06-15 21:12:37 +02:00
|
|
|
/**
|
2015-06-30 17:46:58 +02:00
|
|
|
* @var array tasks to be run. Maps task names to a boolean value denoting
|
|
|
|
* whether the task has been checked/run
|
2015-06-15 21:12:37 +02:00
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
private $_taskList;
|
2015-06-15 21:12:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var TaskManager singleton instance object
|
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
private static $_instance;
|
2015-06-15 21:12:37 +02:00
|
|
|
|
2015-06-16 21:10:08 +02:00
|
|
|
/**
|
2015-06-30 17:46:58 +02:00
|
|
|
* @var int TASK_INTERVAL_SECONDS how often, in seconds, to run the TaskManager tasks
|
2015-06-16 21:10:08 +02:00
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public const TASK_INTERVAL_SECONDS = 30;
|
2015-06-16 21:10:08 +02:00
|
|
|
|
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* @var PDO Propel connection object
|
2015-06-16 21:10:08 +02:00
|
|
|
*/
|
|
|
|
private $_con;
|
|
|
|
|
2015-06-15 21:12:37 +02:00
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Private constructor so class is uninstantiable.
|
2015-06-15 21:12:37 +02:00
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
private function __construct()
|
|
|
|
{
|
2015-11-19 22:08:25 +01:00
|
|
|
foreach (TaskFactory::getTasks() as $k => $task) {
|
|
|
|
$this->_taskList[$task] = false;
|
2015-09-23 02:21:19 +02:00
|
|
|
}
|
2015-06-15 21:12:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Get the singleton instance of this class.
|
2015-06-15 21:12:37 +02:00
|
|
|
*
|
|
|
|
* @return TaskManager the TaskManager instance
|
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public static function getInstance()
|
|
|
|
{
|
2015-06-15 21:12:37 +02:00
|
|
|
if (!self::$_instance) {
|
|
|
|
self::$_instance = new TaskManager();
|
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2015-06-15 21:12:37 +02:00
|
|
|
return self::$_instance;
|
|
|
|
}
|
|
|
|
|
2015-06-30 17:46:58 +02:00
|
|
|
/**
|
|
|
|
* Run a single task.
|
|
|
|
*
|
|
|
|
* @param string $taskName the ENUM name of the task to be run
|
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public function runTask($taskName)
|
|
|
|
{
|
2015-06-30 17:46:58 +02:00
|
|
|
$task = TaskFactory::getTask($taskName);
|
|
|
|
if ($task && $task->shouldBeRun()) {
|
2022-12-16 19:06:56 +01:00
|
|
|
Logging::debug("running task {$taskName}");
|
2015-06-30 17:46:58 +02:00
|
|
|
$task->run();
|
|
|
|
}
|
2022-07-07 16:47:56 +02:00
|
|
|
// Mark that the task has been checked/run.
|
|
|
|
// This is important for prioritized tasks that
|
|
|
|
// we need to run on every request (such as the
|
|
|
|
// schema check/upgrade)
|
|
|
|
$this->_taskList[$taskName] = true;
|
2015-06-30 17:46:58 +02:00
|
|
|
}
|
|
|
|
|
2015-06-15 21:12:37 +02:00
|
|
|
/**
|
2015-06-17 23:16:21 +02:00
|
|
|
* Run all tasks that need to be run.
|
|
|
|
*
|
|
|
|
* To prevent blocking and making too many requests to the database,
|
|
|
|
* we implement a row-level, non-blocking, read-protected lock on a
|
|
|
|
* timestamp that we check each time the application is bootstrapped,
|
|
|
|
* which, assuming enough time has passed, is updated before running
|
|
|
|
* the tasks.
|
2015-06-15 21:12:37 +02:00
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public function runTasks()
|
|
|
|
{
|
2015-06-16 21:10:08 +02:00
|
|
|
// If there is data in auth storage, this could be a user request
|
2015-09-23 02:21:19 +02:00
|
|
|
// so we should just return to avoid blocking
|
2015-06-17 23:16:21 +02:00
|
|
|
if ($this->_isUserSessionRequest()) {
|
|
|
|
return;
|
|
|
|
}
|
2015-06-16 21:10:08 +02:00
|
|
|
$this->_con = Propel::getConnection(CcPrefPeer::DATABASE_NAME);
|
|
|
|
$this->_con->beginTransaction();
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2015-06-16 21:10:08 +02:00
|
|
|
try {
|
|
|
|
$lock = $this->_getLock();
|
2015-10-29 14:23:41 +01:00
|
|
|
if ($lock && (microtime(true) < ($lock['valstr'] + self::TASK_INTERVAL_SECONDS))) {
|
2015-06-23 21:10:02 +02:00
|
|
|
// Propel caches the database connection and uses it persistently, so if we don't
|
|
|
|
// use commit() here, we end up blocking other queries made within this request
|
2015-06-19 00:18:48 +02:00
|
|
|
$this->_con->commit();
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2015-06-17 23:16:21 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-06-16 21:10:08 +02:00
|
|
|
$this->_updateLock($lock);
|
|
|
|
$this->_con->commit();
|
2022-07-07 16:47:56 +02:00
|
|
|
} catch (PDOException $e) {
|
2015-06-16 21:10:08 +02:00
|
|
|
// We get here if there are simultaneous requests trying to fetch the lock row
|
|
|
|
$this->_con->rollBack();
|
2022-07-07 16:47:56 +02:00
|
|
|
|
|
|
|
// Do not log 'could not obtain lock' exception
|
|
|
|
// SQLSTATE[55P03]: Lock not available: 7 ERROR: could not obtain lock on row in relation "cc_pref"
|
|
|
|
if ($e->getCode() != '55P03') {
|
|
|
|
Logging::warn($e->getMessage());
|
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2015-06-16 21:10:08 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-06-30 17:46:58 +02:00
|
|
|
foreach ($this->_taskList as $task => $hasTaskRun) {
|
|
|
|
if (!$hasTaskRun) {
|
|
|
|
$this->runTask($task);
|
2015-06-17 23:16:21 +02:00
|
|
|
}
|
2015-06-15 21:12:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-16 21:10:08 +02:00
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Check if the current session is a user request.
|
2015-06-16 21:10:08 +02:00
|
|
|
*
|
2015-06-17 23:16:21 +02:00
|
|
|
* @return bool true if there is a Zend_Auth object in the current session,
|
|
|
|
* otherwise false
|
2015-06-16 21:10:08 +02:00
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
private function _isUserSessionRequest()
|
|
|
|
{
|
2015-09-25 16:41:51 +02:00
|
|
|
if (!Zend_Session::isStarted()) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-06-16 21:10:08 +02:00
|
|
|
$auth = Zend_Auth::getInstance();
|
|
|
|
$data = $auth->getStorage()->read();
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2015-06-16 21:10:08 +02:00
|
|
|
return !empty($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Get the task_manager_lock from cc_pref with a row-level lock for atomicity.
|
2015-06-16 21:10:08 +02:00
|
|
|
*
|
2015-06-17 23:16:21 +02:00
|
|
|
* The lock is exclusive (prevent reads) and will only last for the duration
|
|
|
|
* of the transaction. We add NOWAIT so reads on the row during the transaction
|
|
|
|
* won't block
|
|
|
|
*
|
2015-06-16 21:10:08 +02:00
|
|
|
* @return array|bool an array containing the row values, or false on failure
|
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
private function _getLock()
|
|
|
|
{
|
2015-06-16 21:10:08 +02:00
|
|
|
$sql = "SELECT * FROM cc_pref WHERE keystr='task_manager_lock' LIMIT 1 FOR UPDATE NOWAIT";
|
|
|
|
$st = $this->_con->prepare($sql);
|
|
|
|
$st->execute();
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2015-06-16 21:10:08 +02:00
|
|
|
return $st->fetch();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Update and commit the new lock value, or insert it if it doesn't exist.
|
2015-06-16 21:10:08 +02:00
|
|
|
*
|
|
|
|
* @param $lock array cc_pref lock row values
|
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
private function _updateLock($lock)
|
|
|
|
{
|
2015-06-16 21:10:08 +02:00
|
|
|
$sql = empty($lock) ? "INSERT INTO cc_pref (keystr, valstr) VALUES ('task_manager_lock', :value)"
|
|
|
|
: "UPDATE cc_pref SET valstr=:value WHERE keystr='task_manager_lock'";
|
|
|
|
$st = $this->_con->prepare($sql);
|
2021-10-11 16:10:47 +02:00
|
|
|
$st->execute([':value' => microtime(true)]);
|
2015-06-16 21:10:08 +02:00
|
|
|
}
|
2015-06-15 21:12:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Interface AirtimeTask Interface for task operations.
|
2015-06-15 21:12:37 +02:00
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
interface AirtimeTask
|
|
|
|
{
|
2015-06-15 21:12:37 +02:00
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Check whether the task should be run.
|
2015-06-15 21:12:37 +02:00
|
|
|
*
|
|
|
|
* @return bool true if the task needs to be run, otherwise false
|
|
|
|
*/
|
|
|
|
public function shouldBeRun();
|
|
|
|
|
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Run the task.
|
2015-06-15 21:12:37 +02:00
|
|
|
*/
|
|
|
|
public function run();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Class CeleryTask.
|
2015-10-29 16:03:28 +01:00
|
|
|
*
|
|
|
|
* Checks the Celery broker task queue and runs callbacks for completed tasks
|
2015-06-15 21:12:37 +02:00
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
class CeleryTask implements AirtimeTask
|
|
|
|
{
|
2015-06-15 21:12:37 +02:00
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Check the ThirdPartyTrackReferences table to see if there are any pending tasks.
|
2015-06-15 21:12:37 +02:00
|
|
|
*
|
|
|
|
* @return bool true if there are pending tasks in ThirdPartyTrackReferences
|
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public function shouldBeRun()
|
|
|
|
{
|
2015-09-15 21:06:03 +02:00
|
|
|
return !CeleryManager::isBrokerTaskQueueEmpty();
|
2015-06-15 21:12:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Poll the task queue for any completed Celery tasks.
|
2015-06-15 21:12:37 +02:00
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public function run()
|
|
|
|
{
|
2015-09-15 21:06:03 +02:00
|
|
|
CeleryManager::pollBrokerTaskQueue();
|
2015-06-15 21:12:37 +02:00
|
|
|
}
|
2015-09-23 02:21:19 +02:00
|
|
|
}
|
|
|
|
|
2017-02-13 15:35:09 +01:00
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Class AutoPlaylistTask.
|
2017-02-13 15:35:09 +01:00
|
|
|
*
|
|
|
|
* Checks for shows with an autoplaylist that needs to be filled in
|
|
|
|
*/
|
|
|
|
class AutoPlaylistTask implements AirtimeTask
|
|
|
|
{
|
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Checks whether or not the autoplaylist polling interval has passed.
|
2017-02-13 15:35:09 +01:00
|
|
|
*
|
|
|
|
* @return bool true if the autoplaylist polling interval has passed
|
|
|
|
*/
|
|
|
|
public function shouldBeRun()
|
|
|
|
{
|
|
|
|
return AutoPlaylistManager::hasAutoPlaylistPollIntervalPassed();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Schedule the autoplaylist for the shows.
|
2017-02-13 15:35:09 +01:00
|
|
|
*/
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
AutoPlaylistManager::buildAutoPlaylist();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-15 20:44:17 +02:00
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Class PodcastTask.
|
2015-10-29 16:03:28 +01:00
|
|
|
*
|
|
|
|
* Checks podcasts marked for automatic ingest and downloads any new episodes
|
|
|
|
* since the task was last run
|
2015-10-15 20:44:17 +02:00
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
class PodcastTask implements AirtimeTask
|
|
|
|
{
|
2015-10-15 20:44:17 +02:00
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Check whether or not the podcast polling interval has passed.
|
2015-10-15 20:44:17 +02:00
|
|
|
*
|
|
|
|
* @return bool true if the podcast polling interval has passed
|
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public function shouldBeRun()
|
|
|
|
{
|
2015-11-17 18:26:21 +01:00
|
|
|
$overQuota = Application_Model_Systemstatus::isDiskOverQuota();
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2015-11-17 18:26:21 +01:00
|
|
|
return !$overQuota && PodcastManager::hasPodcastPollIntervalPassed();
|
2015-10-15 20:44:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Download the latest episode for all podcasts flagged for automatic ingest.
|
2015-10-15 20:44:17 +02:00
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public function run()
|
|
|
|
{
|
2015-10-15 20:44:17 +02:00
|
|
|
PodcastManager::downloadNewestEpisodes();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-29 16:03:28 +01:00
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Class ImportTask.
|
2015-10-29 16:03:28 +01:00
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
class ImportCleanupTask implements AirtimeTask
|
|
|
|
{
|
2015-10-29 16:03:28 +01:00
|
|
|
/**
|
|
|
|
* Check if there are any files that have been stuck
|
2021-10-11 16:10:47 +02:00
|
|
|
* in Pending status for over an hour.
|
2015-10-29 16:03:28 +01:00
|
|
|
*
|
|
|
|
* @return bool true if there are any files stuck pending,
|
|
|
|
* otherwise false
|
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public function shouldBeRun()
|
|
|
|
{
|
2015-10-29 16:03:28 +01:00
|
|
|
return Application_Service_MediaService::areFilesStuckInPending();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Clean up stuck imports by changing their import status to Failed.
|
2015-10-29 16:03:28 +01:00
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public function run()
|
|
|
|
{
|
2015-10-29 16:03:28 +01:00
|
|
|
Application_Service_MediaService::clearStuckPendingImports();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-21 18:54:50 +02:00
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Class StationPodcastTask.
|
2015-10-29 16:03:28 +01:00
|
|
|
*
|
2015-11-19 22:08:25 +01:00
|
|
|
* Checks the Station podcast rollover timer and resets allotted
|
2015-10-29 16:03:28 +01:00
|
|
|
* downloads if enough time has passed (default: 1 month)
|
2015-10-21 18:54:50 +02:00
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
class StationPodcastTask implements AirtimeTask
|
|
|
|
{
|
|
|
|
public const STATION_PODCAST_RESET_TIMER_SECONDS = 2.628e+6; // 1 month
|
2015-10-21 18:54:50 +02:00
|
|
|
|
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Check whether or not the download counter for the station podcast should be reset.
|
2015-10-21 18:54:50 +02:00
|
|
|
*
|
|
|
|
* @return bool true if enough time has passed
|
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public function shouldBeRun()
|
|
|
|
{
|
2015-10-21 18:54:50 +02:00
|
|
|
$lastReset = Application_Model_Preference::getStationPodcastDownloadResetTimer();
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2015-10-29 14:23:41 +01:00
|
|
|
return empty($lastReset) || (microtime(true) > ($lastReset + self::STATION_PODCAST_RESET_TIMER_SECONDS));
|
2015-10-21 18:54:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Reset the station podcast download counter.
|
2015-10-21 18:54:50 +02:00
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public function run()
|
|
|
|
{
|
2015-10-21 18:54:50 +02:00
|
|
|
Application_Model_Preference::resetStationPodcastDownloadCounter();
|
2015-10-29 14:23:41 +01:00
|
|
|
Application_Model_Preference::setStationPodcastDownloadResetTimer(microtime(true));
|
2015-10-21 18:54:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 02:21:19 +02:00
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Class TaskFactory Factory class to abstract task instantiation.
|
2015-09-23 02:21:19 +02:00
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
class TaskFactory
|
|
|
|
{
|
2015-09-23 02:21:19 +02:00
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Check if the class with the given name implements AirtimeTask.
|
2015-11-19 22:08:25 +01:00
|
|
|
*
|
|
|
|
* @param $c string class name
|
|
|
|
*
|
|
|
|
* @return bool true if the class $c implements AirtimeTask
|
2015-09-23 02:21:19 +02:00
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
private static function _isTask($c)
|
|
|
|
{
|
2017-03-11 22:49:05 +01:00
|
|
|
return array_key_exists('AirtimeTask', class_implements($c));
|
2015-11-19 22:08:25 +01:00
|
|
|
}
|
2015-09-23 02:21:19 +02:00
|
|
|
|
2015-11-19 22:08:25 +01:00
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Filter all declared classes to get all classes implementing the AirtimeTask interface.
|
2015-11-19 22:08:25 +01:00
|
|
|
*
|
|
|
|
* @return array all classes implementing the AirtimeTask interface
|
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public static function getTasks()
|
|
|
|
{
|
|
|
|
return array_filter(get_declared_classes(), [__CLASS__, '_isTask']);
|
2015-11-19 22:08:25 +01:00
|
|
|
}
|
2015-09-23 02:21:19 +02:00
|
|
|
|
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Get an AirtimeTask based on class name.
|
2015-09-23 02:21:19 +02:00
|
|
|
*
|
2015-11-27 22:48:44 +01:00
|
|
|
* @param $task string name of the class implementing AirtimeTask to construct
|
2015-09-23 02:21:19 +02:00
|
|
|
*
|
2021-10-11 16:10:47 +02:00
|
|
|
* @return null|AirtimeTask return a task of the given type or null if no corresponding task exists
|
2015-09-23 02:21:19 +02:00
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public static function getTask($task)
|
|
|
|
{
|
2015-11-27 22:48:44 +01:00
|
|
|
// Try to get a valid class name from the given string
|
2021-10-11 16:10:47 +02:00
|
|
|
if (!class_exists($task)) {
|
|
|
|
$task = str_replace(' ', '', ucwords($task)) . 'Task';
|
|
|
|
}
|
|
|
|
|
2015-11-27 22:48:44 +01:00
|
|
|
return class_exists($task) ? new $task() : null;
|
2015-09-23 02:21:19 +02:00
|
|
|
}
|
|
|
|
}
|