|
2 | 2 | import time |
3 | 3 | from datetime import datetime, timedelta |
4 | 4 | from typing import List, Optional, Union |
5 | | - |
| 5 | +import json |
| 6 | +from json import JSONEncoder |
| 7 | +from aio_pika import Message |
6 | 8 | from app import dependencies |
7 | 9 | from app.config import settings |
8 | 10 | from app.db.file.download import _increment_file_downloads |
|
37 | 39 | router = APIRouter() |
38 | 40 | security = HTTPBearer() |
39 | 41 |
|
| 42 | +class CustomJSONEncoder(JSONEncoder): |
| 43 | + def default(self, obj): |
| 44 | + if isinstance(obj, PydanticObjectId): |
| 45 | + return str(obj) |
| 46 | + # Handle other non-serializable types if needed |
| 47 | + return super().default(obj) |
| 48 | + |
40 | 49 |
|
41 | 50 | async def _resubmit_file_extractors( |
42 | 51 | file: FileOut, |
@@ -135,6 +144,20 @@ async def add_file_entry( |
135 | 144 | # Add entry to the file index |
136 | 145 | await index_file(es, FileOut(**new_file.dict())) |
137 | 146 |
|
| 147 | + # Publish a message when indexing is complete |
| 148 | + |
| 149 | + message_body = { |
| 150 | + "event_type": "file_indexed", |
| 151 | + "file_data": json.loads(new_file.json()), # This handles ObjectID serialization |
| 152 | + "timestamp": datetime.now().isoformat() |
| 153 | + } |
| 154 | + |
| 155 | + rabbitmq_client.basic_publish( |
| 156 | + exchange='clowder', |
| 157 | + routing_key='file_indexed_events', |
| 158 | + body=json.dumps(message_body).encode('utf-8') |
| 159 | + ) |
| 160 | + |
138 | 161 | # TODO - timing issue here, check_feed_listeners needs to happen asynchronously. |
139 | 162 | time.sleep(1) |
140 | 163 |
|
@@ -163,6 +186,18 @@ async def add_local_file_entry( |
163 | 186 |
|
164 | 187 | # Add entry to the file index |
165 | 188 | await index_file(es, FileOut(**new_file.dict())) |
| 189 | + # Publish a message when indexing is complete |
| 190 | + message_body = { |
| 191 | + "event_type": "file_indexed", |
| 192 | + "file_data": json.loads(new_file.json()), # This handles ObjectID serialization |
| 193 | + "timestamp": datetime.now().isoformat() |
| 194 | + } |
| 195 | + |
| 196 | + rabbitmq_client.basic_publish( |
| 197 | + exchange='clowder', |
| 198 | + routing_key='file_indexed_events', |
| 199 | + body=json.dumps(message_body).encode('utf-8') |
| 200 | + ) |
166 | 201 |
|
167 | 202 | # TODO - timing issue here, check_feed_listeners needs to happen asynchronously. |
168 | 203 | time.sleep(1) |
|
0 commit comments