Skip to content

Commit

Permalink
Update scanner.py
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptobench committed May 31, 2024
1 parent 9c7fd3a commit d69e634
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions stats-backend/api2/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ def update_providers_info(node_props):
sys.path.append(str(examples_dir))
from .yapapi_utils import build_parser, print_env_info, format_usage # noqa: E402


@app.task
def update_nodes_status(nodes_to_update):
for provider_id, is_online_now in nodes_to_update.items():
Expand All @@ -191,15 +190,19 @@ def update_nodes_status(nodes_to_update):
if latest_status_from_db:
# Compare the latest status from the database with the current status
if latest_status_from_db.is_online != is_online_now:
# Status has changed, update the database
# Status has changed, update the database and Node.online field
NodeStatusHistory.objects.create(
provider=provider, is_online=is_online_now
)
provider.online = is_online_now
provider.save()
else:
# No previous status found in the database, create a new entry
NodeStatusHistory.objects.create(
provider=provider, is_online=is_online_now
)
provider.online = is_online_now
provider.save()

# Store the current status in Redis for future lookups
r.set(f"node_status:{provider_id}", str(is_online_now))
Expand All @@ -208,16 +211,17 @@ def update_nodes_status(nodes_to_update):
# Compare the latest status from Redis with the current status
if latest_status.decode() != str(is_online_now):
print(f"Status has changed for provider {provider_id}")
# Status has changed, update the database
# Status has changed, update the database and Node.online field
provider, created = Node.objects.get_or_create(node_id=provider_id)
NodeStatusHistory.objects.create(
provider=provider, is_online=is_online_now
)
provider.online = is_online_now
provider.save()

# Update the status in Redis
r.set(f"node_status:{provider_id}", str(is_online_now))


from celery import group


Expand Down

0 comments on commit d69e634

Please sign in to comment.