Booting instance with persisted urls

refs https://github.com/TryGhost/Toolbox/issues/116

- This is a PoC to check out how viable this approach is and if it's worth merging into main as a very quick win
- The `urls.json` is in a bad place right now should probably live in a data folder
This commit is contained in:
Naz 2021-11-10 14:19:42 +04:00 committed by naz
parent f5d46d0da0
commit 36bc05447c
3 changed files with 643 additions and 3 deletions

View File

@ -1,3 +1,4 @@
const fs = require('fs-extra');
const _debug = require('@tryghost/debug')._base;
const debug = _debug('ghost:services:url:service');
const _ = require('lodash');
@ -23,6 +24,7 @@ class UrlService {
this.finished = false;
this.urlGenerators = [];
// Get urls
this.urls = new Urls();
this.queue = new Queue();
this.resources = new Resources(this.queue);
@ -286,7 +288,16 @@ class UrlService {
async init() {
this.resources.initResourceConfig();
this.resources.initEvenListeners();
await this.resources.fetchResources();
const persistedUrls = await this.fetchUrls();
if (persistedUrls) {
this.urls = new Urls({
urls: persistedUrls
});
this.finished = true;
} else {
await this.resources.fetchResources();
}
// CASE: all resources are fetched, start the queue
this.queue.start({
@ -296,6 +307,33 @@ class UrlService {
});
}
async persistUrls() {
return fs.writeFileSync('./urls.json', JSON.stringify(this.urls.urls, null, 4));
}
async fetchUrls() {
let urlsCacheExists = false;
let urls;
try {
await fs.stat('./urls.json');
urlsCacheExists = true;
} catch (e) {
urlsCacheExists = false;
}
if (urlsCacheExists) {
try {
const urlsFile = await fs.readFile('./urls.json', 'utf8');
urls = JSON.parse(urlsFile);
} catch (e) {
//noop as we'd start a long boot process if there are any errors in the file
}
}
return urls;
}
/**
* @description Reset this service.
* @param {Object} options

View File

@ -20,8 +20,13 @@ const events = require('../../lib/common/events');
* You can easily ask `this.urls[resourceId]`.
*/
class Urls {
constructor() {
this.urls = {};
/**
*
* @param {Object} [options]
* @param {Object} [options.urls] map of available URLs with their resources
*/
constructor({urls = {}} = {}) {
this.urls = urls;
}
/**

597
urls.json Normal file
View File

@ -0,0 +1,597 @@
{
"618b9c362ca142fcb133ed92": {
"url": "/integrations/",
"generatorId": 0,
"resource": {
"_events": {},
"_eventsCount": 2,
"data": {
"id": "618b9c362ca142fcb133ed92",
"uuid": "1014c0fe-ffe4-4f7f-86fe-58c959c7aa18",
"slug": "integrations",
"comment_id": "618b9c362ca142fcb133ed92",
"feature_image": "https://static.ghost.org/v4.0.0/images/app-integrations.png",
"featured": false,
"type": "post",
"visibility": "public",
"email_recipient_filter": "none",
"created_at": "2021-11-10T10:17:26.000Z",
"created_by": "1",
"updated_at": "2021-11-10T10:17:26.000Z",
"updated_by": "1",
"published_at": "2021-11-10T10:17:26.000Z",
"published_by": "1",
"canonical_url": null,
"tags": [
{
"post_id": "618b9c362ca142fcb133ed92",
"visibility": "public",
"id": "618b9c352ca142fcb133ed2c",
"slug": "getting-started"
}
],
"authors": [
{
"post_id": "618b9c362ca142fcb133ed92",
"id": "5951f5fca366002ebd5dbef7",
"slug": "ghost"
}
],
"author": "5951f5fca366002ebd5dbef7",
"primary_author": {
"post_id": "618b9c362ca142fcb133ed92",
"id": "5951f5fca366002ebd5dbef7",
"slug": "ghost"
},
"primary_tag": {
"post_id": "618b9c362ca142fcb133ed92",
"visibility": "public",
"id": "618b9c352ca142fcb133ed2c",
"slug": "getting-started"
}
},
"config": {
"type": "posts",
"reserved": true
}
}
},
"618b9c372ca142fcb133ed94": {
"url": "/grow/",
"generatorId": 0,
"resource": {
"_events": {},
"_eventsCount": 2,
"data": {
"id": "618b9c372ca142fcb133ed94",
"uuid": "760cd9f7-4972-4899-980f-dde8c4dbb238",
"slug": "grow",
"comment_id": "618b9c372ca142fcb133ed94",
"feature_image": "https://static.ghost.org/v4.0.0/images/admin-settings.png",
"featured": false,
"type": "post",
"visibility": "public",
"email_recipient_filter": "none",
"created_at": "2021-11-10T10:17:27.000Z",
"created_by": "1",
"updated_at": "2021-11-10T10:17:27.000Z",
"updated_by": "1",
"published_at": "2021-11-10T10:17:27.000Z",
"published_by": "1",
"canonical_url": null,
"tags": [
{
"post_id": "618b9c372ca142fcb133ed94",
"visibility": "public",
"id": "618b9c352ca142fcb133ed2c",
"slug": "getting-started"
}
],
"authors": [
{
"post_id": "618b9c372ca142fcb133ed94",
"id": "5951f5fca366002ebd5dbef7",
"slug": "ghost"
}
],
"author": "5951f5fca366002ebd5dbef7",
"primary_author": {
"post_id": "618b9c372ca142fcb133ed94",
"id": "5951f5fca366002ebd5dbef7",
"slug": "ghost"
},
"primary_tag": {
"post_id": "618b9c372ca142fcb133ed94",
"visibility": "public",
"id": "618b9c352ca142fcb133ed2c",
"slug": "getting-started"
}
},
"config": {
"type": "posts",
"reserved": true
}
}
},
"618b9c372ca142fcb133ed96": {
"url": "/sell/",
"generatorId": 0,
"resource": {
"_events": {},
"_eventsCount": 2,
"data": {
"id": "618b9c372ca142fcb133ed96",
"uuid": "85982fdf-4788-463c-8da0-41996425f23d",
"slug": "sell",
"comment_id": "618b9c372ca142fcb133ed96",
"feature_image": "https://static.ghost.org/v4.0.0/images/organizing-your-content.png",
"featured": false,
"type": "post",
"visibility": "paid",
"email_recipient_filter": "none",
"created_at": "2021-11-10T10:17:27.000Z",
"created_by": "1",
"updated_at": "2021-11-10T10:17:27.000Z",
"updated_by": "1",
"published_at": "2021-11-10T10:17:28.000Z",
"published_by": "1",
"canonical_url": null,
"tags": [
{
"post_id": "618b9c372ca142fcb133ed96",
"visibility": "public",
"id": "618b9c352ca142fcb133ed2c",
"slug": "getting-started"
}
],
"authors": [
{
"post_id": "618b9c372ca142fcb133ed96",
"id": "5951f5fca366002ebd5dbef7",
"slug": "ghost"
}
],
"author": "5951f5fca366002ebd5dbef7",
"primary_author": {
"post_id": "618b9c372ca142fcb133ed96",
"id": "5951f5fca366002ebd5dbef7",
"slug": "ghost"
},
"primary_tag": {
"post_id": "618b9c372ca142fcb133ed96",
"visibility": "public",
"id": "618b9c352ca142fcb133ed2c",
"slug": "getting-started"
}
},
"config": {
"type": "posts",
"reserved": true
}
}
},
"618b9c372ca142fcb133ed98": {
"url": "/portal/",
"generatorId": 0,
"resource": {
"_events": {},
"_eventsCount": 2,
"data": {
"id": "618b9c372ca142fcb133ed98",
"uuid": "564c2a9e-0248-4a55-916a-8ac7c49a412c",
"slug": "portal",
"comment_id": "618b9c372ca142fcb133ed98",
"feature_image": "https://static.ghost.org/v4.0.0/images/creating-a-custom-theme.png",
"featured": false,
"type": "post",
"visibility": "public",
"email_recipient_filter": "none",
"created_at": "2021-11-10T10:17:27.000Z",
"created_by": "1",
"updated_at": "2021-11-10T10:17:27.000Z",
"updated_by": "1",
"published_at": "2021-11-10T10:17:29.000Z",
"published_by": "1",
"canonical_url": null,
"tags": [
{
"post_id": "618b9c372ca142fcb133ed98",
"visibility": "public",
"id": "618b9c352ca142fcb133ed2c",
"slug": "getting-started"
}
],
"authors": [
{
"post_id": "618b9c372ca142fcb133ed98",
"id": "5951f5fca366002ebd5dbef7",
"slug": "ghost"
}
],
"author": "5951f5fca366002ebd5dbef7",
"primary_author": {
"post_id": "618b9c372ca142fcb133ed98",
"id": "5951f5fca366002ebd5dbef7",
"slug": "ghost"
},
"primary_tag": {
"post_id": "618b9c372ca142fcb133ed98",
"visibility": "public",
"id": "618b9c352ca142fcb133ed2c",
"slug": "getting-started"
}
},
"config": {
"type": "posts",
"reserved": true
}
}
},
"618b9c372ca142fcb133ed9a": {
"url": "/write/",
"generatorId": 0,
"resource": {
"_events": {},
"_eventsCount": 2,
"data": {
"id": "618b9c372ca142fcb133ed9a",
"uuid": "71fc2d7c-dbe1-4a56-adb9-f74c8cadbfc6",
"slug": "write",
"comment_id": "618b9c372ca142fcb133ed9a",
"feature_image": "https://static.ghost.org/v4.0.0/images/writing-posts-with-ghost.png",
"featured": false,
"type": "post",
"visibility": "public",
"email_recipient_filter": "none",
"created_at": "2021-11-10T10:17:27.000Z",
"created_by": "1",
"updated_at": "2021-11-10T10:17:27.000Z",
"updated_by": "1",
"published_at": "2021-11-10T10:17:30.000Z",
"published_by": "1",
"canonical_url": null,
"tags": [
{
"post_id": "618b9c372ca142fcb133ed9a",
"visibility": "public",
"id": "618b9c352ca142fcb133ed2c",
"slug": "getting-started"
}
],
"authors": [
{
"post_id": "618b9c372ca142fcb133ed9a",
"id": "5951f5fca366002ebd5dbef7",
"slug": "ghost"
}
],
"author": "5951f5fca366002ebd5dbef7",
"primary_author": {
"post_id": "618b9c372ca142fcb133ed9a",
"id": "5951f5fca366002ebd5dbef7",
"slug": "ghost"
},
"primary_tag": {
"post_id": "618b9c372ca142fcb133ed9a",
"visibility": "public",
"id": "618b9c352ca142fcb133ed2c",
"slug": "getting-started"
}
},
"config": {
"type": "posts",
"reserved": true
}
}
},
"618b9c372ca142fcb133ed9c": {
"url": "/design/",
"generatorId": 0,
"resource": {
"_events": {},
"_eventsCount": 2,
"data": {
"id": "618b9c372ca142fcb133ed9c",
"uuid": "cdd30e29-aace-41b9-8d0a-67f4697db701",
"slug": "design",
"comment_id": "618b9c372ca142fcb133ed9c",
"feature_image": "https://static.ghost.org/v4.0.0/images/publishing-options.png",
"featured": false,
"type": "post",
"visibility": "public",
"email_recipient_filter": "none",
"created_at": "2021-11-10T10:17:27.000Z",
"created_by": "1",
"updated_at": "2021-11-10T10:17:27.000Z",
"updated_by": "1",
"published_at": "2021-11-10T10:17:31.000Z",
"published_by": "1",
"canonical_url": null,
"tags": [
{
"post_id": "618b9c372ca142fcb133ed9c",
"visibility": "public",
"id": "618b9c352ca142fcb133ed2c",
"slug": "getting-started"
}
],
"authors": [
{
"post_id": "618b9c372ca142fcb133ed9c",
"id": "5951f5fca366002ebd5dbef7",
"slug": "ghost"
}
],
"author": "5951f5fca366002ebd5dbef7",
"primary_author": {
"post_id": "618b9c372ca142fcb133ed9c",
"id": "5951f5fca366002ebd5dbef7",
"slug": "ghost"
},
"primary_tag": {
"post_id": "618b9c372ca142fcb133ed9c",
"visibility": "public",
"id": "618b9c352ca142fcb133ed2c",
"slug": "getting-started"
}
},
"config": {
"type": "posts",
"reserved": true
}
}
},
"618b9c372ca142fcb133ed9e": {
"url": "/welcome/",
"generatorId": 0,
"resource": {
"_events": {},
"_eventsCount": 2,
"data": {
"id": "618b9c372ca142fcb133ed9e",
"uuid": "b1114329-ee44-4adf-bb96-cbb0da751495",
"slug": "welcome",
"comment_id": "618b9c372ca142fcb133ed9e",
"feature_image": "https://static.ghost.org/v4.0.0/images/welcome-to-ghost.png",
"featured": false,
"type": "post",
"visibility": "public",
"email_recipient_filter": "none",
"created_at": "2021-11-10T10:17:27.000Z",
"created_by": "1",
"updated_at": "2021-11-10T10:17:27.000Z",
"updated_by": "1",
"published_at": "2021-11-10T10:17:32.000Z",
"published_by": "1",
"canonical_url": null,
"tags": [
{
"post_id": "618b9c372ca142fcb133ed9e",
"visibility": "public",
"id": "618b9c352ca142fcb133ed2c",
"slug": "getting-started"
}
],
"authors": [
{
"post_id": "618b9c372ca142fcb133ed9e",
"id": "5951f5fca366002ebd5dbef7",
"slug": "ghost"
}
],
"author": "5951f5fca366002ebd5dbef7",
"primary_author": {
"post_id": "618b9c372ca142fcb133ed9e",
"id": "5951f5fca366002ebd5dbef7",
"slug": "ghost"
},
"primary_tag": {
"post_id": "618b9c372ca142fcb133ed9e",
"visibility": "public",
"id": "618b9c352ca142fcb133ed2c",
"slug": "getting-started"
}
},
"config": {
"type": "posts",
"reserved": true
}
}
},
"618b9c372ca142fcb133eda0": {
"url": "/about/",
"generatorId": 1,
"resource": {
"_events": {},
"_eventsCount": 2,
"data": {
"id": "618b9c372ca142fcb133eda0",
"uuid": "c44a967e-286a-4e7a-9ebe-18d7c6132b13",
"slug": "about",
"comment_id": "618b9c372ca142fcb133eda0",
"feature_image": null,
"featured": false,
"type": "page",
"status": "published",
"visibility": "public",
"email_recipient_filter": "none",
"created_at": "2021-11-10T10:17:27.000Z",
"created_by": "1",
"updated_at": "2021-11-10T10:17:27.000Z",
"updated_by": "1",
"published_at": "2021-11-10T10:17:33.000Z",
"published_by": "1",
"canonical_url": null,
"author": "5951f5fca366002ebd5dbef7",
"primary_author": null,
"primary_tag": null
},
"config": {
"type": "pages",
"reserved": true
}
}
},
"618b9c372ca142fcb133eda2": {
"url": "/contact/",
"generatorId": 1,
"resource": {
"_events": {},
"_eventsCount": 2,
"data": {
"id": "618b9c372ca142fcb133eda2",
"uuid": "d4e907a2-0fb7-400a-b068-0959ff591ac2",
"slug": "contact",
"comment_id": "618b9c372ca142fcb133eda2",
"feature_image": null,
"featured": false,
"type": "page",
"status": "published",
"visibility": "public",
"email_recipient_filter": "none",
"created_at": "2021-11-10T10:17:27.000Z",
"created_by": "1",
"updated_at": "2021-11-10T10:17:27.000Z",
"updated_by": "1",
"published_at": "2021-11-10T10:17:34.000Z",
"published_by": "1",
"canonical_url": null,
"author": "5951f5fca366002ebd5dbef7",
"primary_author": null,
"primary_tag": null
},
"config": {
"type": "pages",
"reserved": true
}
}
},
"618b9c372ca142fcb133eda4": {
"url": "/privacy/",
"generatorId": 1,
"resource": {
"_events": {},
"_eventsCount": 2,
"data": {
"id": "618b9c372ca142fcb133eda4",
"uuid": "3d0261a9-f165-4d50-b7f4-c2001afa3685",
"slug": "privacy",
"comment_id": "618b9c372ca142fcb133eda4",
"feature_image": null,
"featured": false,
"type": "page",
"status": "published",
"visibility": "public",
"email_recipient_filter": "none",
"created_at": "2021-11-10T10:17:27.000Z",
"created_by": "1",
"updated_at": "2021-11-10T10:17:27.000Z",
"updated_by": "1",
"published_at": "2021-11-10T10:17:35.000Z",
"published_by": "1",
"canonical_url": null,
"author": "5951f5fca366002ebd5dbef7",
"primary_author": null,
"primary_tag": null
},
"config": {
"type": "pages",
"reserved": true
}
}
},
"618b9c372ca142fcb133eda6": {
"url": "/contribute/",
"generatorId": 1,
"resource": {
"_events": {},
"_eventsCount": 2,
"data": {
"id": "618b9c372ca142fcb133eda6",
"uuid": "e892e57d-5020-46ce-a673-a79ed113b8b8",
"slug": "contribute",
"comment_id": "618b9c372ca142fcb133eda6",
"feature_image": null,
"featured": false,
"type": "page",
"status": "published",
"visibility": "public",
"email_recipient_filter": "none",
"created_at": "2021-11-10T10:17:27.000Z",
"created_by": "1",
"updated_at": "2021-11-10T10:17:27.000Z",
"updated_by": "1",
"published_at": "2021-11-10T10:17:36.000Z",
"published_by": "1",
"canonical_url": null,
"author": "5951f5fca366002ebd5dbef7",
"primary_author": null,
"primary_tag": null
},
"config": {
"type": "pages",
"reserved": true
}
}
},
"618b9c352ca142fcb133ed2c": {
"url": "/tag/getting-started/",
"generatorId": 2,
"resource": {
"_events": {},
"_eventsCount": 2,
"data": {
"id": "618b9c352ca142fcb133ed2c",
"name": "Getting Started",
"slug": "getting-started",
"feature_image": null,
"visibility": "public",
"og_image": null,
"og_title": null,
"og_description": null,
"twitter_image": null,
"twitter_title": null,
"twitter_description": null,
"codeinjection_head": null,
"codeinjection_foot": null,
"canonical_url": null,
"accent_color": null,
"created_at": "2021-11-10T10:17:25.000Z",
"created_by": "1",
"updated_at": "2021-11-10T10:17:25.000Z",
"updated_by": "1"
},
"config": {
"type": "tags",
"reserved": true
}
}
},
"5951f5fca366002ebd5dbef7": {
"url": "/author/ghost/",
"generatorId": 3,
"resource": {
"_events": {},
"_eventsCount": 2,
"data": {
"id": "5951f5fca366002ebd5dbef7",
"name": "Ghost",
"slug": "ghost",
"email": "ghost-author@example.com",
"profile_image": "https://static.ghost.org/v4.0.0/images/ghost-user.png",
"cover_image": null,
"status": "active",
"visibility": "public",
"last_seen": null,
"created_at": "2021-11-10T10:17:26.000Z",
"created_by": "1",
"updated_at": "2021-11-10T10:17:26.000Z",
"updated_by": "1"
},
"config": {
"type": "authors",
"reserved": true
}
}
}
}