Comments (9)
hi @sallyruthstruik you can write a decorator like below to handle the problem("mysql has gone away"):
from functools import wraps
from django.db import connection
def db_auto_reconnect(func):
"""Auto reconnect db when mysql has gone away."""
@wraps(func)
def wrapper(*args, **kwagrs):
try:
connection.connection.ping()
except Exception:
connection.close()
return func(*args, **kwagrs)
return wrapper
and use this decorator like below:
@db_auto_reconnect
def _process_submission_event(self, event):
.....
@db_auto_reconnect
def _process_execution_event(self, event):
.....
I think it is an elegant way!
but the _ping_interval = 30
is unreliable.
from django-apscheduler.
Thanks for very detail explanation. I sure problem is about Mysql connection handling. Mysql closes connection which sleeps for a long time. I'll take a look today/tomorrow and try to solve it
from django-apscheduler.
Fixed, release 0.2.5 available on Pypi: https://github.com/jarekwg/django-apscheduler/releases/tag/0.2.5
Please, check and if everything is ok I'm close the issue
from django-apscheduler.
You are welcome.
By the way, you can reproduce the issue by:
- Create a scheduler and add
DjangoJobStore
, and start it. - Add a job which will run in 5 minutes.
- Restart the DB service. (
systemctl stop/start mariadb
, etc.) - Wait till 5 min.
You may see the same error.
Another thing is I agree that MySQL may close the connection, but setting the timeout on mysql side may not be a solution, because I may plannify a job in 7 days and... that is too long for timeout.
from django-apscheduler.
I've just added "mysql gone away" exception handling, please update to 0.2.5 and check whether exception gone or not. And please, give feedback to me in order to close the issue.
from django-apscheduler.
Wow, fixed! What did you added in the code? I see try-catch but did you reconnect DB in catch?
You can close the issue. Thanks!
from django-apscheduler.
It is a bit tricky solution: on every access to DjangoJob queryset I try to ping database (https://github.com/jarekwg/django-apscheduler/blob/master/django_apscheduler/models.py#L17). If ping fails I perform reconnect.
Unfortunately, it works only for DjangoJob queries. But you can use this Manager class for any model you want to be protected from "Mysql has gone away" error. I don't know elegant way in Django to beat "Mysql has gone away" error for long-running scripts, only hand-written try-except blocks with reconnect. It is my pain in Django, because mostly I use mysql.
Anyway, I glad exception dissapeared. Feel free to contact me with any other problems.
from django-apscheduler.
Yeah.... I see your pain.. That is I think what is bad in python: it is useful for scripting, but if you want to use a script, it is your obligation to manage all the import and so on, to handle all the possible problems in a relatively isolated situation.
Fortunately, Django manages the connection very well. I now import Django and do django.setup()
every time when I want to execute the job. See the related bug in APScheduler for details.
I didn't expect your reply to be so quick, Thanks again! The code of reconnection is imprescindible for solving this.
from django-apscheduler.
hello, I have the same problem.
APScheduler==3.5.3
django-apscheduler==0.2.13
OperationalError: (2006, 'MySQL server has gone away')
from django-apscheduler.
Related Issues (20)
- PytzUsageWarning on Startup HOT 3
- Missing migration
- Feat: Add management command to clean up job execution table HOT 2
- How to associate django_apscheduler_djangojob with my own model by ForeignKey HOT 1
- Job automatically disappeared in random time HOT 2
- Feature: simplified closing of old db connections HOT 4
- Client number building up leading to fatal error HOT 1
- skipped: maximum number of running instances reached
- cron function that is running over 15 seconds is not working HOT 1
- DB error executing '_get_jobs' ((2013, 'Lost connection to MySQL server during query')). Retrying with a new DB connection... HOT 3
- Add @util.close_old_connections in the _get_jobs(self, **filters) funtion for the DB connection
- AttributeError("'zoneinfo.ZoneInfo' object has no attribute 'localize'") and 'dict' object has no attribute 'has_header'
- ModuleNotFoundError: No module named 'backports'
- how to forbid change_permission and delete_permission... HOT 2
- Scheduler goes idle or does not execute jobs at all HOT 1
- transaction.atomic() problem with DATABASE_ROUTERS HOT 2
- django-apscheduler (0.6.2) not working with psycopg2-binary (2.9.4 and up)
- Trigger scheduled job to be executed manually HOT 2
- Unapplying Migrations Fails
- DjangoJobStore Randomly Deletes Jobs HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-apscheduler.