Better manage price object juggling in Item.price property. The previous way was creating new price objects when an object already existed in the database which could lead to Integrity issues.

This commit is contained in:
blitzmann
2017-04-10 20:48:58 -04:00
parent 1260900668
commit ce01c66b3e
3 changed files with 23 additions and 30 deletions

View File

@@ -254,6 +254,7 @@ class Item(EqBase):
self.__offensive = None
self.__assistive = None
self.__overrides = None
self.__price = None
@property
def attributes(self):
@@ -441,34 +442,22 @@ class Item(EqBase):
@property
def price(self):
try:
if not hasattr(self, "__price"):
# todo: use `from sqlalchemy import inspect` instead (need to verify it works in old and new OS X builds)
if self.__price is not None and getattr(self.__price, '_sa_instance_state', None):
pyfalog.debug("Price data for {} was deleted, resetting object".format(self.ID))
self.__price = None
if self.__price is None:
db_price = eos.db.getPrice(self.ID)
# do not yet have a price in the database for this item, create one
if db_price is None:
print "Creating a price for {}".format(self.ID)
self.__price = types_Price(self.ID)
# Get the price from the DB
price = eos.db.getPrice(self.ID)
if price:
if self.__price.time > price.time:
# The object is newer than the DB, update the DB.
eos.db.add(self.__price)
eos.db.commit()
if self.__price.time < price.time:
# DB object is newer than local object, update the local object
self.__price = price
eos.db.add(self.__price)
eos.db.commit()
else:
pyfalog.debug("Unable to fetch item price from database.")
return self.__price
except Exception as e:
# We want to catch our failure and log it, but don't bail out for a single missing price tag.
pyfalog.error("Failed to get price for typeID: {0}", self.ID)
pyfalog.error(e)
if not self.__price.price:
self.__price.price = 0
self.__price.failed = True
self.__price = db_price
return self.__price

View File

@@ -3,6 +3,7 @@ import gui.mainFrame
# noinspection PyPackageRequirements
import wx
import gui.globalEvents as GE
from service.price import Price
from service.settings import ContextMenuSettings
@@ -21,6 +22,8 @@ class PriceClear(ContextMenu):
return "Reset Price Cache"
def activate(self, fullContext, selection, i):
sPrc = Price.getInstance()
sPrc.clearPriceCache()
wx.PostEvent(self.mainFrame, GE.FitChanged(fitID=self.mainFrame.getActiveFit()))

View File

@@ -34,7 +34,7 @@ from service.market import Market
pyfalog = Logger(__name__)
VALIDITY = 24 * 60 * 60 # Price validity period, 24 hours
VALIDITY = 60 # Price validity period, 24 hours
REREQUEST = 4 * 60 * 60 # Re-request delay for failed fetches, 4 hours
TIMEOUT = 15 * 60 # Network timeout delay for connection issues, 15 minutes
@@ -128,7 +128,6 @@ class Price(object):
priceobj.failed = None
# Update the DB.
db.add(priceobj)
db.commit()
# delete price from working dict
@@ -144,7 +143,6 @@ class Price(object):
priceobj.failed = True
# Update the DB.
db.add(priceobj)
db.commit()
del priceMap[typeID]
@@ -160,7 +158,6 @@ class Price(object):
priceobj.failed = True
# Update the DB.
db.add(priceobj)
db.commit()
@classmethod
@@ -217,6 +214,10 @@ class Price(object):
else:
self.priceWorkerThread.trigger(requests, cb)
def clearPriceCache(self):
pyfalog.debug("Clearing Prices")
db.clearPrices()
class PriceWorkerThread(threading.Thread):
def __init__(self):