-
Notifications
You must be signed in to change notification settings - Fork 3
/
task.py
184 lines (162 loc) · 5.04 KB
/
task.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from datetime import datetime
from enum import Enum
from multiprocessing.synchronize import Event
from typing import Optional, Dict, ClassVar
from beanie import Document
from beanie.odm.enums import SortDirection
from beanie.odm.queries.update import UpdateResponse
from beanie.odm.utils.pydantic import get_model_fields, get_extra_field_info
from pydantic import Field
from pymongo import DESCENDING, ASCENDING
from beanie_batteries_queue.queue import Queue
class State(str, Enum):
CREATED = "CREATED"
RUNNING = "RUNNING"
FINISHED = "FINISHED"
FAILED = "FAILED"
class Priority(Enum):
LOW = 1
MEDIUM = 2
HIGH = 3
class DependencyType(str, Enum):
ALL_OF = "ALL_OF"
ANY_OF = "ANY_OF"
DIRECT = "DIRECT"
class Task(Document):
state: State = State.CREATED
priority: Priority = Priority.MEDIUM
created_at: datetime = Field(default_factory=datetime.utcnow)
_dependency_fields: ClassVar[Optional[Dict[str, DependencyType]]] = None
class Settings:
indexes = [
[
("state", ASCENDING),
("priority", DESCENDING),
("created_at", ASCENDING),
],
# expire after 1 day
[("created_at", ASCENDING), ("expireAfterSeconds", 86400)],
]
@classmethod
async def custom_init(cls):
for name, field in get_model_fields(cls).items():
if get_extra_field_info(field, "dependency_type"):
if cls._dependency_fields is None:
cls._dependency_fields = {}
cls._dependency_fields[name] = get_extra_field_info(
field, "dependency_type"
)
async def push(self):
await self.save()
@classmethod
async def pop(cls) -> Optional["Task"]:
"""
Get the first task from the queue
:return:
"""
task = None
find_query = cls.make_find_query()
found_task = (
await cls.find(find_query, fetch_links=True)
.sort(
[
("priority", SortDirection.DESCENDING),
("created_at", SortDirection.ASCENDING),
]
)
.first_or_none()
)
if found_task is not None:
task = await cls.find_one(
{"_id": found_task.id, "state": State.CREATED}
).update(
{"$set": {"state": State.RUNNING}},
response_type=UpdateResponse.NEW_DOCUMENT,
)
# check if this task was not taken by another worker
if task is None:
task = await cls.pop()
return task
@classmethod
def make_find_query(cls):
queries = [{"state": State.CREATED}]
if cls._dependency_fields is not None:
for (
dependency_field,
dependency_type,
) in cls._dependency_fields.items():
queries.append(
cls.make_dependency_query(
dependency_field, dependency_type
)
)
return {"$and": queries}
@staticmethod
def make_dependency_query(
dependency_field: str, dependency_type: DependencyType
):
if dependency_type == DependencyType.ALL_OF:
# TODO this looks tricky
return {
dependency_field: {
"$not": {"$elemMatch": {"state": {"$ne": State.FINISHED}}}
}
}
elif dependency_type == DependencyType.ANY_OF:
return {
"$or": [
{dependency_field: {"$size": 0}},
{
dependency_field: {
"$elemMatch": {"state": State.FINISHED}
}
},
]
}
elif dependency_type == DependencyType.DIRECT:
return {
"$or": [
{dependency_field: None},
{f"{dependency_field}.state": {"$eq": State.FINISHED}},
]
}
@classmethod
async def is_empty(cls) -> bool:
"""
Check if there are no tasks in the queue
:return:
"""
return await cls.find_one({"state": State.CREATED}) is None
@classmethod
def queue(
cls,
sleep_time: int = 1,
stop_event: Optional[Event] = None,
):
"""
Get queue iterator
:param sleep_time:
:param stop_event:
:return:
"""
return Queue(cls, sleep_time=sleep_time, stop_event=stop_event)
async def finish(self):
"""
Mark task as finished
:return:
"""
self.state = State.FINISHED
await self.save()
async def fail(self):
"""
Mark task as failed
:return:
"""
self.state = State.FAILED
await self.save()
async def run(self):
"""
Run task
:return:
"""
raise NotImplementedError()