|
1 | 1 | import { Redis, Result, Callback, type ChainableCommander } from "ioredis"; |
2 | 2 | import { v4 as uuidv4 } from "uuid"; |
3 | 3 |
|
4 | | -import { logger } from "./logger.js"; |
| 4 | +import { formatErr, logger } from "./logger.js"; |
5 | 5 |
|
6 | 6 | import { |
7 | 7 | MAX_DEPTH, |
@@ -299,11 +299,21 @@ export class RedisDedupeIndex { |
299 | 299 | index, |
300 | 300 | ); |
301 | 301 | if (!waczdata) { |
| 302 | + logger.warn( |
| 303 | + "Invalid WACZ index, WARC-Refers-To-Container will not be set", |
| 304 | + { crawlId, index }, |
| 305 | + "state", |
| 306 | + ); |
302 | 307 | return ""; |
303 | 308 | } |
304 | 309 | const { filename } = JSON.parse(waczdata); |
305 | 310 | return filename; |
306 | | - } catch (_) { |
| 311 | + } catch (e) { |
| 312 | + logger.warn( |
| 313 | + "Error getting WACZ index, WARC-Refers-To-Container will not be set", |
| 314 | + { crawlId, index, ...formatErr(e) }, |
| 315 | + "state", |
| 316 | + ); |
307 | 317 | return ""; |
308 | 318 | } |
309 | 319 | } |
@@ -854,8 +864,6 @@ export class RedisCrawlState extends RedisDedupeIndex { |
854 | 864 |
|
855 | 865 | sitemapDoneKey: string; |
856 | 866 |
|
857 | | - waczFilename: string | null = null; |
858 | | - |
859 | 867 | includedCrawls: Set<string> = new Set<string>(); |
860 | 868 |
|
861 | 869 | rateLimitTTL: number; |
@@ -1265,44 +1273,50 @@ return inx; |
1265 | 1273 | } |
1266 | 1274 |
|
1267 | 1275 | async setWACZFilename(): Promise<string> { |
1268 | | - const filename = process.env.STORE_FILENAME || "@ts-@id.wacz"; |
1269 | | - this.waczFilename = interpolateFilename(filename, this.crawlId); |
1270 | | - if ( |
1271 | | - !(await this.redis.hsetnx( |
1272 | | - `${this.crawlId}:nextWacz`, |
1273 | | - this.uid, |
1274 | | - this.waczFilename, |
1275 | | - )) |
1276 | | - ) { |
1277 | | - this.waczFilename = await this.redis.hget( |
1278 | | - `${this.crawlId}:nextWacz`, |
1279 | | - this.uid, |
| 1276 | + const filenameTemplate = process.env.STORE_FILENAME || "@ts-@id.wacz"; |
| 1277 | + const crawlId = this.crawlId; |
| 1278 | + const filename = interpolateFilename(filenameTemplate, crawlId); |
| 1279 | + if (!(await this.redis.hsetnx(`${crawlId}:nextWacz`, this.uid, filename))) { |
| 1280 | + this.dedupeCurrFilename = |
| 1281 | + (await this.redis.hget(`${crawlId}:nextWacz`, this.uid)) || ""; |
| 1282 | + |
| 1283 | + this.dedupeKeyIndex = Number( |
| 1284 | + (await this.redis.hget(`${crawlId}:nextWaczIndex`, this.uid)) || 0, |
1280 | 1285 | ); |
| 1286 | + |
1281 | 1287 | logger.debug( |
1282 | 1288 | "Keeping WACZ Filename", |
1283 | | - { filename: this.waczFilename }, |
| 1289 | + { filename, index: this.dedupeKeyIndex }, |
1284 | 1290 | "state", |
1285 | 1291 | ); |
1286 | 1292 | } else { |
| 1293 | + await this.addSourceWACZForDedupe(filename); |
| 1294 | + |
| 1295 | + await this.redis.hset( |
| 1296 | + `${crawlId}:nextWaczIndex`, |
| 1297 | + this.uid, |
| 1298 | + this.dedupeKeyIndex, |
| 1299 | + ); |
| 1300 | + |
1287 | 1301 | logger.debug( |
1288 | 1302 | "Using New WACZ Filename", |
1289 | | - { filename: this.waczFilename }, |
| 1303 | + { filename, index: this.dedupeKeyIndex }, |
1290 | 1304 | "state", |
1291 | 1305 | ); |
1292 | 1306 | } |
1293 | | - return this.waczFilename!; |
| 1307 | + return filename; |
1294 | 1308 | } |
1295 | 1309 |
|
1296 | 1310 | async getWACZFilename(): Promise<string> { |
1297 | | - if (!this.waczFilename) { |
| 1311 | + if (!this.dedupeCurrFilename) { |
1298 | 1312 | return await this.setWACZFilename(); |
1299 | 1313 | } |
1300 | | - return this.waczFilename; |
| 1314 | + return this.dedupeCurrFilename; |
1301 | 1315 | } |
1302 | 1316 |
|
1303 | 1317 | async clearWACZFilename(): Promise<void> { |
1304 | 1318 | await this.redis.hdel(`${this.crawlId}:nextWacz`, this.uid); |
1305 | | - this.waczFilename = null; |
| 1319 | + this.dedupeCurrFilename = ""; |
1306 | 1320 |
|
1307 | 1321 | await this.redis.del(`${this.uid}:duperef`); |
1308 | 1322 | } |
|
0 commit comments