public static function run($fileName, $callback) { $logger = new Logger($fileName); try { SingleInstance::run($fileName); } catch (InstanceAlreadyRunningException $e) { $logger->log('Already running'); self::$finished = true; exit(1); } $logger->log('Working'); register_shutdown_function([get_class(), 'shutdown'], $logger); try { $callback($logger); } catch (Exception $e) { $logger->log($e); $logger->log('Finished with errors'); self::$finished = true; exit(1); } $logger->log('Finished'); self::$finished = true; exit(0); }
<?php require_once __DIR__ . '/../src/core.php'; CronRunner::run(__FILE__, function ($logger) { $userCount = Model_User::getCount(); $userCountActive = Model_User::getCountActive(); $mediaCount = []; $distArr = []; foreach (Media::getConstList() as $media) { $distArr[$media] = []; $mediaCount[$media] = Model_Media::getCount($media); } foreach (Media::getConstList() as $media) { $localDist = Model_MixedUserMedia::getRatingDistribution($media); foreach ($localDist->getGroupsKeys() as $key) { if (!isset($distArr[$media][$key])) { $distArr[$media][$key] = 0; } $distArr[$media][$key] += $localDist->getGroupSize($key); } } $globalsCache = ['user-count' => $userCount, 'user-count-active' => $userCountActive, 'media-count' => $mediaCount, 'rating-dist' => $distArr]; TextHelper::putJson(Config::$globalsCachePath, $globalsCache); });
<?php require_once __DIR__ . '/../src/core.php'; CronRunner::run(__FILE__, function ($logger) { $cache = new Cache(); $allFiles = $cache->getAllFiles(); $usedFiles = $cache->getUsedFiles(); $unusedFiles = array_diff($allFiles, $usedFiles); foreach ($unusedFiles as $path) { unlink($path); } $logger->log('Deleted: %s, left: %d', count($unusedFiles), count($usedFiles)); });
CronRunner::run(__FILE__, function ($logger) { $userProcessor = new UserProcessor(); $mediaProcessors = [Media::Anime => new AnimeProcessor(), Media::Manga => new MangaProcessor()]; $userQueue = new Queue(Config::$userQueuePath); $userQueueSize = (new Queue(Config::$userQueuePath))->size(); $mediaQueue = new Queue(Config::$mediaQueuePath); if ($userQueueSize > 30) { Config::$usersPerCronRun = Config::$usersPerCronRunMore; Config::$mediaPerCronRun = Config::$mediaPerCronRunMore; } Downloader::setLogger($logger); #process users processQueue($userQueue, Config::$usersPerCronRun, Config::$userQueueMaxAttempts, $logger, function ($userName) use($userProcessor, $mediaQueue, $logger) { Database::selectUser($userName); $logger->log('Processing user %s... ', $userName); #process the user $userContext = $userProcessor->process($userName); #remove associated cache $cache = new Cache(); $cache->setPrefix($userName); foreach ($cache->getAllFiles() as $path) { unlink($path); } #append media to queue $mediaIds = []; foreach (Media::getConstList() as $media) { foreach ($userContext->user->getMixedUserMedia($media) as $entry) { $mediaAge = time() - strtotime($entry->processed); if ($mediaAge > Config::$mediaQueueMinWait) { $mediaIds[] = TextHelper::serializeMediaId($entry); } } } $mediaQueue->enqueueMultiple(array_map(function ($mediaId) { return new QueueItem($mediaId); }, $mediaIds)); $logger->log('ok'); }); $mediaIds = []; foreach (Media::getConstList() as $media) { $entries = Model_Media::getOldest($media, 100); foreach ($entries as $entry) { $mediaAge = time() - strtotime($entry->processed); if ($mediaAge > Config::$mediaQueueMinWait) { $mediaIds[] = TextHelper::serializeMediaId($entry); } } } $mediaQueue->enqueueMultiple(array_map(function ($mediaId) { return new QueueItem($mediaId); }, $mediaIds)); #process media processQueue($mediaQueue, Config::$mediaPerCronRun, Config::$mediaQueueMaxAttempts, $logger, function ($key) use($mediaProcessors, $logger) { list($media, $malId) = TextHelper::deserializeMediaId($key); $logger->log('Processing %s #%d... ', Media::toString($media), $malId); #process the media $mediaProcessors[$media]->process($malId); $logger->log('ok'); }); });
<?php require_once __DIR__ . '/../src/core.php'; CronRunner::run(__FILE__, function ($logger) { $limit = 2 * 24 * 60 / 5; $queueSizes = TextHelper::loadJson(Config::$userQueueSizesPath, true); $userQueue = new Queue(Config::$userQueuePath); $mediaQueue = new Queue(Config::$mediaQueuePath); $key = date('c'); $queueSizes[$key] = [$userQueue->size(), $mediaQueue->size()]; ksort($queueSizes, SORT_NATURAL | SORT_FLAG_CASE); while (count($queueSizes) > $limit) { array_shift($queueSizes); } TextHelper::putJson(Config::$userQueueSizesPath, $queueSizes); });