-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtransfer-pattern-worker.ts
58 lines (48 loc) · 1.73 KB
/
transfer-pattern-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
import {loadGTFS} from "./gtfs/GTFSLoader";
import {StringResults} from "./transfer-pattern/results/StringResults";
import {TransferPatternRepository} from "./transfer-pattern/TransferPatternRepository";
import * as fs from "fs";
import { RaptorAlgorithmFactory } from "./raptor/RaptorAlgorithmFactory";
import { TransferPatternQuery } from "./query/TransferPatternQuery";
import * as mysql from "mysql2/promise";
/**
* Worker that finds transfer patterns for a given station
*/
async function worker(filename: string, date: Date): Promise<void> {
const stream = fs.createReadStream(filename);
const [trips, transfers, interchange] = await loadGTFS(stream);
const raptor = RaptorAlgorithmFactory.create(trips, transfers, interchange, date);
const query = new TransferPatternQuery(raptor, () => new StringResults(interchange));
const repository = new TransferPatternRepository(getDatabase());
process.on("message", async stop => {
const results = query.plan(stop, date);
await repository.storeTransferPatterns(results);
morePlease();
});
process.on("SIGUSR2", () => {
process.exit();
});
morePlease();
}
function morePlease() {
process.send!("ready");
}
function getDatabase() {
return mysql.createPool({
// host: process.env.DATABASE_HOSTNAME || "localhost",
socketPath: "/run/mysqld/mysqld.sock",
user: process.env.DATABASE_USERNAME || "root",
password: process.env.DATABASE_PASSWORD || "",
database: process.env.OJP_DATABASE_NAME || "ojp",
connectionLimit: 3,
});
}
if (process.argv[2] && process.argv[3]) {
worker(process.argv[2], new Date(process.argv[3])).catch(err => {
console.error(err);
process.exit();
});
}
else {
console.log("Please specify a date and GTFS file.");
}