-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
180 lines (142 loc) · 5.25 KB
/
worker.js
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env node
/* eslint import/no-unassigned-import: off */
import 'dotenv/config.js'
import {cpus} from 'node:os'
import process from 'node:process'
import pLimit from 'p-limit'
import pumpify from 'pumpify'
import {createGeocodeStream} from 'addok-geocode-stream'
import {validateCsvFromStream, createCsvReadStream} from '@livingdata/tabular-data-helpers'
import {processNext, getInputFileDownloadStream, getProject, setOutputFile, endProcessing, updateProcessing, getStalledProjects, resetProcessing, askProcessing} from './lib/models/project.js'
import {createWriteStream as createGeoJsonWriteStream} from './lib/writers/geojson.js'
import {createWriteStream as createCsvWriteStream} from './lib/writers/csv.js'
import {computeOutputFilename} from './lib/util/filename.js'
const OUTPUT_FORMATS = {
csv: createCsvWriteStream,
geojson: createGeoJsonWriteStream
}
const {ADDOK_SERVICE_URL} = process.env
function getConcurrency() {
if (process.env.WORKERS_CONCURRENCY) {
return Number.parseInt(process.env.WORKERS_CONCURRENCY, 10)
}
return cpus().length
}
async function main() {
const concurrency = getConcurrency()
const processingProjects = new Map()
const limit = pLimit(1)
async function getNextJob() {
if (processingProjects.size >= concurrency) {
return
}
const projectId = await processNext()
if (!projectId) {
limit.clearQueue()
return
}
const abortController = new AbortController()
processingProjects.set(projectId, {abortController})
process.nextTick(async () => {
await executeProcessing(projectId, {signal: abortController.signal})
processingProjects.delete(projectId)
limit(() => getNextJob())
})
}
async function executeProcessing(projectId) {
try {
console.log(`${projectId} | start processing`)
const project = await getProject(projectId)
const {inputFile} = project
const upLimit = pLimit(1)
/* Validation */
let totalRows = null
await upLimit(() => updateProcessing(projectId, {
step: 'validating',
validationProgress: {readRows: 0, readBytes: 0, totalBytes: inputFile.size}
}))
const validationInputStream = await getInputFileDownloadStream(projectId)
await new Promise((resolve, reject) => {
const validation = validateCsvFromStream(validationInputStream, project.pipeline)
validation
.on('progress', async progress => {
await upLimit(() => updateProcessing(projectId, {
validationProgress: {readRows: progress.readRows, readBytes: progress.readBytes, totalBytes: inputFile.size}
}))
})
.on('error', async error => {
await upLimit(() => updateProcessing(projectId, {
validationError: error.message
}))
reject(new Error('Validation failed'))
})
.on('complete', async () => {
totalRows = validation.readRows
await upLimit(() => updateProcessing(projectId, {
validationProgress: {readRows: validation.readRows, readBytes: validation.readBytes, totalBytes: inputFile.size}
}))
resolve()
})
})
/* Geocoding */
await upLimit(() => updateProcessing(projectId, {
step: 'geocoding',
geocodingProgress: {readRows: 0, totalRows}
}))
const {geocodeOptions, outputFormat} = project.pipeline
const inputFileName = project.inputFile.filename
const outputFileName = computeOutputFilename(inputFileName || 'result', outputFormat)
const inputFileStream = await getInputFileDownloadStream(projectId)
const createWriteStream = OUTPUT_FORMATS[outputFormat]
const fullGeocodeStream = pumpify(
inputFileStream,
createCsvReadStream(project.pipeline),
createGeocodeStream({
serviceUrl: ADDOK_SERVICE_URL,
concurrency: 2,
strategy: 'batch',
columns: geocodeOptions.q,
citycode: geocodeOptions.citycode,
lon: geocodeOptions.lon,
lat: geocodeOptions.lat,
async onUnwrap(readRows) {
await upLimit(() => updateProcessing(projectId, {
geocodingProgress: {readRows, totalRows}
}))
}
}),
createWriteStream()
)
try {
await setOutputFile(projectId, outputFileName, fullGeocodeStream)
} catch (error) {
await upLimit(() => updateProcessing(projectId, {
geocodingError: error.message
}))
throw new Error('Geocoding failed')
}
await upLimit(() => endProcessing(projectId))
console.log(`${projectId} | processed successfully`)
} catch (error) {
await endProcessing(projectId, error)
console.log(`${projectId} | error during processing`)
console.error(error)
}
}
setInterval(async () => {
for (let i = processingProjects.size; i < concurrency; i++) {
limit(() => getNextJob())
}
const stalledProjects = await getStalledProjects()
await Promise.all(stalledProjects.map(async projectId => {
await resetProcessing(projectId)
await askProcessing(projectId)
}))
}, 1000)
}
try {
await main()
} catch (error) {
console.error(error)
process.exit(1)
}