Skip to content
This repository was archived by the owner on Jun 3, 2020. It is now read-only.

Commit d94d196

Browse files
committed
Rework crawler progress events
1 parent becb862 commit d94d196

4 files changed

Lines changed: 23 additions & 10 deletions

File tree

crawler/followgraph.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const lock = require('../lib/lock')
66
const db = require('../dbs/profile-data-db')
77
const crawler = require('./index')
88
const siteDescriptions = require('./site-descriptions')
9-
const {doCrawl, doCheckpoint} = require('./util')
9+
const {doCrawl, doCheckpoint, emitProgressEvent} = require('./util')
1010
const debug = require('../lib/debug-logger').debugLogger('crawler')
1111

1212
// constants
@@ -46,6 +46,8 @@ exports.crawlSite = async function (archive, crawlSource) {
4646
return
4747
}
4848

49+
emitProgressEvent(archive.url, 'crawl_followgraph', 0, 1)
50+
4951
// read and validate
5052
try {
5153
var followsJson = await readFollowsFile(archive)
@@ -91,6 +93,7 @@ exports.crawlSite = async function (archive, crawlSource) {
9193

9294
// write checkpoint as success
9395
await doCheckpoint('crawl_followgraph', TABLE_VERSION, crawlSource, changes[changes.length - 1].version)
96+
emitProgressEvent(archive.url, 'crawl_followgraph', 1, 1)
9497
})
9598
}
9699

crawler/posts.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const {URL} = require('url')
33
const Events = require('events')
44
const db = require('../dbs/profile-data-db')
55
const crawler = require('./index')
6-
const {doCrawl, doCheckpoint, getMatchingChangesInOrder, generateTimeFilename} = require('./util')
6+
const {doCrawl, doCheckpoint, emitProgressEvent, getMatchingChangesInOrder, generateTimeFilename} = require('./util')
77
const debug = require('../lib/debug-logger').debugLogger('crawler')
88

99
// constants
@@ -40,8 +40,10 @@ exports.crawlSite = async function (archive, crawlSource) {
4040
// collect changed posts
4141
var changedPosts = getMatchingChangesInOrder(changes, JSON_PATH_REGEX)
4242
console.log('collected changed posts', changedPosts)
43+
emitProgressEvent(archive.url, 'crawl_posts', 0, changedPosts.length)
4344

4445
// read and apply each post in order
46+
var progress = 0
4547
for (let changedPost of changedPosts) {
4648
// TODO Currently the crawler will abort reading the feed if any post fails to load
4749
// this means that a single bad or unreachable file can stop the forward progress of post indexing
@@ -89,10 +91,11 @@ exports.crawlSite = async function (archive, crawlSource) {
8991
`, [crawlSource.id, changedPost.name, Date.now(), post.content, post.createdAt, post.updatedAt])
9092
events.emit('post-added', archive.url)
9193
}
92-
93-
// checkpoint our progress
94-
await doCheckpoint('crawl_posts', TABLE_VERSION, crawlSource, changedPost.version)
9594
}
95+
96+
// checkpoint our progress
97+
await doCheckpoint('crawl_posts', TABLE_VERSION, crawlSource, changedPost.version)
98+
emitProgressEvent(archive.url, 'crawl_posts', ++progress, changedPosts.length)
9699
}
97100
})
98101
}

crawler/site-descriptions.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const db = require('../dbs/profile-data-db')
66
const archivesDb = require('../dbs/archives')
77
const dat = require('../dat')
88
const crawler = require('./index')
9-
const {doCrawl, doCheckpoint, getMatchingChangesInOrder, generateTimeFilename} = require('./util')
9+
const {doCrawl, doCheckpoint, emitProgressEvent, getMatchingChangesInOrder, generateTimeFilename} = require('./util')
1010
const debug = require('../lib/debug-logger').debugLogger('crawler')
1111

1212
// constants
@@ -43,8 +43,10 @@ exports.crawlSite = async function (archive, crawlSource) {
4343
// collect changed site descriptions
4444
var changedSiteDescriptions = getMatchingChangesInOrder(changes, JSON_PATH_REGEX)
4545
console.log('collected changed site descriptions', changedSiteDescriptions)
46+
emitProgressEvent(archive.url, 'crawl_site_descriptions', 0, changedSiteDescriptions.length)
4647

4748
// read and apply each post in order
49+
var progress = 0
4850
for (let changedSiteDescription of changedSiteDescriptions) {
4951
// TODO Currently the crawler will abort reading the feed if any description fails to load
5052
// this means that a single bad or unreachable file can stop the forward progress of description indexing
@@ -95,10 +97,11 @@ exports.crawlSite = async function (archive, crawlSource) {
9597
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
9698
`, [crawlSource.id, changedSiteDescription.name, Date.now(), desc.subject, desc.metadata.title, desc.metadata.description, desc.metadata.type.join(','), desc.createdAt])
9799
events.emit('description-added', archive.url)
98-
99-
// checkpoint our progress
100-
await doCheckpoint('crawl_site_descriptions', TABLE_VERSION, crawlSource, changedSiteDescription.version)
101100
}
101+
102+
// checkpoint our progress
103+
await doCheckpoint('crawl_site_descriptions', TABLE_VERSION, crawlSource, changedSiteDescription.version)
104+
emitProgressEvent(archive.url, 'crawl_site_descriptions', ++progress, changedSiteDescription.length)
102105
}
103106
})
104107
}

crawler/util.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ exports.doCrawl = async function (archive, crawlSource, crawlDataset, crawlDatas
5454
}
5555

5656
const doCheckpoint = exports.doCheckpoint = async function (crawlDataset, crawlDatasetVersion, crawlSource, crawlSourceVersion) {
57-
crawlerEvents.emit('crawl-dataset-progress', {sourceUrl: crawlSource.url, crawlDataset, crawledVersion: crawlSourceVersion})
57+
5858
await db.run(`DELETE FROM crawl_sources_meta WHERE crawlDataset = ? AND crawlSourceId = ?`, [crawlDataset, crawlSource.id])
5959
await db.run(`
6060
INSERT
@@ -63,6 +63,10 @@ const doCheckpoint = exports.doCheckpoint = async function (crawlDataset, crawlD
6363
`, [crawlDataset, crawlDatasetVersion, crawlSource.id, crawlSourceVersion, Date.now()])
6464
}
6565

66+
exports.emitProgressEvent = function (sourceUrl, crawlDataset, progress, numUpdates) {
67+
crawlerEvents.emit('crawl-dataset-progress', {sourceUrl, crawlDataset, progress, numUpdates})
68+
}
69+
6670
exports.getMatchingChangesInOrder = function (changes, regex) {
6771
var list = [] // order matters, must be oldest to newest
6872
changes.forEach(c => {

0 commit comments

Comments
 (0)