Skip to content

Commit c380026

Browse files
committed
Factorize code
1 parent d6f1627 commit c380026

File tree

2 files changed

+30
-58
lines changed

2 files changed

+30
-58
lines changed

src/archivist/index.js

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -129,49 +129,39 @@ export default class Archivist extends events.EventEmitter {
129129
}
130130

131131
async track({ services: servicesIds = this.servicesIds, types: termsTypes = [] } = {}) {
132-
const numberOfTerms = Service.getNumberOfTerms(this.services, servicesIds, termsTypes);
133-
134-
this.emit('trackingStarted', servicesIds.length, numberOfTerms, false);
135-
136-
await Promise.all([ launchHeadlessBrowser(), this.recorder.initialize() ]);
137-
138-
this.trackingQueue.concurrency = MAX_PARALLEL_TRACKING;
139-
140-
servicesIds.forEach(serviceId => {
141-
this.services[serviceId].getTermsTypes().forEach(termsType => {
142-
if (termsTypes.length && !termsTypes.includes(termsType)) {
143-
return;
144-
}
145-
146-
this.trackingQueue.push({ terms: this.services[serviceId].getTerms({ type: termsType }), technicalUpgradeOnly: false });
147-
});
132+
await this.processTerms({
133+
servicesIds,
134+
termsTypes,
135+
technicalUpgradeOnly: false,
136+
concurrency: MAX_PARALLEL_TRACKING,
148137
});
149-
150-
if (this.trackingQueue.length()) {
151-
await this.trackingQueue.drain();
152-
}
153-
154-
await Promise.all([ stopHeadlessBrowser(), this.recorder.finalize() ]);
155-
156-
this.emit('trackingCompleted', servicesIds.length, numberOfTerms, false);
157138
}
158139

159140
async applyTechnicalUpgrades({ services: servicesIds = this.servicesIds, types: termsTypes = [] } = {}) {
141+
await this.processTerms({
142+
servicesIds,
143+
termsTypes,
144+
technicalUpgradeOnly: true,
145+
concurrency: MAX_PARALLEL_TECHNICAL_UPGRADES,
146+
});
147+
}
148+
149+
async processTerms({ servicesIds, termsTypes, technicalUpgradeOnly, concurrency }) {
160150
const numberOfTerms = Service.getNumberOfTerms(this.services, servicesIds, termsTypes);
161151

162-
this.emit('trackingStarted', servicesIds.length, numberOfTerms, true);
152+
this.emit('trackingStarted', servicesIds.length, numberOfTerms, technicalUpgradeOnly);
163153

164154
await Promise.all([ launchHeadlessBrowser(), this.recorder.initialize() ]);
165155

166-
this.trackingQueue.concurrency = MAX_PARALLEL_TECHNICAL_UPGRADES;
156+
this.trackingQueue.concurrency = concurrency;
167157

168158
servicesIds.forEach(serviceId => {
169159
this.services[serviceId].getTermsTypes().forEach(termsType => {
170160
if (termsTypes.length && !termsTypes.includes(termsType)) {
171161
return;
172162
}
173163

174-
this.trackingQueue.push({ terms: this.services[serviceId].getTerms({ type: termsType }), technicalUpgradeOnly: true });
164+
this.trackingQueue.push({ terms: this.services[serviceId].getTerms({ type: termsType }), technicalUpgradeOnly });
175165
});
176166
});
177167

@@ -181,7 +171,7 @@ export default class Archivist extends events.EventEmitter {
181171

182172
await Promise.all([ stopHeadlessBrowser(), this.recorder.finalize() ]);
183173

184-
this.emit('trackingCompleted', servicesIds.length, numberOfTerms, true);
174+
this.emit('trackingCompleted', servicesIds.length, numberOfTerms, technicalUpgradeOnly);
185175
}
186176

187177
async trackTermsChanges({ terms, technicalUpgradeOnly = false }) {

src/index.js

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import Reporter from './reporter/index.js';
1313
const require = createRequire(import.meta.url);
1414
const { version: PACKAGE_VERSION } = require('../package.json');
1515

16-
export default async function track({ services, types, schedule }) {
16+
async function initialize(services) {
1717
const archivist = new Archivist({
1818
recorderConfig: config.get('@opentermsarchive/engine.recorder'),
1919
fetcherConfig: config.get('@opentermsarchive/engine.fetcher'),
@@ -40,11 +40,17 @@ export default async function track({ services, types, schedule }) {
4040
});
4141
}
4242

43+
return { archivist, services };
44+
}
45+
46+
export default async function track({ services, types, schedule }) {
47+
const { archivist, services: filteredServices } = await initialize(services);
48+
4349
// Technical upgrade pass: apply changes from engine, dependency, or declaration upgrades.
4450
// This regenerates versions from existing snapshots with updated extraction logic.
4551
// For terms with combined source documents, if a new document was added to the declaration, it will be fetched and combined with existing snapshots to regenerate the complete version.
4652
// All versions from this pass are labeled as technical upgrades to avoid false notifications about content changes.
47-
await archivist.applyTechnicalUpgrades({ services, types });
53+
await archivist.applyTechnicalUpgrades({ services: filteredServices, types });
4854

4955
if (process.env.OTA_ENGINE_SENDINBLUE_API_KEY) {
5056
try {
@@ -70,7 +76,7 @@ export default async function track({ services, types, schedule }) {
7076
}
7177

7278
if (!schedule) {
73-
await archivist.track({ services, types });
79+
await archivist.track({ services: filteredServices, types });
7480

7581
return;
7682
}
@@ -84,36 +90,12 @@ export default async function track({ services, types, schedule }) {
8490
new Cron( // eslint-disable-line no-new
8591
trackingSchedule,
8692
{ protect: job => logger.warn(`Tracking scheduled at ${new Date().toISOString()} were blocked by an unfinished tracking started at ${job.currentRun().toISOString()}`) },
87-
() => archivist.track({ services, types }),
93+
() => archivist.track({ services: filteredServices, types }),
8894
);
8995
}
9096

9197
export async function applyTechnicalUpgrades({ services, types }) {
92-
const archivist = new Archivist({
93-
recorderConfig: config.get('@opentermsarchive/engine.recorder'),
94-
fetcherConfig: config.get('@opentermsarchive/engine.fetcher'),
95-
});
96-
97-
archivist.attach(logger);
98-
99-
await archivist.initialize();
100-
101-
const collection = await getCollection();
102-
const collectionName = collection?.name ? ` with ${collection.name} collection` : '';
103-
104-
logger.info(`Start engine v${PACKAGE_VERSION}${collectionName}\n`);
105-
106-
if (services?.length) {
107-
services = services.filter(serviceId => {
108-
const isServiceDeclared = archivist.services[serviceId];
109-
110-
if (!isServiceDeclared) {
111-
logger.warn(`Parameter "${serviceId}" was interpreted as a service ID to update, but no matching declaration was found; it will be ignored`);
112-
}
113-
114-
return isServiceDeclared;
115-
});
116-
}
98+
const { archivist, services: filteredServices } = await initialize(services);
11799

118-
await archivist.applyTechnicalUpgrades({ services, types });
100+
await archivist.applyTechnicalUpgrades({ services: filteredServices, types });
119101
}

0 commit comments

Comments
 (0)