-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetadata_worker.ts
72 lines (68 loc) · 3.48 KB
/
metadata_worker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import EnvironmentConfig from "./src/EnvironmentConfig";
import RabbitMQConnection from "./src/MessageQueue/RabbitMQConnection";
import RabbitMQConsumer from "./src/MessageQueue/RabbitMQConsumer";
import RabbitMQProducer from "./src/MessageQueue/RabbitMQProducer";
import { MetadataMessage } from "./src/MessageQueue/Messages";
import CampaignMetadata from "./src/Model/CampaignMetadata";
import {unserializeTestCase} from "./src/Model/TestCaseSerializer";
import {
isSerializedCampaignMetadata,
unserializeEntriesToDimensions
} from "./src/Model/MetadataSerializer";
import {FileMetadataRepository, METADATA_FILE} from "./src/FileMetadataRepository";
import path from "path";
import fs from "fs";
import process from "process";
import summarizeMetadata from "./src/Model/MetadataSummarizer";
import {WorkerConfigFactory} from "./src/CommandLine/WorkerConfig";
const config = new EnvironmentConfig();
const cliConfig = new WorkerConfigFactory();
const options = cliConfig.getConfig( 'A queue worker that processes metadata messages', process.cwd() );
const screenshotPath = options.outputPath;
const showMessage = options.verbose ? console.log : () => {};
const repo = new FileMetadataRepository( screenshotPath );
const queueConnection = new RabbitMQConnection( config.queueUrl, "Connection established, hit Ctrl-C to quit worker" );
const consumer = new RabbitMQConsumer( queueConnection );
const producer = new RabbitMQProducer( queueConnection );
process.umask(0o002);
consumer.consumeMetaDataQueue( async ( msgData: MetadataMessage ) => {
switch ( msgData.msgType ) {
case "init":
const initialMetadata = new CampaignMetadata(
msgData.testCases.map(unserializeTestCase),
unserializeEntriesToDimensions(msgData.dimensions),
msgData.campaignName
);
repo.saveMetadata(initialMetadata);
showMessage(`Initialized metadata file for ${initialMetadata.campaign}`);
// Trigger metadata summary update, so the new test shows up immediately
await producer.sendMetadataSummary({msgType: 'summary'});
break;
case "update":
const testCase = unserializeTestCase(msgData.testCase);
const metadata = repo.loadMetadata(msgData.campaignName);
metadata.updateTestCase(testCase);
repo.saveMetadata(metadata);
if ( !metadata.hasPendingTestCases() ) {
await producer.sendMetadataSummary({msgType: 'summary'});
}
showMessage(`Updated metadata for testcase ${msgData.testCase.screenshotFilename}`);
break;
case "summary":
const allMetadata = repo.getCampaignNames().reduce((collectedMetaData, campaignName) => {
const fn = path.join(screenshotPath, campaignName, METADATA_FILE);
const serializedMetadata = JSON.parse(fs.readFileSync(fn, 'utf-8'));
if ( isSerializedCampaignMetadata(serializedMetadata) ) {
collectedMetaData.push(serializedMetadata);
} else {
console.log( `ERROR: File "${fn}" contained invalid metadata` );
}
return collectedMetaData;
}, []);
const summary = summarizeMetadata(allMetadata);
const summaryFileName = path.join(screenshotPath, 'metadata_summary.json');
fs.writeFileSync(summaryFileName, JSON.stringify(summary, null, 4), 'utf-8');
showMessage(`Updated metadata summary`);
break;
}
} );