-
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.
Simplify tmdb-refresher into a single worker
- Loading branch information
Showing
18 changed files
with
219 additions
and
293 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
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
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
functions/tmdb-refresher-scheduler/configuration/rollup.config.js
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
47 changes: 0 additions & 47 deletions
47
functions/tmdb-refresher-scheduler/tmdb-refresher-scheduler.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,105 @@ | ||
import {updateSeries} from '~/global/tmdb.ts'; | ||
import {createPrisma, type Environment} from './shared.ts'; | ||
|
||
export async function handleFetch(request: Request, env: Environment) { | ||
if (request.method !== 'POST') { | ||
// response with method not allowed status | ||
return new Response( | ||
JSON.stringify({ | ||
error: {message: 'You can only use HTTP POST for this request.'}, | ||
}), | ||
{ | ||
status: 405, | ||
headers: { | ||
Allow: 'POST', | ||
'Content-Type': 'application/json', | ||
}, | ||
}, | ||
); | ||
} | ||
|
||
const token = request.headers.get('TMDB-Access-Token'); | ||
|
||
if (token !== env.TMDB_ACCESS_TOKEN) { | ||
return new Response( | ||
JSON.stringify({ | ||
error: { | ||
message: | ||
'You must provide the TMDB token in the TMDB-Access-Token header of the request.', | ||
}, | ||
}), | ||
{ | ||
status: 401, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}, | ||
); | ||
} | ||
|
||
try { | ||
const options = (await request.json()) as { | ||
id?: string | number; | ||
handle?: string; | ||
}; | ||
const idOption = options.id ? String(options.id) : undefined; | ||
const handleOption = options.handle; | ||
|
||
if (typeof idOption !== 'string' && typeof handleOption !== 'string') { | ||
return new Response( | ||
JSON.stringify({ | ||
error: { | ||
message: | ||
'You must provide either an `id` or `handle` option in your JSON request body.', | ||
}, | ||
}), | ||
{ | ||
status: 400, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}, | ||
); | ||
} | ||
|
||
const prisma = await createPrisma(env.DATABASE_URL); | ||
const {id, name, tmdbId} = await prisma.series.findUniqueOrThrow({ | ||
where: idOption ? {id: idOption} : {handle: handleOption}, | ||
select: {id: true, name: true, tmdbId: true}, | ||
}); | ||
|
||
const {results} = await updateSeries({ | ||
id, | ||
name, | ||
tmdbId, | ||
prisma, | ||
accessToken: env.TMDB_ACCESS_TOKEN, | ||
}); | ||
|
||
const result = results.join('\n\n'); | ||
console.log(result); | ||
|
||
return new Response(JSON.stringify({}), { | ||
status: 200, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
} catch (error) { | ||
console.error(error); | ||
|
||
return new Response( | ||
JSON.stringify({ | ||
error: { | ||
message: 'There was an error while updating series.', | ||
}, | ||
}), | ||
{ | ||
status: 500, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}, | ||
); | ||
} | ||
} |
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,43 @@ | ||
import {updateSeries} from '~/global/tmdb.ts'; | ||
import {createPrisma, type Environment, type Message} from './shared.ts'; | ||
import type {MessageBatch} from '@cloudflare/workers-types'; | ||
|
||
export async function handleQueue( | ||
batch: MessageBatch<Message>, | ||
env: Environment, | ||
) { | ||
const prisma = await createPrisma(env.DATABASE_URL); | ||
|
||
try { | ||
await Promise.all( | ||
batch.messages.map(async ({body: {id, name, tmdbId}}) => { | ||
const {results} = await updateSeries({ | ||
id, | ||
name, | ||
tmdbId, | ||
prisma, | ||
accessToken: env.TMDB_ACCESS_TOKEN, | ||
}); | ||
|
||
const result = results.join('\n\n'); | ||
console.log(result); | ||
|
||
const fetchResult = await fetch( | ||
'https://discordapp.com/api/webhooks/656640833063223325/1ofugrkDFpqaSAWvD6mLlg5EN3UDOfBdib4WKNE17Q5YxUoz8wpwuLoKCeaZJqCHyfeC', | ||
{ | ||
method: 'POST', | ||
body: JSON.stringify({ | ||
content: result, | ||
}), | ||
headers: {'Content-Type': 'application/json'}, | ||
}, | ||
); | ||
|
||
console.log(fetchResult); | ||
}), | ||
); | ||
} catch (error) { | ||
console.log((error as any)?.stack); | ||
throw error; | ||
} | ||
} |
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,29 @@ | ||
import type {ScheduledEvent} from '@cloudflare/workers-types'; | ||
import {createPrisma, type Environment} from './shared'; | ||
|
||
export async function handleScheduled(event: ScheduledEvent, env: Environment) { | ||
console.log(JSON.stringify(event, null, 2)); | ||
|
||
try { | ||
const prisma = await createPrisma(env.DATABASE_URL); | ||
|
||
const series = await prisma.series.findMany({ | ||
where: {status: 'RETURNING'}, | ||
}); | ||
|
||
console.log(series); | ||
|
||
await Promise.all( | ||
series.map(async (series) => { | ||
await env.TMDB_REFRESHER_QUEUE.send({ | ||
id: series.id, | ||
name: series.name, | ||
tmdbId: series.tmdbId, | ||
}); | ||
}), | ||
); | ||
} catch (error) { | ||
console.error(error); | ||
throw error; | ||
} | ||
} |
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 @@ | ||
import type {Queue} from '@cloudflare/workers-types'; | ||
import {createEdgeDatabaseConnection} from '~/global/database.ts'; | ||
|
||
export interface Environment { | ||
DATABASE_URL: string; | ||
TMDB_ACCESS_TOKEN: string; | ||
TMDB_REFRESHER_QUEUE: Queue<Message>; | ||
} | ||
|
||
export interface Message { | ||
id: string; | ||
name: string; | ||
tmdbId: string; | ||
} | ||
|
||
let prismaPromise: Promise<import('@prisma/client').PrismaClient> | undefined; | ||
|
||
export async function createPrisma(url: string) { | ||
prismaPromise ??= createEdgeDatabaseConnection({url}); | ||
return await prismaPromise; | ||
} |
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
2 changes: 1 addition & 1 deletion
2
...-refresher/configuration/rollup.config.js → functions/tmdb-refresher/rollup.config.js
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
Oops, something went wrong.