Trying out

test-branch
Inex Code 2022-08-02 23:12:48 +03:00
parent 8f940e64fd
commit b965ffd96a
1 changed files with 35 additions and 12 deletions

View File

@ -8,6 +8,7 @@ from selfprivacy_api.graphql import IsAuthenticated
from selfprivacy_api.jobs import Job, Jobs from selfprivacy_api.jobs import Job, Jobs
@strawberry.type @strawberry.type
class ApiJob: class ApiJob:
name: str name: str
@ -21,27 +22,49 @@ class ApiJob:
error: typing.Optional[str] error: typing.Optional[str]
result: typing.Optional[str] result: typing.Optional[str]
@strawberry.type @strawberry.type
class JobSubscription: class JobSubscription:
@strawberry.subscription() @strawberry.subscription()
async def job_subscription(self) -> AsyncGenerator[typing.List[ApiJob], None]: async def job_subscription(self) -> AsyncGenerator[typing.List[ApiJob], None]:
is_updated = True is_updated = True
def callback(jobs: typing.List[Job]): def callback(jobs: typing.List[Job]):
nonlocal is_updated nonlocal is_updated
is_updated = True is_updated = True
print("Subscribing to job updates...")
Jobs().add_observer(callback) 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: while True:
if is_updated: if is_updated:
is_updated = False is_updated = False
yield [ ApiJob( yield [
name=job.name, ApiJob(
description=job.description, name=job.name,
status=job.status.name, description=job.description,
status_text=job.status_text, status=job.status.name,
progress=job.progress, status_text=job.status_text,
created_at=job.created_at, progress=job.progress,
updated_at=job.updated_at, created_at=job.created_at,
finished_at=job.finished_at, updated_at=job.updated_at,
error=job.error, finished_at=job.finished_at,
result=job.result, error=job.error,
) for job in Jobs().get_jobs() ] result=job.result,
)
for job in Jobs().get_jobs()
]