Code Monkey home page Code Monkey logo

Comments (9)

TreHack avatar TreHack commented on July 18, 2024 3

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.

sallyruthstruik avatar sallyruthstruik commented on July 18, 2024

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.

sallyruthstruik avatar sallyruthstruik commented on July 18, 2024

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.

WesleyBlancoYuan avatar WesleyBlancoYuan commented on July 18, 2024

You are welcome.

By the way, you can reproduce the issue by:

  1. Create a scheduler and add DjangoJobStore, and start it.
  2. Add a job which will run in 5 minutes.
  3. Restart the DB service. (systemctl stop/start mariadb, etc.)
  4. 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.

sallyruthstruik avatar sallyruthstruik commented on July 18, 2024

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.

WesleyBlancoYuan avatar WesleyBlancoYuan commented on July 18, 2024

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.

sallyruthstruik avatar sallyruthstruik commented on July 18, 2024

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.

WesleyBlancoYuan avatar WesleyBlancoYuan commented on July 18, 2024

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.

izhaolinger avatar izhaolinger commented on July 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.