From 11af3dc87bb90e4f5be05617950096f64594af93 Mon Sep 17 00:00:00 2001 From: CJ_Clippy Date: Fri, 2 Aug 2024 17:29:02 -0800 Subject: [PATCH] progress --- services/capture/src/tasks/record.ts | 63 +++++++++++-------- services/capture/src/tasks/startRecording.ts | 3 +- .../migrations/00002_add-records-table.sql | 3 +- services/migrations/package.json | 2 +- 4 files changed, 41 insertions(+), 30 deletions(-) diff --git a/services/capture/src/tasks/record.ts b/services/capture/src/tasks/record.ts index f5fb584..3614aa5 100644 --- a/services/capture/src/tasks/record.ts +++ b/services/capture/src/tasks/record.ts @@ -16,7 +16,9 @@ interface Payload { interface RecordingRecord { id: number; - isAborted: boolean; + recordingState: RecordingState; + fileSize: number; + discordMessageId: string; } function assertPayload(payload: any): asserts payload is Payload { @@ -32,6 +34,7 @@ function assertEnv() { if (!process.env.S3_ENDPOINT) throw new Error('S3_ENDPOINT was missing in env'); if (!process.env.S3_BUCKET) throw new Error('S3_BUCKET was missing in env'); if (!process.env.POSTGREST_URL) throw new Error('POSTGREST_URL was missing in env'); + if (!process.env.AUTOMATION_USER_JWT) throw new Error('AUTOMATION_USER_JWT was missing in env'); } async function getRecording(url: string, recordId: number, abortSignal: AbortSignal) { @@ -48,37 +51,42 @@ async function getRecording(url: string, recordId: number, abortSignal: AbortSig return record } -async function checkIfAborted(recordId: number): Promise { - const res = await fetch(`${process.env.POSTGREST_URL}/records?id=eq.${recordId}`, { - headers: { - 'Content-Type': 'application/json', - 'Accepts': 'application/json' - } - }) - if (!res.ok) { - throw new Error(`failed to checkIfAborted. status=${res.status}, statusText=${res.statusText}`); - } - const body = await res.json() as RecordingRecord[]; - if (!body[0]) throw new Error(`failed to get a record that matched recordId=${recordId}`) - return body[0].isAborted -} +// async function checkIfAborted(recordId: number): Promise { +// const res = await fetch(`${process.env.POSTGREST_URL}/records?id=eq.${recordId}`, { +// headers: { +// 'Content-Type': 'application/json', +// 'Accepts': 'application/json' +// } +// }) +// if (!res.ok) { +// throw new Error(`failed to checkIfAborted. status=${res.status}, statusText=${res.statusText}`); +// } +// const body = await res.json() as RecordingRecord[]; +// if (!body[0]) throw new Error(`failed to get a record that matched recordId=${recordId}`) +// return body[0].isAborted +// } -async function updateDatabaseRecord({recordId, state, filesize}: { recordId: number, state: RecordingState, filesize: number }): Promise { +async function updateDatabaseRecord({recordId, recordingState, fileSize}: { recordId: number, recordingState?: RecordingState, fileSize: number }): Promise { + const payload: any = { + file_size: fileSize + } + if (recordingState) payload.recording_state = recordingState; const res = await fetch(`${process.env.POSTGREST_URL}/records?id=eq.${recordId}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json', 'Accepts': 'application/json', - 'Prefer': 'return=representation' + 'Prefer': 'return=representation', + 'Authorization': `Bearer ${process.env.AUTOMATION_USER_JWT}` }, - body: JSON.stringify({ state, filesize }) + body: JSON.stringify(payload) }) if (!res.ok) { - throw new Error(`failed to checkIfAborted. status=${res.status}, statusText=${res.statusText}`); + throw new Error(`failed to updateDatabaseRecord. status=${res.status}, statusText=${res.statusText}`); } const body = await res.json() as RecordingRecord[]; if (!body[0]) throw new Error(`failed to get a record that matched recordId=${recordId}`) - return body[0].isAborted + return body[0] } export const record: Task = async function (payload, helpers) { @@ -91,20 +99,21 @@ export const record: Task = async function (payload, helpers) { try { const record = await getRecording(url, recordId, abortController.signal) // every 30s, we - // 1. poll db to see if our job has been aborted by the user - // 2. update the db record with the RecordingState and filesize + // 1. update the db record with the RecordingState and filesize + // 2. poll db to see if our job has been aborted by the user interval = setInterval(async () => { try { helpers.logger.info(`checkIfAborted()`) - const isAborted = await checkIfAborted(recordId) - if (isAborted) { + const updatePayload = { recordId, fileSize: record.counter } + const updatedRecord = await updateDatabaseRecord(updatePayload) + if (updatedRecord.recordingState === 'ended') { + helpers.logger.info(`record ${recordId} has been aborted by a user so we stop the recording now.`) abortController.abort() } - let state: RecordingState = 'recording' } catch (e) { - helpers.logger.error(`error while checking if this job was aborted. For sake of the recording in progress we are ignoring the following error. ${e}`) + helpers.logger.error(`error while updating database. For sake of the recording in progress we are ignoring the following error. ${e}`) } - }, 30000) + }, 3000) // start recording and await the S3 upload being finished await record.start() diff --git a/services/capture/src/tasks/startRecording.ts b/services/capture/src/tasks/startRecording.ts index de5ff95..02ef471 100644 --- a/services/capture/src/tasks/startRecording.ts +++ b/services/capture/src/tasks/startRecording.ts @@ -29,7 +29,8 @@ async function createRecordingRecord(payload: Payload, helpers: Helpers): Promis const record = { url, discord_message_id: discordMessageId, - is_aborted: false + recording_state: 'pending', + file_size: 0 } const res = await fetch(`${process.env.POSTGREST_URL}/records`, { method: 'POST', diff --git a/services/migrations/migrations/00002_add-records-table.sql b/services/migrations/migrations/00002_add-records-table.sql index c6406fa..b9cd2e3 100644 --- a/services/migrations/migrations/00002_add-records-table.sql +++ b/services/migrations/migrations/00002_add-records-table.sql @@ -4,7 +4,8 @@ CREATE TABLE api.records ( url TEXT NOT NULL, discord_message_id TEXT NOT NULL, recording_state TEXT NOT NULL DEFAULT 'pending', - file_size BIGINT NOT NULL DEFAULT 0 + file_size BIGINT NOT NULL DEFAULT 0, + is_aborted BOOLEAN NOT NULL DEFAULT FALSE ); -- roles & permissions for our backend automation user diff --git a/services/migrations/package.json b/services/migrations/package.json index 397721c..214a680 100644 --- a/services/migrations/package.json +++ b/services/migrations/package.json @@ -1,7 +1,7 @@ { "name": "@futureporn/migrations", "type": "module", - "version": "0.4.1", + "version": "0.5.0", "description": "", "main": "index.js", "scripts": {