Browse Source

newsroom updates

develop
Rob Colbert 10 months ago
parent
commit
8635a374a9
  1. 21
      .vscode/launch.json
  2. 23
      app/workers/newsroom.js
  3. 14
      lib/site-worker.js

21
.vscode/launch.json

@ -30,6 +30,27 @@
"console": "integratedTerminal",
"args": ["--action=reset-indexes", "all"]
},
{
"type": "node",
"request": "launch",
"name": "worker:newsletter",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder:dtp-base}/app/workers/newsletter.js",
"console": "integratedTerminal",
},
{
"type": "node",
"request": "launch",
"name": "worker:newsroom",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder:dtp-base}/app/workers/newsroom.js",
"console": "integratedTerminal",
},
{
"type": "node",
"request": "launch",

23
app/workers/newsroom.js

@ -9,6 +9,8 @@ const path = require('path');
require('dotenv').config({ path: path.resolve(__dirname, '..', '..', '.env') });
const mongoose = require('mongoose');
const moment = require('moment');
const { read: feedReader } = require('feed-reader');
const {
@ -53,13 +55,30 @@ class NewsroomWorker extends SiteWorker {
const NOW = new Date();
const { feed: feedService } = this.dtp.services;
try {
this.log.info('loading latest feed data', { feedId: feed._id, title: feed.title });
this.log.info('loading latest feed data', {
feed: {
_id: feed._id,
title: feed.title,
published: {
date: feed.published,
moment: moment(feed.published).fromNow(),
},
},
});
const response = await feedReader(feed.url);
await SiteAsync.each(response.entries, async (entry) => {
await Feed.updateOne({ _id: feed._id }, { $set: { published: feed.published || NOW }});
await feedService.createEntry(feed, entry);
}, 4);
this.log.info('feed updated', { entries: response.entries.length });
this.log.info('feed updated', {
feed: {
_id: feed._id,
title: feed.title,
},
entryCount: response.entries.length,
});
} catch (error) {
this.log.error('failed to update feed', { feedId: feed._id, title: feed.title, error });
}

14
lib/site-worker.js

@ -71,7 +71,7 @@ class SiteWorker extends SiteCommon {
const { COMPONENT } = ProcessorClass;
this.log.info('loading worker processor', { component: COMPONENT.logId });
this.processors[COMPONENT.name] = processor;
this.processors[COMPONENT.index] = processor;
return processor;
}
@ -83,20 +83,20 @@ class SiteWorker extends SiteCommon {
*/
async startProcessors ( ) {
const slugs = Object.keys(this.processors);
await SiteAsync.each(slugs, async (slug) => {
for (const slug of slugs) {
const processor = this.processors[slug];
try {
this.log.info('starting worker processor', {
component: processor.component.name,
component: processor.component.logId,
});
await processor.start();
} catch (error) {
this.log.error('failed to start processor', {
component: processor.component.name,
component: processor.component.logId,
error,
});
}
}, 1);
}
}
/**
@ -108,12 +108,12 @@ class SiteWorker extends SiteCommon {
const processor = this.processors[slug];
try {
this.log.info('stopping worker processor', {
component: processor.component.name,
component: processor.component.logId,
});
await processor.stop();
} catch (error) {
this.log.error('failed to stop processor', {
component: processor.component.name,
component: processor.component.logId,
error,
});
}

Loading…
Cancel
Save