-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimple_worker.ts
41 lines (32 loc) · 1.62 KB
/
simple_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
// A simple "message echo" worker to test the server
import RabbitMQConsumer from "./src/MessageQueue/RabbitMQConsumer";
import {TestCaseMessage} from "./src/MessageQueue/Messages";
import EnvironmentConfig from "./src/EnvironmentConfig";
import {TestCase, TestCaseFinishedState} from "./src/Model/TestCase";
import RabbitMQProducer from "./src/MessageQueue/RabbitMQProducer";
import {serializeTestCase, unserializeTestCase} from "./src/Model/TestCaseSerializer";
import RabbitMQConnection from "./src/MessageQueue/RabbitMQConnection";
const config = new EnvironmentConfig();
const TASK_DELAY = 500;
const queueConnection = new RabbitMQConnection( config.queueUrl, "Connection established, hit Ctrl-C to quit worker" );
const consumer = new RabbitMQConsumer( queueConnection );
const producer = new RabbitMQProducer( queueConnection );
async function sendMetadataUpdate( testCase: TestCase, campaignName: string ): Promise<void> {
await producer.sendMetadataUpdate( {
msgType: "update",
testCase: serializeTestCase( testCase ),
campaignName
} );
}
// "consume" will run in an endless loop, where the client waits for new messages
consumer.consumeScreenshotQueue( async function( msgData: TestCaseMessage ) {
console.log("processing message", msgData);
console.log('started at', new Date());
const testCase = unserializeTestCase( msgData.testCase );
return new Promise<void>( (resolve) => setTimeout( async () => {
console.log("processing finished at", new Date());
testCase.updateState( new TestCaseFinishedState( 'Finished' ) );
await sendMetadataUpdate( testCase, msgData.trackingName );
resolve();
}, TASK_DELAY ) );
});