Forums

Reload Web if error appears in error.log

Hello,

I have a script running every hour to reload my site, but I have an error that occurs with the database connection going down... this is usually fixed by a reload.

I wanted to know if it's possible to check the error.log file... I have the script, but I'm not sure what the path file would be? I've tried "/home/MYNAME/mysite/error.log" and others but they failed.

Thanks

Links to logs are one the "Web" page.

Hello fjl,

This is the web page for my error logs...

https://www.pythonanywhere.com/user/JamesNorman/files/var/log/jamesnorman.pythonanywhere.com.error.log

I've tried many variations of these as LOG_FILE...

def check_error(): with open(LOG_FILE, "r") as f:

And I get the same FileNotFoundError... could you tell me what I should be using please?

What is the most recent error on the bottom of that error log?

2025-02-19 21:42:23,946: Exception on /auslogin [POST] Traceback (most recent call last): File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1819, in execute_context self.dialect.do_execute( File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/default.py", line 732, in do_execute cursor.execute(statement, parameters) File "/home/JamesNorman/.local/lib/python3.10/site-packages/pymysql/cursors.py", line 153, in execute result = self._query(query) File "/home/JamesNorman/.local/lib/python3.10/site-packages/pymysql/cursors.py", line 322, in _query conn.query(q) File "/home/JamesNorman/.local/lib/python3.10/site-packages/pymysql/connections.py", line 563, in query self._affected_rows = self._read_query_result(unbuffered=unbuffered) File "/home/JamesNorman/.local/lib/python3.10/site-packages/pymysql/connections.py", line 825, in _read_query_result result.read() File "/home/JamesNorman/.local/lib/python3.10/site-packages/pymysql/connections.py", line 1199, in read first_packet = self.connection._read_packet() File "/home/JamesNorman/.local/lib/python3.10/site-packages/pymysql/connections.py", line 753, in _read_packet raise err.OperationalError( pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query') NO MATCH The above exception was the direct cause of the following exception: NO MATCH Traceback (most recent call last): File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 2077, in wsgi_app response = self.full_dispatch_request() File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 1525, in full_dispatch_request rv = self.handle_user_exception(e) File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 1523, in full_dispatch_request rv = self.dispatch_request() File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 1509, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args) File "/home/JamesNorman/mysite/flask_app.py", line 724, in auslogin client = AUSDB.AusClients.query.filter_by(Username=username).first() File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/query.py", line 2819, in first return self.limit(1)._iter().first() File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/query.py", line 2903, in _iter result = self.session.execute( File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/session.py", line 1696, in execute result = conn._execute_20(statement, params or {}, execution_options) File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1631, in _execute_20 return meth(self, args_10style, kwargs_10style, execution_options) File "/usr/local/lib/python3.10/site-packages/sqlalchemy/sql/elements.py", line 325, in _execute_on_connection return connection._execute_clauseelement( File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1498, in _execute_clauseelement ret = self._execute_context( File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1862, in _execute_context self._handle_dbapi_exception( File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 2043, in _handle_dbapi_exception util.raise( File "/usr/local/lib/python3.10/site-packages/sqlalchemy/util/compat.py", line 207, in raise_ raise exception File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1819, in _execute_context self.dialect.do_execute( File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/default.py", line 732, in do_execute cursor.execute(statement, parameters) File "/home/JamesNorman/.local/lib/python3.10/site-packages/pymysql/cursors.py", line 153, in execute result = self._query(query) File "/home/JamesNorman/.local/lib/python3.10/site-packages/pymysql/cursors.py", line 322, in _query conn.query(q) File "/home/JamesNorman/.local/lib/python3.10/site-packages/pymysql/connections.py", line 563, in query self._affected_rows = self._read_query_result(unbuffered=unbuffered) File "/home/JamesNorman/.local/lib/python3.10/site-packages/pymysql/connections.py", line 825, in _read_query_result result.read() File "/home/JamesNorman/.local/lib/python3.10/site-packages/pymysql/connections.py", line 1199, in read first_packet = self.connection._read_packet() File "/home/JamesNorman/.local/lib/python3.10/site-packages/pymysql/connections.py", line 753, in _read_packet raise err.OperationalError( sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2013, 'Lost connection to MySQL server during query') [SQL: SELECT AussieUsers.Username AS AussieUsers_Username, AussieUsers.Password AS AussieUsers_Password, AussieUsers.TennisTimestamp AS AussieUsers_TennisTimestamp, AussieUsers.Email AS AussieUsers_Email FROM AussieUsers WHERE AussieUsers.Username = %(Username_1)s LIMIT %(param_1)s] [parameters: {'Username_1': 'JamesNorman', 'param_1': 1}] (Background on this error at: https://sqlalche.me/e/14/e3q8)

This is the error that causes the crash and I'm not sure why... It doesn't happen on other end points. The MySQL query takes 0.00 seconds so I don't know why it would crash.

I've tried ChatGPT for advice but it confirmed the settings were correct.

Refreshing does work so I thought that could be a viable solution, but if you have a better idea I'm open to suggestions :)

Have you seen this help page? https://help.pythonanywhere.com/pages/UsingSQLAlchemywithMySQL/

Thank you Nkahr,

I hadn't seen that, but it does help my understanding.

I'm curious as to how you would suggest I check usernames and passwords when client login?

When I started I just had a JSON file on the main site script and just checked that... but in my attempts to be a little more professional I thought I should have them in a database and make requests that way.

However, this The best solution to this is simply to not access the database from code outside view functions. makes me think that I might be better off reverting to JSON files again?

I'm happy to do that if you believe it's the most effective way.

Many thanks,

James

It's totally fine to access the database from within view functions and, since that is where you would be checking login details, it will be fine to have your auth code use the database

Thanks Glenn,

So how exactly can I prevent these from crashing then?

I had already included a config as..

SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_ENGINE_OPTIONS = {
'pool_recycle': 280,
'pool_pre_ping': True
}
PERMANENT_SESSION_LIFETIME = timedelta(hours=1)

Which has the suggested pool_recycle

Apologies but my knowledge about accessing databases within view functions is limited. Whenever this crashes, refreshing the site works, but I understand that probably isn't the most efficient / effective solution.

Is there a guide to this functionality of checking a database for users at login?

We would need to see more of your code to help you, but at the same time as we could investigate problems with PythonAnywhere we might not have capacity to debug your code.