Skip to content

Commit

Permalink
fix: hotfix for db connection issue (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
codebender37 authored Dec 11, 2024
1 parent 2f30931 commit 5251701
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 32 deletions.
57 changes: 27 additions & 30 deletions commons/score_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import torch
from bittensor.btlogging import logging as logger

from database.client import connect_db, disconnect_db
from database.client import connect_db
from database.prisma.models import Score_Model


Expand All @@ -28,35 +28,32 @@ async def migrate_from_db(cls) -> bool:
# Connect to database first
await connect_db()

try:
# Get scores from database
score_record = await Score_Model.prisma().find_first(
order={"created_at": "desc"}
)
if not score_record:
logger.warning("No scores found in database to migrate")
return True # Not an error, just no scores yet

scores = torch.tensor(json.loads(score_record.score))

# Create scores directory if it doesn't exist
cls.SCORES_DIR.mkdir(exist_ok=True)

# Save scores to .pt file
torch.save(scores, cls.SCORES_FILE)
logger.success(f"Successfully migrated scores to {cls.SCORES_FILE}")

# Verify the migration
loaded_scores = torch.load(cls.SCORES_FILE)
if torch.equal(scores, loaded_scores):
logger.success("Migration verification successful - scores match")
return True
else:
logger.error("Migration verification failed - scores do not match")
return False

finally:
await disconnect_db()
# Get scores from database
score_record = await Score_Model.prisma().find_first(
order={"created_at": "desc"}
)
if not score_record:
logger.warning("No scores found in database to migrate")
return True # Not an error, just no scores yet

scores = torch.tensor(json.loads(score_record.score))

# Create scores directory if it doesn't exist
cls.SCORES_DIR.mkdir(exist_ok=True)

# Save scores to .pt file
torch.save(scores, cls.SCORES_FILE)
logger.success(f"Successfully migrated scores to {cls.SCORES_FILE}")

# Verify the migration
loaded_scores = torch.load(cls.SCORES_FILE)

if torch.equal(scores, loaded_scores):
logger.success("Migration verification successful - scores match")
return True
else:
logger.error("Migration verification failed - scores do not match")
return False

except Exception as e:
logger.error(f"Failed to migrate scores: {e}")
Expand Down
4 changes: 2 additions & 2 deletions neurons/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ def __init__(self):
)
self.check_registered()

init_wandb(config=self.config, my_uid=self.uid, wallet=self.wallet)

# Run score migration before loading state
migration_success = self.loop.run_until_complete(ScoreStorage.migrate_from_db())
if not migration_success:
Expand All @@ -121,8 +123,6 @@ def __init__(self):
self.executor = ThreadPoolExecutor(max_workers=2)
self.load_state()

init_wandb(config=self.config, my_uid=self.uid, wallet=self.wallet)

async def send_scores(self, synapse: ScoringResult, hotkeys: List[str]):
"""Send consensus score back to miners who participated in the request."""
axons = [axon for axon in self.metagraph.axons if axon.hotkey in hotkeys]
Expand Down

0 comments on commit 5251701

Please sign in to comment.