-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(trash): remove expired nodes in trash
- Loading branch information
1 parent
540427f
commit 5f97a39
Showing
3 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
email, | ||
notice, | ||
extend_node, | ||
auto_clean_trash, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |