make quue for tileserver thread
This commit is contained in:
parent
230cf8811a
commit
0078626d7c
|
@ -26,6 +26,28 @@ const CanvasUpdaters = {};
|
||||||
*/
|
*/
|
||||||
const worker = new Worker('./workers/tilewriter.js');
|
const worker = new Worker('./workers/tilewriter.js');
|
||||||
|
|
||||||
|
/*
|
||||||
|
* queue of tasks that is worked on in FIFO
|
||||||
|
*/
|
||||||
|
const taskQueue = [];
|
||||||
|
|
||||||
|
function enqueueTask(task) {
|
||||||
|
if (!taskQueue.length) {
|
||||||
|
worker.postMessage(task);
|
||||||
|
}
|
||||||
|
taskQueue.push(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
worker.on('message', () => {
|
||||||
|
taskQueue.shift();
|
||||||
|
if (taskQueue.length) {
|
||||||
|
worker.postMessage(taskQueue[0]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
/*
|
||||||
|
* every canvas gets an instance of this class
|
||||||
|
*/
|
||||||
class CanvasUpdater {
|
class CanvasUpdater {
|
||||||
TileLoadingQueues;
|
TileLoadingQueues;
|
||||||
id;
|
id;
|
||||||
|
@ -58,7 +80,7 @@ class CanvasUpdater {
|
||||||
const cy = Math.floor(tile / width);
|
const cy = Math.floor(tile / width);
|
||||||
|
|
||||||
if (zoom === this.maxTiledZoom - 1) {
|
if (zoom === this.maxTiledZoom - 1) {
|
||||||
worker.postMessage({
|
enqueueTask({
|
||||||
task: 'createZoomTileFromChunk',
|
task: 'createZoomTileFromChunk',
|
||||||
args: [
|
args: [
|
||||||
this.id,
|
this.id,
|
||||||
|
@ -68,7 +90,7 @@ class CanvasUpdater {
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
} else if (zoom !== this.maxTiledZoom) {
|
} else if (zoom !== this.maxTiledZoom) {
|
||||||
worker.postMessage({
|
enqueueTask({
|
||||||
task: 'createZoomedTile',
|
task: 'createZoomedTile',
|
||||||
args: [
|
args: [
|
||||||
this.canvas,
|
this.canvas,
|
||||||
|
@ -79,7 +101,7 @@ class CanvasUpdater {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (zoom === 0) {
|
if (zoom === 0) {
|
||||||
worker.postMessage({
|
enqueueTask({
|
||||||
task: 'createTexture',
|
task: 'createTexture',
|
||||||
args: [
|
args: [
|
||||||
this.id,
|
this.id,
|
||||||
|
@ -115,31 +137,10 @@ class CanvasUpdater {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
initializeTiles() {
|
|
||||||
return new Promise((resolve) => {
|
|
||||||
worker.postMessage({
|
|
||||||
task: 'initializeTiles',
|
|
||||||
args: [
|
|
||||||
this.id,
|
|
||||||
this.canvas,
|
|
||||||
this.canvasTileFolder,
|
|
||||||
false,
|
|
||||||
],
|
|
||||||
});
|
|
||||||
worker.once('message', (msg) => {
|
|
||||||
logger.info(
|
|
||||||
// eslint-disable-next-line max-len
|
|
||||||
`Tiling: Worker thread finished initializing Tiles with message ${msg}`,
|
|
||||||
);
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* initialize queues and start loops for updating tiles
|
* initialize queues and start loops for updating tiles
|
||||||
*/
|
*/
|
||||||
async initialize() {
|
initialize() {
|
||||||
logger.info(`Tiling: Using folder ${this.canvasTileFolder}`);
|
logger.info(`Tiling: Using folder ${this.canvasTileFolder}`);
|
||||||
if (!fs.existsSync(`${this.canvasTileFolder}/0`)) {
|
if (!fs.existsSync(`${this.canvasTileFolder}/0`)) {
|
||||||
if (!fs.existsSync(this.canvasTileFolder)) {
|
if (!fs.existsSync(this.canvasTileFolder)) {
|
||||||
|
@ -148,7 +149,15 @@ class CanvasUpdater {
|
||||||
logger.warn(
|
logger.warn(
|
||||||
'Tiling: tiledir empty, will initialize it, this can take some time',
|
'Tiling: tiledir empty, will initialize it, this can take some time',
|
||||||
);
|
);
|
||||||
await this.initializeTiles();
|
enqueueTask({
|
||||||
|
task: 'initializeTiles',
|
||||||
|
args: [
|
||||||
|
this.id,
|
||||||
|
this.canvas,
|
||||||
|
this.canvasTileFolder,
|
||||||
|
false,
|
||||||
|
],
|
||||||
|
});
|
||||||
}
|
}
|
||||||
for (let c = 0; c < this.maxTiledZoom; c += 1) {
|
for (let c = 0; c < this.maxTiledZoom; c += 1) {
|
||||||
this.TileLoadingQueues.push([]);
|
this.TileLoadingQueues.push([]);
|
||||||
|
@ -185,8 +194,7 @@ export async function startAllCanvasLoops() {
|
||||||
if (!canvas.v) {
|
if (!canvas.v) {
|
||||||
// just 2D canvases
|
// just 2D canvases
|
||||||
const updater = new CanvasUpdater(id);
|
const updater = new CanvasUpdater(id);
|
||||||
// eslint-disable-next-line no-await-in-loop
|
updater.initialize();
|
||||||
await updater.initialize();
|
|
||||||
CanvasUpdaters[id] = updater;
|
CanvasUpdaters[id] = updater;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,34 +27,27 @@ connectRedis()
|
||||||
try {
|
try {
|
||||||
switch (task) {
|
switch (task) {
|
||||||
case 'createZoomTileFromChunk':
|
case 'createZoomTileFromChunk':
|
||||||
createZoomTileFromChunk(...args);
|
await createZoomTileFromChunk(...args);
|
||||||
break;
|
break;
|
||||||
case 'createZoomedTile':
|
case 'createZoomedTile':
|
||||||
createZoomedTile(...args);
|
await createZoomedTile(...args);
|
||||||
break;
|
break;
|
||||||
case 'createTexture':
|
case 'createTexture':
|
||||||
createTexture(...args);
|
await createTexture(...args);
|
||||||
break;
|
break;
|
||||||
case 'initializeTiles':
|
case 'initializeTiles':
|
||||||
try {
|
await initializeTiles(...args);
|
||||||
await initializeTiles(...args);
|
|
||||||
parentPort.postMessage('Done!');
|
|
||||||
} catch (err) {
|
|
||||||
console.warn(
|
|
||||||
// eslint-disable-next-line max-len
|
|
||||||
`Tiling: Error on initializeTiles args ${args}: ${err.message}`,
|
|
||||||
);
|
|
||||||
parentPort.postMessage('Failure!');
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
console.warn(`Tiling: Main thread requested unknown task ${task}`);
|
console.warn(`Tiling: Main thread requested unknown task ${task}`);
|
||||||
}
|
}
|
||||||
|
parentPort.postMessage('Done!');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.warn(
|
console.warn(
|
||||||
// eslint-disable-next-line max-len
|
// eslint-disable-next-line max-len
|
||||||
`Tiling: Error on executing task ${task} args ${args}: ${error.message}`,
|
`Tiling: Error on executing task ${task} args ${args}: ${error.message}`,
|
||||||
);
|
);
|
||||||
|
parentPort.postMessage('Failure!');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue
Block a user