Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add cache cleaner #66

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions scripts/clear-cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'dotenv/config';
import { storageEngine } from '../src/helpers/utils';

/**
* Clear all the cached files in the storage engine,
* as configured in .env
*/
async function main() {
if (process.argv.length < 2) {
console.error(`Usage: yarn ts-node scripts/clear-cache.ts`);
return process.exit(1);
}

const engine = storageEngine(process.env.VOTE_REPORT_SUBDIR);
const list = await engine.list();

console.log(`> Deleting ${list.length} cached files on ${engine.constructor.name}`);
await engine.clear();

const finalList = await engine.list();
console.log(`> Finished! ${finalList.length} files remaining`);
}

(async () => {
try {
await main();
process.exit(0);
} catch (e) {
console.error(e);
process.exit(1);
}
})();
29 changes: 29 additions & 0 deletions scripts/list-cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'dotenv/config';
import { storageEngine } from '../src/helpers/utils';

/**
* Display the contents of the cached storage engine,
* as configured in .env
*/
async function main() {
if (process.argv.length < 2) {
console.error(`Usage: yarn ts-node scripts/list-cache.ts`);
return process.exit(1);
}

const engine = storageEngine(process.env.VOTE_REPORT_SUBDIR);
const list = await engine.list();

console.log(`> Found ${list.length} cached items`);
console.log(list);
}

(async () => {
try {
await main();
process.exit(0);
} catch (e) {
console.error(e);
process.exit(1);
}
})();
48 changes: 47 additions & 1 deletion src/lib/storage/aws.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { S3Client, GetObjectCommand, PutObjectCommand } from '@aws-sdk/client-s3';
import {
S3Client,
GetObjectCommand,
PutObjectCommand,
ListObjectsV2Command,
DeleteObjectCommand
} from '@aws-sdk/client-s3';
import type { IStorage } from './types';

const CACHE_PATH = 'public';
Expand Down Expand Up @@ -54,6 +60,46 @@ class Aws implements IStorage {
}
}

async list() {
try {
const command = new ListObjectsV2Command({
Bucket: process.env.AWS_BUCKET_NAME,
Prefix: this.#path()
});
const list = await this.client.send(command);

return list.Contents || [];
} catch (e) {
console.error('[storage:aws] List failed', e);
return [];
}
}

async delete(key: string): Promise<any> {
try {
const command = new DeleteObjectCommand({
Bucket: process.env.AWS_BUCKET_NAME,
Key: key
});
return await this.client.send(command);
} catch (e) {
console.error('[storage:aws] File delete failed', e);
return false;
}
}

async clear() {
const items = await this.list();

items.map(item => {
if (item.Key) {
this.delete(item.Key);
}
});

return true;
}

#path(key?: string) {
return [CACHE_PATH, this.subDir?.replace(/^\/+|\/+$/, ''), key].filter(p => p).join('/');
}
Expand Down
34 changes: 33 additions & 1 deletion src/lib/storage/file.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { writeFileSync, existsSync, mkdirSync, readFileSync } from 'fs';
import { writeFileSync, existsSync, mkdirSync, readFileSync, readdirSync, rmSync } from 'fs';
import type { IStorage } from './types';

const CACHE_PATH = `${__dirname}/../../../tmp`;
Expand All @@ -14,6 +14,10 @@ class File implements IStorage {
}
}

clearAll(): Promise<boolean> {
throw new Error('Method not implemented.');
}

async set(key: string, value: string) {
try {
writeFileSync(this.#path(key), value);
Expand All @@ -39,6 +43,34 @@ class File implements IStorage {
}
}

async delete(key: string) {
try {
return rmSync(this.#path(key));
} catch (e) {
console.error('[storage:file] Fetch delete failed', e);
return false;
}
}

async list() {
try {
return readdirSync(this.#path());
} catch (e) {
console.error('[storage:file] List failed', e);
return [];
}
}

async clear() {
const items = await this.list();

items.map(item => {
this.delete(item);
});

return true;
}

#path(key?: string) {
return [CACHE_PATH, this.subDir?.replace(/^\/+|\/+$/, ''), key].filter(p => p).join('/');
}
Expand Down
3 changes: 3 additions & 0 deletions src/lib/storage/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ export interface IStorage {

set(key: string, value: string): Promise<unknown>;
get(key: string): Promise<unknown>;
delete(key: string): Promise<any>;
list(): Promise<any>;
clear(): Promise<boolean>;
}

export interface IStorageConstructor {
Expand Down