Repository: meilisearch/strapi-plugin-meilisearch (js)

Issue:
feedback status when tasks are running
**Description**
When we try to update or make the first sync on big amount of data a lot of tasks can be in queued.
Because no feedback are seen, we may check the box many time and juste crash the cloud or make sync append many times.

It should be great to add a feedback about how many indexing operations left and add a warning if too many of them are left before clicking on update

**Basic example**
it can also be usefull when  we use the same meilisearch instance for many indexes


Pull request:
Add task reporting after every async action in the strapi logs
Maybe fixes #537 

The task numbers for every async operation are now communicated in the strapi logs.

Patch:
diff --git a/server/__mocks__/strapi.js b/server/__mocks__/strapi.js
index 9f52639..e6426a3 100644
--- a/server/__mocks__/strapi.js
+++ b/server/__mocks__/strapi.js
@@ -1,5 +1,11 @@
 const defaultContentTypes = require('../__tests__/utils/content-types-list')
 
+const mockLogger = {
+  error: jest.fn(() => {}),
+  info: jest.fn(() => {}),
+  warn: jest.fn(() => {}),
+}
+
 /**
  * @param {object} config
  * @param  {object} [config.restaurantConfig]
@@ -41,11 +47,6 @@ function createStrapiMock({
     }
   })
 
-  const mockLogger = {
-    error: jest.fn(() => {}),
-    warn: jest.fn(() => {}),
-  }
-
   const mockConfig = {
     get: jest.fn(() => {
       return {
@@ -90,4 +91,5 @@ function createStrapiMock({
 
 module.exports = {
   createStrapiMock,
+  mockLogger,
 }
diff --git a/server/services/meilisearch/connector.js b/server/services/meilisearch/connector.js
index 23c18ca..47b0054 100644
--- a/server/services/meilisearch/connector.js
+++ b/server/services/meilisearch/connector.js
@@ -99,7 +99,13 @@ module.exports = ({ strapi, adapter, config }) => {
         adapter.addCollectionNamePrefixToId({ entryId, contentType })
       )
 
-      return await client.index(indexUid).deleteDocuments(documentsIds)
+      const task = await client.index(indexUid).deleteDocuments(documentsIds)
+
+      strapi.log.info(
+        `A task to delete ${documentsIds.length} documents of the index "${indexUid}" in Meilisearch has been enqueued (Task uid: ${task.taskUid}).`
+      )
+
+      return task
     },
 
     /**
@@ -125,13 +131,20 @@ module.exports = ({ strapi, adapter, config }) => {
           config,
           adapter,
         })
+
         if (sanitized.length === 0) {
-          return client.index(indexUid).deleteDocument(
+          const task = await client.index(indexUid).deleteDocument(
             adapter.addCollectionNamePrefixToId({
               contentType,
               entryId: entry.id,
             })
           )
+
+          strapi.log.info(
+            `A task to delete one document from the Meilisearch index "${indexUid}" has been enqueued (Task uid: ${task.taskUid}).`
+          )
+
+          return task
         } else {
           return client
             .index(indexUid)
@@ -258,8 +271,13 @@ module.exports = ({ strapi, adapter, config }) => {
       const task = await client
         .index(indexUid)
         .addDocuments(documents, { primaryKey: '_meilisearch_id' })
+
       await store.addIndexedContentType({ contentType })
 
+      strapi.log.info(
+        `The task to add ${documents.length} documents to the Meilisearch index "${indexUid}" has been enqueued (Task uid: ${task.taskUid}).`
+      )
+
       return task
     },
 
@@ -278,7 +296,11 @@ module.exports = ({ strapi, adapter, config }) => {
 
       // Get Meilisearch Index settings from model
       const settings = config.getSettings({ contentType })
-      await client.index(indexUid).updateSettings(settings)
+      const task = await client.index(indexUid).updateSettings(settings)
+
+      strapi.log.info(
+        `A task to update the settings to the Meilisearch index "${indexUid}" has been enqueued (Task uid: ${task.taskUid}).`
+      )
 
       // Callback function for batching action
       const addDocuments = async ({ entries, contentType }) => {
@@ -291,11 +313,15 @@ module.exports = ({ strapi, adapter, config }) => {
         })
 
         // Add documents in Meilisearch
-        const task = await client
+        const { taskUid } = await client
           .index(indexUid)
           .addDocuments(documents, { primaryKey: '_meilisearch_id' })
 
-        return task.taskUid
+        strapi.log.info(
+          `A task to add ${documents.length} documents to the Meilisearch index "${indexUid}" has been enqueued (Task uid: ${taskUid}).`
+        )
+
+        return taskUid
       }
 
       const tasksUids = await contentTypeService.actionInBatches({
@@ -367,7 +393,11 @@ module.exports = ({ strapi, adapter, config }) => {
         const client = Meilisearch({ apiKey, host })
 
         const indexUid = config.getIndexNameOfContentType({ contentType })
-        await client.index(indexUid).delete()
+        const { taskUid } = await client.index(indexUid).delete()
+
+        strapi.log.info(
+          `A task to delete the Meilisearch index "${indexUid}" has been added to the queue (Task uid: ${taskUid}).`
+        )
       }
 
       await store.removeIndexedContentType({ contentType })


Test patch:
diff --git a/server/__tests__/meilisearch.test.js b/server/__tests__/meilisearch.test.js
index 061fd9f..1d69ef5 100644
--- a/server/__tests__/meilisearch.test.js
+++ b/server/__tests__/meilisearch.test.js
@@ -1,7 +1,7 @@
 const createMeilisearchService = require('../services/meilisearch')
 
 const { MeiliSearch: Meilisearch } = require('meilisearch')
-const { createStrapiMock } = require('../__mocks__/strapi')
+const { createStrapiMock, mockLogger } = require('../__mocks__/strapi')
 
 jest.mock('meilisearch')
 
@@ -46,6 +46,11 @@ describe('Tests content types', () => {
       contentType: 'restaurant',
       entriesId: [1, 2],
     })
+
+    expect(customStrapi.log.info).toHaveBeenCalledTimes(1)
+    expect(customStrapi.log.info).toHaveBeenCalledWith(
+      'A task to delete 2 documents of the index "customIndex" in Meilisearch has been enqueued (Task uid: undefined).'
+    )
     expect(client.index('').deleteDocuments).toHaveBeenCalledTimes(1)
     expect(client.index('').deleteDocuments).toHaveBeenCalledWith([
       'restaurant-1',
@@ -108,6 +113,10 @@ describe('Tests content types', () => {
         publicationState: 'preview',
       },
     })
+    expect(customStrapi.log.info).toHaveBeenCalledTimes(1)
+    expect(customStrapi.log.info).toHaveBeenCalledWith(
+      'A task to update the settings to the Meilisearch index "restaurant" has been enqueued (Task uid: undefined).'
+    )
   })
 
   test('sanitizes the private fields from the entries', async () => {
@@ -145,13 +154,15 @@ describe('Tests content types', () => {
           },
         },
         config: { get: jest.fn(() => ({ restaurant: jest.fn() })) },
+        log: mockLogger,
       },
     })
 
     await service.addContentTypeInMeiliSearch({ contentType: 'restaurant' })
 
-    expect(Meilisearch().index).toHaveBeenCalledWith('restaurant')
-    expect(Meilisearch().index().addDocuments).toHaveBeenNthCalledWith(
+    const client = new Meilisearch({ host: '' })
+    expect(client.index).toHaveBeenCalledWith('restaurant')
+    expect(client.index('restaurant').addDocuments).toHaveBeenNthCalledWith(
       1,
       [
         {