-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from Kyria/develop
add db rollback and try blocks for database update in tasks
- Loading branch information
Showing
8 changed files
with
68 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
# -*- encoding: utf-8 -*- | ||
__version__ = 1.0 | ||
__version__ = 1.0.2 |
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
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 |
---|---|---|
@@ -1,36 +1,43 @@ | ||
# -*- encoding: utf-8 -*- | ||
""" Purging task """ | ||
import datetime | ||
from sqlalchemy.exc import SQLAlchemyError | ||
|
||
import config | ||
from lazyblacksmith.models import ItemPrice, TokenScope, db | ||
from lazyblacksmith.utils.time import utcnow | ||
|
||
from ... import celery_app | ||
from ... import celery_app, logger | ||
|
||
|
||
@celery_app.task(name="schedule.purge") | ||
def task_purge(): | ||
""" Purge all old stuff everywhere. """ | ||
# purge all tokens people never updated | ||
purge_date_limit = ( | ||
utcnow() - datetime.timedelta(days=config.PURGE_INVALID_TOKENS) | ||
) | ||
TokenScope.query.filter_by( | ||
valid=False | ||
).filter( | ||
( | ||
(TokenScope.last_update.is_(None)) & | ||
(TokenScope.updated_at <= purge_date_limit) | ||
) | ( | ||
TokenScope.last_update < purge_date_limit | ||
|
||
try: | ||
purge_date_limit = ( | ||
utcnow() - datetime.timedelta(days=config.PURGE_INVALID_TOKENS) | ||
) | ||
TokenScope.query.filter_by( | ||
valid=False | ||
).filter( | ||
( | ||
(TokenScope.last_update.is_(None)) & | ||
(TokenScope.updated_at <= purge_date_limit) | ||
) | ( | ||
TokenScope.last_update < purge_date_limit | ||
) | ||
).delete() | ||
db.session.commit() | ||
|
||
# purge old market data | ||
purge_date_limit = ( | ||
utcnow() - datetime.timedelta(days=config.PURGE_OLD_PRICES) | ||
) | ||
).delete() | ||
db.session.commit() | ||
ItemPrice.query.filter(ItemPrice.updated_at < purge_date_limit).delete() | ||
db.session.commit() | ||
|
||
# purge old market data | ||
purge_date_limit = ( | ||
utcnow() - datetime.timedelta(days=config.PURGE_OLD_PRICES) | ||
) | ||
ItemPrice.query.filter(ItemPrice.updated_at < purge_date_limit).delete() | ||
db.session.commit() | ||
except SQLAlchemyError: | ||
db.session.rollback() | ||
logger.exception("Error while trying to purge data") |
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
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