Browse Source

update monolog conf and dolibarrsync logger

Olivier Massot 3 years ago
parent
commit
ca865434d8

+ 22 - 21
config/packages/dev/monolog.yaml

@@ -23,7 +23,7 @@ monolog:
 #            type: deduplication
 #            handler: mailer
 #        mailer:
-#            type: swift_mailer
+#            type: symfony_mailer
 #            from_email: "process@opentalent.fr"
 #            to_email: "exploitation@opentalent.fr"
 #            subject: AP2I - Critical Error Occurred
@@ -34,7 +34,7 @@ monolog:
         # logging fichier
         file_main:
             type: rotating_file
-            path: "%kernel.logs_dir%/%kernel.environment%/%kernel.environment%.main.log"
+            path: "%kernel.logs_dir%/%kernel.environment%.main.log"
             level: debug
             max_files: 3
             channels: [php, doctrine, http_client, elastica]
@@ -46,35 +46,36 @@ monolog:
             channels: security
 
         # logs spécifiques à certains process
-        cron:
+        # * synchro dolibarr
+        dolibarrsync:
             type: group
-            members: [cron_file]
-        cron_file:
+            members: [dolibarrsync_file]
+            channels: dolibarrsync
+        dolibarrsync_file:
             type: rotating_file
-            path: "%kernel.logs_dir%/%kernel.environment%.cron.log"
+            path: "%kernel.logs_dir%/%kernel.environment%.dolibarrsync.log"
             level: debug
             max_files: 7
-            channels: cron
-#        cron_critical:
+            formatter: monolog.formatter.message
+
+#        dolibarrsync_critical:
 #            type:           fingers_crossed
 #            action_level:   critical
-#            handler:        cron_swift
-#        cron_swift:
-#            type:           swift_mailer
+#            handler:        dolibarrsync_deduplicated
+#        dolibarrsync_deduplicated:
+#            type: deduplication
+#            # the time in seconds during which duplicate entries are discarded (default: 60)
+#            time: 10
+#            handler: dolibarrsync_mailer
+#        dolibarrsync_mailer:
+#            type:           symfony_mailer
 #            from_email:     "process@opentalent.fr"
 #            to_email:       "exploitation@opentalent.fr"
-#            subject:        Critical Error Occurred
+#            subject:        "Dolibarr Sync - Critical Error"
 #            level:          error
-#            formatter:  monolog.formatter.html
-#            content_type: text/html
+#            formatter:      monolog.formatter.html
+#            content_type:   text/html
 
-        # logs spécifiques au service de synchro dolibarr
-        dolibarrsync:
-            type: rotating_file
-            path: "%kernel.logs_dir%/%kernel.environment%.dolibarrsync.log"
-            level: debug
-            max_files: 7
-            channels: dolibarrsync
 
         # uncomment to get logging in your browser
         # you may have to allow bigger header sizes in your Web server configuration

+ 8 - 1
config/services.yaml

@@ -10,6 +10,13 @@ services:
         bind:
             $opentalentConfig: '%kernel.project_dir%%env(OPENTALENT_CONFIG)%'
 
+    # Logging: a shorter version of the default monolog line formatter
+    monolog.formatter.message:
+        class: Monolog\Formatter\LineFormatter
+        arguments:
+            - "[%%datetime%%] %%level_name%% : %%message%%\n"
+            - "Y-m-d H:i:s.v"
+
     # makes classes in src/ available to be used as services
     # this creates a service per class whose id is the fully-qualified class name
     App\:
@@ -43,7 +50,7 @@ services:
         - !tagged_iterator app.optionalsroles
 
 
-    App\Service\Dolibarr\DolibarrSync\DolibarrSyncService:
+    App\Service\Dolibarr\DolibarrSyncService:
         tags:
             - { name: monolog.logger, channel: dolibarrsync }
 

+ 13 - 6
src/Service/Dolibarr/DolibarrSync/DolibarrSyncService.php → src/Service/Dolibarr/DolibarrSyncService.php

@@ -26,7 +26,6 @@ use HttpException;
 use libphonenumber\PhoneNumber;
 use libphonenumber\PhoneNumberFormat;
 use libphonenumber\PhoneNumberUtil;
-use Monolog\Handler\RotatingFileHandler;
 use Psr\Log\LoggerInterface;
 use Symfony\Contracts\Translation\TranslatorInterface;
 
@@ -153,7 +152,7 @@ class DolibarrSyncService
                 ARRAY_FILTER_USE_BOTH
             );
 
-            // add an update operation if some data has to be updated
+            // Add an update operation if some data has to be updated
             if (!empty($newSocietyData)) {
                 $operations[] = new DolibarrUpdateOperation(
                     'Update society : ' . $organization->getName() . ' (' . $organization->getId() . ')',
@@ -181,11 +180,16 @@ class DolibarrSyncService
                         }
                         $contactsProcessed[] = $person->getId();
 
+                        // special: if the contact has no name, ignore it
+                        if (!$person->getName()) {
+                            continue;
+                        }
+
                         // Build parameters for the query (if a query is needed
                         $dolibarrContact = $dolibarrContactsIndex[$person->getId()] ?? null;
 
                         // <-- special case: for phone numbers, dolibarr api returns empty strings even when field is null in DB
-                        // if left like this, it would leads to unnecessary updates
+                        // if left like this, it would lead to unnecessary updates
                         if ($dolibarrContact !== null) {
                             if ($dolibarrContact['phone_pro'] === '')
                                 $dolibarrContact['phone_pro'] = null;
@@ -213,7 +217,7 @@ class DolibarrSyncService
                                 $newContactData
                             );
                         } else {
-                            // Only update the fields that are different
+                            // Only update the fields that are different from existing
                             $newContactData = array_filter(
                                 $newContactData,
                                 function($v, $k) use ($dolibarrContact) { return $v !== $dolibarrContact[$k]; },
@@ -289,7 +293,10 @@ class DolibarrSyncService
                 $this->logger->warning('Tried to execute an operation that was not marked as ready : ' . $operation);
                 continue;
             }
-            $operation->execute();
+
+            $operation->execute(
+                $this->dolibarrApiService
+            );
 
             if ($operation->getStatus() === DolibarrSyncOperation::STATUS_ERROR) {
                 $this->logger->error('Error while executing operation : ' . $operation);
@@ -509,7 +516,7 @@ class DolibarrSyncService
      * @return string
      */
     private function formatContactPosition(array $missions, ?string $gender = 'X'): string {
-        $to_exclude = [FunctionEnum::ADHERENT(), FunctionEnum::STUDENT()];
+        $to_exclude = [FunctionEnum::ADHERENT(), FunctionEnum::STUDENT(), FunctionEnum::OTHER()];
         return implode(
             ', ',
             array_map(

+ 8 - 0
src/Service/Rest/ApiRequestInterface.php

@@ -0,0 +1,8 @@
+<?php
+
+namespace App\Service\Rest;
+
+class ApiRequestInterface
+{
+
+}

+ 0 - 0
src/Service/ApiRequestService.php → src/Service/Rest/ApiRequestService.php


+ 7 - 11
src/Service/Dolibarr/DolibarrSync/SyncOperation/DolibarrSyncOperation.php → src/Service/Rest/Operation/BaseRestOperation.php

@@ -1,11 +1,9 @@
 <?php
 declare(strict_types=1);
 
-namespace App\Service\Dolibarr\DolibarrSync\SyncOperation;
+namespace App\Service\Rest\SyncOperation;
 
-use App\Service\Dolibarr\DolibarrApiService;
-use HttpException;
-use phpDocumentor\Reflection\Exception;
+use App\Service\Rest\ApiRequestService;
 use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
 use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
 use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
@@ -14,16 +12,16 @@ use Symfony\Contracts\Service\Attribute\Required;
 
 /**
  * Single synchronization operation, corresponding to a single request
- * to the Dolibarr API
+ * to a REST API
  */
-abstract class DolibarrSyncOperation
+abstract class BaseRestOperation
 {
     const STATUS_READY = 0;
     const STATUS_PENDING = 1;
     const STATUS_DONE = 2;
     const STATUS_ERROR = 3;
 
-    protected DolibarrApiService $dolibarrApiService;
+    protected ApiRequestService $apiService;
     protected int $status = self::STATUS_READY;
     protected string $label;
     protected string $method;
@@ -33,9 +31,7 @@ abstract class DolibarrSyncOperation
     protected string $errorMessage = "";
 
     #[Required]
-    public function setDolibarrApiService(DolibarrApiService $dolibarrApiService) {
-        $this->dolibarrApiService = $dolibarrApiService;
-    }
+    public function setApiRequestService(ApiRequestService $apiService) { $this->apiService = $apiService; }
 
     public function __construct(string $label, string $method, string $path, array $parameters = [], array $currentData = []) {
         $this->label = $label;
@@ -51,7 +47,7 @@ abstract class DolibarrSyncOperation
     public function execute() {
         $this->status = self::STATUS_PENDING;
         try {
-            $this->dolibarrApiService->request($this->method, $this->path, $this->parameters);
+            $this->apiService->request($this->method, $this->path, $this->parameters);
             $this->status = self::STATUS_DONE;
         } catch (ClientExceptionInterface | TransportExceptionInterface | RedirectionExceptionInterface | ServerExceptionInterface $e) {
             $this->status = self::STATUS_ERROR;

+ 2 - 2
src/Service/Dolibarr/DolibarrSync/SyncOperation/DolibarrCreateOperation.php → src/Service/Rest/Operation/CreateOperation.php

@@ -1,11 +1,11 @@
 <?php
 declare(strict_types=1);
 
-namespace App\Service\Dolibarr\DolibarrSync\SyncOperation;
+namespace App\Service\Rest\SyncOperation;
 
 use JetBrains\PhpStorm\Pure;
 
-class DolibarrCreateOperation extends DolibarrSyncOperation
+class CreateOperation extends BaseRestOperation
 {
     protected string $entity;
 

+ 2 - 2
src/Service/Dolibarr/DolibarrSync/SyncOperation/DolibarrDeleteOperation.php → src/Service/Rest/Operation/DeleteOperation.php

@@ -1,11 +1,11 @@
 <?php
 declare(strict_types=1);
 
-namespace App\Service\Dolibarr\DolibarrSync\SyncOperation;
+namespace App\Service\Rest\SyncOperation;
 
 use JetBrains\PhpStorm\Pure;
 
-class DolibarrDeleteOperation extends DolibarrSyncOperation
+class DeleteOperation extends BaseRestOperation
 {
     protected string $entity;
     protected int $id;

+ 2 - 2
src/Service/Dolibarr/DolibarrSync/SyncOperation/DolibarrUpdateOperation.php → src/Service/Rest/Operation/UpdateOperation.php

@@ -1,11 +1,11 @@
 <?php
 declare(strict_types=1);
 
-namespace App\Service\Dolibarr\DolibarrSync\SyncOperation;
+namespace App\Service\Rest\SyncOperation;
 
 use JetBrains\PhpStorm\Pure;
 
-class DolibarrUpdateOperation extends DolibarrSyncOperation
+class UpdateOperation extends BaseRestOperation
 {
     protected string $entity;
     protected int $id;

+ 1 - 1
tests/Service/ApiRequestServiceTest.php → tests/Service/Rest/ApiRequestServiceTest.php

@@ -2,7 +2,7 @@
 
 namespace App\Tests\Service;
 
-use App\Service\ApiRequestService;
+use App\Service\Rest\ApiRequestService;
 use PHPUnit\Framework\TestCase;
 use Symfony\Contracts\HttpClient\HttpClientInterface;
 use Symfony\Contracts\HttpClient\ResponseInterface;