submission_processor.py 7.11 KB
Newer Older
1
import json
Peter Stanko's avatar
Peter Stanko committed
2
import secrets
Peter Stanko's avatar
Peter Stanko committed
3
from pathlib import Path
4
from typing import Optional
Peter Stanko's avatar
Peter Stanko committed
5

6
from portal import logger
Peter Stanko's avatar
Peter Stanko committed
7
from portal.database import Project, Submission, SubmissionState, Worker
8
from portal.database.enums import WorkerState
9
from portal.service import errors
10
from portal.storage import UploadedEntity, entities
Peter Stanko's avatar
Peter Stanko committed
11

Peter Stanko's avatar
Peter Stanko committed
12
log = logger.get_logger(__name__)
Peter Stanko's avatar
Peter Stanko committed
13
14
15


class SubmissionProcessor:
16
    def __init__(self, submission: Submission):
Peter Stanko's avatar
Peter Stanko committed
17
        self._submission = submission
Peter Stanko's avatar
Peter Stanko committed
18
        from portal.service.services_collection import ServicesCollection
19
        self._services = ServicesCollection()
Peter Stanko's avatar
Peter Stanko committed
20
21
22
23
24
25
26

    @property
    def submission(self) -> Submission:
        return self._submission

    @property
    def params(self) -> dict:
Peter Stanko's avatar
Peter Stanko committed
27
        return self.submission.parameters
Peter Stanko's avatar
Peter Stanko committed
28
29
30
31
32
33
34
35
36
37
38
39

    @property
    def project(self) -> Project:
        return self.submission.project

    @property
    def celery(self):
        from portal.async_celery import celery_app
        return celery_app

    @property
    def storage(self):
40
41
        from portal import storage_wrapper
        return storage_wrapper
Peter Stanko's avatar
Peter Stanko committed
42
43
44
45
46
47

    def reset_task_id(self, state=None):
        if state is not None:
            self.submission.state = state

        self.submission.async_task_id = None
48
        self._save_submission()
Peter Stanko's avatar
Peter Stanko committed
49
50

    def submission_enqueue_ended(self):
51
        log.info(f"[ASYNC] Submission enqueue ended {self.submission.log_name}: {self.submission}")
Peter Stanko's avatar
Peter Stanko committed
52
53
54
        self.reset_task_id(state=SubmissionState.QUEUED)

    def get_delay_for_submission(self):
Peter Stanko's avatar
Peter Stanko committed
55
        log.info(f"[ASYNC] Submission delay {self.submission.log_name}: {self.submission}")
Peter Stanko's avatar
Peter Stanko committed
56
57
58
59
60
        time_wait = self.project.config.submissions_cancellation_period
        return time_wait

    def dispatch_submission_processing(self):
        delay = self.get_delay_for_submission()
61
        args = (self.submission.id,)
Peter Stanko's avatar
Peter Stanko committed
62
        from .tasks import start_processing_submission
63
        self.submission.scheduled_for = delay
64
        self._save_submission()
Peter Stanko's avatar
Peter Stanko committed
65
66
67
        start_processing_submission.apply_async(args=args, countdown=delay)

    def submission_store_ended(self, version: str):
68
69
        log.info(f"[ASYNC] Submission preparation ended {self.submission.log_name}: "
                 f"{self.submission}")
Peter Stanko's avatar
Peter Stanko committed
70
71
72
73
        self.submission.source_hash = version
        self.reset_task_id(state=SubmissionState.READY)

    def download_submission(self):
Peter Stanko's avatar
Peter Stanko committed
74
        file_params = self.params['file_params']
75
        log.info(f"[ASYNC] Uploading submission: {self.submission.log_name} with {file_params}")
76

Peter Stanko's avatar
Peter Stanko committed
77
        updated_entity: UploadedEntity = self.storage. \
78
            submissions.create(dirname=self.submission.id, **file_params)
Peter Stanko's avatar
Peter Stanko committed
79
80
81
        self.submission_store_ended(version=updated_entity.version)

    def clone(self, target):
82
        log.info(f"[ASYNC] Cloning submission: {self.submission.log_name} to {target.log_name}")
Peter Stanko's avatar
Peter Stanko committed
83
84
85
86
87
        self.storage.submissions.clone(self.submission.id, target.id)
        self.submission_store_ended(version=self.submission.source_hash)

    def send_to_worker(self):
        # TODO: implement processing
88
        log.info(f"[ASYNC] Sending submission to worker: {self.submission.log_name}")
Peter Stanko's avatar
Peter Stanko committed
89
        worker = self.schedule_submission_to_worker()
90
91
        if worker:
            self.execute_submission(worker)
92
93
94
95
        else:
            log.warning(f"[EXEC] Worker not available to process submission: "
                        f"{self.submission.log_name}")
        self.abort_submission("No Worker available")
Peter Stanko's avatar
Peter Stanko committed
96

Peter Stanko's avatar
Peter Stanko committed
97
98
    def upload_result(self, path, file_params):
        log.info(f"[ASYNC] Uploading result for the submission "
99
                 f"{self.submission.log_name} with {file_params}")
100
        self.storage.results.create(dirname=self.submission.id, **file_params)
Peter Stanko's avatar
Peter Stanko committed
101
102
103
104
        Path(path).unlink()
        self.reset_task_id(SubmissionState.FINISHED)

    def process_submission(self):
105
        log.info(f"[ASYNC] Processing submission {self.submission.log_name}")
106
107
108
109
110
        try:
            self.download_submission()
        except Exception as ex:
            log.error(f"[PROC] Storage submission download aborted due error: {ex}")
            self.abort_submission('Unable to download a submission')
111
            return None
Peter Stanko's avatar
Peter Stanko committed
112
        self.dispatch_submission_processing()
113
114

    def revoke_task(self):
115
        log.info(f'[ASYNC] Submission processing cancelled {self.submission.log_name}')
116
117
118
119
120
        task_id = self.submission.async_task_id
        if task_id:
            self.celery.control.revoke(task_id=task_id, terminate=True)
            self.reset_task_id(state=SubmissionState.CANCELLED)
        # TODO: Storage clean up
Peter Stanko's avatar
Peter Stanko committed
121

Peter Stanko's avatar
Peter Stanko committed
122
123
    def _get_avail_workers(self):
        course = self.submission.course
124
        workers = self._services.workers.find_all()
Peter Stanko's avatar
Peter Stanko committed
125
126
127
        return [worker for worker in workers
                if worker.state == WorkerState.READY and course in worker.courses]

Peter Stanko's avatar
Peter Stanko committed
128
129
    # TODO implement - @mdujava
    # STUB: Select initialized worker
130
    def schedule_submission_to_worker(self) -> Optional[Worker]:
Peter Stanko's avatar
Peter Stanko committed
131
132
133
134
135
        """Based on the features (worker tags) and preferences in project config
        schedule submission for the execution on initialized worker

        Returns(Worker): Worker instance on which the submission will be executed
        """
Peter Stanko's avatar
Peter Stanko committed
136
        workers = self._get_avail_workers()
Peter Stanko's avatar
Peter Stanko committed
137
138
        if not workers:
            self._worker_not_available()
139
            return None
Peter Stanko's avatar
Peter Stanko committed
140
        worker = secrets.choice(workers)  # randomly select a worker
Peter Stanko's avatar
Peter Stanko committed
141
        log.debug(f"[SCHED] Scheduling submission to worker -> {worker.log_name}")
Peter Stanko's avatar
Peter Stanko committed
142
143
144
        return worker

    def execute_submission(self, worker: Worker):
145
        worker_client = self._services.workers(worker).worker_client
146
147
        self.submission.change_state(SubmissionState.IN_PROGRESS)
        self._save_submission()
Peter Stanko's avatar
Peter Stanko committed
148
149
150
        worker_client.execute_submission(self.submission)

    def _worker_not_available(self):
151
        log.warning(f"[PROC] Worker is no available for submission: {self.submission.log_name}")
152
153

    def process_result(self):
154
        storage_entity = self.storage.results.get(self.submission.storage_dirname)
155
        # @mdujava - here put submission processing
Peter Stanko's avatar
Peter Stanko committed
156
        return self._submission_result_processing(storage_entity)
157
158
159
160
161

    def _submission_result_processing(self, storage_entity: entities.Entity):
        suite_stats = storage_entity.get('suite-stats.json')
        if not suite_stats.exists():
            log.error(f"[PROC] Suite stats for the {self.submission.log_name} have not been found.")
162
            raise errors.SuiteStatsNotExists(self.submission.storage_dirname)
163
164
165
166
        stats = json.loads(suite_stats.read_text('utf-8'))
        return self._parse_stats(stats)

    def _parse_stats(self, stats: dict):
Peter Stanko's avatar
Peter Stanko committed
167
        log.debug(f"[PROC] Processing the stats: {stats}")
168
169
        points = stats.get('final_points', 0)
        result = stats.get('result', 'none').lower()
Peter Stanko's avatar
Peter Stanko committed
170
        log.debug(f"[PROC] Processing stats: points={points}, result={result}")
171
172
173
        self.submission.result = result
        self.submission.points = points
        return self.submission
174
175
176

    def abort_submission(self, message: str = 'Unknown error!'):
        self.submission.note['error'] = message
177
        self._save_submission()
178
        self.reset_task_id(SubmissionState.ABORTED)
179
180

    def _save_submission(self):
181
        self._services.submissions.write_entity(self.submission)