-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfilesystem.py
293 lines (251 loc) · 10.2 KB
/
filesystem.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
"""(Local) filesystem based Celery application."""
import logging
import os
from datetime import datetime
from typing import Any, Dict, Iterable, Iterator, List, Optional, Set, cast
from celery import Celery
from kombu.message import Message
from kombu.utils.encoding import bytes_to_str
from kombu.utils.json import loads
from ..contrib.kombu_filesystem import (
LOCK_SH,
backport_filesystem_transport,
lock,
unlock,
)
from ..utils import makedirs, remove, unc_path
logger = logging.getLogger(__name__)
backport_filesystem_transport()
def _get_fs_config(
wdir: str,
mkdir: bool = False,
task_serializer: str = "json",
result_serializer: str = "json",
) -> Dict[str, Any]:
broker_path = os.path.join(wdir, "broker")
broker_control_path = unc_path(os.path.join(broker_path, "control"))
broker_in_path = unc_path(os.path.join(broker_path, "in"))
broker_processed_path = unc_path(os.path.join(broker_path, "processed"))
result_path = os.path.join(wdir, "result")
if mkdir:
for path in (
broker_control_path,
broker_in_path,
broker_processed_path,
result_path,
):
makedirs(path, exist_ok=True)
return {
"broker_url": "filesystem://",
"broker_transport_options": {
"control_folder": broker_control_path,
"data_folder_in": broker_in_path,
"data_folder_out": broker_in_path,
"processed_folder": broker_processed_path,
"store_processed": True,
},
"result_backend": f"file://{unc_path(result_path)}",
"result_persistent": True,
"task_serializer": task_serializer,
"result_serializer": result_serializer,
"accept_content": [task_serializer],
}
class FSApp(Celery):
"""Local filesystem-based Celery application.
Uses Kombu filesystem:// broker and results backend
"""
def __init__(
self,
*args,
wdir: Optional[str] = None,
mkdir: bool = False,
task_serializer: str = "json",
result_serializer: str = "json",
**kwargs: Any,
):
"""Construct an FSApp.
Arguments:
wdir: App broker/results directory. Defaults to current working
directory.
mkdir: Create broker/results subdirectories if they do not already
exist.
task_serializer: Default task serializer.
result_serializer: Default result serializer.
Additional arguments will be passed into the Celery constructor.
"""
super().__init__(*args, **kwargs)
self.wdir = wdir or os.getcwd()
self.conf.update(
_get_fs_config(
self.wdir,
mkdir=mkdir,
task_serializer=task_serializer,
result_serializer=result_serializer,
)
)
logger.debug("Initialized filesystem:// app in '%s'", wdir)
self._processed_msg_path_cache: Dict[str, str] = {}
self._queued_msg_path_cache: Dict[str, str] = {}
def __reduce_keys__(self) -> Dict[str, Any]:
keys = super().__reduce_keys__() # type: ignore[misc]
keys.update({"wdir": self.wdir})
return keys
def _iter_folder(
self,
folder_name: str,
path_cache: Dict[str, str],
queue: Optional[str] = None,
) -> Iterator[Message]:
"""Iterate over queued tasks inside a folder
Arguments:
folder_name: the folder to iterate
path_cache: cache of message path.
queue: Optional name of queue.
"""
with self.connection_for_read() as conn: # type: ignore[attr-defined]
with conn.channel() as channel:
folder = getattr(channel, folder_name)
for filename in sorted(os.listdir(folder)):
path = os.path.join(folder, filename)
try:
with open(path, "rb") as fobj:
lock(fobj, LOCK_SH)
try:
payload = fobj.read()
finally:
unlock(fobj)
except FileNotFoundError:
# Messages returned by `listdir` call may have been
# acknowledged and moved to `processed_folder` by the
# time we try to read them here
continue
if not payload:
continue
msg = channel.Message(loads(bytes_to_str(payload)), channel=channel)
path_cache[msg.delivery_tag] = path
if queue is None:
yield msg
else:
delivery_info = msg.properties.get("delivery_info", {})
if delivery_info.get("routing_key") == queue:
yield msg
def _iter_data_folder(self, queue: Optional[str] = None) -> Iterator[Message]:
yield from self._iter_folder(
"data_folder_in", self._queued_msg_path_cache, queue=queue
)
def _iter_processed_folder(self, queue: Optional[str] = None) -> Iterator[Message]:
yield from self._iter_folder(
"processed_folder", self._processed_msg_path_cache, queue=queue
)
def iter_queued(self, queue: Optional[str] = None) -> Iterator[Message]:
"""Iterate over queued tasks which have not been taken by a worker.
Arguments:
queue: Optional name of queue.
"""
queue = queue or self.conf.task_default_queue
yield from self._iter_data_folder(queue=queue)
def iter_processed(self, queue: Optional[str] = None) -> Iterator[Message]:
"""Iterate over tasks which have been taken by a worker.
Arguments:
queue: Optional name of queue.
"""
queue = queue or self.conf.task_default_queue
yield from self._iter_processed_folder(queue=queue)
@staticmethod
def _delete_msg(
delivery_tag: str,
msg_collection: Iterable[Message],
path_cache: Dict[str, str],
):
"""delete the specified message.
Arguments:
delivery_tag: delivery tag of the message to be deleted.
msg_collection: where to found this message.
path_cache: cache of message path.
Raises:
ValueError: Invalid delivery_tag
"""
path = path_cache.get(delivery_tag)
if path and os.path.exists(path):
remove(path)
del path_cache[delivery_tag]
return
for msg in msg_collection:
if msg.delivery_tag == delivery_tag:
remove(path_cache[delivery_tag])
del path_cache[delivery_tag]
return
raise ValueError(f"Message '{delivery_tag}' not found")
def reject(self, delivery_tag: str):
"""Reject the specified message.
Allows the caller to reject FS broker messages without establishing a
full Kombu consumer. Requeue is not supported.
Raises:
ValueError: Invalid delivery_tag
"""
self._delete_msg(delivery_tag, self.iter_queued(), self._queued_msg_path_cache)
def purge(self, delivery_tag: str):
"""Purge the specified processed message.
Allows the caller to purge completed FS broker messages without
establishing a full Kombu consumer. Requeue is not supported.
Raises:
ValueError: Invalid delivery_tag
"""
self._delete_msg(
delivery_tag, self.iter_processed(), self._processed_msg_path_cache
)
def _gc(self, exclude: Optional[List[str]] = None):
"""Garbage collect expired FS broker messages.
Arguments:
exclude: Exclude (do not garbage collect) messages from the specified
queues.
"""
def _delete_expired(
msg: Message,
queues: Set[str],
now: float,
cache: Dict[str, str],
include_tickets: bool = False,
):
assert isinstance(msg.properties, dict)
properties = cast(Dict[str, Any], msg.properties)
delivery_info: Dict[str, str] = properties.get("delivery_info", {})
if queues:
routing_key = delivery_info.get("routing_key")
if routing_key and routing_key in queues:
return
headers = cast(Dict[str, Any], msg.headers)
expires: Optional[float] = headers.get("expires")
ticket = msg.headers.get("ticket")
if include_tickets and ticket or (expires is not None and expires <= now):
assert msg.delivery_tag
try:
self._delete_msg(msg.delivery_tag, [], cache)
except ValueError:
pass
queues = set(exclude) if exclude else set()
now = datetime.now().timestamp()
for msg in self._iter_data_folder():
_delete_expired(msg, queues, now, self._queued_msg_path_cache)
for msg in self._iter_processed_folder():
_delete_expired(
msg, queues, now, self._processed_msg_path_cache, include_tickets=True
)
def clean(self):
"""Clean extraneous celery messages from this FSApp."""
self._gc(exclude=[self.conf.task_default_queue])
self._clean_pidbox(f"reply.{self.conf.task_default_queue}.pidbox")
def _clean_pidbox(self, exchange: str):
"""Clean pidbox replies for the specified exchange."""
def _delete_replies(msg: Message, exchange: str, cache: Dict[str, str]):
assert isinstance(msg.properties, dict)
properties = cast(Dict[str, Any], msg.properties)
delivery_info: Dict[str, str] = properties.get("delivery_info", {})
if delivery_info.get("exchange", "") == exchange:
assert msg.delivery_tag
try:
self._delete_msg(msg.delivery_tag, [], cache)
except ValueError:
pass
for msg in self._iter_data_folder():
_delete_replies(msg, exchange, self._queued_msg_path_cache)