Skip to content

Commit 134e755

Browse files
committed
Fix idle check
1 parent 617ee7d commit 134e755

File tree

6 files changed

+32
-13
lines changed

6 files changed

+32
-13
lines changed

src/utils/processImage.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ function errorCallback(err: unknown) {
1818
}
1919
}
2020

21+
/**
22+
* Called on a worker thread to signal current work is complete
23+
*/
24+
const workerIsDone = () => parentPort?.postMessage('complete');
25+
2126
/**
2227
* Processes the given image with various image manipulation options.
2328
*
@@ -203,21 +208,26 @@ function continueProcessing(image: Jimp, options: Options): void {
203208
}
204209
outputFilename = `${outputFilename}.png`;
205210

206-
image.write(outputFilename, errorCallback);
207-
console.log(`Image saved: ${outputFilename}`);
211+
image
212+
.writeAsync(outputFilename)
213+
.then(() => {
214+
console.log(`Image saved: ${outputFilename}`);
215+
216+
if (!isMainThread) {
217+
workerIsDone();
218+
}
219+
})
220+
.catch(errorCallback);
208221
}
209222

210223
// If used as a worker thread, get file name from message
211224
if (!isMainThread) {
212-
const workIsDone = () => parentPort?.postMessage('complete');
213-
214225
parentPort?.on(
215226
'message',
216227
async (message: { filePath: string; options: Options }) => {
217228
const { filePath, options } = message;
218229
options.filename = filePath;
219230
processImage(options, true);
220-
workIsDone();
221231
},
222232
);
223233
}

src/utils/processPath.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export async function processPath(
2121

2222
// Check if it's a file (not a subdirectory)
2323
if ((await fs.stat(filePath)).isFile()) {
24-
console.log(filePath, options);
2524
workerPool.addTask(filePath, options);
2625
}
2726
}

src/utils/workerPool.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ export class WorkerPool {
6464
path.join(__dirname, 'processImage.js'),
6565
) as TWorker;
6666

67-
worker.postMessage({ filePath, options });
6867
worker.isIdle = false;
68+
worker.postMessage({ filePath, options });
6969

7070
// Listen for messages and errors from the worker
7171
worker.on('message', () => {
@@ -93,6 +93,7 @@ export class WorkerPool {
9393
} else {
9494
const worker = this.workers.find((w) => w.isIdle);
9595
if (worker) {
96+
worker.isIdle = false;
9697
worker.postMessage(nextTask);
9798
} else {
9899
// Something went wrong, there are no idle workers somehow

utils/processImage.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ function errorCallback(err) {
1818
console.error(err);
1919
}
2020
}
21+
/**
22+
* Called on a worker thread to signal current work is complete
23+
*/
24+
const workerIsDone = () => worker_threads_1.parentPort?.postMessage('complete');
2125
/**
2226
* Processes the given image with various image manipulation options.
2327
*
@@ -168,16 +172,21 @@ function continueProcessing(image, options) {
168172
outputFilename = `${outputFilename}-z_${pixelSize}`;
169173
}
170174
outputFilename = `${outputFilename}.png`;
171-
image.write(outputFilename, errorCallback);
172-
console.log(`Image saved: ${outputFilename}`);
175+
image
176+
.writeAsync(outputFilename)
177+
.then(() => {
178+
console.log(`Image saved: ${outputFilename}`);
179+
if (!worker_threads_1.isMainThread) {
180+
workerIsDone();
181+
}
182+
})
183+
.catch(errorCallback);
173184
}
174185
// If used as a worker thread, get file name from message
175186
if (!worker_threads_1.isMainThread) {
176-
const workIsDone = () => worker_threads_1.parentPort?.postMessage('complete');
177187
worker_threads_1.parentPort?.on('message', async (message) => {
178188
const { filePath, options } = message;
179189
options.filename = filePath;
180190
processImage(options, true);
181-
workIsDone();
182191
});
183192
}

utils/processPath.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ async function processPath(directoryPath, options, maxWorkers) {
1616
const filePath = path.join(directoryPath, file);
1717
// Check if it's a file (not a subdirectory)
1818
if ((await fs.stat(filePath)).isFile()) {
19-
console.log(filePath, options);
2019
workerPool.addTask(filePath, options);
2120
}
2221
}

utils/workerPool.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ class WorkerPool {
5050
*/
5151
createWorker(filePath, options) {
5252
const worker = new worker_threads_1.Worker(path.join(__dirname, 'processImage.js'));
53-
worker.postMessage({ filePath, options });
5453
worker.isIdle = false;
54+
worker.postMessage({ filePath, options });
5555
// Listen for messages and errors from the worker
5656
worker.on('message', () => {
5757
worker.isIdle = true;
@@ -76,6 +76,7 @@ class WorkerPool {
7676
else {
7777
const worker = this.workers.find((w) => w.isIdle);
7878
if (worker) {
79+
worker.isIdle = false;
7980
worker.postMessage(nextTask);
8081
}
8182
else {

0 commit comments

Comments
 (0)