Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Celery task for refresh materialized view (#975)
- Loading branch information
1 parent
374f850
commit a799433
Showing
3 changed files
with
46 additions
and
18 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
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,40 @@ | ||
import logging | ||
import json | ||
from src.tasks.celery_app import celery | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
def update_views(self, db): | ||
with db.scoped_session() as session: | ||
logger.info('index_materialized_views.py | Updating materialized views') | ||
session.execute("REFRESH MATERIALIZED VIEW user_lexeme_dict") | ||
session.execute("REFRESH MATERIALIZED VIEW track_lexeme_dict") | ||
session.execute("REFRESH MATERIALIZED VIEW playlist_lexeme_dict") | ||
session.execute("REFRESH MATERIALIZED VIEW album_lexeme_dict") | ||
logger.info('index_materialized_views.py | Finished updating materialized views') | ||
|
||
######## CELERY TASKS ######## | ||
@celery.task(name="update_materialized_views", bind=True) | ||
def update_materialized_views(self): | ||
# Cache custom task class properties | ||
# Details regarding custom task context can be found in wiki | ||
# Custom Task definition can be found in src/__init__.py | ||
db = update_materialized_views.db | ||
redis = update_materialized_views.redis | ||
# Define lock acquired boolean | ||
have_lock = False | ||
# Define redis lock object | ||
update_lock = redis.lock("update_materialized_views", timeout=7200) | ||
try: | ||
# Attempt to acquire lock - do not block if unable to acquire | ||
have_lock = update_lock.acquire(blocking=False) | ||
if have_lock: | ||
update_views(self, db) | ||
else: | ||
logger.info("index_materialized_views.py | Failed to acquire update_materialized_views") | ||
except Exception as e: | ||
logger.error("index_materialized_views.py | Fatal error in main loop", exc_info=True) | ||
raise e | ||
finally: | ||
if have_lock: | ||
update_lock.release() |