mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-10 23:36:14 -05:00
Refactored URL service code to aid with debugging
- replaced a couple of uses of lodash.each in favor of native for loops - tidied up `debug` statements and spacing - pulled out common statements into variables
This commit is contained in:
parent
c11c516e14
commit
9c6992535b
5 changed files with 27 additions and 33 deletions
|
@ -93,7 +93,7 @@ class Queue extends EventEmitter {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
debug('add', options.event, options.tolerance);
|
debug('register', options.event, options.tolerance);
|
||||||
|
|
||||||
this.queue[options.event].subscribers.push(fn);
|
this.queue[options.event].subscribers.push(fn);
|
||||||
}
|
}
|
||||||
|
@ -103,19 +103,20 @@ class Queue extends EventEmitter {
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
*/
|
*/
|
||||||
run(options) {
|
run(options) {
|
||||||
const event = options.event;
|
const {event, action, eventData} = options;
|
||||||
const action = options.action;
|
|
||||||
const eventData = options.eventData;
|
|
||||||
|
|
||||||
clearTimeout(this.toNotify[action].timeout);
|
clearTimeout(this.toNotify[action].timeout);
|
||||||
this.toNotify[action].timeout = null;
|
this.toNotify[action].timeout = null;
|
||||||
|
|
||||||
debug('run', action, event, this.queue[event].subscribers.length, this.toNotify[action].notified.length);
|
const subscribers = this.queue[event].subscribers;
|
||||||
|
const notified = this.toNotify[action].notified;
|
||||||
|
|
||||||
if (this.queue[event].subscribers.length && this.queue[event].subscribers.length !== this.toNotify[action].notified.length) {
|
debug('run', action, event, subscribers.length, notified.length);
|
||||||
const fn = this.queue[event].subscribers[this.toNotify[action].notified.length];
|
|
||||||
|
|
||||||
debug('execute', action, event, this.toNotify[action].notified.length);
|
if (subscribers.length && subscribers.length !== notified.length) {
|
||||||
|
const fn = subscribers[notified.length];
|
||||||
|
|
||||||
|
debug('run.execute', action, event, notified.length);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @NOTE: Currently no async operations happen in the subscribers functions.
|
* @NOTE: Currently no async operations happen in the subscribers functions.
|
||||||
|
@ -124,7 +125,7 @@ class Queue extends EventEmitter {
|
||||||
try {
|
try {
|
||||||
fn(eventData);
|
fn(eventData);
|
||||||
|
|
||||||
debug('executed', action, event, this.toNotify[action].notified.length);
|
debug('run.executed', action, event, notified.length);
|
||||||
this.toNotify[action].notified.push(fn);
|
this.toNotify[action].notified.push(fn);
|
||||||
this.run(options);
|
this.run(options);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
@ -144,15 +145,15 @@ class Queue extends EventEmitter {
|
||||||
// CASE 3: wait for more subscribers, i am still tolerant
|
// CASE 3: wait for more subscribers, i am still tolerant
|
||||||
if (this.queue[event].tolerance === 0) {
|
if (this.queue[event].tolerance === 0) {
|
||||||
delete this.toNotify[action];
|
delete this.toNotify[action];
|
||||||
debug('ended (1)', event, action);
|
debug('run.ended (1)', event, action);
|
||||||
this.emit('ended', event);
|
this.emit('ended', event);
|
||||||
} else if (this.queue[options.event].subscribers.length >= this.queue[options.event].requiredSubscriberCount &&
|
} else if (subscribers.length >= this.queue[event].requiredSubscriberCount &&
|
||||||
this.toNotify[action].timeoutInMS > this.queue[event].tolerance) {
|
this.toNotify[action].timeoutInMS > this.queue[event].tolerance) {
|
||||||
delete this.toNotify[action];
|
delete this.toNotify[action];
|
||||||
debug('ended (2)', event, action);
|
debug('run.ended (2)', event, action);
|
||||||
this.emit('ended', event);
|
this.emit('ended', event);
|
||||||
} else {
|
} else {
|
||||||
debug('retry', event, action, this.toNotify[action].timeoutInMS);
|
debug('run.retry', event, action, this.toNotify[action].timeoutInMS);
|
||||||
|
|
||||||
this.toNotify[action].timeoutInMS = this.toNotify[action].timeoutInMS * 1.1;
|
this.toNotify[action].timeoutInMS = this.toNotify[action].timeoutInMS * 1.1;
|
||||||
|
|
||||||
|
|
|
@ -128,12 +128,13 @@ class Resources {
|
||||||
modelOptions.limit = options.limit;
|
modelOptions.limit = options.limit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const now = Date.now();
|
||||||
const objects = await models.Base.Model.raw_knex.fetchAll(modelOptions);
|
const objects = await models.Base.Model.raw_knex.fetchAll(modelOptions);
|
||||||
debug('fetched', resourceConfig.type, objects.length);
|
debug('_fetch.fetched', resourceConfig.type, objects.length, `${Date.now() - now}ms`);
|
||||||
|
|
||||||
_.each(objects, (object) => {
|
for (const object of objects) {
|
||||||
this.data[resourceConfig.type].push(new Resource(resourceConfig.type, object));
|
this.data[resourceConfig.type].push(new Resource(resourceConfig.type, object));
|
||||||
});
|
}
|
||||||
|
|
||||||
if (objects.length && isSQLite) {
|
if (objects.length && isSQLite) {
|
||||||
options.offset = options.offset + options.limit;
|
options.offset = options.offset + options.limit;
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
const _ = require('lodash');
|
|
||||||
const nql = require('@tryghost/nql');
|
const nql = require('@tryghost/nql');
|
||||||
const debug = require('@tryghost/debug')('services:url:generator');
|
const debug = require('@tryghost/debug')('services:url:generator');
|
||||||
const localUtils = require('../../../shared/url-utils');
|
const localUtils = require('../../../shared/url-utils');
|
||||||
|
@ -121,16 +120,14 @@ class UrlGenerator {
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
_onInit() {
|
_onInit() {
|
||||||
debug('_onInit', this.resourceType);
|
|
||||||
|
|
||||||
// @NOTE: get the resources of my type e.g. posts.
|
// @NOTE: get the resources of my type e.g. posts.
|
||||||
const resources = this.resources.getAllByType(this.resourceType);
|
const resources = this.resources.getAllByType(this.resourceType);
|
||||||
|
|
||||||
debug(resources.length);
|
debug('_onInit', this.resourceType, resources.length);
|
||||||
|
|
||||||
_.each(resources, (resource) => {
|
for (const resource of resources) {
|
||||||
this._try(resource);
|
this._try(resource);
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -141,7 +138,7 @@ class UrlGenerator {
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
_onAdded(event) {
|
_onAdded(event) {
|
||||||
debug('onAdded', this.toString());
|
debug('_onAdded', this.toString());
|
||||||
|
|
||||||
// CASE: you are type "pages", but the incoming type is "users"
|
// CASE: you are type "pages", but the incoming type is "users"
|
||||||
if (event.type !== this.resourceType) {
|
if (event.type !== this.resourceType) {
|
||||||
|
@ -149,7 +146,6 @@ class UrlGenerator {
|
||||||
}
|
}
|
||||||
|
|
||||||
const resource = this.resources.getByIdAndType(event.type, event.id);
|
const resource = this.resources.getByIdAndType(event.type, event.id);
|
||||||
|
|
||||||
this._try(resource);
|
this._try(resource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
const _debug = require('@tryghost/debug')._base;
|
const debug = require('@tryghost/debug')('services:url:service');
|
||||||
const debug = _debug('ghost:services:url:service');
|
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const errors = require('@tryghost/errors');
|
const errors = require('@tryghost/errors');
|
||||||
const labs = require('../../../shared/labs');
|
const labs = require('../../../shared/labs');
|
||||||
|
@ -92,7 +91,7 @@ class UrlService {
|
||||||
* @param {String} permalink
|
* @param {String} permalink
|
||||||
*/
|
*/
|
||||||
onRouterAddedType(identifier, filter, resourceType, permalink) {
|
onRouterAddedType(identifier, filter, resourceType, permalink) {
|
||||||
debug('Registering route: ', filter, resourceType, permalink);
|
debug('Registering route:', filter, resourceType, permalink);
|
||||||
|
|
||||||
let urlGenerator = new UrlGenerator({
|
let urlGenerator = new UrlGenerator({
|
||||||
identifier,
|
identifier,
|
||||||
|
|
|
@ -37,11 +37,8 @@ class Urls {
|
||||||
* @param {string} options.url
|
* @param {string} options.url
|
||||||
*/
|
*/
|
||||||
add(options) {
|
add(options) {
|
||||||
const url = options.url;
|
const {url, generatorId, resource} = options;
|
||||||
const generatorId = options.generatorId;
|
debug('add', resource.data.id, url);
|
||||||
const resource = options.resource;
|
|
||||||
|
|
||||||
debug('cache', url);
|
|
||||||
|
|
||||||
if (this.urls[resource.data.id]) {
|
if (this.urls[resource.data.id]) {
|
||||||
const error = new errors.InternalServerError({
|
const error = new errors.InternalServerError({
|
||||||
|
@ -131,7 +128,7 @@ class Urls {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
debug('removed', this.urls[id].url, this.urls[id].generatorId);
|
debug('removeResourceId', this.urls[id].url, this.urls[id].generatorId);
|
||||||
|
|
||||||
events.emit('url.removed', {
|
events.emit('url.removed', {
|
||||||
url: this.urls[id].url,
|
url: this.urls[id].url,
|
||||||
|
|
Loading…
Add table
Reference in a new issue