2023-05-12 02:32:50 +08:00
|
|
|
import logging
|
|
|
|
from typing import List
|
|
|
|
|
2024-07-24 04:30:01 +08:00
|
|
|
from felicity.apps.analysis.entities.analysis import Sample
|
2024-07-28 03:52:31 +08:00
|
|
|
from felicity.apps.analysis.enum import SampleState
|
2024-01-27 01:08:02 +08:00
|
|
|
from felicity.apps.impress.sample import utils
|
2024-07-24 04:30:01 +08:00
|
|
|
from felicity.apps.job import entities as job_entities
|
2024-01-27 01:08:02 +08:00
|
|
|
from felicity.apps.job import schemas as job_schemas
|
2024-07-28 03:52:31 +08:00
|
|
|
from felicity.apps.job.enum import JobAction, JobCategory, JobState
|
2024-07-24 04:30:01 +08:00
|
|
|
from felicity.apps.user import entities as user_entities
|
|
|
|
from felicity.apps.user.entities import User
|
2024-01-27 01:08:02 +08:00
|
|
|
from felicity.core.config import get_settings
|
2023-05-12 02:32:50 +08:00
|
|
|
|
2024-01-26 01:52:03 +08:00
|
|
|
settings = get_settings()
|
2023-05-12 02:32:50 +08:00
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
async def impress_results(job_uid: str):
|
|
|
|
logger.info(f"starting impress job {job_uid} ....")
|
2024-07-24 04:30:01 +08:00
|
|
|
job = await job_entities.Job.get(uid=job_uid)
|
2023-05-12 02:32:50 +08:00
|
|
|
if not job:
|
|
|
|
return
|
|
|
|
|
2024-07-24 04:30:01 +08:00
|
|
|
if job.status != JobState.PENDING:
|
2023-05-12 02:32:50 +08:00
|
|
|
return
|
|
|
|
|
2024-07-24 04:30:01 +08:00
|
|
|
await job.change_status(new_status=JobState.RUNNING)
|
2023-05-12 02:32:50 +08:00
|
|
|
|
2024-07-24 04:30:01 +08:00
|
|
|
user = await user_entities.User.get(uid=job.creator_uid)
|
2023-05-12 02:32:50 +08:00
|
|
|
|
|
|
|
try:
|
|
|
|
await utils.impress_samples(job.data, user)
|
2024-07-24 04:30:01 +08:00
|
|
|
await job.change_status(new_status=JobState.FINISHED)
|
2023-09-11 13:02:05 +08:00
|
|
|
await report_notifier.notify("Your results were successfully published", user)
|
2023-05-12 02:32:50 +08:00
|
|
|
except Exception as e:
|
2024-07-24 04:30:01 +08:00
|
|
|
await job.change_status(new_status=JobState.FAILED)
|
2024-02-16 23:48:19 +08:00
|
|
|
logger.info(f"Failed impress job {job_uid} with errr: {str(e)}")
|
2023-05-12 02:32:50 +08:00
|
|
|
await report_notifier.notify(
|
|
|
|
f"Failed to publish results in job with uid: {job.uid} with error: {str(e)}",
|
|
|
|
user,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def prepare_for_impress():
|
2024-07-24 04:30:01 +08:00
|
|
|
samples: List[Sample] = await Sample.get_all(status__in=[SampleState.APPROVED])
|
2023-05-12 02:32:50 +08:00
|
|
|
sample_uids = [sample.uid for sample in samples]
|
|
|
|
|
|
|
|
await Sample.bulk_update_with_mappings(
|
2024-07-24 04:30:01 +08:00
|
|
|
[{"uid": uid, "status": SampleState.PUBLISHING} for uid in sample_uids]
|
2023-05-12 02:32:50 +08:00
|
|
|
)
|
|
|
|
|
2024-01-26 01:52:03 +08:00
|
|
|
system_daemon: User = await User.get(email=settings.SYSTEM_DAEMON_EMAIL)
|
2023-05-12 02:32:50 +08:00
|
|
|
|
|
|
|
job_schema = job_schemas.JobCreate(
|
2024-07-24 04:30:01 +08:00
|
|
|
action=JobAction.IMPRESS_REPORT,
|
|
|
|
category=JobCategory.IMPRESS,
|
|
|
|
priority=JobCategory.NORMAL,
|
2023-05-12 02:32:50 +08:00
|
|
|
job_id="0",
|
2024-07-24 04:30:01 +08:00
|
|
|
status=JobState.PENDING,
|
2023-05-12 02:32:50 +08:00
|
|
|
creator_uid=system_daemon.uid,
|
|
|
|
data=sample_uids,
|
|
|
|
)
|
|
|
|
|
2024-07-24 04:30:01 +08:00
|
|
|
await job_entities.Job.create(job_schema)
|