diff --git a/app/app/settings.py b/app/app/settings.py index f216abb70e4..ea381be96a7 100644 --- a/app/app/settings.py +++ b/app/app/settings.py @@ -386,6 +386,7 @@ DJANGO_REDIS_LOG_IGNORED_EXCEPTIONS = env.bool('REDIS_LOG_IGNORED_EXCEPTIONS', default=True) COLLECTFAST_CACHE = env('COLLECTFAST_CACHE', default='collectfast') COLLECTFAST_DEBUG = env.bool('COLLECTFAST_DEBUG', default=False) +REDIS_URL = env('REDIS_URL', default='rediscache://redis:6379/0?client_class=django_redis.client.DefaultClient') CACHES = { 'default': env.cache( diff --git a/app/app/utils.py b/app/app/utils.py index e59dded9baf..37b7461c228 100644 --- a/app/app/utils.py +++ b/app/app/utils.py @@ -354,3 +354,13 @@ def get_default_network(): if settings.DEBUG: return 'rinkeby' return 'mainnet' + + +def get_semaphor(namespace, count=1): + from redis import Redis + from redis_semaphore import Semaphore + from urllib.parse import urlparse + redis = urlparse(settings.REDIS_URL) + + semaphore = Semaphore(Redis(host=redis.hostname, port=redis.port), count=count, namespace=namespace) + return semaphore diff --git a/app/dashboard/utils.py b/app/dashboard/utils.py index 7b968bc3d00..080f06df1ef 100644 --- a/app/dashboard/utils.py +++ b/app/dashboard/utils.py @@ -25,6 +25,7 @@ import ipfsapi import requests +from app.utils import get_semaphor from dashboard.helpers import UnsupportedSchemaException, normalize_url, process_bounty_changes, process_bounty_details from dashboard.models import Activity, Bounty, UserAction from eth_utils import to_checksum_address @@ -319,14 +320,17 @@ def web3_process_bounty(bounty_data): print(f"--*--") return None - did_change, old_bounty, new_bounty = process_bounty_details(bounty_data) + semaphor_key = f"bounty_processor_{bounty_data['id']}" + semaphor = get_semaphor(semaphor_key) + with semaphor: + did_change, old_bounty, new_bounty = process_bounty_details(bounty_data) - if did_change and new_bounty: - _from = old_bounty.pk if old_bounty else None - print(f"- processing changes, {_from} => {new_bounty.pk}") - process_bounty_changes(old_bounty, new_bounty) + if did_change and new_bounty: + _from = old_bounty.pk if old_bounty else None + print(f"- processing changes, {_from} => {new_bounty.pk}") + process_bounty_changes(old_bounty, new_bounty) - return did_change, old_bounty, new_bounty + return did_change, old_bounty, new_bounty def has_tx_mined(txid, network): diff --git a/requirements/base.txt b/requirements/base.txt index 8fd49e40227..2bd3559bb96 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -76,3 +76,5 @@ mkdocs-material==3.0.4 pydoc-markdown==2.0.4 oauth2client==4.1.3 pyvips==2.1.3 +redis-semaphore +