Commit b863e82a by Vladislav

#22080 Физически удалены РК с целевых токенах (Решение блокировок в goal_advertisements)

1 parent 7e55342f
<?php
namespace App\Database;
use App\Database\MySQL\AutoRetryMySqlConnection;
use Illuminate\Database\Connectors\ConnectionFactory;
use Illuminate\Database\Connection;
/**
* Class DatabaseConnectionFactory
*
* @package App\Database
*/
class DatabaseConnectionFactory extends ConnectionFactory
{
/**
* Create a new connection instance.
*
* @param string $driver
* @param \PDO|\Closure $connection
* @param string $database
* @param string $prefix
* @param array $config
* @return \Illuminate\Database\Connection
*
* @throws \InvalidArgumentException
*/
protected function createConnection($driver, $connection, $database, $prefix = '', array $config = [])
{
if ($driver !== 'mysql') {
return parent::createConnection($driver, $connection, $database, $prefix, $config);
}
if ($resolver = Connection::getResolver($driver)) {
return $resolver($connection, $database, $prefix, $config);
}
return new AutoRetryMySqlConnection($connection, $database, $prefix, $config);
}
}
<?php
namespace App\Database\MySQL;
use Closure;
use Illuminate\Database\MySqlConnection;
use Illuminate\Database\QueryException;
use Log;
/**
* Class AutoRetryMySqlConnection
*
* @package App\Helpers
*/
class AutoRetryMySqlConnection extends MySqlConnection
{
/**
* Error code of deadlock exception
*/
const DEADLOCK_ERROR_CODE = 40001;
/**
* Number of attempts to retry
*/
const ATTEMPTS_COUNT = 10;
/**
* Run a SQL statement.
*
* @param string $query
* @param array $bindings
* @param \Closure $callback
* @return mixed
*
* @throws \Illuminate\Database\QueryException
*/
protected function runQueryCallback($query, $bindings, Closure $callback)
{
$attempts_count = self::ATTEMPTS_COUNT;
for ($attempt = 1; $attempt <= $attempts_count; $attempt++) {
try {
return parent::runQueryCallback($query, $bindings, $callback);
} catch (QueryException $e) {
if ($attempt > $attempts_count) {
throw $e;
}
if (!$this->shouldRetry($errorCode = $e->getCode())) {
throw $e;
}
$this->logRetry($attempt, $attempts_count, $bindings, $query, $errorCode);
}
}
}
/**
* Use the provided error code to determine if the transaction should be retried.
*
* @param string|integer $errorCode
*
* @return boolean
*/
protected function shouldRetry($errorCode)
{
return (int)$errorCode === self::DEADLOCK_ERROR_CODE;
}
/**
* Log when a transaction is automatically retried.
*
* @param integer $attempt
* @param integer $attempts_count
* @param array $bindings
* @param string $query
* @param string $errorCode
* @return void
*/
protected function logRetry($attempt, $attempts_count, $bindings, $query, $errorCode)
{
Log::warning("Transaction has been restarted due to error {$errorCode}. Attempt {$attempt}/{$attempts_count}. SQL: {$query} " . print_r($bindings, true));
}
}
......@@ -537,7 +537,7 @@ class DictionaryCampaign extends Pivot
public function goalVCardsForNotExternalForNotReserveCreate()
{
return $this->goalVCards()->forNotExternal()->forNotReserveCreate();
return $this->goalVCards()->forNotExternal()->forNotReserveCreate()->whereNotNull('dictionary_campaign_external_id');
}
public function goalVCardsForNeedDeleteForNotReserveDelete()
......
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Database\DatabaseConnectionFactory;
/**
* Class DatabaseServiceProvider
*
* @package App\Providers
*/
class DatabaseServiceProvider extends ServiceProvider
{
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->singleton('db.factory', function ($app) {
return new DatabaseConnectionFactory($app);
});
}
}
......@@ -48,6 +48,7 @@ class ArchiveAds extends DirectRequest
'status_clarification' => 'Archived.',
'archived_need' => null,
'reserve_archive_at' => null,
'reserve_delete_at' => null,
]);
} else {
GoalAdvertisement::whereIn('external_id', $external_ids)
......@@ -55,6 +56,7 @@ class ArchiveAds extends DirectRequest
'archive_at' => Carbon::now(),
'archived_need' => null,
'reserve_archive_at' => null,
'reserve_delete_at' => null,
]);
}
}
......
......@@ -83,12 +83,14 @@ class DeleteAds extends DirectRequest
if ($this->getToken()->isMain()) {
Advertisement::where('external_id', $external_id)
->update([
'archived_need' => Carbon::now()
'archived_need' => Carbon::now(),
'reserve_delete_at' => null,
]);
} else {
GoalAdvertisement::where('external_id', $external_id)
->update([
'archived_need' => Carbon::now()
'archived_need' => Carbon::now(),
'reserve_delete_at' => null,
]);
}
......
......@@ -64,19 +64,20 @@ class UpdateAds extends DirectRequest
if (isset($update_result['Errors'][0]['Details']) && $update_result['Errors'][0]['Details'] === 'You cannot update an archived ad') {
GoalAdvertisement::whereExternalId($external_id)
->update([
'deleted_need' => Carbon::now(),
'archived_need' => null,
'reserve_archive_at' => null,
'archive_at' => Carbon::now(),
'updated_need' => null,
'reserve_update_at' => null,
]);
continue;
} elseif (isset($update_result['Errors'][0]['Details']) && $update_result['Errors'][0]['Details'] === 'Ad not found') {
$model = GoalAdvertisement::whereExternalId($external_id)->first();
if ($model) {
$model->goalAdGroup()->delete();
$model->delete();
}
// if ($model) {
// $model->goalAdGroup()->delete();
// $model->delete();
// }
GoalAdvertisement::whereExternalId($external_id)
->delete();
continue;
......
......@@ -174,6 +174,7 @@ return [
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
App\Providers\DatabaseServiceProvider::class,
Clockwork\Support\Laravel\ClockworkServiceProvider::class,
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!