From 740818bbd2d1f5597a317fa2696a6f900f935638 Mon Sep 17 00:00:00 2001 From: Shubham Mahajan Date: Sun, 12 Jun 2022 13:25:18 +0400 Subject: [PATCH] bug: Add timeout to the wordpress query if there is no response --- env.template | 1 + plugins/wp/cms.js | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/env.template b/env.template index fd3486b1..d3dd09c1 100644 --- a/env.template +++ b/env.template @@ -6,6 +6,7 @@ API_URL=http://127.0.0.1:5000/api/3/action/ WP_URL=http://127.0.0.1:6000 WP_BLOG_PATH= WP_TOKEN= +WP_TIMEOUT= # Change that in production! SESSION_SECRET="keyboard cat" diff --git a/plugins/wp/cms.js b/plugins/wp/cms.js index bab86229..41f43c4d 100644 --- a/plugins/wp/cms.js +++ b/plugins/wp/cms.js @@ -3,7 +3,7 @@ const config = require('../../config') const wpcom = require('wpcom')(config.get('WP_TOKEN')) - +const timeout = config.get('WP_TIMEOUT', 60000) class CmsModel { constructor() { @@ -40,14 +40,21 @@ class CmsModel { if (id) { query.id = id } - query.slug = slug + query.slug = slug; + let queryCompleted = false this.blog.post(query).get((err, data) => { + queryCompleted = true if (err) { reject(err) } else { resolve(data) } }) + setTimeout(() => { + if (!queryCompleted) { + reject(new Error('Timeout')) + } + }, timeout) } }) }