From 4e0dbbb2c8367c25a221512a96937467366da8a1 Mon Sep 17 00:00:00 2001 From: CJ_Clippy Date: Fri, 2 Aug 2024 15:00:49 -0800 Subject: [PATCH] rewind time and fix our past mistakes --- services/bot/package.json | 1 + services/bot/pnpm-lock.yaml | 8 ++++++++ services/bot/src/commands/utilities/record.ts | 1 - services/bot/src/tasks/updateDiscordMessage.ts | 18 ++++++++++-------- services/capture/package.json | 1 + services/capture/pnpm-lock.yaml | 8 ++++++++ services/capture/src/tasks/startRecording.ts | 10 +++++++--- .../migrations/00002_add-records-table.sql | 3 ++- .../00003_add-filesize-and-state.sql | 9 --------- services/migrations/package.json | 2 +- 10 files changed, 38 insertions(+), 23 deletions(-) delete mode 100644 services/migrations/migrations/00003_add-filesize-and-state.sql diff --git a/services/bot/package.json b/services/bot/package.json index eb0661b..324f0c8 100644 --- a/services/bot/package.json +++ b/services/bot/package.json @@ -18,6 +18,7 @@ "author": "@CJ_Clippy", "license": "Unlicense", "dependencies": { + "date-fns": "^3.6.0", "discord.js": "^14.15.3", "dotenv": "^16.4.5", "graphile-config": "0.0.1-beta.9", diff --git a/services/bot/pnpm-lock.yaml b/services/bot/pnpm-lock.yaml index 9ce012b..0146130 100644 --- a/services/bot/pnpm-lock.yaml +++ b/services/bot/pnpm-lock.yaml @@ -8,6 +8,9 @@ importers: .: dependencies: + date-fns: + specifier: ^3.6.0 + version: 3.6.0 discord.js: specifier: ^14.15.3 version: 14.15.3 @@ -418,6 +421,9 @@ packages: create-require@1.1.1: resolution: {integrity: sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==} + date-fns@3.6.0: + resolution: {integrity: sha512-fRHTG8g/Gif+kSh50gaGEdToemgfj74aRX3swtiouboip5JDLAyDE9F11nHMIcvOaXeOC6D7SpNhi7uFyB7Uww==} + debug@4.3.6: resolution: {integrity: sha512-O/09Bd4Z1fBrU4VzkhFqVgpPzaGbw6Sm9FEkBT1A/YBXQFGuuSxa1dN2nxgxS34JmKXqYx8CZAwEVoJFImUXIg==} engines: {node: '>=6.0'} @@ -1113,6 +1119,8 @@ snapshots: create-require@1.1.1: {} + date-fns@3.6.0: {} + debug@4.3.6(supports-color@5.5.0): dependencies: ms: 2.1.2 diff --git a/services/bot/src/commands/utilities/record.ts b/services/bot/src/commands/utilities/record.ts index 56efec1..7cdc22e 100644 --- a/services/bot/src/commands/utilities/record.ts +++ b/services/bot/src/commands/utilities/record.ts @@ -74,7 +74,6 @@ export default { const message = await idk.fetch() const discordMessageId = message.id await workerUtils.addJob('startRecording', { url, discordMessageId }, { maxAttempts: 3 }) - }, }; diff --git a/services/bot/src/tasks/updateDiscordMessage.ts b/services/bot/src/tasks/updateDiscordMessage.ts index 9ee8a2a..046eb56 100644 --- a/services/bot/src/tasks/updateDiscordMessage.ts +++ b/services/bot/src/tasks/updateDiscordMessage.ts @@ -1,6 +1,7 @@ import 'dotenv/config' import type { RecordingState } from '@futureporn/types' -import { type Task, type Helpers } from 'graphile-worker'; +import { type Task, type Helpers } from 'graphile-worker' +import { add } from 'date-fns' import { Client, GatewayIntentBits, @@ -13,11 +14,6 @@ import { Guild } from 'discord.js'; -// export interface DiscordMessageUpdateJob extends Job { -// data: { -// captureJobId: string; -// } -// } interface Payload { recordId: number; @@ -41,7 +37,7 @@ if (!process.env.DISCORD_GUILD_ID) throw new Error("DISCORD_GUILD_ID was missing async function editDiscordMessage({ helpers, state, discordMessageId }: { helpers: Helpers, state: RecordingState, discordMessageId: string }) { // const { captureJobId } = job.data - helpers.logger.info(`discordMessageUpdate job has begun with discordMessageId=${discordMessageId}, state=${state}`) + helpers.logger.info(`editDiscordMessage has begun with discordMessageId=${discordMessageId}, state=${state}`) // create a discord.js client @@ -94,7 +90,7 @@ async function getRecordFromDatabase(recordId: number) { if (!res.ok) { throw new Error(`failed fetching record ${recordId}. status=${res.status}, statusText=${res.statusText}`) } - const body = await res.json() as any + const body = await res.json() as any return body[0]; } @@ -114,6 +110,12 @@ export const updateDiscordMessage: Task = async function (payload, helpers: Help const record = await getRecordFromDatabase(payload.recordId) const { discordMessageId, state } = record editDiscordMessage({ helpers, state, discordMessageId }) + // schedule the next update 10s from now, but only if the recording is still happening + if (state !== 'ended') { + const runAt = add(new Date(), { seconds: 10 }) + const recordId = record.id + await helpers.addJob('updateDiscordMessage', { recordId }, { jobKey: `record_${recordId}_update_discord_message`, maxAttempts: 3, runAt }) + } } catch (e) { helpers.logger.error(`caught an error during updateDiscordMessage. e=${e}`) } diff --git a/services/capture/package.json b/services/capture/package.json index fda6652..e2f5da9 100644 --- a/services/capture/package.json +++ b/services/capture/package.json @@ -27,6 +27,7 @@ "@types/chai": "^4.3.16", "@types/fluent-ffmpeg": "^2.1.24", "@types/mocha": "^10.0.7", + "date-fns": "^3.6.0", "diskusage": "^1.2.0", "dotenv": "^16.4.5", "execa": "^6.1.0", diff --git a/services/capture/pnpm-lock.yaml b/services/capture/pnpm-lock.yaml index 9d989c6..8c7d809 100644 --- a/services/capture/pnpm-lock.yaml +++ b/services/capture/pnpm-lock.yaml @@ -38,6 +38,9 @@ importers: '@types/mocha': specifier: ^10.0.7 version: 10.0.7 + date-fns: + specifier: ^3.6.0 + version: 3.6.0 diskusage: specifier: ^1.2.0 version: 1.2.0 @@ -1395,6 +1398,9 @@ packages: resolution: {integrity: sha512-t/Ygsytq+R995EJ5PZlD4Cu56sWa8InXySaViRzw9apusqsOO2bQP+SbYzAhR0pFKoB+43lYy8rWban9JSuXnA==} engines: {node: '>= 0.4'} + date-fns@3.6.0: + resolution: {integrity: sha512-fRHTG8g/Gif+kSh50gaGEdToemgfj74aRX3swtiouboip5JDLAyDE9F11nHMIcvOaXeOC6D7SpNhi7uFyB7Uww==} + dateformat@4.6.3: resolution: {integrity: sha512-2P0p0pFGzHS5EMnhdxQi7aJN+iMheud0UhG4dlE1DLAlvL8JHjJJTX/CSm4JXwV0Ka5nGk3zC5mcb5bUQUxxMA==} @@ -4514,6 +4520,8 @@ snapshots: es-errors: 1.3.0 is-data-view: 1.0.1 + date-fns@3.6.0: {} + dateformat@4.6.3: {} debug@3.2.7(supports-color@5.5.0): diff --git a/services/capture/src/tasks/startRecording.ts b/services/capture/src/tasks/startRecording.ts index 7ae7a94..de5ff95 100644 --- a/services/capture/src/tasks/startRecording.ts +++ b/services/capture/src/tasks/startRecording.ts @@ -1,5 +1,6 @@ import { Helpers, type Task } from 'graphile-worker' +import { add } from 'date-fns' /** * url is the URL to be recorded. Ex: chaturbate.com/projektmelody @@ -20,6 +21,7 @@ function assertPayload(payload: any): asserts payload is Payload { function assertEnv() { if (!process.env.AUTOMATION_USER_JWT) throw new Error('AUTOMATION_USER_JWT was missing in env'); + if (!process.env.POSTGREST_URL) throw new Error('POSTGREST_URL was missing in env'); } async function createRecordingRecord(payload: Payload, helpers: Helpers): Promise { @@ -29,7 +31,7 @@ async function createRecordingRecord(payload: Payload, helpers: Helpers): Promis discord_message_id: discordMessageId, is_aborted: false } - const res = await fetch('http://postgrest.futureporn.svc.cluster.local:9000/records', { + const res = await fetch(`${process.env.POSTGREST_URL}/records`, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -55,8 +57,10 @@ export const startRecording: Task = async function (payload, helpers) { assertEnv() const recordId = await createRecordingRecord(payload, helpers) const { url } = payload; - console.log(`@todo simulated start_recording with url=${url}, recordId=${recordId}`) - await helpers.addJob('record', { url, recordId }, { maxAttempts: 3 }) + console.log(`@todo simulated startRecording with url=${url}, recordId=${recordId}`) + await helpers.addJob('record', { url, recordId }, { maxAttempts: 3, jobKey: `record_${recordId}` }) + const runAt = add(new Date(), { seconds: 10 }) + await helpers.addJob('updateDiscordMessage', { recordId }, { jobKey: `record_${recordId}_update_discord_message`, maxAttempts: 3, runAt }) } export default startRecording \ No newline at end of file diff --git a/services/migrations/migrations/00002_add-records-table.sql b/services/migrations/migrations/00002_add-records-table.sql index 21f13f3..4f9efb1 100644 --- a/services/migrations/migrations/00002_add-records-table.sql +++ b/services/migrations/migrations/00002_add-records-table.sql @@ -3,7 +3,8 @@ CREATE TABLE api.records ( id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY, url TEXT NOT NULL, discord_message_id TEXT NOT NULL, - is_aborted BOOLEAN DEFAULT FALSE + recording_state TEXT NOT NULL DEFAULT 'pending', + file_size BIGINT NOT NULL DEFAULT 0; ); -- roles & permissions for our backend automation user diff --git a/services/migrations/migrations/00003_add-filesize-and-state.sql b/services/migrations/migrations/00003_add-filesize-and-state.sql deleted file mode 100644 index 77b29d3..0000000 --- a/services/migrations/migrations/00003_add-filesize-and-state.sql +++ /dev/null @@ -1,9 +0,0 @@ -ALTER TABLE api.records - ADD COLUMN recording_state TEXT NOT NULL DEFAULT 'pending' CHECK(recording_state IN ('pending', 'recording', 'aborted', 'ended')), - ADD COLUMN file_size BIGINT NOT NULL DEFAULT 0; - --- Grant permissions to the new columns for our backend automation user -GRANT UPDATE (recording_state, file_size) ON api.records TO automation; - --- Grant read-only access to the new columns for our web anon user -GRANT SELECT (recording_state, file_size) ON api.records TO web_anon; \ No newline at end of file diff --git a/services/migrations/package.json b/services/migrations/package.json index bbc6044..30479b2 100644 --- a/services/migrations/package.json +++ b/services/migrations/package.json @@ -1,7 +1,7 @@ { "name": "@futureporn/migrations", "type": "module", - "version": "0.3.0", + "version": "0.4.0", "description": "", "main": "index.js", "scripts": {