Skip to content

Commit

Permalink
Use the bun workers api for getting shiggies
Browse files Browse the repository at this point in the history
  • Loading branch information
lillithkt committed Sep 15, 2023
1 parent 4d2075e commit 50ab5a6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/events.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import EventEmitter from "./utils/EventEmitter";

interface Events {
export interface Events {
UPDATE_SHIGGIES: [];
}

Expand Down
18 changes: 10 additions & 8 deletions src/pages/api/v0/healthcheck.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { APIRoute } from "astro";
import getShiggies from "../../../shiggyGetter";
import emitter, { Events } from "../../../events";

let gotShiggies = false;

export const GET: APIRoute = () => {
if (!gotShiggies) {
gotShiggies = true;
setTimeout(() => {
getShiggies(
import.meta.env.MAX_SHIGGIES
? Number.parseInt(import.meta.env.MAX_SHIGGIES)
: 100,
);
}, 1000);

const url = new URL("../../../../scripts/getshiggies", import.meta.url);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const worker = new Worker(url);

worker.onmessage = (event: MessageEvent<keyof Events>) => {
emitter.emit(event.data);
};

return new Response(null, { status: 200 });
}
return new Response(null, { status: 200 });
Expand Down
7 changes: 2 additions & 5 deletions src/shiggyGetter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import booru, { Post } from "booru";

import { PUBLIC_DIR, SHIGGY_DIR, ZIP_NAME } from "./constants";
import { rm, mkdir } from "fs/promises";
import emitter from "./events";

const deniedTags = new Set(["nsfw"]); // idk, work on this

Expand Down Expand Up @@ -125,10 +124,8 @@ export default async function getShiggies(limit = 50): Promise<void> {
]),
);

Bun.write(sizesFile, JSON.stringify(sizes));
Bun.write(shiggyFile, JSON.stringify(Object.keys(posts)));

emitter.emit("UPDATE_SHIGGIES");
await Bun.write(sizesFile, JSON.stringify(sizes));
await Bun.write(shiggyFile, JSON.stringify(Object.keys(posts)));

page = await page.nextPage();
}
Expand Down

0 comments on commit 50ab5a6

Please sign in to comment.