summaryrefslogtreecommitdiff
path: root/app/src/lib/server
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/lib/server')
-rw-r--r--app/src/lib/server/assignments.ts27
-rw-r--r--app/src/lib/server/beanstalkd.ts19
2 files changed, 38 insertions, 8 deletions
diff --git a/app/src/lib/server/assignments.ts b/app/src/lib/server/assignments.ts
index c66f25c..9c28cff 100644
--- a/app/src/lib/server/assignments.ts
+++ b/app/src/lib/server/assignments.ts
@@ -1,6 +1,7 @@
-import { PutObjectCommand, type S3Client } from "@aws-sdk/client-s3"; /*=;* /
+import { PutObjectCommand, type S3Client } from "@aws-sdk/client-s3"; /*=;*/
import type { ClientBase } from "pg";
-import type { CemetaryPlot, Assignment, AssignmentState } from "../common/assignments";
+import type { CemetaryPlot, Assignment } from "../common/assignments";
+import type BeanstalkdClient from "beanstalkd";
/**
* Retrieves all assignments for the given user.
@@ -73,17 +74,24 @@ export async function getAssignmentAndCemetaryById(
}
export interface FinishAssignmentArgs {
+ dbClient: ClientBase;
+ s3Client: S3Client;
+ beanstalkdClient: BeanstalkdClient;
+
images: { bytes: Uint8Array; name: string }[];
note?: string;
assignmentId: number;
}
// TODO: Error recovery.
-export async function finishAssignment(
- dbClient: ClientBase,
- s3Client: S3Client,
- { images, note, assignmentId }: FinishAssignmentArgs,
-): Promise<void> {
+export async function finishAssignment({
+ dbClient,
+ s3Client,
+ beanstalkdClient,
+ images,
+ note,
+ assignmentId,
+}: FinishAssignmentArgs): Promise<void> {
// Upload to S3, returning path
// FIXME: Should be factored out?
const uploadPromises = images.map(async (image) => {
@@ -101,7 +109,10 @@ export async function finishAssignment(
});
const uploadedImages = await Promise.all(uploadPromises);
- // TODO: Add beanstalkd job
+ // Instruct background worker to do watermarking.
+ // FIXME: magic constants yay how fun
+ await beanstalkdClient.use("watermarking");
+ console.debug(await beanstalkdClient.put(0, 0, 60, JSON.stringify({ assignmentId })));
await dbClient.query("BEGIN");
diff --git a/app/src/lib/server/beanstalkd.ts b/app/src/lib/server/beanstalkd.ts
new file mode 100644
index 0000000..10f392a
--- /dev/null
+++ b/app/src/lib/server/beanstalkd.ts
@@ -0,0 +1,19 @@
+import pkg from "beanstalkd";
+import type { Handle } from "@sveltejs/kit";
+
+// Annoying CommonJS interop issue (vitejs/vite#2139) which combines with incorrect typings from DefinitelyTyped project :(
+// @ts-ignore
+const BeanstalkdClient = pkg.default as typeof pkg;
+
+export const beanstalkdHandle = (async ({ event, resolve }) => {
+ // FIXME: Should obv. read from env.
+ const beanstalkdClient = new BeanstalkdClient("localhost", 11300);
+ await beanstalkdClient.connect();
+
+ event.locals.beanstalkdClient = beanstalkdClient;
+ try {
+ return await resolve(event);
+ } finally {
+ beanstalkdClient.quit();
+ }
+}) satisfies Handle;