jcass77 / django-apscheduler

APScheduler for Django
MIT License
649 stars 93 forks source link

DjangoJobStore Randomly Deletes Jobs #188

Open unitedsoftworks opened 5 months ago

unitedsoftworks commented 5 months ago

I have apscheduler running with a DjangoJobStore from a management command in django, but every so often some (not all) of the jobs will delete themselves.

The scheduler still runs, still gets other jobs, but doesnt find the specific one in the code below. When i check the admin, its deleted.

Restarting the code fixes it as it adds the job back for a random amount of time before deleting again.

I added a listener for EVENT_JOB_REMOVED and it is never fired, and i added breakpoints in all the delete methods for DjangoJob and none of them ever hit.

the function must_reply simply checks an email inbox and sends a reply back over smtp, nothing in there accesses the DjangoJob table, nor does anything in the rest of the code.

must_reply is decorated with @close_old_connections

removing DjangoJobStore as a job store fixes the problem, but obviously I would like to use it haha

It is truly strange.

class Command(BaseCommand):
    help = "Runs APScheduler."

    def handle(self, *args, **options):

            executors = {
                'default': ThreadPoolExecutor(max_workers=10)
            }

            job_defaults = {
                'coalesce': True,
                'misfire_grace_time': 10

            }

            scheduler = BlockingScheduler(timezone=settings.TIME_ZONE, executors=executors, job_defaults=job_defaults)

            scheduler.add_jobstore(DjangoJobStore(), "default")

            scheduler.add_job(
                must_reply,
                IntervalTrigger(
                    seconds=10
                ),
                id="must_reply",
                max_instances=1,
                replace_existing=True,
            )

            try:
                scheduler.start()
            except:

                scheduler.shutdown()
                pass
unitedsoftworks commented 5 months ago

update i finally found an error

"module backports not found" inside of

jobs.append(self._reconstitute_job(job_state))

but this happens randomly during execution, and then it runs the code

# Remove all the jobs we failed to restore
if failed_job_ids:
    logger.warning(f"Removing failed jobs: {failed_job_ids}")
    DjangoJob.objects.filter(id__in=failed_job_ids).delete()
eduardopdev commented 1 month ago

Hello. Are you still having this problem?

eduardopdev commented 4 weeks ago

@unitedsoftworks wich versions of django and django-apscheduler are you running?