# Path: ncdjango/geoprocessing/celery_tasks.py
# @task
# def cleanup_temporary_services():
#     cutoff = now() - timedelta(seconds=MAX_TEMPORARY_SERVICE_AGE)
#     services = ProcessingResultService.objects.filter(is_temporary=True, created__lt=cutoff)
#     files_to_delete = []
# 
#     with transaction.atomic():
#         for service in services:
#             files_to_delete.append(os.path.join(SERVICE_DATA_ROOT, service.service.data_path))
#             service.service.delete()
# 
#     for path in files_to_delete:
#         try:
#             os.remove(path)
#             os.rmdir(os.path.dirname(path))  # Delete the enclosing directory, if empty
#         except OSError as e:
#             if e.errno != errno.ENOTEMPTY:
#                 logger.warn('Error deleting temporary service data: {}'.format(str(e)))


from django.core.management import BaseCommand
from ncdjango.geoprocessing.celery_tasks import cleanup_temporary_services



class Command(BaseCommand):
    help = 'Delete expired, temporary services created from geoprocessing results.'

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

# Path: ncdjango/geoprocessing/celery_tasks.py
# @task
# def cleanup_temporary_services():
#     cutoff = now() - timedelta(seconds=MAX_TEMPORARY_SERVICE_AGE)
#     services = ProcessingResultService.objects.filter(is_temporary=True, created__lt=cutoff)
#     files_to_delete = []
# 
#     with transaction.atomic():
#         for service in services:
#             files_to_delete.append(os.path.join(SERVICE_DATA_ROOT, service.service.data_path))
#             service.service.delete()
# 
#     for path in files_to_delete:
#         try:
#             os.remove(path)
#             os.rmdir(os.path.dirname(path))  # Delete the enclosing directory, if empty
#         except OSError as e:
#             if e.errno != errno.ENOTEMPTY:
#                 logger.warn('Error deleting temporary service data: {}'.format(str(e)))


# Path: ncdjango/management/commands/cleanup_temporary_services.py
from django.core.management import BaseCommand
from ncdjango.geoprocessing.celery_tasks import cleanup_temporary_services




class Command(BaseCommand):
    help = 'Delete expired, temporary services created from geoprocessing results.'

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

        cleanup_temporary_services()