from __future__ import absolute_import

import os

from celery import Celery
from django.conf import settings
from celery.signals import celeryd_init

from django.core.management import call_command


os.environ.setdefault("DJANGO_SETTINGS_MODULE",
                      "nurseconnect.settings.production")

app = Celery("proj")

# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object("django.conf:settings")
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)  # pragma: no cover


@celeryd_init.connect
def ensure_search_index_updated(sender, instance, **kwargs):
    '''
    Run update_index when celery starts
    '''
    try:
        from wagtail.wagtailsearch.backends.db import DBSearch
        backend = DBSearch
    except ImportError:
        from wagtail.wagtailsearch.backends.db import DatabaseSearchBackend
        backend = DatabaseSearchBackend

    from wagtail.wagtailsearch.backends import get_search_backend

    if not isinstance(get_search_backend(), backend):
        call_command('update_index')
