Skip to content

Commit 87034c1

Browse files
committed
wait for files to finish downloading and streaming before moving on
1 parent 14fc9ac commit 87034c1

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/cli.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,10 @@ async function spawn(args) {
326326
}
327327

328328
// Close download pool
329-
getPool.close();
329+
await new Promise((resolve) => {
330+
getPool.close(resolve);
331+
});
332+
await Promise.all(getPool.promises);
330333

331334
// Replace filename
332335
const filename = args["filename"].replace("[id]", args["id"]).replace("[endpoint]", args["_"]);

src/getpool/getPool.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,21 @@ class GetJob {
2424
public async start() {
2525
mkdirp.sync(this.directory);
2626
try {
27-
await axios({
27+
// Get data
28+
const response = await axios({
2829
method: "get",
2930
responseType: "stream",
3031
url: this.url,
31-
}).then((response) => {
32+
});
33+
34+
// Write to file
35+
await new Promise(async (resolve) => {
36+
const stream = fs.createWriteStream(this.directory + "/" + this.name + "." + this.extension);
3237
// noinspection TypeScriptValidateJSTypes
33-
response.data.pipe(fs.createWriteStream(this.directory + "/" + this.name + "." + this.extension));
38+
response.data.pipe(stream);
39+
stream.on("finish", resolve);
3440
});
41+
3542
} catch (e) {
3643
this.logger.info(`Downloading ${this.url} failed`);
3744
this.logger.debug(e);
@@ -44,6 +51,10 @@ class GetJob {
4451
* A pool of jobs that only executes k jobs 'simultaneously'
4552
*/
4653
export class GetPool {
54+
55+
// Job promises
56+
public promises: Array<Promise<void>> = [];
57+
4758
// Jobs that are currently being executed
4859
private runningJobs: GetJob[] = [];
4960

@@ -62,6 +73,9 @@ export class GetPool {
6273
// End-of-input signal triggered externally by close()
6374
private finished: boolean = false;
6475

76+
// End-of-input resolve function
77+
private resolve: () => {};
78+
6579
constructor(connections: number = 1) {
6680
this.maxConnections = connections;
6781
this.loop = setInterval(() => {
@@ -74,8 +88,9 @@ export class GetPool {
7488
this.queuedJobs.push(new GetJob(url, name, extension, directory, logger));
7589
}
7690

77-
public close() {
91+
public close(resolve) {
7892
this.finished = true;
93+
this.resolve = resolve;
7994
}
8095

8196
private poolLoop() {
@@ -97,13 +112,14 @@ export class GetPool {
97112
// Add new jobs to empty running slots
98113
while (this.queuedJobs.length > 0 && this.runningJobs.length < this.maxConnections) {
99114
const job = this.queuedJobs.shift();
100-
job.start();
115+
this.promises.push(job.start());
101116
this.runningJobs.push(job);
102117
}
103118

104119
// End the interval when end-of-input signal given
105120
if (this.finished && this.queuedJobs.length === 0 && this.runningJobs.length === 0) {
106121
clearInterval(this.loop);
122+
this.resolve();
107123
}
108124

109125
// Release lock

0 commit comments

Comments
 (0)