This repository has been archived by the owner on Sep 16, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathAssignmentRunner.ts
96 lines (84 loc) · 2.9 KB
/
AssignmentRunner.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import Queue from 'p-queue';
import IAssignment from '@double-agent/collect-controller/interfaces/IAssignment';
import AssignmentsClient from './AssignmentsClient';
import { IRunner, IRunnerFactory } from '../interfaces/IRunnerFactory';
export default class AssignmentRunner {
public readonly queue: Queue;
public beforeFinishFn?: () => Promise<void>;
constructor(
public runnerFactory: IRunnerFactory,
private userAgentsToTestPath: string,
private assignmentsDataOutDir: string,
runnerConcurrency = 5,
) {
this.queue = new Queue({ concurrency: runnerConcurrency });
}
async run(): Promise<void> {
const runnerID = this.runnerFactory.runnerId();
try {
await this.runnerFactory.startFactory();
} catch (error) {
console.error(`failed to start runner factory ${runnerID}`, error);
return;
}
try {
await this.runFactoryRunners(runnerID);
} catch (error) {
console.error(`failed to run runners for factory runner with runner Id ${runnerID}`, error);
} finally {
try {
await this.runnerFactory.stopFactory();
} catch (error) {
console.error(`failed to stop runner factory with Id ${runnerID}`, error);
}
}
}
async runFactoryRunners(runnerID: string): Promise<void> {
console.log(`run all assignments for runner: ${runnerID}!`);
const assignmentsClient = new AssignmentsClient(`runner-${runnerID}`);
const assignments = await assignmentsClient.start({
dataDir: this.assignmentsDataOutDir,
userAgentsToTestPath: this.userAgentsToTestPath,
});
for (const { id: assignmentId } of assignments) {
void this.queue.add(async () => {
let assignment: IAssignment;
console.log(`Getting assignment %s of %s`, assignmentId, assignments.length);
try {
assignment = await assignmentsClient.activate(assignmentId);
} catch (error) {
console.error('ERROR activating assignment: ', error);
process.exit();
}
console.log(
'[%s._] RUNNING %s assignment (%s)',
assignment.sessionId,
assignment.type,
assignment.id,
);
let runner: IRunner;
try {
runner = await this.runnerFactory.spawnRunner(assignment);
} catch (error) {
console.error(`failed to create runner ${runnerID}`, error);
return;
}
try {
await runner.run(assignment);
} catch (error) {
console.error(`runner ${runnerID} run failed with exception`, error);
} finally {
try {
await runner.stop();
} catch (error) {
console.error(`failed to stop runner ${runnerID}`, error);
}
}
});
}
await this.queue.onIdle();
if (this.beforeFinishFn) await this.beforeFinishFn();
await assignmentsClient.finish();
console.log('FINISHED');
}
}