Add temporary fix for race conditions
[MAILPOET-2436]
This commit is contained in:
committed by
Jack Kitterhing
parent
826ba18e14
commit
d3aecc718d
@ -109,7 +109,7 @@ class SettingsController {
|
||||
}
|
||||
}
|
||||
$setting[$last_key] = $value;
|
||||
$this->saveValue($main_key, $this->settings[$main_key]);
|
||||
$this->settings_repository->createOrUpdateByName($main_key, $this->settings[$main_key]);
|
||||
}
|
||||
|
||||
function delete($key) {
|
||||
@ -150,17 +150,6 @@ class SettingsController {
|
||||
return $setting ? $setting->getValue() : null;
|
||||
}
|
||||
|
||||
private function saveValue($key, $value) {
|
||||
$setting = $this->settings_repository->findOneByName($key);
|
||||
if (!$setting) {
|
||||
$setting = new SettingEntity();
|
||||
$setting->setName($key);
|
||||
$this->settings_repository->persist($setting);
|
||||
}
|
||||
$setting->setValue($value);
|
||||
$this->settings_repository->flush();
|
||||
}
|
||||
|
||||
function resetCache() {
|
||||
$this->settings = [];
|
||||
$this->loaded = false;
|
||||
|
@ -2,8 +2,10 @@
|
||||
|
||||
namespace MailPoet\Settings;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use MailPoet\Doctrine\Repository;
|
||||
use MailPoet\Entities\SettingEntity;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
|
||||
/**
|
||||
* @method SettingEntity[] findBy(array $criteria, array $order_by = null, int $limit = null, int $offset = null)
|
||||
@ -18,6 +20,24 @@ class SettingsRepository extends Repository {
|
||||
return $this->doctrine_repository->findOneBy(['name' => $name]);
|
||||
}
|
||||
|
||||
function createOrUpdateByName($name, $value) {
|
||||
// Temporarily use low-level INSERT ... ON DUPLICATE KEY UPDATE query to avoid race conditions
|
||||
// between entity fetch and creation with multiple concurrent requests. This will be replaced
|
||||
// by a code solving atomicity of create-or-update on entity (ORM) level in a follow-up ticket.
|
||||
$now = Carbon::createFromTimestamp(WPFunctions::get()->currentTime('timestamp'));
|
||||
$table_name = $this->entity_manager->getClassMetadata(SettingEntity::class)->getTableName();
|
||||
$this->entity_manager->getConnection()->executeUpdate("
|
||||
INSERT INTO $table_name (name, value, created_at, updated_at)
|
||||
VALUES (:name, :value, :now, :now)
|
||||
ON DUPLICATE KEY UPDATE value = :value, updated_at = :now
|
||||
", [
|
||||
'name' => $name,
|
||||
'value' => is_array($value) ? serialize($value) : $value,
|
||||
'now' => $now,
|
||||
]);
|
||||
$this->entity_manager->clear(SettingEntity::class);
|
||||
}
|
||||
|
||||
protected function getEntityClassName() {
|
||||
return SettingEntity::class;
|
||||
}
|
||||
|
Reference in New Issue
Block a user