Add a script for deleting the old temporary access tokens in small batches.
This commit is contained in:
parent
291c1c810b
commit
3bbe064291
1 changed files with 48 additions and 0 deletions
48
util/delete_access_tokens.py
Normal file
48
util/delete_access_tokens.py
Normal file
|
@ -0,0 +1,48 @@
|
|||
import logging
|
||||
import time
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from data.database import RepositoryBuild, AccessToken
|
||||
from app import app
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
BATCH_SIZE = 5000
|
||||
|
||||
def delete_temporary_access_tokens(older_than):
|
||||
# Find the higest ID up to which we should delete
|
||||
up_to_id = (AccessToken
|
||||
.select(AccessToken.id)
|
||||
.where(AccessToken.created < older_than)
|
||||
.limit(1)
|
||||
.order_by(AccessToken.id.desc())
|
||||
.get().id)
|
||||
logger.debug('Deleting temporary access tokens with ids lower than: %s', up_to_id)
|
||||
|
||||
|
||||
access_tokens_in_builds = (RepositoryBuild.select(RepositoryBuild.access_token).distinct())
|
||||
|
||||
while up_to_id > 0:
|
||||
starting_at_id = max(up_to_id - BATCH_SIZE, 0)
|
||||
logger.debug('Deleting tokens with ids between %s and %s', starting_at_id, up_to_id)
|
||||
start_time = datetime.utcnow()
|
||||
(AccessToken
|
||||
.delete()
|
||||
.where(AccessToken.id > starting_at_id,
|
||||
AccessToken.id < up_to_id,
|
||||
AccessToken.temporary == 1,
|
||||
~(AccessToken.id << access_tokens_in_builds))
|
||||
.execute())
|
||||
|
||||
time_to_delete = datetime.utcnow() - start_time
|
||||
|
||||
up_to_id -= BATCH_SIZE
|
||||
|
||||
logger.debug('Sleeping for %s seconds', time_to_delete.total_seconds())
|
||||
time.sleep(time_to_delete.total_seconds())
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
delete_temporary_access_tokens(datetime.utcnow() - timedelta(days=2))
|
Reference in a new issue