···2323 PRIMARY KEY (uri, val)
2424 );
2525 `);
2626+ await this.db.execute(`
2727+ CREATE INDEX IF NOT EXISTS idx_active_labels_uri ON active_labels (uri);
2828+ `);
2629 }
27302831 async computeLabelsDiff(uri: string, desiredOutcome: string[]): Promise<{
···6871 }
69727073 async registerLabelActivation(uri: string, val: string) {
7474+ await this.dbInitLock;
7575+7176 const result = await this.db.execute({
7277 sql: `
7378 INSERT INTO active_labels (uri, val)
···8085 }
81868287 async registerLabelDeactivation(uri: string, val: string) {
8888+ await this.dbInitLock;
8989+8390 const result = await this.db.execute({
8491 sql: `
8592 DELETE FROM active_labels
···8996 });
90979198 if (!result.rowsAffected) throw new Error("Failed to register label deactivation");
9999+ }
100100+101101+ async* getActiveLabelSubjects() {
102102+ await this.dbInitLock;
103103+104104+ let cursor = "";
105105+ while (true) {
106106+ const result = await this.db.execute({
107107+ sql: `
108108+ SELECT DISTINCT uri
109109+ FROM active_labels
110110+ WHERE uri > ?
111111+ ORDER BY uri
112112+ LIMIT 100
113113+ `,
114114+ args: [cursor],
115115+ });
116116+117117+ if (!result.rows.length) {
118118+ return;
119119+ }
120120+121121+ for (const row of result.rows) {
122122+ const v = row.uri as string;
123123+ yield v;
124124+ cursor = v;
125125+ }
126126+ }
92127 }
93128}
+29
labeler.ts
···217217 }
218218 }
219219220220+ private async existingLabelsProcessing() {
221221+ console.log("Reprocessing currently active labels");
222222+ let promises: Promise<void>[] = [];
223223+ let totalProcessed = 0;
224224+ for await (const uri of this.activeLabelsStorage.getActiveLabelSubjects()) {
225225+ const p = new Promise<void>((resolve) => {
226226+ this.scheduleTask(async () => {
227227+ const did = uri as `did:${"plc" | "web"}:${string}`;
228228+ const pds = await this.crawler.identifyPDSofDID(did, true);
229229+ await this.processSingleDID(pds, did);
230230+ totalProcessed++;
231231+ resolve();
232232+ });
233233+ });
234234+ // avoid having too many in-flight tasks at once
235235+ promises.push(p);
236236+ if (promises.length >= 100) {
237237+ await Promise.all(promises);
238238+ promises = [];
239239+ console.log(`Active label processing: processed ${totalProcessed} DIDs so far`);
240240+ }
241241+ }
242242+ await Promise.all(promises);
243243+ console.log(`Active label processing: processed a total of ${totalProcessed} DIDs`);
244244+ }
245245+220246 private async updateLabelerLabels(forceUpdate: boolean) {
221247 const topPDSs = await Array.fromAsync(this.knownPDSStorage.getBiggestKnownPDSs(this.maxPDSDedicatedLabels));
222248 const updated = await this.labelDefiner.updateDedicatedPDSLabels(topPDSs, forceUpdate);
···243269 console.log("Labeler server started on port", this.listenerOptions.port);
244270245271 wrapAsyncInCatch(this.activityBasedProcessing());
272272+273273+ // should be ok to only do this once when the server starts
274274+ wrapAsyncInCatch(this.existingLabelsProcessing());
246275247276 setInterval(() => {
248277 this.scheduleTask(this.longevityBasedProcessing.bind(this));