forked from heyxyz/hey
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
name: Cron - Cleanup Clickhouse | ||
|
||
on: | ||
schedule: | ||
- cron: '0 1 * * *' | ||
workflow_dispatch: | ||
|
||
jobs: | ||
cleanup: | ||
name: Cleanup | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Cleanup Clickhouse | ||
env: | ||
SECRET: ${{ secrets.SECRET }} | ||
run: | | ||
curl -X POST \ | ||
-H "Content-Type: application/json" \ | ||
-H "Referer: https://hey.xyz" \ | ||
-d '{"secret": "'"$SECRET"'"}' \ | ||
https://api.hey.xyz/internal/cleanup/clickhouse |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import type { Handler } from 'express'; | ||
|
||
import { Errors } from '@hey/data/errors'; | ||
import logger from '@hey/lib/logger'; | ||
import catchedError from '@utils/catchedError'; | ||
import createClickhouseClient from '@utils/createClickhouseClient'; | ||
import { invalidBody, noBody } from '@utils/responses'; | ||
import { object, string } from 'zod'; | ||
|
||
type ExtensionRequest = { | ||
secret: string; | ||
}; | ||
|
||
const validationSchema = object({ | ||
secret: string() | ||
}); | ||
|
||
export const post: Handler = async (req, res) => { | ||
const { body } = req; | ||
|
||
if (!body) { | ||
return noBody(res); | ||
} | ||
|
||
const validation = validationSchema.safeParse(body); | ||
|
||
if (!validation.success) { | ||
return invalidBody(res); | ||
} | ||
|
||
const { secret } = body as ExtensionRequest; | ||
|
||
if (secret !== process.env.SECRET) { | ||
return res | ||
.status(400) | ||
.json({ error: Errors.InvalidSecret, success: false }); | ||
} | ||
|
||
try { | ||
const client = createClickhouseClient(); | ||
|
||
// Cleanup events that are not from Hey | ||
await client.command({ | ||
query: "ALTER TABLE events DELETE WHERE url NOT LIKE '%hey.xyz%';" | ||
}); | ||
logger.info('Cleaned up Clickhouse'); | ||
|
||
return res.status(200).json({ success: true }); | ||
} catch (error) { | ||
return catchedError(res, error); | ||
} | ||
}; |