Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The old issue (2006, 'MySQL server has gone away') which have been fixed in 0.2.5 earlier but again came on 19th July 2020 #128

Closed
anandkumar99 opened this issue Dec 1, 2020 · 7 comments
Labels

Comments

@anandkumar99
Copy link

same issue which is mentioned in #13
but since the fix have been removed on 19th July 2020 now getting this issue.

Create a scheduler and add DjangoJobStore, and start it.
Add a job which will run in 5 minutes.
Restart the DB service.
Wait till 5 min.

Since the DB server is having some schedule restart this issue starts. On server after every 10 sec its keep checking the connectivity and throwing the error (2006, 'MySQL server has gone away')
error
even when DB server backs it keep in that error and not calling the next schedule.

@jcass77
Copy link
Owner

jcass77 commented Dec 1, 2020

This issue appears to be MySQL specific, and affects all Django applications. Have a look at https://code.djangoproject.com/ticket/21597#comment:29 for some context and history.

I haven’t come across an example of an elegant solution being used in any other Django projects.

The overhead of pinging the database to keep the connection alive as proposed in #13 is probably not fair towards users of other, non-MySQL backends, and would interfere with connection poolers like pgbouncer.

@anandkumar99
Copy link
Author

Thanks but since MySQL is also widely used with Django and our Django application is facing issue only with apscheduler.
Can you please suggest some solution which we can use with apscheduler so it will work with MySQL

@jcass77
Copy link
Owner

jcass77 commented Dec 2, 2020

From the link to the upstream Django project issue #21597 shared above:

If you hit this problem and don't want to understand what's going on, don't reopen this ticket, just do this:

RECOMMENDED SOLUTION: close the connection with from django.db import connection; connection.close() when you know that your program is going to be idle for a long time.

@anandkumar99
Copy link
Author

anandkumar99 commented Dec 2, 2020

Thanks for reply but I don't understand how to do that.

We are having the below steps:
Create a scheduler and add DjangoJobStore, and start it.
Add a job which will run in 1 minutes.
Restart the DB service.
Wait till few min.

I have pasted the code which is just printing message and closing the connection object as you shared the comment link. When DB service is restarting then again same issue starting (2006, 'MySQL server has gone away') after every 10 sec and its even not calling the demo method even the DB service is available now.

`
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.executors.pool import ProcessPoolExecutor,ThreadPoolExecutor
from apscheduler.events import EVENT_JOB_EXECUTED, EVENT_JOB_ERROR, JobExecutionEvent, EVENT_JOB_MISSED, EVENT_JOB_MAX_INSTANCES
from django_apscheduler.jobstores import register_events, register_job
from django.db import connection
from django_apscheduler.jobstores import DjangoJobStore
from django_apscheduler.models import DjangoJobExecution
from django.shortcuts import redirect, get_object_or_404
from django.conf import settings
import time

scheduler=BackgroundScheduler({'apscheduler.job_defaults.max_instances':'1','daemon':True, 'apscheduler.job_defaults.coalesce':'true'})
def start():
try:
#scheduler=BackgroundScheduler({'apscheduler.job_defaults.max_instances':'1','daemon':True, 'apscheduler.job_defaults.coalesce':'true'})
scheduler.add_jobstore(DjangoJobStore(), "default")
scheduler.add_listener(my_listener, EVENT_JOB_EXECUTED | EVENT_JOB_ERROR | EVENT_JOB_MISSED | EVENT_JOB_MAX_INSTANCES)
scheduler.add_job(demo ,'cron', minute='*', id="demo_id", replace_existing=True, misfire_grace_time=None ,coalesce=True)
register_events(scheduler)
scheduler.start()
except:
pass

def demo():
print('schedule called')
connection.close()

`

@anandkumar99
Copy link
Author

image

in my post its lost the tabbing so adding the image

@jcass77
Copy link
Owner

jcass77 commented Dec 2, 2020

Sorry this might not be very helpful, but re-opening database connections automatically after a DB server crash is not something that Django supports.

You should probably be supervising the DB server process at operating system level, and restarting your web server automatically as well in case an unexpected DB crash occurs. If you are restarting the DB on a pre-determined schedule then it would probably make sense to stop the web server first, and then start it up again once the DB is available (otherwise, how would Django be able to service any HTTP client requests while the DB is down?).

If you look through the related issues that have been logged in the Django project up stream then you will notice that there are many nuances that make a 'catch-all' solution quite difficult to implement.

If you don't care and just want a quick fix, then it might be sensible to revert to an earlier version of django-apscheduler, or try to re-use elements of the old workaround that this project used to include: https://github.com/jarekwg/django-apscheduler/blob/d7dad0f7901291e1dae497082bf95fd3c214c0b2/django_apscheduler/models.py#L9-L35.

https://github.com/jarekwg/django-apscheduler/blob/d7dad0f7901291e1dae497082bf95fd3c214c0b2/django_apscheduler/models.py#L44

@jcass77
Copy link
Owner

jcass77 commented Dec 28, 2020

Closing this issue as the example provided above should be sufficient for anyone wanting to re-implement the database pinging approach to detect when the database goes away.

We might also consider trying:

django.db import connection

connection.close()

...after every new job is scheduled if anyone encounters this issue under normal operating conditions (i.e. even if the database remains online and available).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants