Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

Commit

Permalink
Add endpoint of upload status (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
liulw2022 committed Dec 17, 2023
1 parent 5f5b7aa commit 2e725cd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
10 changes: 10 additions & 0 deletions backend/src/upload/file.processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,19 @@ export class FileProcessor {
fs.mkdirSync(tempUnzipPath, { recursive: true });
}

// Start the unzip process with a progress of 0%
await job.progress(0);

// Stream the zip file and extract to the temporary directory

fs.createReadStream(filePath)
.pipe(unzip.Extract({ path: tempUnzipPath }))
.on('error', (error) => {
console.error(`Error unzipping file: ${filePath}`, error);
})
.on('close', async () => {
await this.handleUnzippedContent(tempUnzipPath, filePath);
await job.progress(60);
});
} catch (error) {
console.error(`Error processing file: ${job.data.filePath}`, error);
Expand Down Expand Up @@ -121,6 +126,8 @@ export class FileProcessor {

// TODO(Seb-sti1): (when rest working) add valhalla here

await job.progress(65);

// Upload all data and images to the database
await Promise.all([
...data.map(async (data) => {
Expand All @@ -145,6 +152,7 @@ export class FileProcessor {
);
}),
]);
await job.progress(99);
}

// Delete the unzipped file
Expand All @@ -161,6 +169,8 @@ export class FileProcessor {
console.log(
`File processed successfully and imported into Database: ${filePath}`,
);
await job.progress(100);
console.log(`Job ${job.id} completed`);
} catch (error) {
console.error(`Error processing file: ${job.data.filePath}`, error);
}
Expand Down
22 changes: 22 additions & 0 deletions backend/src/upload/upload.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Post,
UploadedFile,
UseInterceptors,
Get,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { InjectQueue } from '@nestjs/bull';
Expand Down Expand Up @@ -79,4 +80,25 @@ export class UploadController {
};
}
}
@Get('status')
async getUploadStatus() {
const jobs = await this.fileQueue.getJobs([
'waiting',
'active',
'completed',
'failed',
]);
const jobStatus = await Promise.all(
jobs.map(async (job) => ({
id: job.id,
name: job.name,
timestamp: job.timestamp,
jobData: job.data,
status: await job.getState(),
progress: await job.progress(),
})),
);
console.log('Job Status:', jobStatus);
return jobStatus;
}
}

0 comments on commit 2e725cd

Please sign in to comment.