Skip to content

Commit

Permalink
fix: emitting saving event only when persisted
Browse files Browse the repository at this point in the history
  • Loading branch information
Farenheith committed Jun 17, 2023
1 parent 2ded99a commit d28c9c8
Showing 1 changed file with 29 additions and 21 deletions.
50 changes: 29 additions & 21 deletions src/s3-cache.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { S3CacheOptions } from './s3-cache-options';
import { AlternativePersistence } from '@remembered/redis';
import { S3 } from '@aws-sdk/client-s3';
import { PutObjectCommandInput, S3 } from '@aws-sdk/client-s3';
import { Redis } from 'ioredis';
import { EventEmitter } from 'events';
import TypedEmitter from 'typed-emitter';
Expand Down Expand Up @@ -48,6 +48,7 @@ export class S3Cache
private async _saveOnRedis(key: string, content: string | Buffer) {
if (this.options.transientTtl) {
await this.redis?.setex(key, this.options.transientTtl, content);
return true;
}
}

Expand All @@ -65,33 +66,40 @@ export class S3Cache
const redisKey = this._getRedisKey(objectKey);
const results = await Promise.allSettled([
this._saveOnRedis(redisKey, content),
this.s3.putObject(params),
this._saveOnS3(params),
]);

results.forEach((x, index) => {
const persistenceType = persistanceTypes[index];
const meta: S3CacheSuccessSaveEventMetadata = {
persistenceType,
key:
persistenceType === 's3'
? this.getLogKey(bucketName, objectKey)
: redisKey,
};
if (x.status === 'rejected') {
this.emit(
'saveError',
'Error while persisting cache',
Object.assign(meta, {
errorMessage: x.reason.message,
syscall: (x.reason as AWSError).syscall,
}),
);
} else {
this.emit('saveSuccess', 'Cache persisted', meta);
if (x.status === 'rejected' || x.value) {
const persistenceType = persistanceTypes[index];
const meta: S3CacheSuccessSaveEventMetadata = {
persistenceType,
key:
persistenceType === 's3'
? this.getLogKey(bucketName, objectKey)
: redisKey,
};
if (x.status === 'rejected') {
this.emit(
'saveError',
'Error while persisting cache',
Object.assign(meta, {
errorMessage: x.reason.message,
syscall: (x.reason as AWSError).syscall,
}),
);
} else {
this.emit('saveSuccess', 'Cache persisted', meta);
}
}
});
}

private async _saveOnS3(params: PutObjectCommandInput) {
await this.s3.putObject(params);
return true;
}

private getLogKey(bucketName: string, objectKey: string): string {
return `${bucketName}/${objectKey}`;
}
Expand Down

0 comments on commit d28c9c8

Please sign in to comment.