@vegan-friendly/strapi-plugin-elasticsearch 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vegan-friendly/strapi-plugin-elasticsearch",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "A Strapi plugin to enable using Elasticsearch with Strapi CMS.",
5
5
  "homepage": "https://github.com/vegan-friendly/strapi-plugin-elasticsearch",
6
6
  "strapi": {
@@ -156,6 +156,9 @@ exports.default = async ({ strapi }) => {
156
156
  },
157
157
  });
158
158
  });
159
+ // clean up old indexing tasks, as server is booting.
160
+ // allow 60 seconds, in case strapi is being run in cluster mode and this is the second instance
161
+ await scheduleIndexingService.getActiveFullIndexingTasks(60);
159
162
  configureIndexingService.markInitialized();
160
163
  }
161
164
  catch (err) {
@@ -144,7 +144,7 @@ declare const _default: {
144
144
  getItemsPendingToBeIndexed(): Promise<any>;
145
145
  markIndexingTaskComplete(recId: any, error?: string | null): Promise<void>;
146
146
  markIndexingTaskInProgress(recId: any): Promise<void>;
147
- getFullIndexingInProgress(): Promise<any>;
147
+ getActiveFullIndexingTasks(staleTaskThresholdSeconds?: number): Promise<any[]>;
148
148
  };
149
149
  esInterface: ({ strapi }: {
150
150
  strapi: any;
@@ -40,7 +40,7 @@ declare const _default: {
40
40
  getItemsPendingToBeIndexed(): Promise<any>;
41
41
  markIndexingTaskComplete(recId: any, error?: string | null): Promise<void>;
42
42
  markIndexingTaskInProgress(recId: any): Promise<void>;
43
- getFullIndexingInProgress(): Promise<any>;
43
+ getActiveFullIndexingTasks(staleTaskThresholdSeconds?: number): Promise<any[]>;
44
44
  };
45
45
  esInterface: ({ strapi }: {
46
46
  strapi: any;
@@ -2,17 +2,18 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.default = ({ strapi }) => ({
4
4
  async rebuildIndex(task = null) {
5
- const helper = strapi.plugins['elasticsearch'].services.helper;
6
- const esInterface = strapi.plugins['elasticsearch'].services.esInterface;
7
- const scheduleIndexingService = strapi.plugins['elasticsearch'].services.scheduleIndexing;
8
- const configureIndexingService = strapi.plugins['elasticsearch'].services.configureIndexing;
9
- const logIndexingService = strapi.plugins['elasticsearch'].services.logIndexing;
10
- const virtualCollectionsIndexer = strapi.plugins['elasticsearch'].services['virtualCollectionsIndexer'];
11
- const virtualCollectionsRegistry = strapi.plugins['elasticsearch'].services['virtualCollectionsRegistry'];
5
+ const pluginServices = strapi.plugins['elasticsearch'].services;
6
+ const helper = pluginServices.helper;
7
+ const esInterface = pluginServices.esInterface;
8
+ const scheduleIndexingService = pluginServices.scheduleIndexing;
9
+ const configureIndexingService = pluginServices.configureIndexing;
10
+ const logIndexingService = pluginServices.logIndexing;
11
+ const virtualCollectionsIndexer = pluginServices.virtualCollectionsIndexer;
12
+ const virtualCollectionsRegistry = pluginServices.virtualCollectionsRegistry;
12
13
  let taskError = null;
13
14
  try {
14
15
  console.log('strapi-plugin-elasticsearch : Request to rebuild the index received.');
15
- const fullIndexingInProgress = await scheduleIndexingService.getFullIndexingInProgress();
16
+ const fullIndexingInProgress = await scheduleIndexingService.getActiveFullIndexingTasks();
16
17
  if (fullIndexingInProgress.length > 0) {
17
18
  const msg = `Indexing is already in progress - see tasks ${fullIndexingInProgress.map((t) => t.id)}. This request is ignored and marked as failed.`;
18
19
  console.log('strapi-plugin-elasticsearch : ' + msg);
@@ -16,6 +16,6 @@ declare const _default: ({ strapi }: {
16
16
  getItemsPendingToBeIndexed(): Promise<any>;
17
17
  markIndexingTaskComplete(recId: any, error?: string | null): Promise<void>;
18
18
  markIndexingTaskInProgress(recId: any): Promise<void>;
19
- getFullIndexingInProgress(): Promise<any>;
19
+ getActiveFullIndexingTasks(staleTaskThresholdSeconds?: number): Promise<any[]>;
20
20
  };
21
21
  export default _default;
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ const STUCK_THRESHOLD_SECONDS = 60 * 60; // 1 hour
3
4
  exports.default = ({ strapi }) => ({
4
5
  async addFullSiteIndexingTask() {
5
6
  const data = await strapi.entityService.create('plugin::elasticsearch.task', {
@@ -68,13 +69,29 @@ exports.default = ({ strapi }) => ({
68
69
  },
69
70
  });
70
71
  },
71
- async getFullIndexingInProgress() {
72
+ async getActiveFullIndexingTasks(staleTaskThresholdSeconds) {
73
+ if (!staleTaskThresholdSeconds) {
74
+ staleTaskThresholdSeconds = Number(strapi.config.get('plugin.elasticsearch').staleTaskThresholdSeconds) || STUCK_THRESHOLD_SECONDS;
75
+ }
76
+ staleTaskThresholdSeconds *= 1000;
72
77
  const entries = await strapi.entityService.findMany('plugin::elasticsearch.task', {
73
78
  filters: {
74
79
  indexing_status: 'in-progress',
75
80
  full_site_indexing: true,
76
81
  },
77
82
  });
78
- return entries;
83
+ const now = Date.now();
84
+ const activeTasks = [];
85
+ for (const t of entries) {
86
+ const updatedAt = new Date(t.updatedAt || t.createdAt).getTime();
87
+ if (now - updatedAt > staleTaskThresholdSeconds) {
88
+ await strapi.plugins['elasticsearch'].services.logIndexing.recordIndexingFail(`Task ${t.id} was stuck in-progress for too long. Marking as failed.`);
89
+ await this.markIndexingTaskComplete(t.id, 'Stuck in-progress');
90
+ }
91
+ else {
92
+ activeTasks.push(t);
93
+ }
94
+ }
95
+ return activeTasks;
79
96
  },
80
97
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vegan-friendly/strapi-plugin-elasticsearch",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "A Strapi plugin to enable using Elasticsearch with Strapi CMS.",
5
5
  "homepage": "https://github.com/vegan-friendly/strapi-plugin-elasticsearch",
6
6
  "strapi": {