Add repair step to clear frontend related caches

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl 2018-01-26 17:46:42 +01:00
parent 79e81c15c5
commit 7381a81d30
No known key found for this signature in database
GPG Key ID: 4C614C6ED2CDE6DF
5 changed files with 118 additions and 6 deletions

View File

@ -33,6 +33,7 @@ namespace OC;
use OC\App\AppStore\Bundles\BundleFetcher; use OC\App\AppStore\Bundles\BundleFetcher;
use OC\Files\AppData\Factory; use OC\Files\AppData\Factory;
use OC\Repair\CleanTags; use OC\Repair\CleanTags;
use OC\Repair\ClearFrontendCaches;
use OC\Repair\Collation; use OC\Repair\Collation;
use OC\Repair\MoveUpdaterStepFile; use OC\Repair\MoveUpdaterStepFile;
use OC\Repair\NC11\FixMountStorages; use OC\Repair\NC11\FixMountStorages;
@ -45,6 +46,8 @@ use OC\Repair\NC13\RepairInvalidPaths;
use OC\Repair\SqliteAutoincrement; use OC\Repair\SqliteAutoincrement;
use OC\Repair\RepairMimeTypes; use OC\Repair\RepairMimeTypes;
use OC\Repair\RepairInvalidShares; use OC\Repair\RepairInvalidShares;
use OC\Template\JSCombiner;
use OC\Template\SCSSCacher;
use OCP\AppFramework\QueryException; use OCP\AppFramework\QueryException;
use OCP\Migration\IOutput; use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep; use OCP\Migration\IRepairStep;
@ -131,6 +134,7 @@ class Repair implements IOutput{
new FixMountStorages(\OC::$server->getDatabaseConnection()), new FixMountStorages(\OC::$server->getDatabaseConnection()),
new RepairInvalidPaths(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()), new RepairInvalidPaths(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()),
new AddLogRotateJob(\OC::$server->getJobList()), new AddLogRotateJob(\OC::$server->getJobList()),
new ClearFrontendCaches(\OC::$server->getMemCacheFactory(), \OC::$server->query(SCSSCacher::class), \OC::$server->query(JSCombiner::class))
]; ];
} }

View File

@ -0,0 +1,78 @@
<?php
/**
* @copyright Copyright (c) 2018 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OC\Repair;
use OC\Core\Command\Maintenance\ClearCacheJSCSS;
use OC\Template\JSCombiner;
use OC\Template\SCSSCacher;
use OCP\AppFramework\QueryException;
use OCP\ICacheFactory;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\BufferedOutput;
class ClearFrontendCaches implements IRepairStep {
/** @var ICacheFactory */
protected $cacheFactory;
/** @var SCSSCacher */
protected $scssCacher;
/** @var JSCombiner */
protected $jsCombiner;
public function __construct(ICacheFactory $cacheFactory,
SCSSCacher $SCSSCacher,
JSCombiner $JSCombiner) {
$this->cacheFactory = $cacheFactory;
$this->scssCacher = $SCSSCacher;
$this->jsCombiner = $JSCombiner;
}
public function getName() {
return 'Clear frontend caches';
}
public function run(IOutput $output) {
try {
$c = $this->cacheFactory->createDistributed('imagePath');
$c->clear('');
$output->info('Image cache cleared');
$this->scssCacher->resetCache();
$c = $this->cacheFactory->createDistributed('SCSS');
$c->clear('');
$output->info('SCSS cache cleared');
$this->jsCombiner->resetCache();
$c = $this->cacheFactory->createDistributed('JS');
$c->clear('');
$output->info('JS cache cleared');
} catch (\Exception $e) {
$output->warning('Unable to clear the frontend cache');
}
}
}

View File

@ -112,6 +112,7 @@ use OC\Share20\ProviderFactory;
use OC\Share20\ShareHelper; use OC\Share20\ShareHelper;
use OC\SystemTag\ManagerFactory as SystemTagManagerFactory; use OC\SystemTag\ManagerFactory as SystemTagManagerFactory;
use OC\Tagging\TagMapper; use OC\Tagging\TagMapper;
use OC\Template\JSCombiner;
use OC\Template\SCSSCacher; use OC\Template\SCSSCacher;
use OCA\Theming\ThemingDefaults; use OCA\Theming\ThemingDefaults;
@ -967,6 +968,17 @@ class Server extends ServerContainer implements IServerContainer {
$cacheFactory->createDistributed('SCSS') $cacheFactory->createDistributed('SCSS')
); );
}); });
$this->registerService(JSCombiner::class, function (Server $c) {
/** @var Factory $cacheFactory */
$cacheFactory = $c->query(Factory::class);
return new JSCombiner(
$c->getAppDataDir('js'),
$c->getURLGenerator(),
$cacheFactory->createDistributed('JS'),
$c->getSystemConfig(),
$c->getLogger()
);
});
$this->registerService(EventDispatcher::class, function () { $this->registerService(EventDispatcher::class, function () {
return new EventDispatcher(); return new EventDispatcher();
}); });

View File

@ -184,9 +184,10 @@ class JSCombiner {
$depFile->putContent($deps); $depFile->putContent($deps);
$this->depsCache->set($folder->getName() . '-' . $depFileName, $deps); $this->depsCache->set($folder->getName() . '-' . $depFileName, $deps);
$gzipFile->putContent(gzencode($res, 9)); $gzipFile->putContent(gzencode($res, 9));
$this->logger->debug('JSCombiner: successfully cached: ' . $fileName);
return true; return true;
} catch (NotPermittedException $e) { } catch (NotPermittedException $e) {
$this->logger->error('JSCombiner: unable to cache: ' . $fileName);
return false; return false;
} }
} }
@ -227,4 +228,22 @@ class JSCombiner {
return $result; return $result;
} }
/**
* Clear cache with combined javascript files
*
* @throws NotFoundException
*/
public function resetCache() {
$appDirectory = $this->appData->getDirectoryListing();
if(empty($appDirectory)){
return;
}
foreach ($appDirectory as $folder) {
foreach ($folder->getDirectoryListing() as $file) {
$file->delete();
}
}
}
} }

View File

@ -249,9 +249,10 @@ class SCSSCacher {
$depFile->putContent($deps); $depFile->putContent($deps);
$this->depsCache->set($folder->getName() . '-' . $depFileName, $deps); $this->depsCache->set($folder->getName() . '-' . $depFileName, $deps);
$gzipFile->putContent(gzencode($data, 9)); $gzipFile->putContent(gzencode($data, 9));
$this->logger->debug($webDir.'/'.$fileNameSCSS.' compiled and successfully cached', ['app' => 'core']); $this->logger->debug('SCSSCacher: '.$webDir.'/'.$fileNameSCSS.' compiled and successfully cached', ['app' => 'core']);
return true; return true;
} catch(NotPermittedException $e) { } catch(NotPermittedException $e) {
$this->logger->error('SCSSCacher: unable to cache: ' . $fileNameSCSS);
return false; return false;
} }
} }
@ -260,7 +261,7 @@ class SCSSCacher {
* Reset scss cache by deleting all generated css files * Reset scss cache by deleting all generated css files
* We need to regenerate all files when variables change * We need to regenerate all files when variables change
*/ */
private function resetCache() { public function resetCache() {
$this->injectedVariables = null; $this->injectedVariables = null;
$appDirectory = $this->appData->getDirectoryListing(); $appDirectory = $this->appData->getDirectoryListing();
if(empty($appDirectory)){ if(empty($appDirectory)){
@ -268,12 +269,10 @@ class SCSSCacher {
} }
foreach ($appDirectory as $folder) { foreach ($appDirectory as $folder) {
foreach ($folder->getDirectoryListing() as $file) { foreach ($folder->getDirectoryListing() as $file) {
if (substr($file->getName(), -3) === 'css' || substr($file->getName(), -4) === 'deps') {
$file->delete(); $file->delete();
} }
} }
} }
}
/** /**
* @return string SCSS code for variables from OC_Defaults * @return string SCSS code for variables from OC_Defaults