Skip to content
Merged
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
31 changes: 31 additions & 0 deletions .github/workflows/cleanup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Firebase Cleanup

on:
schedule:
- cron: '0 2 * * *' # Daily at 2 AM UTC
workflow_dispatch:

jobs:
cleanup:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Bun
uses: oven-sh/setup-bun@v2

- name: Install
run: bun install

- name: Build with Vite
run: bun run build --base=/cellpack-client

- name: Run cleanup
env:
API_KEY: ${{ secrets.FIREBASE_API_KEY }}
MESSAGING_SENDER_ID: ${{ secrets.FIREBASE_MESSAGING_SENDER_ID }}
APP_ID: ${{ secrets.FIREBASE_APP_ID }}
MEASUREMENT_ID: ${{ secrets.FIREBASE_MEASUREMENT_ID }}
run: bun run scripts/cleanup.ts
16 changes: 16 additions & 0 deletions scripts/cleanup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

import { docCleanup } from '../src/firebase.ts';

async function main() {
try {
console.log("Starting Firebase cleanup...");
await docCleanup();
console.log("Cleanup completed successfully");
process.exit(0);
} catch (error) {
console.error("Cleanup failed:", error);
process.exit(1);
}
}

main();
12 changes: 11 additions & 1 deletion src/constants/firebaseConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,14 @@ export const FIRESTORE_FIELDS = {
REGIONS: "regions",
INTERIOR: "interior",
GRADIENT: "gradient",
} as const;
TIMESTAMP: "timestamp",
} as const;

export const RETENTION_POLICY = {
RETENTION_PERIODS: {
RECIPES_EDITED: 24 * 60 * 60 * 1000, // 24 hours
JOB_STATUS: 24 * 60 * 60 * 1000, // 24 hours
},

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed batch deletion related configs for now, as they feel overkill for our current doc size, for 1-100 docs, Promise.all() handles deletion faster, plus we run cleanup daily so batching isn't necessary at the moment.

TIMESTAMP_FIELD: "timestamp",
} as const;
59 changes: 54 additions & 5 deletions src/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import {
DocumentData,
setDoc,
doc,
Timestamp,
deleteDoc,
} from "firebase/firestore";
import {
FIREBASE_CONFIG,
FIRESTORE_COLLECTIONS,
FIRESTORE_FIELDS,
RETENTION_POLICY,
} from "./constants/firebaseConstants";
import {
Dictionary,
Expand All @@ -30,14 +33,26 @@ import {
} from "./types";
import { resolveRefs, isFirebaseRef, isInRefsByCollection, addRef } from "./recipeLoader";

const getEnvVar = (key: string): string => {
// check if we're in a browser environment (Vite)
if (typeof window !== "undefined" && import.meta.env) {
return import.meta.env[key] || "";
}
// check if we're in Node.js environment (GitHub Actions)
if (typeof process !== "undefined" && process.env) {
return process.env[key] || "";
}
return "";
};

const firebaseConfig = {
apiKey: import.meta.env.API_KEY,
apiKey: getEnvVar("API_KEY"),
authDomain: FIREBASE_CONFIG.AUTH_DOMAIN,
projectId: FIREBASE_CONFIG.PROJECT_ID,
storageBucket: FIREBASE_CONFIG.STORAGE_BUCKET,
messagingSenderId: import.meta.env.MESSAGING_SENDER_ID,
appId: import.meta.env.APP_ID,
measurementId: import.meta.env.MEASUREMENT_ID,
messagingSenderId: getEnvVar("MESSAGING_SENDER_ID"),
appId: getEnvVar("APP_ID"),
measurementId: getEnvVar("MEASUREMENT_ID"),
};

// Initialize Firebase
Expand Down Expand Up @@ -74,6 +89,14 @@ const queryAllDocuments = async (collectionName: string) => {
return await getDocs(q);
};

const queryDocumentsByTime = async (collectionName: string, field: string, operator: "<" | "<=" | "==" | "!=" | ">=" | ">" , value: Timestamp) => {
const q = query(
collection(db, collectionName),
where(field, operator, value)
);
return await getDocs(q);
};

const mapQuerySnapshotToDocs = (querySnapshot: QuerySnapshot<DocumentData>) => {
return querySnapshot.docs.map((doc) => ({
id: doc.id,
Expand Down Expand Up @@ -274,4 +297,30 @@ const updateRecipe = async (id: string, data: object) => {
await setDoc(doc(db, FIRESTORE_COLLECTIONS.EDITED_RECIPES, id), data);
}

export { db, getLocationDict, getDocById, getFirebaseRecipe, getJobStatus, getResultPath, updateRecipe };
const docCleanup = async () => {
const now = Date.now();
const collectionsToClean = [
{ name: FIRESTORE_COLLECTIONS.EDITED_RECIPES, retention: RETENTION_POLICY.RETENTION_PERIODS.RECIPES_EDITED },
{ name: FIRESTORE_COLLECTIONS.JOB_STATUS, retention: RETENTION_POLICY.RETENTION_PERIODS.JOB_STATUS }
];

for (const collectionConfig of collectionsToClean) {
const cutoffTime = Timestamp.fromMillis(now - collectionConfig.retention);
const querySnapshot = await queryDocumentsByTime(
collectionConfig.name,
RETENTION_POLICY.TIMESTAMP_FIELD,
"<",
cutoffTime
);

const deletePromises: Promise<void>[] = [];

querySnapshot.forEach((document) => {
deletePromises.push(deleteDoc(doc(db, collectionConfig.name, document.id)));
});

await Promise.all(deletePromises);
console.log(`Cleaned up ${deletePromises.length} documents from ${collectionConfig.name}`);
}
}
export { db, getLocationDict, getDocById, getFirebaseRecipe, getJobStatus, getResultPath, updateRecipe, docCleanup };