mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -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.
|
||||
* @return {Promise}
|
||||
*/
|
||||
_private.loadScheduledResources = function () {
|
||||
_private.loadScheduledResources = async function () {
|
||||
const api = require('../../../api');
|
||||
// Fetches all scheduled resources(posts/pages) with default API
|
||||
return Promise.mapSeries(SCHEDULED_RESOURCES, (resourceType) => {
|
||||
return api.schedules.getScheduled.query({
|
||||
const results = await Promise.mapSeries(SCHEDULED_RESOURCES, async (resourceType) => {
|
||||
const result = await api.schedules.getScheduled.query({
|
||||
options: {
|
||||
resource: resourceType
|
||||
}
|
||||
}).then((result) => {
|
||||
return result[resourceType] || [];
|
||||
});
|
||||
}).then((results) => {
|
||||
return SCHEDULED_RESOURCES.reduce(function (obj, entry, index) {
|
||||
return Object.assign(obj, {
|
||||
[entry]: results[index]
|
||||
});
|
||||
}, {});
|
||||
|
||||
return result[resourceType] || [];
|
||||
});
|
||||
|
||||
return SCHEDULED_RESOURCES.reduce(function (obj, entry, index) {
|
||||
return Object.assign(obj, {
|
||||
[entry]: results[index]
|
||||
});
|
||||
}, {});
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -60,7 +60,7 @@ _private.loadScheduledResources = function () {
|
|||
* @param {Object} options
|
||||
* @return {*}
|
||||
*/
|
||||
exports.init = function init(options = {}) {
|
||||
exports.init = async function init(options = {}) {
|
||||
const {apiUrl} = options;
|
||||
let adapter = null;
|
||||
let integration = null;
|
||||
|
@ -73,56 +73,47 @@ exports.init = function init(options = {}) {
|
|||
return Promise.reject(new errors.IncorrectUsageError({message: 'post-scheduling: no apiUrl was provided'}));
|
||||
}
|
||||
|
||||
return getSchedulerIntegration()
|
||||
.then((_integration) => {
|
||||
integration = _integration;
|
||||
return localUtils.createAdapter();
|
||||
})
|
||||
.then((_adapter) => {
|
||||
adapter = _adapter;
|
||||
integration = await getSchedulerIntegration();
|
||||
adapter = await localUtils.createAdapter();
|
||||
|
||||
if (!adapter.rescheduleOnBoot) {
|
||||
return [];
|
||||
}
|
||||
let scheduledResources;
|
||||
|
||||
return _private.loadScheduledResources();
|
||||
})
|
||||
.then((scheduledResources) => {
|
||||
if (!Object.keys(scheduledResources).length) {
|
||||
return;
|
||||
}
|
||||
if (!adapter.rescheduleOnBoot) {
|
||||
scheduledResources = [];
|
||||
} else {
|
||||
scheduledResources = await _private.loadScheduledResources();
|
||||
}
|
||||
|
||||
// Reschedules all scheduled resources on boot
|
||||
// NOTE: We are using reschedule, because custom scheduling adapter could use a database, which needs to be updated
|
||||
// and not an in-process implementation!
|
||||
Object.keys(scheduledResources).forEach((resourceType) => {
|
||||
scheduledResources[resourceType].forEach((model) => {
|
||||
adapter.unschedule(_private.normalize({model, apiUrl, integration, resourceType}, 'unscheduled'), {bootstrap: true});
|
||||
adapter.schedule(_private.normalize({model, apiUrl, integration, resourceType}));
|
||||
});
|
||||
});
|
||||
})
|
||||
.then(() => {
|
||||
adapter.run();
|
||||
})
|
||||
.then(() => {
|
||||
SCHEDULED_RESOURCES.forEach((resource) => {
|
||||
events.on(`${resource}.scheduled`, (model) => {
|
||||
adapter.schedule(_private.normalize({model, apiUrl, integration, resourceType: resource}));
|
||||
});
|
||||
|
||||
/** We want to do reschedule as (unschedule + schedule) due to how token(+url) is generated
|
||||
* We want to first remove existing schedule by generating a matching token(+url)
|
||||
* followed by generating a new token(+url) for the new schedule
|
||||
*/
|
||||
events.on(`${resource}.rescheduled`, (model) => {
|
||||
adapter.unschedule(_private.normalize({model, apiUrl, integration, resourceType: resource}, 'unscheduled'));
|
||||
adapter.schedule(_private.normalize({model, apiUrl, integration, resourceType: resource}));
|
||||
});
|
||||
|
||||
events.on(`${resource}.unscheduled`, (model) => {
|
||||
adapter.unschedule(_private.normalize({model, apiUrl, integration, resourceType: resource}, 'unscheduled'));
|
||||
});
|
||||
if (Object.keys(scheduledResources).length) {
|
||||
// Reschedules all scheduled resources on boot
|
||||
// NOTE: We are using reschedule, because custom scheduling adapter could use a database, which needs to be updated
|
||||
// and not an in-process implementation!
|
||||
Object.keys(scheduledResources).forEach((resourceType) => {
|
||||
scheduledResources[resourceType].forEach((model) => {
|
||||
adapter.unschedule(_private.normalize({model, apiUrl, integration, resourceType}, 'unscheduled'), {bootstrap: true});
|
||||
adapter.schedule(_private.normalize({model, apiUrl, integration, resourceType}));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
adapter.run();
|
||||
|
||||
SCHEDULED_RESOURCES.forEach((resource) => {
|
||||
events.on(`${resource}.scheduled`, (model) => {
|
||||
adapter.schedule(_private.normalize({model, apiUrl, integration, resourceType: resource}));
|
||||
});
|
||||
|
||||
/** We want to do reschedule as (unschedule + schedule) due to how token(+url) is generated
|
||||
* We want to first remove existing schedule by generating a matching token(+url)
|
||||
* followed by generating a new token(+url) for the new schedule
|
||||
*/
|
||||
events.on(`${resource}.rescheduled`, (model) => {
|
||||
adapter.unschedule(_private.normalize({model, apiUrl, integration, resourceType: resource}, 'unscheduled'));
|
||||
adapter.schedule(_private.normalize({model, apiUrl, integration, resourceType: resource}));
|
||||
});
|
||||
|
||||
events.on(`${resource}.unscheduled`, (model) => {
|
||||
adapter.unschedule(_private.normalize({model, apiUrl, integration, resourceType: resource}, 'unscheduled'));
|
||||
});
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue