Skip to content

Commit

Permalink
Delete unused rooms
Browse files Browse the repository at this point in the history
  • Loading branch information
ed-asriyan committed Feb 25, 2024
1 parent f8ea43f commit ba2320e
Show file tree
Hide file tree
Showing 8 changed files with 2,249 additions and 19 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/static.yml → .github/workflows/CD.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ jobs:
uses: actions/checkout@v4

- name: Build the app
run: docker build -t movie-together .
run: docker build -t watch-together --target app .

- name: Copy static files
run: |
id=$(docker create movie-together)
id=$(docker create watch-together)
docker cp $id:/dist $(pwd)/dist
- name: Generate the sitemap
Expand Down
30 changes: 30 additions & 0 deletions .github/workflows/clean-db.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Delete unused rooms

env:
DIFF: 2678400 # 31 day

on:
workflow_dispatch:
schedule:
- cron: '0 0 1 * *'

concurrency:
group: "clean-db"

jobs:
clean-db:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Save service-account-key.json file
env:
FIREBASE_SERVICE_ACCOUNT_KEY: ${{ secrets.FIREBASE_SERVICE_ACCOUNT_KEY }}
run: echo "$FIREBASE_SERVICE_ACCOUNT_KEY" >> service-account-key.json

- name: Build the app
run: docker build -t watch-together-cleaner --target clean-db .

- name: Run the container
run: docker run -e DIFF=$DIFF watch-together-cleaner
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ node_modules
!.env.example
vite.config.js.timestamp-*
vite.config.ts.timestamp-*
service-account-key.json
6 changes: 5 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ COPY src src

RUN npm run build

FROM scratch
FROM builder as clean-db
ENV DIFF 0
RUN npm run clean-db -- $DIFF

FROM scratch as app
COPY --from=builder /app/build /dist
ENTRYPOINT sh
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
# Movie Together
# Movie Together [![CD](https://github.com/ed-asriyan/watch-together/actions/workflows/CD.yml/badge.svg)](https://github.com/ed-asriyan/watch-together/actions/workflows/CD.yml)
Web application built on Svelte.js and Firebase for whatching movies together on different devices.

<h3 align="center">
<a href="https://watchtogether.online">👉 watchtogether.online 👈</a>
<a href="https://watchtogether.online" target="_blank">👉 watchtogether.online 👈</a>
</h3>

https://github.com/ed-asriyan/movie-together/assets/7848847/08522a1e-31d5-4138-9150-c4574321536a
https://github.com/ed-asriyan/watch-together/assets/7848847/2d2799f1-cc79-4732-8657-74f78268b8c2

# Setup
To setup/deploy new version of the app, you should:
## Init
1. Setup Firebase project
1. Create Firebase Realtime database: https://console.firebase.google.com
2. Copy Firebase project cofiguration to [firebase-config.json](./src/firebase-config.json)
2. Copy Firebase project cofiguration
1. Copy firebase config to [firebase-config.json](./src/firebase-config.json)
2. Copy service account key to `FIREBASE_SERVICE_ACCOUNT_KEY` repository secret
3. Setup Firebase Realtime database rules:
```json
{
Expand All @@ -32,8 +34,10 @@ To setup/deploy new version of the app, you should:
4. Add domain where you're going to host the website to "Authorized Domains" section of "Authentication"
2. Setup Google Analytics (should be created with Firebase)
1. Copy Google Analytics cofiguration to [google-analytics.js](./src/google-analytics.js)
3. Install node.js v18 or newer
4. Install npm dependencies:

## Local development
1. Install node.js v18 or newer
2. Install npm dependencies:
```console
npm ci
```
Expand All @@ -47,3 +51,7 @@ Or build static files to serve:
```console
npm rub build
```

## GitHub Actions
* Each push to `master` triggers deployment to production
* Every 1 day of a month, teams last updated more than 31 days ago are deleted
41 changes: 41 additions & 0 deletions clean-db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import admin from 'firebase-admin';
import serviceAccount from './service-account-key.json' assert { type: "json" };
import firebaseConfig from './src/firebase-config.json' assert { type: "json" };

const diff = process.argv[2];
const now = Math.round(new Date().getTime() / 1000);

admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: firebaseConfig.databaseURL
});

const main = async function() {
const root = admin.database().ref('room');
const promises = [];
let count = 0;

process.stdout.write('Started...\n');
(await root.once('value')).forEach(childSnapshot => {
const task = (async function () {
const timestamp = childSnapshot.child('timestamp').val();
if (typeof timestamp !== 'number') {
process.stdout.write(`room#${childSnapshot.key}\twrong type: ${typeof timestamp}\n`);
} else if (timestamp < now - diff) {
process.stdout.write(`room#${childSnapshot.key}\tupdated ${Math.round((now - timestamp) / 60 / 60 / 24)} hours ago\n`);
} else if (timestamp > now + diff) {
process.stdout.write(`room#${childSnapshot.key}\tupdated ${Math.round((timestamp - now) / 60 / 60 / 24)} hours ahead (in future)\n`);
} else {
return;
}
await childSnapshot.ref.remove();
++count;
})();
promises.push(task);
});

await Promise.all(promises);
process.stdout.write(`\nDone:\n- ${promises.length} rooms total\n- ${count} rooms deleted\n- ${promises.length - count} rooms left\n`);
};

main().catch(e => process.stderr.write(e.toString())).finally(() => process.exit(0));
Loading

0 comments on commit ba2320e

Please sign in to comment.