4
4
} from '@aws-sdk/client-s3' ;
5
5
import { Injectable } from '@nestjs/common' ;
6
6
import { bufferFromStream , cleanJoin , Nil } from '@seedcompany/common' ;
7
- import { Connection } from 'cypher-query-builder' ;
8
7
import { fileTypeFromStream } from 'file-type' ;
9
8
import { intersection } from 'lodash' ;
10
9
import { Duration } from 'luxon' ;
@@ -23,7 +22,13 @@ import {
23
22
UnauthorizedException ,
24
23
} from '~/common' ;
25
24
import { withAddedPath } from '~/common/url.util' ;
26
- import { ConfigService , IEventBus , ILogger , Logger } from '~/core' ;
25
+ import {
26
+ ConfigService ,
27
+ IEventBus ,
28
+ ILogger ,
29
+ Logger ,
30
+ RollbackManager ,
31
+ } from '~/core' ;
27
32
import { FileBucket } from './bucket' ;
28
33
import {
29
34
CreateDefinedFileVersionInput ,
@@ -56,7 +61,7 @@ export class FileService {
56
61
constructor (
57
62
private readonly bucket : FileBucket ,
58
63
private readonly repo : FileRepository ,
59
- private readonly db : Connection ,
64
+ private readonly rollbacks : RollbackManager ,
60
65
private readonly config : ConfigService ,
61
66
private readonly mediaService : MediaService ,
62
67
private readonly eventBus : IEventBus ,
@@ -384,28 +389,17 @@ export class FileService {
384
389
if ( existingUpload . status === 'rejected' ) {
385
390
await this . bucket . moveObject ( `temp/${ uploadId } ` , uploadId ) ;
386
391
387
- // A bit of a hacky way to move files back to the temp/ folder on
388
- // mutation error / transaction rollback. This prevents orphaned files in bucket.
389
- const tx = this . db . currentTransaction ;
390
- // The mutation can be retried multiple times, when neo4j deems the error
391
- // is retry-able, but we only want to attach this rollback logic once.
392
- if ( tx && ! ( tx as any ) . __S3_ROLLBACK ) {
393
- ( tx as any ) . __S3_ROLLBACK = true ;
394
-
395
- const orig = tx . rollback . bind ( tx ) ;
396
- tx . rollback = async ( ) => {
397
- // Undo above operation by moving it back to temp folder.
398
- await this . bucket
399
- . moveObject ( uploadId , `temp/${ uploadId } ` )
400
- . catch ( ( e ) => {
401
- this . logger . error ( 'Failed to move file back to temp holding' , {
402
- uploadId,
403
- exception : e ,
404
- } ) ;
392
+ // Undo the above operation by moving it back to temp folder.
393
+ this . rollbacks . add ( async ( ) => {
394
+ await this . bucket
395
+ . moveObject ( uploadId , `temp/${ uploadId } ` )
396
+ . catch ( ( e ) => {
397
+ this . logger . error ( 'Failed to move file back to temp holding' , {
398
+ uploadId,
399
+ exception : e ,
405
400
} ) ;
406
- await orig ( ) ;
407
- } ;
408
- }
401
+ } ) ;
402
+ } ) ;
409
403
}
410
404
411
405
await this . mediaService . detectAndSave ( fv , media ) ;
0 commit comments