Merge pull request #2683 from cryonox/dev/refresh_tokens
Refresh tokens when pyfa start to avoid expiry
This commit is contained in:
6
pyfa.py
6
pyfa.py
@@ -150,6 +150,12 @@ if __name__ == "__main__":
|
||||
mf = MainFrame(options.title)
|
||||
ErrorHandler.SetParent(mf)
|
||||
|
||||
# Start ESI token validation, this helps avoid token expiry
|
||||
from service.esi import Esi
|
||||
esi = Esi.getInstance()
|
||||
esi.startTokenValidation()
|
||||
pyfalog.info("ESI token validation started")
|
||||
|
||||
if options.profile_path:
|
||||
profile_path = os.path.join(options.profile_path, 'pyfa-{}.profile'.format(datetime.datetime.now().strftime('%Y%m%d_%H%M%S')))
|
||||
pyfalog.debug("Starting pyfa with a profiler, saving to {}".format(profile_path))
|
||||
|
||||
@@ -25,6 +25,44 @@ pyfalog = Logger(__name__)
|
||||
_t = wx.GetTranslation
|
||||
|
||||
|
||||
class EsiTokenValidationThread(threading.Thread):
|
||||
def __init__(self, callback=None):
|
||||
threading.Thread.__init__(self)
|
||||
self.name = "EsiTokenValidation"
|
||||
self.callback = callback
|
||||
self.running = True
|
||||
|
||||
def run(self):
|
||||
with config.logging_setup.threadbound():
|
||||
try:
|
||||
esi = Esi.getInstance()
|
||||
chars = esi.getSsoCharacters()
|
||||
|
||||
for char in chars:
|
||||
if not self.running:
|
||||
return
|
||||
|
||||
if char.is_token_expired():
|
||||
pyfalog.info(f"Token expired for {char.characterName}, attempting refresh")
|
||||
try:
|
||||
esi.refresh(char)
|
||||
eos.db.save(char)
|
||||
pyfalog.info(f"Successfully refreshed token for {char.characterName}")
|
||||
except Exception as e:
|
||||
pyfalog.error(f"Failed to refresh token for {char.characterName}: {e}")
|
||||
else:
|
||||
pyfalog.debug(f"Token valid for {char.characterName}")
|
||||
|
||||
except Exception as e:
|
||||
pyfalog.error(f"Error validating ESI tokens: {e}")
|
||||
finally:
|
||||
if self.callback:
|
||||
wx.CallAfter(self.callback)
|
||||
|
||||
def stop(self):
|
||||
self.running = False
|
||||
|
||||
|
||||
class Esi(EsiAccess):
|
||||
_instance = None
|
||||
|
||||
@@ -194,3 +232,9 @@ class Esi(EsiAccess):
|
||||
pyfalog.debug("Handling SSO login with: {0}", message)
|
||||
|
||||
self.handleLogin(message['code'])
|
||||
|
||||
def startTokenValidation(self):
|
||||
pyfalog.debug("Starting ESI token validation thread")
|
||||
tokenValidationThread = EsiTokenValidationThread()
|
||||
tokenValidationThread.daemon = True
|
||||
tokenValidationThread.start()
|
||||
|
||||
Reference in New Issue
Block a user