from __future__ import absolute_import

from django.db.models.signals import post_delete

from celery.signals import task_postrun, task_prerun, task_revoked


@task_postrun.connect
@task_prerun.connect
def task_completed(sender=None, **kwargs):
    task_kwargs = kwargs.get('kwargs')
    job_id = task_kwargs.get('djangui_job')
    from .models import DjanguiJob
    from celery import states
    job = DjanguiJob.objects.get(pk=job_id)
    state = kwargs.get('state')
    if state:
        job.status = DjanguiJob.COMPLETED if state == states.SUCCESS else state
    job.celery_id = kwargs.get('task_id')
    job.save()

def reload_scripts(**kwargs):
    from .backend import utils
    utils.load_scripts()

# TODO: Figure out why relative imports fail here
from djangui.models import Script, ScriptGroup, ScriptParameter, ScriptParameterGroup
post_delete.connect(reload_scripts, sender=Script)
post_delete.connect(reload_scripts, sender=ScriptGroup)
post_delete.connect(reload_scripts, sender=ScriptParameter)
post_delete.connect(reload_scripts, sender=ScriptParameterGroup)
@@ -81,16 +81,81 @@
 t_delete
+%0Afrom django.db.utils import InterfaceError%0Afrom django import db
 %0A%0Afrom c
@@ -451,16 +451,134 @@
  states%0A
+    try:%0A        job = DjanguiJob.objects.get(pk=job_id)%0A    except InterfaceError:%0A        db.connection.close()%0A    
     job 

patch for long running jobs finishing