Skip to content

Commit

Permalink
Defer removing assets until after upload all completes (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanjduffy authored Sep 26, 2023
1 parent 018e21c commit f76cbb8
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions packages/replay/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,8 @@ async function doUploadRecording(
recording: RecordingEntry,
verbose?: boolean,
apiKey?: string,
agent?: any
agent?: any,
removeAssets: boolean = false
) {
debug("Uploading %s from %s to %s", recording.id, dir, server);
maybeLog(verbose, `Starting upload for ${recording.id}...`);
Expand All @@ -370,7 +371,9 @@ async function doUploadRecording(
debug("Uploading crash %o", recording);
await doUploadCrash(dir, server, recording, verbose, apiKey, agent);
maybeLog(verbose, `Crash report uploaded for ${recording.id}`);
removeRecordingAssets(recording, { directory: dir });
if (removeAssets) {
removeRecordingAssets(recording, { directory: dir });
}
return recording.id;
}

Expand Down Expand Up @@ -456,7 +459,9 @@ async function doUploadRecording(
{ concurrency: 10, stopOnError: false }
);

removeRecordingAssets(recording, { directory: dir });
if (removeAssets) {
removeRecordingAssets(recording, { directory: dir });
}

addRecordingEvent(dir, "uploadFinished", recording.id);
maybeLog(
Expand All @@ -478,7 +483,7 @@ async function uploadRecording(id: string, opts: Options = {}) {
return null;
}

return doUploadRecording(dir, server, recording, opts.verbose, opts.apiKey, opts.agent);
return doUploadRecording(dir, server, recording, opts.verbose, opts.apiKey, opts.agent, true);
}

async function processUploadedRecording(recordingId: string, opts: Options) {
Expand Down Expand Up @@ -546,10 +551,18 @@ async function uploadAllRecordings(opts: Options & UploadOptions = {}) {

const recordingIds: (string | null)[] = await pMap(
recordings,
(r: RecordingEntry) => doUploadRecording(dir, server, r, opts.verbose, opts.apiKey, opts.agent),
(r: RecordingEntry) =>
doUploadRecording(dir, server, r, opts.verbose, opts.apiKey, opts.agent, false),
{ concurrency: batchSize, stopOnError: false }
);

recordingIds.forEach(id => {
const recording = recordings.find(r => r.id === id);
if (!recording) return;

removeRecordingAssets(recording, opts);
});

return recordingIds.every(r => r !== null);
}

Expand All @@ -570,7 +583,7 @@ async function doViewRecording(
recordingId = recording.recordingId;
server = recording.server!;
} else {
recordingId = await doUploadRecording(dir, server, recording, verbose, apiKey, agent);
recordingId = await doUploadRecording(dir, server, recording, verbose, apiKey, agent, true);

if (!recordingId) {
return false;
Expand Down Expand Up @@ -618,8 +631,10 @@ async function viewLatestRecording(opts: Options = {}) {
function maybeRemoveAssetFile(asset?: string) {
if (asset) {
try {
debug("Removing asset file %s", asset);
fs.unlinkSync(asset);
if (fs.existsSync(asset)) {
debug("Removing asset file %s", asset);
fs.unlinkSync(asset);
}
} catch (e) {
debug("Failed to remove asset file: %s", e);
}
Expand Down

0 comments on commit f76cbb8

Please sign in to comment.