Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache fetched apps in update check #7264

Merged
merged 3 commits into from
Nov 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions apps/updatenotification/lib/Notification/BackgroundJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class BackgroundJob extends TimedJob {
/** @var IClientService */
protected $client;

/** @var Installer */
protected $installer;

/** @var string[] */
protected $users;

Expand All @@ -64,8 +67,9 @@ class BackgroundJob extends TimedJob {
* @param IGroupManager $groupManager
* @param IAppManager $appManager
* @param IClientService $client
* @param Installer $installer
*/
public function __construct(IConfig $config, IManager $notificationManager, IGroupManager $groupManager, IAppManager $appManager, IClientService $client) {
public function __construct(IConfig $config, IManager $notificationManager, IGroupManager $groupManager, IAppManager $appManager, IClientService $client, Installer $installer) {
// Run once a day
$this->setInterval(60 * 60 * 24);

Expand All @@ -74,6 +78,7 @@ public function __construct(IConfig $config, IManager $notificationManager, IGro
$this->groupManager = $groupManager;
$this->appManager = $appManager;
$this->client = $client;
$this->installer = $installer;
}

protected function run($argument) {
Expand Down Expand Up @@ -257,6 +262,6 @@ protected function getChannel() {
* @return string|false
*/
protected function isUpdateAvailable($app) {
return Installer::isUpdateAvailable($app, \OC::$server->getAppFetcher());
return $this->installer->isUpdateAvailable($app);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
namespace OCA\UpdateNotification\Tests\Notification;


use OC\Installer;
use OCA\UpdateNotification\Notification\BackgroundJob;
use OCP\App\IAppManager;
use OCP\Http\Client\IClientService;
Expand All @@ -47,6 +48,8 @@ class BackgroundJobTest extends TestCase {
protected $appManager;
/** @var IClientService|\PHPUnit_Framework_MockObject_MockObject */
protected $client;
/** @var Installer|\PHPUnit_Framework_MockObject_MockObject */
protected $installer;

public function setUp() {
parent::setUp();
Expand All @@ -56,6 +59,7 @@ public function setUp() {
$this->groupManager = $this->createMock(IGroupManager::class);
$this->appManager = $this->createMock(IAppManager::class);
$this->client = $this->createMock(IClientService::class);
$this->installer = $this->createMock(Installer::class);
}

/**
Expand All @@ -69,7 +73,8 @@ protected function getJob(array $methods = []) {
$this->notificationManager,
$this->groupManager,
$this->appManager,
$this->client
$this->client,
$this->installer
);
} {
return $this->getMockBuilder(BackgroundJob::class)
Expand All @@ -79,6 +84,7 @@ protected function getJob(array $methods = []) {
$this->groupManager,
$this->appManager,
$this->client,
$this->installer,
])
->setMethods($methods)
->getMock();
Expand Down
8 changes: 1 addition & 7 deletions core/Command/App/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
}

try {
$installer = new Installer(
\OC::$server->getAppFetcher(),
\OC::$server->getHTTPClientService(),
\OC::$server->getTempManager(),
\OC::$server->getLogger(),
\OC::$server->getConfig()
);
$installer = \OC::$server->query(Installer::class);
$installer->downloadApp($appId);
$result = $installer->installApp($appId);
} catch(\Exception $e) {
Expand Down
13 changes: 10 additions & 3 deletions core/Command/Maintenance/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
namespace OC\Core\Command\Maintenance;

use InvalidArgumentException;
use OC\Installer;
use OC\Setup;
use OC\SystemConfig;
use OCP\Defaults;
Expand Down Expand Up @@ -73,9 +74,15 @@ protected function execute(InputInterface $input, OutputInterface $output) {

// validate the environment
$server = \OC::$server;
$setupHelper = new Setup($this->config, $server->getIniWrapper(),
$server->getL10N('lib'), $server->query(Defaults::class), $server->getLogger(),
$server->getSecureRandom());
$setupHelper = new Setup(
$this->config,
$server->getIniWrapper(),
$server->getL10N('lib'),
$server->query(Defaults::class),
$server->getLogger(),
$server->getSecureRandom(),
\OC::$server->query(Installer::class)
);
$sysInfo = $setupHelper->getSystemInfo(true);
$errors = $sysInfo['errors'];
if (count($errors) > 0) {
Expand Down
8 changes: 6 additions & 2 deletions core/Command/Upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
namespace OC\Core\Command;

use OC\Console\TimestampFormatter;
use OC\Installer;
use OC\Updater;
use OCP\IConfig;
use OCP\ILogger;
Expand Down Expand Up @@ -63,11 +64,13 @@ class Upgrade extends Command {
/**
* @param IConfig $config
* @param ILogger $logger
* @param Installer $installer
*/
public function __construct(IConfig $config, ILogger $logger) {
public function __construct(IConfig $config, ILogger $logger, Installer $installer) {
parent::__construct();
$this->config = $config;
$this->logger = $logger;
$this->installer = $installer;
}

protected function configure() {
Expand Down Expand Up @@ -101,7 +104,8 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$updater = new Updater(
$this->config,
\OC::$server->getIntegrityCodeChecker(),
$this->logger
$this->logger,
$this->installer
);

if ($input->getOption('no-app-disable')) {
Expand Down
3 changes: 2 additions & 1 deletion core/ajax/update.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ public function handleRepairFeedback($event) {
$updater = new \OC\Updater(
$config,
\OC::$server->getIntegrityCodeChecker(),
$logger
$logger,
\OC::$server->query(\OC\Installer::class)
);
$incompatibleApps = [];
$disabledThirdPartyApps = [];
Expand Down
2 changes: 1 addition & 1 deletion core/register_command.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
$application->add(new OC\Core\Command\Maintenance\UpdateHtaccess());
$application->add(new OC\Core\Command\Maintenance\UpdateTheme(\OC::$server->getMimeTypeDetector(), \OC::$server->getMemCacheFactory()));

$application->add(new OC\Core\Command\Upgrade(\OC::$server->getConfig(), \OC::$server->getLogger()));
$application->add(new OC\Core\Command\Upgrade(\OC::$server->getConfig(), \OC::$server->getLogger(), \OC::$server->query(\OC\Installer::class)));
$application->add(new OC\Core\Command\Maintenance\Repair(
new \OC\Repair(\OC\Repair::getRepairSteps(), \OC::$server->getEventDispatcher()), \OC::$server->getConfig(),
\OC::$server->getEventDispatcher(), \OC::$server->getAppManager()));
Expand Down
12 changes: 9 additions & 3 deletions lib/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -915,9 +915,15 @@ public static function handleRequest() {
// Check if Nextcloud is installed or in maintenance (update) mode
if (!$systemConfig->getValue('installed', false)) {
\OC::$server->getSession()->clear();
$setupHelper = new OC\Setup(\OC::$server->getSystemConfig(), \OC::$server->getIniWrapper(),
\OC::$server->getL10N('lib'), \OC::$server->query(\OCP\Defaults::class), \OC::$server->getLogger(),
\OC::$server->getSecureRandom());
$setupHelper = new OC\Setup(
\OC::$server->getSystemConfig(),
\OC::$server->getIniWrapper(),
\OC::$server->getL10N('lib'),
\OC::$server->query(\OCP\Defaults::class),
\OC::$server->getLogger(),
\OC::$server->getSecureRandom(),
\OC::$server->query(\OC\Installer::class)
);
$controller = new OC\Core\Controller\SetupController($setupHelper);
$controller->run($_POST);
exit();
Expand Down
25 changes: 14 additions & 11 deletions lib/private/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ class Installer {
private $logger;
/** @var IConfig */
private $config;
/** @var array - for caching the result of app fetcher */
private $apps = null;
/** @var bool|null - for caching the result of the ready status */
private $isInstanceReadyForUpdates = null;

/**
* @param AppFetcher $appFetcher
Expand Down Expand Up @@ -187,7 +191,7 @@ public static function isInstalled( $app ) {
* @return bool
*/
public function updateAppstoreApp($appId) {
if(self::isUpdateAvailable($appId, $this->appFetcher)) {
if($this->isUpdateAvailable($appId)) {
try {
$this->downloadApp($appId);
} catch (\Exception $e) {
Expand Down Expand Up @@ -375,27 +379,26 @@ public function downloadApp($appId) {
* Check if an update for the app is available
*
* @param string $appId
* @param AppFetcher $appFetcher
* @return string|false false or the version number of the update
*/
public static function isUpdateAvailable($appId,
AppFetcher $appFetcher) {
static $isInstanceReadyForUpdates = null;

if ($isInstanceReadyForUpdates === null) {
public function isUpdateAvailable($appId) {
if ($this->isInstanceReadyForUpdates === null) {
$installPath = OC_App::getInstallPath();
if ($installPath === false || $installPath === null) {
$isInstanceReadyForUpdates = false;
$this->isInstanceReadyForUpdates = false;
} else {
$isInstanceReadyForUpdates = true;
$this->isInstanceReadyForUpdates = true;
}
}

if ($isInstanceReadyForUpdates === false) {
if ($this->isInstanceReadyForUpdates === false) {
return false;
}

$apps = $appFetcher->get();
if ($this->apps === null) {
$apps = $this->appFetcher->get();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obviously I failed here too. 🙈 cc @rullzer @skjnldsv Let me fix that.

}

foreach($apps as $app) {
if($app['id'] === $appId) {
$currentVersion = OC_App::getAppVersion($appId);
Expand Down
10 changes: 10 additions & 0 deletions lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,16 @@ public function __construct($webRoot, \OC\Config $config) {
$c->query(\OCP\Share\IManager::class)
);
});

$this->registerService(Installer::class, function(Server $c) {
return new Installer(
$c->getAppFetcher(),
$c->getHTTPClientService(),
$c->getTempManager(),
$c->getLogger(),
$c->getConfig()
);
});
}

/**
Expand Down
28 changes: 16 additions & 12 deletions lib/private/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class Setup {
protected $logger;
/** @var ISecureRandom */
protected $random;
/** @var Installer */
protected $installer;

/**
* @param SystemConfig $config
Expand All @@ -73,20 +75,23 @@ class Setup {
* @param Defaults $defaults
* @param ILogger $logger
* @param ISecureRandom $random
* @param Installer $installer
*/
public function __construct(SystemConfig $config,
IniGetWrapper $iniWrapper,
IL10N $l10n,
Defaults $defaults,
ILogger $logger,
ISecureRandom $random
ISecureRandom $random,
Installer $installer
) {
$this->config = $config;
$this->iniWrapper = $iniWrapper;
$this->l10n = $l10n;
$this->defaults = $defaults;
$this->logger = $logger;
$this->random = $random;
$this->installer = $installer;
}

static protected $dbSetupClasses = [
Expand Down Expand Up @@ -371,18 +376,11 @@ public function install($options) {

// Install shipped apps and specified app bundles
Installer::installShippedApps();
$installer = new Installer(
\OC::$server->getAppFetcher(),
\OC::$server->getHTTPClientService(),
\OC::$server->getTempManager(),
\OC::$server->getLogger(),
\OC::$server->getConfig()
);
$bundleFetcher = new BundleFetcher(\OC::$server->getL10N('lib'));
$defaultInstallationBundles = $bundleFetcher->getDefaultInstallationBundle();
foreach($defaultInstallationBundles as $bundle) {
try {
$installer->installAppBundle($bundle);
$this->installer->installAppBundle($bundle);
} catch (Exception $e) {}
}

Expand Down Expand Up @@ -444,9 +442,15 @@ public static function updateHtaccess() {
$webRoot = !empty(\OC::$WEBROOT) ? \OC::$WEBROOT : '/';
}

$setupHelper = new \OC\Setup($config, \OC::$server->getIniWrapper(),
\OC::$server->getL10N('lib'), \OC::$server->query(Defaults::class), \OC::$server->getLogger(),
\OC::$server->getSecureRandom());
$setupHelper = new \OC\Setup(
$config,
\OC::$server->getIniWrapper(),
\OC::$server->getL10N('lib'),
\OC::$server->query(Defaults::class),
\OC::$server->getLogger(),
\OC::$server->getSecureRandom(),
\OC::$server->query(Installer::class)
);

$htaccessContent = file_get_contents($setupHelper->pathToHtaccess());
$content = "#### DO NOT CHANGE ANYTHING ABOVE THIS LINE ####\n";
Expand Down
19 changes: 9 additions & 10 deletions lib/private/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ class Updater extends BasicEmitter {
/** @var Checker */
private $checker;

/** @var Installer */
private $installer;

/** @var bool */
private $skip3rdPartyAppsDisable;

Expand All @@ -78,13 +81,16 @@ class Updater extends BasicEmitter {
* @param IConfig $config
* @param Checker $checker
* @param ILogger $log
* @param Installer $installer
*/
public function __construct(IConfig $config,
Checker $checker,
ILogger $log = null) {
ILogger $log = null,
Installer $installer) {
$this->log = $log;
$this->config = $config;
$this->checker = $checker;
$this->installer = $installer;

// If at least PHP 7.0.0 is used we don't need to disable apps as we catch
// fatal errors and exceptions and disable the app just instead.
Expand Down Expand Up @@ -461,17 +467,10 @@ private function isCodeUpgrade() {
private function upgradeAppStoreApps(array $disabledApps) {
foreach($disabledApps as $app) {
try {
$installer = new Installer(
\OC::$server->getAppFetcher(),
\OC::$server->getHTTPClientService(),
\OC::$server->getTempManager(),
$this->log,
\OC::$server->getConfig()
);
$this->emit('\OC\Updater', 'checkAppStoreAppBefore', [$app]);
if (Installer::isUpdateAvailable($app, \OC::$server->getAppFetcher())) {
if ($this->installer->isUpdateAvailable($app)) {
$this->emit('\OC\Updater', 'upgradeAppStoreApp', [$app]);
$installer->updateAppstoreApp($app);
$this->installer->updateAppstoreApp($app);
}
$this->emit('\OC\Updater', 'checkAppStoreApp', [$app]);
} catch (\Exception $ex) {
Expand Down
Loading