Skip to content

Commit

Permalink
feat(trash): remove expired nodes in trash
Browse files Browse the repository at this point in the history
  • Loading branch information
MorvanZhou committed Aug 23, 2024
1 parent 540427f commit 5f97a39
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/retk/core/scheduler/schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ def init_tasks():
func=tasks.extend_node.deliver_unscheduled_extend_nodes,
minutes=config.get_settings().LLM_KNOWLEDGE_SCHEDULE_MINUTES,
)

# auto delete expired trash nodes every day
run_every_at(
job_id="auto_clean_trash",
func=tasks.auto_clean_trash.auto_clean_trash,
kwargs={"delta_days": 30},
hour=1,
)
return


Expand Down
1 change: 1 addition & 0 deletions src/retk/core/scheduler/tasks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
email,
notice,
extend_node,
auto_clean_trash,
)
41 changes: 41 additions & 0 deletions src/retk/core/scheduler/tasks/auto_clean_trash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import asyncio
from datetime import datetime, timedelta

from bson.tz_util import utc

from retk import config
from retk.models.client import init_mongo
from retk.models.coll import CollNameEnum


def auto_clean_trash(delta_days=30):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
res = loop.run_until_complete(_auto_clean_trash(delta_days=delta_days))
loop.close()
return res


async def _auto_clean_trash(delta_days=30):
_, db = init_mongo(connection_timeout=5)
# Get all nodes in trash
if config.is_local_db():
nodes = await db[CollNameEnum.nodes.value].find({
"inTrash": True
}).to_list(None)
old_nodes = [
node for node in nodes
if node["inTrashAt"].astimezone(utc) < datetime.now(tz=utc) - timedelta(days=delta_days)
]
else:
old_nodes = await db[CollNameEnum.nodes.value].find({
"inTrash": True,
# Get all nodes in trash that are older than 30 days
"inTrashAt": {"$lt": datetime.now(tz=utc) - timedelta(days=delta_days)}
}).to_list(None)

# Delete all old nodes in trash
result = await db[CollNameEnum.nodes.value].delete_many({
"_id": {"$in": [node["_id"] for node in old_nodes]}
})
return result.deleted_count

0 comments on commit 5f97a39

Please sign in to comment.