Ghost/core/server/adapters/scheduling/post-scheduling/index.js
Naz d67ee68b0d Fixed initialization logic in post scheduler
refs e370d33378

- The initialization logic was simplified and not refactored in all placed during the refed change
2021-05-26 10:44:11 +04:00

55 lines
1.5 KiB
JavaScript

const Promise = require('bluebird');
const events = require('../../../lib/common/events');
const localUtils = require('../utils');
const PostScheduler = require('./post-scheduler');
const getSchedulerIntegration = require('./scheduler-intergation');
/**
* @description Load all scheduled posts/pages from database.
* @return {Promise}
*/
const loadScheduledResources = async function () {
const api = require('../../../api');
const SCHEDULED_RESOURCES = ['post', 'page'];
// Fetches all scheduled resources(posts/pages) with default API
const results = await Promise.mapSeries(SCHEDULED_RESOURCES, async (resourceType) => {
const result = await api.schedules.getScheduled.query({
options: {
resource: resourceType
}
});
return result[resourceType] || [];
});
return SCHEDULED_RESOURCES.reduce(function (obj, entry, index) {
return Object.assign(obj, {
[entry]: results[index]
});
}, {});
};
const init = async (options) => {
const integration = await getSchedulerIntegration();
const adapter = await localUtils.createAdapter();
let scheduledResources;
if (!adapter.rescheduleOnBoot) {
scheduledResources = [];
} else {
scheduledResources = await loadScheduledResources();
}
return new PostScheduler({
apiUrl: options.apiUrl,
integration,
adapter,
scheduledResources,
events
});
};
module.exports = init;