-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_process.py
47 lines (39 loc) · 1.52 KB
/
search_process.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import typing
import uuid
import logging
from wrapper.core.progress import Progress
class SearchProcess(object):
def __init__(self, search_id: uuid, priority: int):
self.search_id: str = search_id
self.priority: int = priority
self.completed: bool = False
self.progress: Progress = Progress()
self.should_stop = False
self.mongo_id = None
def __lt__(self, other):
return self.priority > other.priority
def to_json_structure(self) -> typing.Dict:
json_structure = {
'search_id': self.search_id,
'priority': self.priority,
'completed': self.completed,
'progress': self.progress.to_json_structure(),
'should_stop': self.should_stop
}
if self.mongo_id is not None:
json_structure['_id'] = self.mongo_id
return json_structure
def complete(self):
self.completed = True
self.progress.complete()
def start(self):
self.progress.start_running()
@staticmethod
def from_json_structure(json_structure):
search_process = SearchProcess(json_structure['search_id'], json_structure['priority'])
search_process.completed = json_structure['completed']
search_process.progress = Progress.from_json_structure(json_structure['progress'])
search_process.should_stop = json_structure['should_stop']
if '_id' in json_structure:
search_process.mongo_id = json_structure['_id']
return search_process