mirror of
https://github.com/friendica/friendica
synced 2025-04-26 15:10:11 +00:00
Adding force to update routine
- Introduced Cache::NEVER Lock (never expiring lock) - Force flag for dbstructure update - Moving the business logic to central place in Update class
This commit is contained in:
parent
f2ec963b95
commit
270e817954
9 changed files with 141 additions and 104 deletions
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace Friendica\Core;
|
||||
|
||||
use Friendica\Database\DBA;
|
||||
use Friendica\Database\DBStructure;
|
||||
|
||||
class Update
|
||||
|
@ -38,9 +39,21 @@ class Update
|
|||
|
||||
/**
|
||||
* Automatic database updates
|
||||
*
|
||||
* @param bool $force Force the Update-Check even if the lock is set
|
||||
* @param bool $verbose Run the Update-Check verbose
|
||||
* @param bool $sendMail Sends a Mail to the administrator in case of success/failure
|
||||
*
|
||||
* @return string Empty string if the update is successful, error messages otherwise
|
||||
*/
|
||||
public static function run()
|
||||
public static function run($force = false, $verbose = false, $sendMail = true)
|
||||
{
|
||||
// In force mode, we release the dbupdate lock first
|
||||
// Necessary in case of an stuck update
|
||||
if ($force) {
|
||||
Lock::release('dbupdate');
|
||||
}
|
||||
|
||||
$build = Config::get('system', 'build');
|
||||
|
||||
if (empty($build) || ($build > DB_UPDATE_VERSION)) {
|
||||
|
@ -57,7 +70,8 @@ class Update
|
|||
Config::load('database');
|
||||
|
||||
// Compare the current structure with the defined structure
|
||||
if (Lock::acquire('dbupdate')) {
|
||||
// If the Lock is acquired, never release it automatically to avoid double updates
|
||||
if (Lock::acquire('dbupdate', 120, Cache::NEVER)) {
|
||||
|
||||
// run the pre_update_nnnn functions in update.php
|
||||
for ($x = $stored + 1; $x <= $current; $x++) {
|
||||
|
@ -68,14 +82,17 @@ class Update
|
|||
}
|
||||
|
||||
// update the structure in one call
|
||||
$retval = DBStructure::update(false, true);
|
||||
$retval = DBStructure::update($verbose, true);
|
||||
if ($retval) {
|
||||
DBStructure::updateFail(
|
||||
DB_UPDATE_VERSION,
|
||||
$retval
|
||||
);
|
||||
if ($sendMail) {
|
||||
self::updateFailed(
|
||||
DB_UPDATE_VERSION,
|
||||
$retval
|
||||
);
|
||||
}
|
||||
Lock::release('dbcheck');
|
||||
Lock::release('dbupdate');
|
||||
return;
|
||||
return $retval;
|
||||
} else {
|
||||
Config::set('database', 'last_successful_update', $current);
|
||||
Config::set('database', 'last_successful_update_time', time());
|
||||
|
@ -89,10 +106,16 @@ class Update
|
|||
}
|
||||
}
|
||||
|
||||
if ($sendMail) {
|
||||
self::updateSuccessfull($stored, $current);
|
||||
}
|
||||
|
||||
Lock::release('dbupdate');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -115,14 +138,14 @@ class Update
|
|||
// If the update fails or times-out completely you may need to
|
||||
// delete the config entry to try again.
|
||||
|
||||
if (Lock::acquire('dbupdate_function')) {
|
||||
if (Lock::acquire('dbupdate_function', 120,Cache::NEVER)) {
|
||||
|
||||
// call the specific update
|
||||
$retval = $funcname();
|
||||
|
||||
if ($retval) {
|
||||
//send the administrator an e-mail
|
||||
DBStructure::updateFail(
|
||||
self::updateFailed(
|
||||
$x,
|
||||
L10n::t('Update %s failed. See error logs.', $x)
|
||||
);
|
||||
|
@ -153,4 +176,87 @@ class Update
|
|||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* send the email and do what is needed to do on update fails
|
||||
*
|
||||
* @param int $update_id number of failed update
|
||||
* @param string $error_message error message
|
||||
*/
|
||||
private static function updateFailed($update_id, $error_message) {
|
||||
//send the administrators an e-mail
|
||||
$admin_mail_list = "'".implode("','", array_map(['Friendica\Database\DBA', 'escape'], explode(",", str_replace(" ", "", Config::get('config', 'admin_email')))))."'";
|
||||
$adminlist = q("SELECT uid, language, email FROM user WHERE email IN (%s)",
|
||||
$admin_mail_list
|
||||
);
|
||||
|
||||
// No valid result?
|
||||
if (!DBA::isResult($adminlist)) {
|
||||
logger(sprintf('Cannot notify administrators about update_id=%d, error_message=%s', $update_id, $error_message), LOGGER_INFO);
|
||||
|
||||
// Don't continue
|
||||
return;
|
||||
}
|
||||
|
||||
// every admin could had different language
|
||||
foreach ($adminlist as $admin) {
|
||||
$lang = (($admin['language'])?$admin['language']:'en');
|
||||
L10n::pushLang($lang);
|
||||
|
||||
$preamble = deindent(L10n::t("
|
||||
The friendica developers released update %s recently,
|
||||
but when I tried to install it, something went terribly wrong.
|
||||
This needs to be fixed soon and I can't do it alone. Please contact a
|
||||
friendica developer if you can not help me on your own. My database might be invalid.",
|
||||
$update_id));
|
||||
$body = L10n::t("The error message is\n[pre]%s[/pre]", $error_message);
|
||||
|
||||
notification([
|
||||
'uid' => $admin['uid'],
|
||||
'type' => SYSTEM_EMAIL,
|
||||
'to_email' => $admin['email'],
|
||||
'preamble' => $preamble,
|
||||
'body' => $body,
|
||||
'language' => $lang]
|
||||
);
|
||||
L10n::popLang();
|
||||
}
|
||||
|
||||
//try the logger
|
||||
logger("CRITICAL: Database structure update failed: ".$error_message);
|
||||
}
|
||||
|
||||
private static function updateSuccessfull($from_build, $to_build)
|
||||
{
|
||||
//send the administrators an e-mail
|
||||
$admin_mail_list = "'".implode("','", array_map(['Friendica\Database\DBA', 'escape'], explode(",", str_replace(" ", "", Config::get('config', 'admin_email')))))."'";
|
||||
$adminlist = q("SELECT uid, language, email FROM user WHERE email IN (%s)",
|
||||
$admin_mail_list
|
||||
);
|
||||
|
||||
if (DBA::isResult($adminlist)) {
|
||||
// every admin could had different language
|
||||
foreach ($adminlist as $admin) {
|
||||
$lang = (($admin['language']) ? $admin['language'] : 'en');
|
||||
L10n::pushLang($lang);
|
||||
|
||||
$preamble = deindent(L10n::t("
|
||||
The friendica database was successfully update from %s to %s.",
|
||||
$from_build, $to_build));
|
||||
|
||||
notification([
|
||||
'uid' => $admin['uid'],
|
||||
'type' => SYSTEM_EMAIL,
|
||||
'to_email' => $admin['email'],
|
||||
'preamble' => $preamble,
|
||||
'body' => $preamble,
|
||||
'language' => $lang]
|
||||
);
|
||||
L10n::popLang();
|
||||
}
|
||||
}
|
||||
|
||||
//try the logger
|
||||
logger("CRITICAL: Database structure update successful.", LOGGER_TRACE);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue