From b965ffd96ae30dfd866d3fe07bccc8a476a76232 Mon Sep 17 00:00:00 2001 From: Inex Code Date: Tue, 2 Aug 2022 23:12:48 +0300 Subject: [PATCH] Trying out --- selfprivacy_api/graphql/subscriptions/jobs.py | 47 ++++++++++++++----- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/selfprivacy_api/graphql/subscriptions/jobs.py b/selfprivacy_api/graphql/subscriptions/jobs.py index 82f5f29..597ef5b 100644 --- a/selfprivacy_api/graphql/subscriptions/jobs.py +++ b/selfprivacy_api/graphql/subscriptions/jobs.py @@ -8,6 +8,7 @@ from selfprivacy_api.graphql import IsAuthenticated from selfprivacy_api.jobs import Job, Jobs + @strawberry.type class ApiJob: name: str @@ -21,27 +22,49 @@ class ApiJob: error: typing.Optional[str] result: typing.Optional[str] + @strawberry.type class JobSubscription: @strawberry.subscription() async def job_subscription(self) -> AsyncGenerator[typing.List[ApiJob], None]: is_updated = True + def callback(jobs: typing.List[Job]): nonlocal is_updated is_updated = True + + print("Subscribing to job updates...") Jobs().add_observer(callback) + yield [ + ApiJob( + name=job.name, + description=job.description, + status=job.status.name, + status_text=job.status_text, + progress=job.progress, + created_at=job.created_at, + updated_at=job.updated_at, + finished_at=job.finished_at, + error=job.error, + result=job.result, + ) + for job in Jobs().get_jobs() + ] while True: if is_updated: is_updated = False - yield [ ApiJob( - name=job.name, - description=job.description, - status=job.status.name, - status_text=job.status_text, - progress=job.progress, - created_at=job.created_at, - updated_at=job.updated_at, - finished_at=job.finished_at, - error=job.error, - result=job.result, - ) for job in Jobs().get_jobs() ] + yield [ + ApiJob( + name=job.name, + description=job.description, + status=job.status.name, + status_text=job.status_text, + progress=job.progress, + created_at=job.created_at, + updated_at=job.updated_at, + finished_at=job.finished_at, + error=job.error, + result=job.result, + ) + for job in Jobs().get_jobs() + ]