diff options
-rw-r--r-- | app/src/routes/assignments/[assignmentId]/+page.server.ts | 1 | ||||
-rw-r--r-- | local/minio/images/70d92ac7-524d-44f9-9121-9c06d5428a78/09bcd567-9faf-4f13-b3bd-9d16b7b002d8/part.1 | bin | 0 -> 9563381 bytes | |||
-rw-r--r-- | local/minio/images/70d92ac7-524d-44f9-9121-9c06d5428a78/xl.meta | bin | 0 -> 393 bytes | |||
-rw-r--r-- | local/minio/images/f4778e62-0550-40c3-8f6e-22d111cea70e/6871c468-11d4-4f22-9ab1-74cd06845cc3/part.1 | bin | 0 -> 5321947 bytes | |||
-rw-r--r-- | local/minio/images/f4778e62-0550-40c3-8f6e-22d111cea70e/xl.meta | bin | 0 -> 360 bytes | |||
-rw-r--r-- | modd.conf | 4 | ||||
-rw-r--r-- | notification_worker/notifications.py | 53 | ||||
-rw-r--r-- | watermark_worker/watermark.py | 30 |
8 files changed, 83 insertions, 5 deletions
diff --git a/app/src/routes/assignments/[assignmentId]/+page.server.ts b/app/src/routes/assignments/[assignmentId]/+page.server.ts index b6634f0..790492c 100644 --- a/app/src/routes/assignments/[assignmentId]/+page.server.ts +++ b/app/src/routes/assignments/[assignmentId]/+page.server.ts @@ -43,7 +43,6 @@ export const actions = { ); const { beanstalkdClient, dbClient, s3Client } = locals; - await finishAssignment({ beanstalkdClient, dbClient, diff --git a/local/minio/images/70d92ac7-524d-44f9-9121-9c06d5428a78/09bcd567-9faf-4f13-b3bd-9d16b7b002d8/part.1 b/local/minio/images/70d92ac7-524d-44f9-9121-9c06d5428a78/09bcd567-9faf-4f13-b3bd-9d16b7b002d8/part.1 Binary files differnew file mode 100644 index 0000000..dfc12f5 --- /dev/null +++ b/local/minio/images/70d92ac7-524d-44f9-9121-9c06d5428a78/09bcd567-9faf-4f13-b3bd-9d16b7b002d8/part.1 diff --git a/local/minio/images/70d92ac7-524d-44f9-9121-9c06d5428a78/xl.meta b/local/minio/images/70d92ac7-524d-44f9-9121-9c06d5428a78/xl.meta Binary files differnew file mode 100644 index 0000000..02bc78b --- /dev/null +++ b/local/minio/images/70d92ac7-524d-44f9-9121-9c06d5428a78/xl.meta diff --git a/local/minio/images/f4778e62-0550-40c3-8f6e-22d111cea70e/6871c468-11d4-4f22-9ab1-74cd06845cc3/part.1 b/local/minio/images/f4778e62-0550-40c3-8f6e-22d111cea70e/6871c468-11d4-4f22-9ab1-74cd06845cc3/part.1 Binary files differnew file mode 100644 index 0000000..dd3931f --- /dev/null +++ b/local/minio/images/f4778e62-0550-40c3-8f6e-22d111cea70e/6871c468-11d4-4f22-9ab1-74cd06845cc3/part.1 diff --git a/local/minio/images/f4778e62-0550-40c3-8f6e-22d111cea70e/xl.meta b/local/minio/images/f4778e62-0550-40c3-8f6e-22d111cea70e/xl.meta Binary files differnew file mode 100644 index 0000000..35cf6e4 --- /dev/null +++ b/local/minio/images/f4778e62-0550-40c3-8f6e-22d111cea70e/xl.meta @@ -15,6 +15,10 @@ watermark_worker/*.py { daemon: python3 watermark_worker/watermark.py } +notification_worker/*.py { + daemon: python3 notification_worker/notifications.py +} + # Database server db/*.sh { prep: bash db/create_db.sh ./local/postgresql diff --git a/notification_worker/notifications.py b/notification_worker/notifications.py new file mode 100644 index 0000000..2a3bb71 --- /dev/null +++ b/notification_worker/notifications.py @@ -0,0 +1,53 @@ +import psycopg +import beanstalkc +import json + +def connect_to_beanstalkd(): + return beanstalkc.Connection("localhost", 11300, parse_yaml=False, encoding="utf-8") + +def connect_to_db(): + return psycopg.connect("dbname=testdb user=postgres") + +def handle_job(db_conn: psycopg.Connection, job: beanstalkc.Job): + body = json.loads(job.body) + + user_id = body["userId"] + message = body["message"] + url = body["url"] + + # TODO: Fetch push creds from DB by userId + + # TODO: Send event to push server + + # Super ugly hack for demo + import os + os.system(f""" + curl https://notifications.linus.onl/api/send-notification/40a466025b3aac7 \ + --request POST \ + --header "Content-Type: application/json" \ + --data '{ + "title": "{message}", + "url": "{url}" + }' + """) + + print(f"PRETEND SENDIGN MESSAGE {user_id=} {message=} {url=}") + print(f"PRETEND SENDIGN MESSAGE {user_id=} {message=} {url=}") + print(f"PRETEND SENDIGN MESSAGE {user_id=} {message=} {url=}") + print(f"PRETEND SENDIGN MESSAGE {user_id=} {message=} {url=}") + print(f"PRETEND SENDIGN MESSAGE {user_id=} {message=} {url=}") + +def main(): + beanstalkc_conn = connect_to_beanstalkd() + db_conn = connect_to_db() + + beanstalkc_conn.watch("notification") + + while True: + job = beanstalkc_conn.reserve() + if not job: continue + handle_job(db_conn, job) + job.delete() + +if __name__ == "__main__": + main() diff --git a/watermark_worker/watermark.py b/watermark_worker/watermark.py index f2b02f0..5d6388b 100644 --- a/watermark_worker/watermark.py +++ b/watermark_worker/watermark.py @@ -21,7 +21,7 @@ def connect_to_s3(): verify=False) # disable SSL def connect_to_beanstalkd(): - return beanstalkc.Connection("localhost", 11300) + return beanstalkc.Connection("localhost", 11300, encoding="utf-8") def watermark(image: PIL.Image.Image) -> PIL.Image.Image: w, h = image.size @@ -76,7 +76,27 @@ def process_image(db_conn: psycopg.Connection, s3_client, image_id: int, s3_key: """, [image_id]) db_conn.commit() -def process_assignment(assignment_id: int): +def notify_owner(beanstakd_conn: beanstalkc.Connection, db_conn: psycopg.Connection, assignment_id: int): + result = db_conn.execute(""" + SELECT + c.owner_id, + a.cemetary_plot_id + FROM assignments AS a + INNER JOIN cemetary_plots AS c ON c.id = a.cemetary_plot_id + WHERE a.id = %s; + """, [assignment_id]).fetchone() + assert result is not None + owner_id, cemetary_plot_id = result + + beanstakd_conn.use("notification") + body = json.dumps({ + "userId": owner_id, + "message": "Vi har besøgt dit gravsted", + "url": f"http://localhost:8000/cemetary_plots/{cemetary_plot_id}", + }) + beanstakd_conn.put(body) + +def process_assignment(beanstakd_conn: beanstalkc.Connection, assignment_id: int): # Retrieve all UNPROCESSED images from DB. # Some images may have already been processed by an earlier, partial job. db_conn = connect_to_db() @@ -101,12 +121,14 @@ def process_assignment(assignment_id: int): UPDATE assignments SET - state = 'AWAITING_OWNER_NOTIFICATION' :: assignment_state + state = 'AWAITING_DONE' :: assignment_state WHERE id = %s """, [assignment_id]) db_conn.commit() + notify_owner(beanstakd_conn, db_conn, assignment_id) + beanstalk = connect_to_beanstalkd() beanstalk.watch("watermarking") @@ -117,6 +139,6 @@ while True: print(f"!! Got job: {job.body}") job_body = json.loads(job.body) - process_assignment(job_body["assignmentId"]) + process_assignment(beanstalk, job_body["assignmentId"]) job.delete() |