# Path: hyperkitty/search_indexes.py
# def update_index():
#     """
#     Update the search index with the new emails since the last index update.
#     """
#     update_cmd = UpdateIndexCommand()
#     # Find the last email in the index:
#     try:
#         last_email = SearchQuerySet().latest('archived_date')
#     except Exception: # pylint: disable-msg=broad-except
#         # Different backends can raise different exceptions unfortunately
#         update_cmd.start_date = None
#     else:
#         update_cmd.start_date = last_email.object.archived_date
#     # set defaults
#     update_cmd.verbosity = 0
#     update_cmd.batchsize = None
#     update_cmd.end_date = None
#     update_cmd.workers = 0
#     # Setting remove to True is extremely slow, it needs to scan the entire
#     # index and database. About 15 minutes on Fedora's lists, so not for a
#     # frequent operation.
#     update_cmd.remove = False
#     update_cmd.update_backend("hyperkitty", "default")


import os.path
import logging
from tempfile import gettempdir
from django.conf import settings
from django_extensions.management.jobs import BaseJob
from lockfile import LockFile, AlreadyLocked, LockFailed
from hyperkitty.search_indexes import update_index

"""
Update the full-text index
"""

from __future__ import absolute_import, print_function, unicode_literals



logger = logging.getLogger(__name__)


class Job(BaseJob):
    help = "Update the full-text index"
    when = "minutely"

    def execute(self):
        lock = LockFile(getattr(
            settings, "HYPERKITTY_JOBS_UPDATE_INDEX_LOCKFILE",
            os.path.join(gettempdir(), "hyperkitty-jobs-update-index.lock")))
        try:
            lock.acquire(timeout=0)
        except AlreadyLocked:
            logger.warning("The job 'update_index' is already running")
            return
        except LockFailed as e:
            logger.warning("Could not obtain a lock for the 'update_index' "
                           "job (%s)", e)
            return
        try:

# Path: hyperkitty/search_indexes.py
# def update_index():
#     """
#     Update the search index with the new emails since the last index update.
#     """
#     update_cmd = UpdateIndexCommand()
#     # Find the last email in the index:
#     try:
#         last_email = SearchQuerySet().latest('archived_date')
#     except Exception: # pylint: disable-msg=broad-except
#         # Different backends can raise different exceptions unfortunately
#         update_cmd.start_date = None
#     else:
#         update_cmd.start_date = last_email.object.archived_date
#     # set defaults
#     update_cmd.verbosity = 0
#     update_cmd.batchsize = None
#     update_cmd.end_date = None
#     update_cmd.workers = 0
#     # Setting remove to True is extremely slow, it needs to scan the entire
#     # index and database. About 15 minutes on Fedora's lists, so not for a
#     # frequent operation.
#     update_cmd.remove = False
#     update_cmd.update_backend("hyperkitty", "default")


# Path: hyperkitty/jobs/update_index.py
import os.path
import logging
from tempfile import gettempdir
from django.conf import settings
from django_extensions.management.jobs import BaseJob
from lockfile import LockFile, AlreadyLocked, LockFailed
from hyperkitty.search_indexes import update_index


"""
Update the full-text index
"""

from __future__ import absolute_import, print_function, unicode_literals



logger = logging.getLogger(__name__)


class Job(BaseJob):
    help = "Update the full-text index"
    when = "minutely"

    def execute(self):
        lock = LockFile(getattr(
            settings, "HYPERKITTY_JOBS_UPDATE_INDEX_LOCKFILE",
            os.path.join(gettempdir(), "hyperkitty-jobs-update-index.lock")))
        try:
            lock.acquire(timeout=0)
        except AlreadyLocked:
            logger.warning("The job 'update_index' is already running")
            return
        except LockFailed as e:
            logger.warning("Could not obtain a lock for the 'update_index' "
                           "job (%s)", e)
            return
        try:

            update_index()