Skip to content

Commit

Permalink
fix(aws): throw if upload has failed
Browse files Browse the repository at this point in the history
  • Loading branch information
MaeIg authored and Almouro committed Oct 28, 2022
1 parent 133e3e6 commit ac59072
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions packages/aws-device-farm/src/repositories/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
DeleteUploadCommand,
GetUploadCommand,
ListUploadsCommand,
UploadStatus,
UploadType,
} from "@aws-sdk/client-device-farm";
import path from "path";
Expand All @@ -11,12 +12,20 @@ import { BaseRepository } from "./BaseRepository";
import { uploadFile } from "../utils/uploadFile";

export class UploadRepository extends BaseRepository {
async isUploadSucceeded({ arn }: { arn: string }) {
async getByArn({ arn }: { arn: string }) {
const { upload } = await this.client.send(new GetUploadCommand({ arn }));

if (!upload) throw new Error("Could not find upload");

return upload.status === "SUCCEEDED";
return upload;
}

async isUploadProcessed({ arn }: { arn: string }) {
const upload = await this.getByArn({ arn });
return (
upload.status !== UploadStatus.INITIALIZED &&
upload.status !== UploadStatus.PROCESSING
);
}

async getByName({
Expand Down Expand Up @@ -125,11 +134,18 @@ export class UploadRepository extends BaseRepository {
});
await uploadFile(url, filePath);

while (!(await this.isUploadSucceeded({ arn }))) {
while (!(await this.isUploadProcessed({ arn }))) {
Logger.info(`Waiting 2s for ${name} to be ready`);
await new Promise((resolve) => setTimeout(resolve, 2000));
}

const { status, metadata } = await this.getByArn({ arn });

if (status === UploadStatus.FAILED) {
Logger.error(`Upload failed: ${metadata}`);
throw new Error(metadata);
}

Logger.success(`Upload ${name} ready for use`);

return arn;
Expand Down

0 comments on commit ac59072

Please sign in to comment.