Skip to content

Commit edec81f

Browse files
committed
Refresh tokens when pyfa start to avoid expiry
1 parent 405cf78 commit edec81f

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

pyfa.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ def _process_args(self, largs, rargs, values):
150150
mf = MainFrame(options.title)
151151
ErrorHandler.SetParent(mf)
152152

153+
# Start ESI token validation, this helps avoid token expiry
154+
from service.esi import Esi
155+
esi = Esi.getInstance()
156+
esi.startTokenValidation()
157+
pyfalog.info("ESI token validation started")
158+
153159
if options.profile_path:
154160
profile_path = os.path.join(options.profile_path, 'pyfa-{}.profile'.format(datetime.datetime.now().strftime('%Y%m%d_%H%M%S')))
155161
pyfalog.debug("Starting pyfa with a profiler, saving to {}".format(profile_path))

service/esi.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,44 @@
2525
_t = wx.GetTranslation
2626

2727

28+
class EsiTokenValidationThread(threading.Thread):
29+
def __init__(self, callback=None):
30+
threading.Thread.__init__(self)
31+
self.name = "EsiTokenValidation"
32+
self.callback = callback
33+
self.running = True
34+
35+
def run(self):
36+
with config.logging_setup.threadbound():
37+
try:
38+
esi = Esi.getInstance()
39+
chars = esi.getSsoCharacters()
40+
41+
for char in chars:
42+
if not self.running:
43+
return
44+
45+
if char.is_token_expired():
46+
pyfalog.info(f"Token expired for {char.characterName}, attempting refresh")
47+
try:
48+
esi.refresh(char)
49+
eos.db.save(char)
50+
pyfalog.info(f"Successfully refreshed token for {char.characterName}")
51+
except Exception as e:
52+
pyfalog.error(f"Failed to refresh token for {char.characterName}: {e}")
53+
else:
54+
pyfalog.debug(f"Token valid for {char.characterName}")
55+
56+
except Exception as e:
57+
pyfalog.error(f"Error validating ESI tokens: {e}")
58+
finally:
59+
if self.callback:
60+
wx.CallAfter(self.callback)
61+
62+
def stop(self):
63+
self.running = False
64+
65+
2866
class Esi(EsiAccess):
2967
_instance = None
3068

@@ -194,3 +232,9 @@ def handleServerRequest(self, message):
194232
pyfalog.debug("Handling SSO login with: {0}", message)
195233

196234
self.handleLogin(message['code'])
235+
236+
def startTokenValidation(self):
237+
pyfalog.debug("Starting ESI token validation thread")
238+
tokenValidationThread = EsiTokenValidationThread()
239+
tokenValidationThread.daemon = True
240+
tokenValidationThread.start()

0 commit comments

Comments
 (0)