2017-03-15 16:43:43 +00:00
|
|
|
|
# ~*~ coding: utf-8 ~*~
|
|
|
|
|
from __future__ import absolute_import, unicode_literals
|
2020-09-27 08:02:44 +00:00
|
|
|
|
from datetime import timedelta
|
2017-03-15 16:43:43 +00:00
|
|
|
|
|
2020-09-29 05:34:55 +00:00
|
|
|
|
from django.db.transaction import atomic
|
2020-11-20 08:12:24 +00:00
|
|
|
|
from django.conf import settings
|
2017-03-15 16:43:43 +00:00
|
|
|
|
from celery import shared_task
|
2020-08-16 15:08:58 +00:00
|
|
|
|
from common.utils import get_logger
|
2020-09-29 05:34:55 +00:00
|
|
|
|
from common.utils.timezone import now, dt_formater, dt_parser
|
2020-11-20 08:12:24 +00:00
|
|
|
|
from ops.celery.decorator import register_as_period_task
|
2021-02-05 05:29:29 +00:00
|
|
|
|
from perms.models import AssetPermission
|
|
|
|
|
from perms.utils.asset.user_permission import UserGrantedTreeRefreshController
|
2017-03-15 16:43:43 +00:00
|
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
|
|
|
|
|
2020-11-20 08:12:24 +00:00
|
|
|
|
@register_as_period_task(interval=settings.PERM_EXPIRED_CHECK_PERIODIC)
|
2021-07-14 06:58:40 +00:00
|
|
|
|
@shared_task()
|
2020-09-29 05:34:55 +00:00
|
|
|
|
@atomic()
|
2020-09-27 08:02:44 +00:00
|
|
|
|
def check_asset_permission_expired():
|
|
|
|
|
"""
|
|
|
|
|
这里的任务要足够短,不要影响周期任务
|
|
|
|
|
"""
|
2020-09-29 05:34:55 +00:00
|
|
|
|
from settings.models import Setting
|
|
|
|
|
|
|
|
|
|
setting_name = 'last_asset_perm_expired_check'
|
|
|
|
|
|
2020-09-27 08:02:44 +00:00
|
|
|
|
end = now()
|
2020-09-29 05:34:55 +00:00
|
|
|
|
default_start = end - timedelta(days=36000) # Long long ago in china
|
|
|
|
|
|
|
|
|
|
defaults = {'value': dt_formater(default_start)}
|
|
|
|
|
setting, created = Setting.objects.get_or_create(
|
|
|
|
|
name=setting_name, defaults=defaults
|
|
|
|
|
)
|
|
|
|
|
if created:
|
|
|
|
|
start = default_start
|
|
|
|
|
else:
|
|
|
|
|
start = dt_parser(setting.value)
|
|
|
|
|
setting.value = dt_formater(end)
|
|
|
|
|
setting.save()
|
|
|
|
|
|
2021-02-05 05:29:29 +00:00
|
|
|
|
asset_perm_ids = AssetPermission.objects.filter(
|
2020-09-29 05:34:55 +00:00
|
|
|
|
date_expired__gte=start, date_expired__lte=end
|
2020-09-27 08:02:44 +00:00
|
|
|
|
).distinct().values_list('id', flat=True)
|
2021-02-05 05:29:29 +00:00
|
|
|
|
asset_perm_ids = list(asset_perm_ids)
|
|
|
|
|
logger.info(f'>>> checking {start} to {end} have {asset_perm_ids} expired')
|
|
|
|
|
UserGrantedTreeRefreshController.add_need_refresh_by_asset_perm_ids_cross_orgs(asset_perm_ids)
|