mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-20 22:42:53 -05:00
Refactored post scheduler to use async/await syntax
refs https://github.com/TryGhost/Team/issues/694 - This should allow to reason about what's happening inside the module a little easier
This commit is contained in:
parent
00f6c76d1f
commit
90e5af12ae
1 changed files with 50 additions and 59 deletions
|
@ -35,24 +35,24 @@ _private.normalize = function normalize({model, apiUrl, resourceType, integratio
|
||||||
* @description Load all scheduled posts/pages from database.
|
* @description Load all scheduled posts/pages from database.
|
||||||
* @return {Promise}
|
* @return {Promise}
|
||||||
*/
|
*/
|
||||||
_private.loadScheduledResources = function () {
|
_private.loadScheduledResources = async function () {
|
||||||
const api = require('../../../api');
|
const api = require('../../../api');
|
||||||
// Fetches all scheduled resources(posts/pages) with default API
|
// Fetches all scheduled resources(posts/pages) with default API
|
||||||
return Promise.mapSeries(SCHEDULED_RESOURCES, (resourceType) => {
|
const results = await Promise.mapSeries(SCHEDULED_RESOURCES, async (resourceType) => {
|
||||||
return api.schedules.getScheduled.query({
|
const result = await api.schedules.getScheduled.query({
|
||||||
options: {
|
options: {
|
||||||
resource: resourceType
|
resource: resourceType
|
||||||
}
|
}
|
||||||
}).then((result) => {
|
});
|
||||||
|
|
||||||
return result[resourceType] || [];
|
return result[resourceType] || [];
|
||||||
});
|
});
|
||||||
}).then((results) => {
|
|
||||||
return SCHEDULED_RESOURCES.reduce(function (obj, entry, index) {
|
return SCHEDULED_RESOURCES.reduce(function (obj, entry, index) {
|
||||||
return Object.assign(obj, {
|
return Object.assign(obj, {
|
||||||
[entry]: results[index]
|
[entry]: results[index]
|
||||||
});
|
});
|
||||||
}, {});
|
}, {});
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -60,7 +60,7 @@ _private.loadScheduledResources = function () {
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @return {*}
|
* @return {*}
|
||||||
*/
|
*/
|
||||||
exports.init = function init(options = {}) {
|
exports.init = async function init(options = {}) {
|
||||||
const {apiUrl} = options;
|
const {apiUrl} = options;
|
||||||
let adapter = null;
|
let adapter = null;
|
||||||
let integration = null;
|
let integration = null;
|
||||||
|
@ -73,25 +73,18 @@ exports.init = function init(options = {}) {
|
||||||
return Promise.reject(new errors.IncorrectUsageError({message: 'post-scheduling: no apiUrl was provided'}));
|
return Promise.reject(new errors.IncorrectUsageError({message: 'post-scheduling: no apiUrl was provided'}));
|
||||||
}
|
}
|
||||||
|
|
||||||
return getSchedulerIntegration()
|
integration = await getSchedulerIntegration();
|
||||||
.then((_integration) => {
|
adapter = await localUtils.createAdapter();
|
||||||
integration = _integration;
|
|
||||||
return localUtils.createAdapter();
|
let scheduledResources;
|
||||||
})
|
|
||||||
.then((_adapter) => {
|
|
||||||
adapter = _adapter;
|
|
||||||
|
|
||||||
if (!adapter.rescheduleOnBoot) {
|
if (!adapter.rescheduleOnBoot) {
|
||||||
return [];
|
scheduledResources = [];
|
||||||
}
|
} else {
|
||||||
|
scheduledResources = await _private.loadScheduledResources();
|
||||||
return _private.loadScheduledResources();
|
|
||||||
})
|
|
||||||
.then((scheduledResources) => {
|
|
||||||
if (!Object.keys(scheduledResources).length) {
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (Object.keys(scheduledResources).length) {
|
||||||
// Reschedules all scheduled resources on boot
|
// Reschedules all scheduled resources on boot
|
||||||
// NOTE: We are using reschedule, because custom scheduling adapter could use a database, which needs to be updated
|
// NOTE: We are using reschedule, because custom scheduling adapter could use a database, which needs to be updated
|
||||||
// and not an in-process implementation!
|
// and not an in-process implementation!
|
||||||
|
@ -101,11 +94,10 @@ exports.init = function init(options = {}) {
|
||||||
adapter.schedule(_private.normalize({model, apiUrl, integration, resourceType}));
|
adapter.schedule(_private.normalize({model, apiUrl, integration, resourceType}));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
})
|
}
|
||||||
.then(() => {
|
|
||||||
adapter.run();
|
adapter.run();
|
||||||
})
|
|
||||||
.then(() => {
|
|
||||||
SCHEDULED_RESOURCES.forEach((resource) => {
|
SCHEDULED_RESOURCES.forEach((resource) => {
|
||||||
events.on(`${resource}.scheduled`, (model) => {
|
events.on(`${resource}.scheduled`, (model) => {
|
||||||
adapter.schedule(_private.normalize({model, apiUrl, integration, resourceType: resource}));
|
adapter.schedule(_private.normalize({model, apiUrl, integration, resourceType: resource}));
|
||||||
|
@ -124,5 +116,4 @@ exports.init = function init(options = {}) {
|
||||||
adapter.unschedule(_private.normalize({model, apiUrl, integration, resourceType: resource}, 'unscheduled'));
|
adapter.unschedule(_private.normalize({model, apiUrl, integration, resourceType: resource}, 'unscheduled'));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue