2016-11-13 14:34:38 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
2018-02-27 11:39:27 +00:00
|
|
|
import datetime
|
|
|
|
|
2017-11-14 01:44:16 +00:00
|
|
|
from celery import shared_task
|
2018-12-18 03:29:21 +00:00
|
|
|
from celery.utils.log import get_task_logger
|
2018-02-27 11:39:27 +00:00
|
|
|
from django.utils import timezone
|
2018-12-18 03:29:21 +00:00
|
|
|
from django.conf import settings
|
|
|
|
from django.core.files.storage import default_storage
|
|
|
|
|
2018-02-27 11:39:27 +00:00
|
|
|
|
2019-01-15 02:23:30 +00:00
|
|
|
from ops.celery.decorator import (
|
|
|
|
register_as_period_task, after_app_ready_start, after_app_shutdown_clean_periodic
|
|
|
|
)
|
2018-12-18 03:29:21 +00:00
|
|
|
from .models import Status, Session, Command
|
2017-12-04 08:41:00 +00:00
|
|
|
|
2017-12-07 05:28:11 +00:00
|
|
|
|
2017-12-04 08:41:00 +00:00
|
|
|
CACHE_REFRESH_INTERVAL = 10
|
2017-12-04 12:15:47 +00:00
|
|
|
RUNNING = False
|
2018-12-18 03:29:21 +00:00
|
|
|
logger = get_task_logger(__name__)
|
2016-11-13 14:34:38 +00:00
|
|
|
|
2017-11-14 01:44:16 +00:00
|
|
|
|
|
|
|
@shared_task
|
2018-02-27 11:39:27 +00:00
|
|
|
@register_as_period_task(interval=3600)
|
|
|
|
@after_app_ready_start
|
2019-01-15 02:23:30 +00:00
|
|
|
@after_app_shutdown_clean_periodic
|
2018-02-27 11:39:27 +00:00
|
|
|
def delete_terminal_status_period():
|
2019-01-08 03:15:09 +00:00
|
|
|
yesterday = timezone.now() - datetime.timedelta(days=1)
|
2018-02-27 11:39:27 +00:00
|
|
|
Status.objects.filter(date_created__lt=yesterday).delete()
|
2017-12-04 08:41:00 +00:00
|
|
|
|
|
|
|
|
2018-03-06 09:05:36 +00:00
|
|
|
@shared_task
|
2019-01-15 02:23:30 +00:00
|
|
|
@register_as_period_task(interval=600)
|
2018-03-06 09:05:36 +00:00
|
|
|
@after_app_ready_start
|
2019-01-15 02:23:30 +00:00
|
|
|
@after_app_shutdown_clean_periodic
|
2018-03-06 09:05:36 +00:00
|
|
|
def clean_orphan_session():
|
|
|
|
active_sessions = Session.objects.filter(is_finished=False)
|
|
|
|
for session in active_sessions:
|
2019-02-19 04:50:33 +00:00
|
|
|
if session.is_active():
|
2019-01-15 02:23:30 +00:00
|
|
|
continue
|
|
|
|
session.is_finished = True
|
|
|
|
session.date_end = timezone.now()
|
|
|
|
session.save()
|
2018-12-18 03:29:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
@shared_task
|
|
|
|
@register_as_period_task(interval=3600*24)
|
|
|
|
@after_app_ready_start
|
2019-01-15 02:23:30 +00:00
|
|
|
@after_app_shutdown_clean_periodic
|
2018-12-18 03:29:21 +00:00
|
|
|
def clean_expired_session_period():
|
|
|
|
logger.info("Start clean expired session record, commands and replay")
|
|
|
|
days = settings.TERMINAL_SESSION_KEEP_DURATION
|
|
|
|
dt = timezone.now() - timezone.timedelta(days=days)
|
|
|
|
expired_sessions = Session.objects.filter(date_start__lt=dt)
|
|
|
|
for session in expired_sessions:
|
|
|
|
logger.info("Clean session: {}".format(session.id))
|
|
|
|
Command.objects.filter(session=str(session.id)).delete()
|
|
|
|
# 删除录像文件
|
|
|
|
session_path = session.get_rel_replay_path()
|
|
|
|
local_path = session.get_local_path()
|
|
|
|
local_path_v1 = session.get_local_path(version=1)
|
|
|
|
|
|
|
|
# 去default storage中查找
|
|
|
|
for _local_path in (local_path, local_path_v1, session_path):
|
|
|
|
if default_storage.exists(_local_path):
|
|
|
|
default_storage.delete(_local_path)
|
|
|
|
# 删除session记录
|
|
|
|
session.delete()
|
2019-01-15 02:23:30 +00:00
|
|
|
|