Commit 97baaa22 authored by Jean-Philippe Metivier's avatar Jean-Philippe Metivier
Browse files

Merge branch 'master' of git.unicaen.fr:dsi/sygal

parents 29f96c1f 45776729
Loading
Loading
Loading
Loading
Loading
+16 −0
Changes for module/Import/src/Import/Model/Mapping/Import.Model.SynchroLog.dcm.xml: 16 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
    <entity name="Import\Model\SynchroLog" table="SYNCHRO_LOG">
        <id name="id" type="integer" column="ID">
            <generator strategy="SEQUENCE"/>
        </id>
        <field name="logDate" column="LOG_DATE" type="datetime"/>
        <field name="startDate" column="START_DATE" type="datetime"/>
        <field name="finishDate" column="FINISH_DATE" type="datetime"/>
        <field name="status" column="STATUS"/>
        <field name="sql" column="SQL"/>
        <field name="message" column="MESSAGE"/>
    </entity>
</doctrine-mapping>
 No newline at end of file
+147 −0
Changes for module/Import/src/Import/Model/SynchroLog.php: 147 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Import\Model;

/**
 * SynchroLog
 */
class SynchroLog
{
    private $id;

    private $logDate;

    private $startDate;

    private $finishDate;

    private $status;

    private $sql;

    private $message;

    /**
     * @return string
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @return \DateTime
     */
    public function getLogDate()
    {
        return $this->logDate;
    }

    /**
     * @param \DateTime $logDate
     * @return self
     */
    public function setLogDate(\DateTime $logDate)
    {
        $this->logDate = $logDate;

        return $this;
    }

    /**
     * @return \DateTime
     */
    public function getStartDate()
    {
        return $this->startDate;
    }

    /**
     * @param \DateTime $startDate
     * @return self
     */
    public function setStartDate(\DateTime $startDate)
    {
        $this->startDate = $startDate;

        return $this;
    }

    /**
     * @return \DateTime
     */
    public function getFinishDate()
    {
        return $this->finishDate;
    }

    /**
     * @param \DateTime $finishDate
     * @return self
     */
    public function setFinishDate(\DateTime $finishDate)
    {
        $this->finishDate = $finishDate;

        return $this;
    }

    /**
     * @return mixed
     */
    public function getStatus()
    {
        return $this->status;
    }

    /**
     * @param mixed $status
     * @return self
     */
    public function setStatus($status)
    {
        $this->status = $status;

        return $this;
    }

    /**
     * @return string
     */
    public function getSql()
    {
        return $this->sql;
    }

    /**
     * @param string $sql
     * @return self
     */
    public function setSql($sql)
    {
        $this->sql = $sql;

        return $this;
    }

    /**
     * @return string
     */
    public function getMessage()
    {
        return $this->message;
    }

    /**
     * @param string $message
     * @return self
     */
    public function setMessage($message)
    {
        $this->message = $message;

        return $this;
    }

}
+28 −17
Changes for module/Import/src/Import/Service/FetcherService.php: 28 added lines, 17 removed lines.
Original line number Diff line number Diff line
@@ -119,9 +119,11 @@ class FetcherService
    }

    /**
     * Réalise l'import d'un seul enregistrement d'un service.
     *
     * @param string $serviceName
     * @param string $entityClass
     * @param string $sourceCode
     * @param string $sourceCode Source code de l'enregistrement à importer
     */
    private function fetchRow($serviceName, $entityClass, $sourceCode)
    {
@@ -171,11 +173,16 @@ class FetcherService

        /** Commit **/
        try {
            $this->entityManager->getConnection()->commit();
            $connection->commit();
        } catch (ConnectionException $e) {
            throw new RuntimeException("Le commit a échoué", null, $e);
        } catch (\Exception $e) {
            throw new RuntimeException("Erreur inattendue", null, $e);
            try {
                $connection->rollBack();
            } catch (ConnectionException $e) {
                throw new RuntimeException("Erreur inattendue et en plus le rollback a échoué!", null, $e);
            }
            throw new RuntimeException("Erreur inattendue, rollback effectué.", null, $e);
        }
        $_fin = microtime(true);
        $this->logger->debug("Commit : " . ($_fin - $_debut) . " secondes.");
@@ -190,6 +197,8 @@ class FetcherService
    }

    /**
     * Réalise l'import de tous les enregistrements d'un service.
     *
     * @param string $service
     * @param string $entityClass
     * @param array  $filters
@@ -252,13 +261,7 @@ class FetcherService
                try {
                    $connection->executeQuery($query);
                } catch (DBALException $e) {
                    $message = "Erreur lors de la mise à jour de la table $tableName en BDD";
                    try {
                        $connection->rollBack();
                    } catch (ConnectionException $e) {
                        throw new RuntimeException($message . " et le rollback a échoué", null, $e);
                    }
                    throw new RuntimeException($message, null, $e);
                    throw new RuntimeException("Erreur lors de la mise à jour de la table $tableName en BDD", null, $e);
                }
            }
            $_fin = microtime(true);
@@ -269,11 +272,14 @@ class FetcherService
        /** Commit **/
        $_debut = microtime(true);
        try {
            $this->entityManager->getConnection()->commit();
        } catch (ConnectionException $e) {
            throw new RuntimeException("Le commit a échoué", null, $e);
            $connection->commit();
        } catch (\Exception $e) {
            throw new RuntimeException("Erreur inattendue", null, $e);
            try {
                $connection->rollBack();
            } catch (ConnectionException $e) {
                throw new RuntimeException("Le commit et le rollback ont échoué!", null, $e);
            }
            throw new RuntimeException("Le commit a échoué, un rollback a été effectué.", null, $e);
        }
        $_fin = microtime(true);
        $this->logger->debug("Commit : " . ($_fin - $_debut) . " secondes.");
@@ -434,13 +440,18 @@ class FetcherService
        $log_query .= "'" . $table . "'";
        $log_query .= ")";

        try {
        $connection = $this->entityManager->getConnection();
        $connection->beginTransaction();
        try {
            $connection->executeQuery($log_query);
            $this->entityManager->getConnection()->commit();
            $connection->commit();
        } catch (\Exception $e) {
            throw new RuntimeException("Problème lors de l'écriture du log en base.", null, $e);
            try {
                $connection->rollBack();
            } catch (ConnectionException $e) {
                throw new RuntimeException("Ecriture du log en base et rollback impossibles.", null, $e);
            }
            throw new RuntimeException("Ecriture du log en base impossible.", null, $e);
        }

        $this->logger->info($response);
+4 −0
Changes for module/Import/src/Import/Service/ImportService.php: 4 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -128,6 +128,8 @@ class ImportService
        } catch (CallException $e) {
            if ($e->getCode() === 404) {
                throw new RuntimeException("Le service '$service' n'existe pas !", null, $e);
            } else {
                throw new RuntimeException("Erreur rencontrée lors de l'import (service: '$service')", null, $e);
            }
        }

@@ -167,6 +169,8 @@ class ImportService
                    } else {
                        $this->logger->alert("$message On continue tout de même.");
                    }
                } else {
                    throw new RuntimeException("Erreur rencontrée lors de l'import (service: tous)", null, $e);
                }
            }
        }
+48 −2
Changes for module/Import/src/Import/Service/SynchroService.php: 48 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -2,7 +2,11 @@

namespace Import\Service;

use DateTime;
use Doctrine\DBAL\ConnectionException;
use Doctrine\DBAL\DBALException;
use Doctrine\ORM\OptimisticLockException;
use Import\Model\SynchroLog;
use UnicaenApp\Exception\LogicException;
use UnicaenApp\Exception\RuntimeException;
use UnicaenApp\Service\EntityManagerAwareTrait;
@@ -68,11 +72,28 @@ class SynchroService
    {
        $plsql = implode(PHP_EOL, array_merge(['BEGIN'], $calls, ['END;']));

        $startDate = new DateTime();

        $connection = $this->entityManager->getConnection();
        $connection->beginTransaction();
        try {
            $this->entityManager->getConnection()->executeQuery($plsql);
            $connection->executeQuery($plsql);
            $connection->commit();
            $status = 'SUCCESS';
            $message = null;
        } catch (DBALException $e) {
            throw new RuntimeException("Erreur rrencontrée lors de l'exécution des procédures de synchro (import)", null, $e);
            $status = 'FAILURE';
            $message = "Erreur rencontrée lors de l'exécution des procédures de synchro (import) : " . PHP_EOL .
                $e->getTraceAsString();
            try {
                $connection->rollBack();
            } catch (ConnectionException $e) {
                $message .= PHP_EOL . "Et en plus le rollback a échoué!";
            }
        }

        $finishDate = new DateTime();
        $this->log($startDate, $finishDate, $plsql, $status, $message);
    }

    /**
@@ -161,4 +182,29 @@ EOS;

        return sprintf($template, str_replace("'", "''", $plsql));
    }

    /**
     * @param DateTime $startDate
     * @param DateTime $finishDate
     * @param string    $sql
     * @param string    $status
     * @param string    $message
     */
    private function log(DateTime $startDate, DateTime $finishDate, $sql, $status, $message = null)
    {
        $log = (new SynchroLog())
            ->setLogDate(new DateTime())
            ->setStartDate($startDate)
            ->setFinishDate($finishDate)
            ->setSql($sql)
            ->setStatus($status)
            ->setMessage($message);

        $this->entityManager->persist($log);
        try {
            $this->entityManager->flush($log);
        } catch (OptimisticLockException $e) {
            throw new RuntimeException("Erreur rencontrée lors de l'enregistrement du SynchroLog.", null, $e);
        }
    }
}
 No newline at end of file