
POST /api/v1/sponsored-projects payload name description is_active scope REALM_URL/sponsored-projects/write GET /api/v1/sponsored-projects scope REALM_URL/sponsored-projects/read PUT /api/v1/sponsored-projects/{id} payload name description is_active scope REALM_URL/sponsored-projects/write GET /api/v1/sponsored-projects/{id} scope REALM_URL/sponsored-projects/read PUBLIC GET /api/public/v1/sponsored-projects/{slug} DELETE /api/v1/sponsored-projects/{id} scope REALM_URL/sponsored-projects/write POST /api/v1/sponsored-projects/{id}/sponsorship-types payload name description is_active order scope REALM_URL/sponsored-projects/write GET /api/v1/sponsored-projects/{id}/sponsorship-types scope REALM_URL/sponsored-projects/read GET /api/v1/sponsored-projects/{id}/sponsorship-types/{id} scope REALM_URL/sponsored-projects/read DELETE /api/v1/sponsored-projects/{id}/sponsorship-types/{id} scope REALM_URL/sponsored-projects/write PUT /api/v1/sponsored-projects/{id}/sponsorship-types/{id} payload name description is_active order scope REALM_URL/sponsored-projects/write PUT /api/v1/sponsored-projects/{id}/sponsorship-types/{id}/supporting-companies/{id} payload order (optional) scope REALM_URL/sponsored-projects/write DELETE /api/v1/sponsored-projects/{id}/sponsorship-types/{id}/supporting-companies/{id} scope REALM_URL/sponsored-projects/write GET /api/v1/sponsored-projects/{id}/sponsorship-types/{id}/supporting-companies scope REALM_URL/sponsored-projects/read Change-Id: I9c0b1bb457a1c583afd284f56f2aced5deceaa02 Signed-off-by: smarcet <smarcet@gmail.com>
111 lines
3.8 KiB
PHP
111 lines
3.8 KiB
PHP
<?php namespace services\utils;
|
|
/**
|
|
* Copyright 2016 OpenStack Foundation
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
**/
|
|
use Doctrine\DBAL\Connection;
|
|
use Doctrine\DBAL\TransactionIsolationLevel;
|
|
use Illuminate\Support\Facades\Log;
|
|
use libs\utils\ITransactionService;
|
|
use Closure;
|
|
use LaravelDoctrine\ORM\Facades\Registry;
|
|
use Doctrine\DBAL\Exception\RetryableException;
|
|
use Exception;
|
|
/**
|
|
* Class DoctrineTransactionService
|
|
* @package services\utils
|
|
*/
|
|
final class DoctrineTransactionService implements ITransactionService
|
|
{
|
|
/**
|
|
* @var string
|
|
*/
|
|
private $manager_name;
|
|
|
|
const MaxRetries = 3;
|
|
|
|
/**
|
|
* DoctrineTransactionService constructor.
|
|
* @param string $manager_name
|
|
*/
|
|
public function __construct($manager_name)
|
|
{
|
|
$this->manager_name = $manager_name;
|
|
}
|
|
|
|
/**
|
|
* Execute a Closure within a transaction.
|
|
*
|
|
* @param Closure $callback
|
|
* @param int $isolationLevel
|
|
* @return mixed
|
|
*
|
|
* @throws \Exception
|
|
*/
|
|
public function transaction(Closure $callback, int $isolationLevel = TransactionIsolationLevel::READ_COMMITTED)
|
|
{
|
|
$retry = 0;
|
|
$done = false;
|
|
$result = null;
|
|
|
|
while (!$done and $retry < self::MaxRetries) {
|
|
try {
|
|
$em = Registry::getManager($this->manager_name);
|
|
$con = $em->getConnection();
|
|
|
|
/**
|
|
* Some database systems close the connection after a period of time, in MySQL this is system variable
|
|
* `wait_timeout`. Given the daemon is meant to run indefinitely we need to make sure we have an open
|
|
* connection before working any job. Otherwise we would see `MySQL has gone away` type errors.
|
|
*/
|
|
|
|
if ($con->ping() === false) {
|
|
$con->close();
|
|
$con->connect();
|
|
}
|
|
|
|
if (!$em->isOpen()) {
|
|
Log::warning("DoctrineTransactionService::transaction: entity manager is closed!, trying to re open...");
|
|
$em = Registry::resetManager($this->manager_name);
|
|
// new entity manager
|
|
$con = $em->getConnection();
|
|
}
|
|
$con->setTransactionIsolation($isolationLevel);
|
|
$con->beginTransaction(); // suspend auto-commit
|
|
$result = $callback($this);
|
|
$em->flush();
|
|
$con->commit();
|
|
$done = true;
|
|
} catch (RetryableException $ex) {
|
|
Log::warning("retrying ...");
|
|
Registry::resetManager($this->manager_name);
|
|
Log::warning("DoctrineTransactionService::transaction con->rollBack");
|
|
$con->rollBack();
|
|
Log::warning($ex);
|
|
$retry++;
|
|
if ($retry === self::MaxRetries) {
|
|
$em->close();
|
|
$con->rollBack();
|
|
throw $ex;
|
|
}
|
|
} catch (Exception $ex) {
|
|
Log::warning("rolling back transaction");
|
|
Log::warning($ex);
|
|
$em->close();
|
|
Log::warning("DoctrineTransactionService::transaction con->rollBack");
|
|
$con->rollBack();
|
|
throw $ex;
|
|
}
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
} |