Browse Source

perf: Add task description (#14033)

Co-authored-by: ZhaoJiSen <97007455+ZhaoJiSen@users.noreply.github.com>
pull/14110/head^2
fit2bot 2 months ago committed by GitHub
parent
commit
1417abecfb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 28
      apps/accounts/tasks/automation.py
  2. 10
      apps/accounts/tasks/backup_account.py
  3. 6
      apps/accounts/tasks/gather_accounts.py
  4. 10
      apps/accounts/tasks/push_account.py
  5. 23
      apps/accounts/tasks/remove_account.py
  6. 8
      apps/accounts/tasks/template.py
  7. 10
      apps/accounts/tasks/vault.py
  8. 10
      apps/accounts/tasks/verify_account.py
  9. 6
      apps/assets/tasks/automation.py
  10. 11
      apps/assets/tasks/gather_facts.py
  11. 21
      apps/assets/tasks/nodes_amount.py
  12. 11
      apps/assets/tasks/ping.py
  13. 14
      apps/assets/tasks/ping_gateway.py
  14. 26
      apps/audits/tasks.py
  15. 10
      apps/authentication/tasks.py
  16. 31
      apps/common/tasks.py
  17. 10
      apps/common/utils/verify_code.py
  18. 754
      apps/i18n/core/zh/LC_MESSAGES/django.po
  19. 3
      apps/i18n/lina/en.json
  20. 3
      apps/i18n/lina/zh.json
  21. 10
      apps/notifications/notifications.py
  22. 1
      apps/ops/models/celery.py
  23. 79
      apps/ops/tasks.py
  24. 5
      apps/orgs/tasks.py
  25. 24
      apps/perms/tasks.py
  26. 19
      apps/settings/tasks/ldap.py
  27. 68
      apps/terminal/tasks.py
  28. 10
      apps/users/signal_handlers.py
  29. 56
      apps/users/tasks.py

28
apps/accounts/tasks/automation.py

@ -28,8 +28,16 @@ def task_activity_callback(self, pid, trigger, tp, *args, **kwargs):
@shared_task(
queue='ansible', verbose_name=_('Account execute automation'),
activity_callback=task_activity_callback
queue='ansible',
verbose_name=_('Account execute automation'),
activity_callback=task_activity_callback,
description=_(
"""
Unified execution entry for account automation tasks: when the system performs tasks
such as account push, password change, account verification, account collection,
and gateway account verification, all tasks are executed through this unified entry
"""
)
)
def execute_account_automation_task(pid, trigger, tp):
model = AutomationTypes.get_type_model(tp)
@ -54,8 +62,14 @@ def record_task_activity_callback(self, record_ids, *args, **kwargs):
@shared_task(
queue='ansible', verbose_name=_('Execute automation record'),
activity_callback=record_task_activity_callback
queue='ansible',
verbose_name=_('Execute automation record'),
activity_callback=record_task_activity_callback,
description=_(
"""
When manually executing password change records, this task is used
"""
)
)
def execute_automation_record_task(record_ids, tp):
from accounts.models import ChangeSecretRecord
@ -84,7 +98,11 @@ def execute_automation_record_task(record_ids, tp):
@shared_task(
verbose_name=_('Clean change secret and push record period'),
description=_('Clean change secret and push record period description')
description=_(
"""
Clean change secret and push record period description
"""
)
)
@register_as_period_task(crontab=CRONTAB_AT_AM_THREE)
def clean_change_secret_and_push_record_period():

10
apps/accounts/tasks/backup_account.py

@ -22,7 +22,15 @@ def task_activity_callback(self, pid, trigger, *args, **kwargs):
return resource_ids, org_id
@shared_task(verbose_name=_('Execute account backup plan'), activity_callback=task_activity_callback)
@shared_task(
verbose_name=_('Execute account backup plan'),
activity_callback=task_activity_callback,
description=_(
"""
When performing scheduled or manual account backups, this task is used
"""
)
)
def execute_account_backup_task(pid, trigger, **kwargs):
from accounts.models import AccountBackupAutomation
with tmp_to_root_org():

6
apps/accounts/tasks/gather_accounts.py

@ -26,8 +26,10 @@ def gather_asset_accounts_util(nodes, task_name):
@shared_task(
queue="ansible", verbose_name=_('Gather asset accounts'),
activity_callback=lambda self, node_ids, task_name=None, *args, **kwargs: (node_ids, None)
queue="ansible",
verbose_name=_('Gather asset accounts'),
activity_callback=lambda self, node_ids, task_name=None, *args, **kwargs: (node_ids, None),
description=_("Unused")
)
def gather_asset_accounts_task(node_ids, task_name=None):
if task_name is None:

10
apps/accounts/tasks/push_account.py

@ -12,8 +12,14 @@ __all__ = [
@shared_task(
queue="ansible", verbose_name=_('Push accounts to assets'),
activity_callback=lambda self, account_ids, *args, **kwargs: (account_ids, None)
queue="ansible",
verbose_name=_('Push accounts to assets'),
activity_callback=lambda self, account_ids, *args, **kwargs: (account_ids, None),
description=_(
"""
When creating or modifying an account requires account push, this task is executed
"""
)
)
def push_accounts_to_assets_task(account_ids, params=None):
from accounts.models import PushAccountAutomation

23
apps/accounts/tasks/remove_account.py

@ -21,8 +21,15 @@ __all__ = ['remove_accounts_task']
@shared_task(
queue="ansible", verbose_name=_('Remove account'),
activity_callback=lambda self, gather_account_ids, *args, **kwargs: (gather_account_ids, None)
queue="ansible",
verbose_name=_('Remove account'),
activity_callback=lambda self, gather_account_ids, *args, **kwargs: (gather_account_ids, None),
description=_(
"""
When clicking "Sync deletion" in 'Console - Gather Account - Gathered accounts' this
task will be executed
"""
)
)
def remove_accounts_task(gather_account_ids):
from accounts.models import GatheredAccount
@ -41,7 +48,17 @@ def remove_accounts_task(gather_account_ids):
quickstart_automation_by_snapshot(task_name, tp, task_snapshot)
@shared_task(verbose_name=_('Clean historical accounts'))
@shared_task(
verbose_name=_('Clean historical accounts'),
description=_(
"""
Each time an asset account is updated, a historical account is generated, so it is
necessary to clean up the asset account history. The system will clean up excess account
records at 2 a.m. daily based on the configuration in the "System settings - Features -
Account storage - Record limit
"""
)
)
@register_as_period_task(crontab=CRONTAB_AT_AM_TWO)
@tmp_to_root_org()
def clean_historical_accounts():

8
apps/accounts/tasks/template.py

@ -9,7 +9,13 @@ from orgs.utils import tmp_to_root_org, tmp_to_org
@shared_task(
verbose_name=_('Template sync info to related accounts'),
activity_callback=lambda self, template_id, *args, **kwargs: (template_id, None)
activity_callback=lambda self, template_id, *args, **kwargs: (template_id, None),
description=_(
"""
When clicking 'Sync new secret to accounts' in 'Console - Account - Templates -
Accounts' this task will be executed
"""
)
)
def template_sync_related_accounts(template_id, user_id=None):
from accounts.models import Account, AccountTemplate

10
apps/accounts/tasks/vault.py

@ -28,7 +28,15 @@ def sync_instance(instance):
return "succeeded", msg
@shared_task(verbose_name=_('Sync secret to vault'))
@shared_task(
verbose_name=_('Sync secret to vault'),
description=_(
"""
When clicking 'Sync' in 'System Settings - Features - Account Storage' this task will
be executed
"""
)
)
def sync_secret_to_vault():
if not vault_client.enabled:
# 这里不能判断 settings.VAULT_ENABLED, 必须判断当前 vault_client 的类型

10
apps/accounts/tasks/verify_account.py

@ -46,8 +46,14 @@ def verify_accounts_connectivity_util(accounts, task_name):
@shared_task(
queue="ansible", verbose_name=_('Verify asset account availability'),
activity_callback=lambda self, account_ids, *args, **kwargs: (account_ids, None)
queue="ansible",
verbose_name=_('Verify asset account availability'),
activity_callback=lambda self, account_ids, *args, **kwargs: (account_ids, None),
description=_(
"""
When clicking 'Test' in 'Console - Asset details - Accounts' this task will be executed
"""
)
)
def verify_accounts_connectivity_task(account_ids):
from accounts.models import Account, VerifyAccountAutomation

6
apps/assets/tasks/automation.py

@ -21,8 +21,10 @@ def task_activity_callback(self, pid, trigger, tp, *args, **kwargs):
@shared_task(
queue='ansible', verbose_name=_('Asset execute automation'),
activity_callback=task_activity_callback
queue='ansible',
verbose_name=_('Asset execute automation'),
activity_callback=task_activity_callback,
description=_("Unused")
)
def execute_asset_automation_task(pid, trigger, tp):
model = AutomationTypes.get_type_model(tp)

11
apps/assets/tasks/gather_facts.py

@ -18,8 +18,15 @@ __all__ = [
@shared_task(
queue="ansible", verbose_name=_('Gather assets facts'),
activity_callback=lambda self, asset_ids, org_id, *args, **kwargs: (asset_ids, org_id)
queue="ansible",
verbose_name=_('Gather assets facts'),
activity_callback=lambda self, asset_ids, org_id, *args, **kwargs: (asset_ids, org_id),
description=_(
"""
When clicking 'Refresh hardware info' in 'Console - Asset Details - Basic' this task
will be executed
"""
)
)
def gather_assets_facts_task(asset_ids, org_id, task_name=None):
from assets.models import GatherFactsAutomation

21
apps/assets/tasks/nodes_amount.py

@ -13,7 +13,16 @@ from common.const.crontab import CRONTAB_AT_AM_TWO
logger = get_logger(__file__)
@shared_task(verbose_name=_('Check the amount of assets under the node'))
@shared_task(
verbose_name=_('Check the amount of assets under the node'),
description=_(
"""
Manually verifying asset quantities updates the asset count for nodes under the
current organization. This task will be called in the following two cases: when updating
nodes and when the number of nodes exceeds 100
"""
)
)
def check_node_assets_amount_task(org_id=None):
if org_id is None:
orgs = Organization.objects.all()
@ -30,7 +39,15 @@ def check_node_assets_amount_task(org_id=None):
logger.error(error)
@shared_task(verbose_name=_('Periodic check the amount of assets under the node'))
@shared_task(
verbose_name=_('Periodic check the amount of assets under the node'),
description=_(
"""
Schedule the check_node_assets_amount_task to periodically update the asset count of
all nodes under all organizations
"""
)
)
@register_as_period_task(crontab=CRONTAB_AT_AM_TWO)
def check_node_assets_amount_period_task():
check_node_assets_amount_task()

11
apps/assets/tasks/ping.py

@ -17,8 +17,15 @@ __all__ = [
@shared_task(
verbose_name=_('Test assets connectivity'), queue='ansible',
activity_callback=lambda self, asset_ids, org_id, *args, **kwargs: (asset_ids, org_id)
verbose_name=_('Test assets connectivity'),
queue='ansible',
activity_callback=lambda self, asset_ids, org_id, *args, **kwargs: (asset_ids, org_id),
description=_(
"""
When clicking 'Test Asset Connectivity' in 'Asset Details - Basic Settings' this task
will be executed
"""
)
)
def test_assets_connectivity_task(asset_ids, org_id, task_name=None):
from assets.models import PingAutomation

14
apps/assets/tasks/ping_gateway.py

@ -16,8 +16,15 @@ __all__ = [
@shared_task(
verbose_name=_('Test gateways connectivity'), queue='ansible',
activity_callback=lambda self, asset_ids, org_id, *args, **kwargs: (asset_ids, org_id)
verbose_name=_('Test gateways connectivity'),
queue='ansible',
activity_callback=lambda self, asset_ids, org_id, *args, **kwargs: (asset_ids, org_id),
description=_(
"""
When clicking 'Test Connection' in 'Domain Details - Gateway' this task will be
executed
"""
)
)
def test_gateways_connectivity_task(asset_ids, org_id, local_port, task_name=None):
from assets.models import PingAutomation
@ -33,4 +40,5 @@ def test_gateways_connectivity_task(asset_ids, org_id, local_port, task_name=Non
def test_gateways_connectivity_manual(gateway_ids, local_port):
task_name = gettext_noop("Test gateways connectivity")
gateway_ids = [str(i) for i in gateway_ids]
return test_gateways_connectivity_task.delay(gateway_ids, str(current_org.id), local_port, task_name)
return test_gateways_connectivity_task.delay(gateway_ids, str(current_org.id), local_port,
task_name)

26
apps/audits/tasks.py

@ -128,7 +128,21 @@ def clean_expired_session_period():
logger.info("Clean session replay done")
@shared_task(verbose_name=_('Clean audits session task log'))
@shared_task(
verbose_name=_('Clean audits session task log'),
description=_(
"""
Since the system generates login logs, operation logs, file upload logs, activity
logs, Celery execution logs, session recordings, and command records, as well as password
change logs, it will clean up these records based on the periodic cleanup configuration
in "System Settings - Tasks" The system will clean up login logs, task logs, operation
logs, password change logs, upload and download logs, session logs, activity records,
job center execution history logs, and cloud synchronization records that exceed the set
time limit every day at 2 a.m., according to the periodic cleanup configuration in
'System Settings - Tasks'
"""
)
)
@register_as_period_task(crontab=CRONTAB_AT_AM_TWO)
def clean_audits_log_period():
print("Start clean audit session task log")
@ -142,7 +156,15 @@ def clean_audits_log_period():
clean_password_change_log_period()
@shared_task(verbose_name=_('Upload FTP file to external storage'))
@shared_task(
verbose_name=_('Upload FTP file to external storage'),
description=_(
"""
If SERVER_REPLAY_STORAGE is configured, files uploaded through file management will be
synchronized to external storage
"""
)
)
def upload_ftp_file_to_external_storage(ftp_log_id, file_name):
logger.info(f'Start upload FTP file record to external storage: {ftp_log_id} - {file_name}')
ftp_log = FTPLog.objects.filter(id=ftp_log_id).first()

10
apps/authentication/tasks.py

@ -8,7 +8,15 @@ from django.utils import timezone
from django.utils.translation import gettext_lazy as _
@shared_task(verbose_name=_('Clean expired session'))
@shared_task(
verbose_name=_('Clean expired session'),
description=_(
"""
Since user logins create sessions, the system will clean up expired sessions every 24
hours
"""
)
)
@register_as_period_task(interval=3600 * 24)
def clean_django_sessions():
Session.objects.filter(expire_date__lt=timezone.now()).delete()

31
apps/common/tasks.py

@ -28,7 +28,15 @@ def task_activity_callback(self, subject, message, recipient_list, *args, **kwar
return resource_ids,
@shared_task(verbose_name=_("Send email"), activity_callback=task_activity_callback)
@shared_task(
verbose_name=_("Send email"),
activity_callback=task_activity_callback,
description=_(
"""
This task will be executed when sending email notifications
"""
)
)
def send_mail_async(*args, **kwargs):
""" Using celery to send email async
@ -55,7 +63,16 @@ def send_mail_async(*args, **kwargs):
logger.error("Sending mail error: {}".format(e))
@shared_task(verbose_name=_("Send email attachment"), activity_callback=task_activity_callback)
@shared_task(
verbose_name=_("Send email attachment"),
activity_callback=task_activity_callback,
description=_(
"""
When an account password is changed or an account backup generates attachments,
this task needs to be executed for sending emails and handling attachments
"""
)
)
def send_mail_attachment_async(subject, message, recipient_list, attachment_list=None):
if attachment_list is None:
attachment_list = []
@ -77,7 +94,15 @@ def send_mail_attachment_async(subject, message, recipient_list, attachment_list
logger.error("Sending mail attachment error: {}".format(e))
@shared_task(verbose_name=_('Upload session replay to external storage'))
@shared_task(
verbose_name=_('Upload account backup to external storage'),
description=_(
"""
When performing an account backup, this task needs to be executed to external storage
(SFTP)
"""
)
)
def upload_backup_to_obj_storage(recipient, upload_file):
logger.info(f'Start upload file : {upload_file}')
remote_path = os.path.join('account_backup', os.path.basename(upload_file))

10
apps/common/utils/verify_code.py

@ -13,7 +13,15 @@ from common.utils.random import random_string
logger = get_logger(__file__)
@shared_task(verbose_name=_('Send SMS code'))
@shared_task(
verbose_name=_('Send SMS code'),
description=_(
"""
When resetting a password, forgetting a password, or verifying MFA, this task needs to
be executed to send SMS messages
"""
)
)
def send_sms_async(target, code):
SMS().send_verify_code(target, code)

754
apps/i18n/core/zh/LC_MESSAGES/django.po

File diff suppressed because it is too large Load Diff

3
apps/i18n/lina/en.json

@ -1396,5 +1396,6 @@
"ZoneUpdate": "Update the zone",
"disallowSelfUpdateFields": "Not allowed to modify the current fields yourself",
"forceEnableMFAHelpText": "If force enable, user can not disable by themselves",
"removeWarningMsg": "Are you sure you want to remove"
"removeWarningMsg": "Are you sure you want to remove",
"TaskPath": "Task path"
}

3
apps/i18n/lina/zh.json

@ -1399,5 +1399,6 @@
"ZoneUpdate": "更新网域",
"disallowSelfUpdateFields": "不允许自己修改当前字段",
"forceEnableMFAHelpText": "如果强制启用,用户无法自行禁用",
"removeWarningMsg": "你确定要移除"
"removeWarningMsg": "你确定要移除",
"TaskPath": "任务路径"
}

10
apps/notifications/notifications.py

@ -43,7 +43,15 @@ class MessageType(type):
return clz
@shared_task(verbose_name=_('Publish the station message'))
@shared_task(
verbose_name=_('Publish the station message'),
description=_(
"""
This task needs to be executed for sending internal messages for system alerts,
work orders, and other notifications
"""
)
)
def publish_task(receive_user_ids, backends_msg_mapper):
Message.send_msg(receive_user_ids, backends_msg_mapper)

1
apps/ops/models/celery.py

@ -23,6 +23,7 @@ class CeleryTask(models.Model):
task = app.tasks.get(self.name, None)
return {
"comment": getattr(task, 'verbose_name', None),
"description": getattr(task, 'description', None),
"queue": getattr(task, 'queue', 'default')
}

79
apps/ops/tasks.py

@ -47,8 +47,15 @@ def _run_ops_job_execution(execution):
@shared_task(
soft_time_limit=60, queue="ansible", verbose_name=_("Run ansible task"),
activity_callback=job_task_activity_callback
soft_time_limit=60,
queue="ansible",
verbose_name=_("Run ansible task"),
activity_callback=job_task_activity_callback,
description=_(
"""
Execute scheduled adhoc and playbooks, periodically invoking the task for execution
"""
)
)
def run_ops_job(job_id):
with tmp_to_root_org():
@ -73,8 +80,15 @@ def job_execution_task_activity_callback(self, execution_id, *args, **kwargs):
@shared_task(
soft_time_limit=60, queue="ansible", verbose_name=_("Run ansible task execution"),
activity_callback=job_execution_task_activity_callback
soft_time_limit=60,
queue="ansible",
verbose_name=_("Run ansible task execution"),
activity_callback=job_execution_task_activity_callback,
description=_(
"""
Execute the task when manually adhoc or playbooks
"""
)
)
def run_ops_job_execution(execution_id, **kwargs):
with tmp_to_root_org():
@ -86,7 +100,14 @@ def run_ops_job_execution(execution_id, **kwargs):
_run_ops_job_execution(execution)
@shared_task(verbose_name=_('Clear celery periodic tasks'))
@shared_task(
verbose_name=_('Clear celery periodic tasks'),
description=_(
"""
At system startup, clean up celery tasks that no longer exist
"""
)
)
@after_app_ready_start
def clean_celery_periodic_tasks():
"""清除celery定时任务"""
@ -107,7 +128,16 @@ def clean_celery_periodic_tasks():
logger.info('Clean task failure: {}'.format(task))
@shared_task(verbose_name=_('Create or update periodic tasks'))
@shared_task(
verbose_name=_('Create or update periodic tasks'),
description=_(
"""
With version iterations, new tasks may be added, or task names and execution times may
be modified. Therefore, upon system startup, tasks will be registered or the parameters
of scheduled tasks will be updated
"""
)
)
@after_app_ready_start
def create_or_update_registered_periodic_tasks():
from .celery.decorator import get_register_period_tasks
@ -115,20 +145,48 @@ def create_or_update_registered_periodic_tasks():
create_or_update_celery_periodic_tasks(task)
@shared_task(verbose_name=_("Periodic check service performance"))
@shared_task(
verbose_name=_("Periodic check service performance"),
description=_(
"""
Check every hour whether each component is offline and whether the CPU, memory,
and disk usage exceed the thresholds, and send an alert message to the administrator
"""
)
)
@register_as_period_task(interval=3600)
def check_server_performance_period():
ServerPerformanceCheckUtil().check_and_publish()
@shared_task(verbose_name=_("Clean up unexpected jobs"))
@shared_task(
verbose_name=_("Clean up unexpected jobs"),
description=_(
"""
Due to exceptions caused by executing adhoc and playbooks in the Job Center,
which result in the task status not being updated, the system will clean up abnormal jobs
that have not been completed for more than 3 hours every hour and mark these tasks as
failed
"""
)
)
@register_as_period_task(interval=3600)
def clean_up_unexpected_jobs():
with tmp_to_root_org():
JobExecution.clean_unexpected_execution()
@shared_task(verbose_name=_('Clean job_execution db record'))
@shared_task(
verbose_name=_('Clean job_execution db record'),
description=_(
"""
Due to the execution of adhoc and playbooks in the Job Center, execution records will
be generated. The system will clean up records that exceed the retention period every day
at 2 a.m., based on the configuration of 'System Settings - Tasks - Regular clean-up -
Job execution retention days'
"""
)
)
@register_as_period_task(crontab=CRONTAB_AT_AM_TWO)
def clean_job_execution_period():
logger.info("Start clean job_execution db record")
@ -137,7 +195,8 @@ def clean_job_execution_period():
expired_day = now - datetime.timedelta(days=days)
with tmp_to_root_org():
del_res = JobExecution.objects.filter(date_created__lt=expired_day).delete()
logger.info(f"clean job_execution db record success! delete {days} days {del_res[0]} records")
logger.info(
f"clean job_execution db record success! delete {days} days {del_res[0]} records")
# 测试使用,注释隐藏
# @shared_task

5
apps/orgs/tasks.py

@ -6,7 +6,10 @@ from common.utils import get_logger
logger = get_logger(__file__)
@shared_task(verbose_name=_("Refresh organization cache"))
@shared_task(
verbose_name=_("Refresh organization cache"),
description=_("Unused")
)
def refresh_org_cache_task(*fields):
from .caches import OrgResourceStatisticsCache
OrgResourceStatisticsCache.refresh(*fields)

24
apps/perms/tasks.py

@ -24,7 +24,17 @@ from perms.utils import UserPermTreeExpireUtil
logger = get_logger(__file__)
@shared_task(verbose_name=_('Check asset permission expired'))
@shared_task(
verbose_name=_('Check asset permission expired'),
description=_(
"""
The cache of organizational collections, which have completed user authorization tree
construction, will expire. Therefore, expired collections need to be cleared from the
cache, and this task will be executed periodically based on the time interval specified
by PERM_EXPIRED_CHECK_PERIODIC in the system configuration file config.txt
"""
)
)
@register_as_period_task(interval=settings.PERM_EXPIRED_CHECK_PERIODIC)
@atomic()
@tmp_to_root_org()
@ -37,7 +47,17 @@ def check_asset_permission_expired():
UserPermTreeExpireUtil().expire_perm_tree_for_perms(perm_ids)
@shared_task(verbose_name=_('Send asset permission expired notification'))
@shared_task(
verbose_name=_('Send asset permission expired notification'),
description=_(
"""
Check every day at 10 a.m. and send a notification message to users associated with
assets whose authorization is about to expire, as well as to the organization's
administrators, 3 days in advance, to remind them that the asset authorization will
expire in a few days
"""
)
)
@register_as_period_task(crontab=CRONTAB_AT_AM_TEN)
@atomic()
@tmp_to_root_org()

19
apps/settings/tasks/ldap.py

@ -25,7 +25,14 @@ def sync_ldap_user():
LDAPSyncUtil().perform_sync()
@shared_task(verbose_name=_('Periodic import ldap user'))
@shared_task(
verbose_name=_('Periodic import ldap user'),
description=_(
"""
When LDAP auto-sync is configured, this task will be invoked to synchronize users
"""
)
)
def import_ldap_user():
start_time = time.time()
time_start_display = local_now_display()
@ -63,7 +70,15 @@ def import_ldap_user():
LDAPImportMessage(user, extra_kwargs).publish()
@shared_task(verbose_name=_('Registration periodic import ldap user task'))
@shared_task(
verbose_name=_('Registration periodic import ldap user task'),
description=_(
"""
When LDAP auto-sync parameters change, such as Crontab parameters, the LDAP sync task
will be re-registered or updated, and this task will be invoked
"""
)
)
@after_app_ready_start
def import_ldap_user_periodic(**kwargs):
task_name = 'import_ldap_user_periodic'

68
apps/terminal/tasks.py

@ -28,7 +28,10 @@ RUNNING = False
logger = get_task_logger(__name__)
@shared_task(verbose_name=_('Periodic delete terminal status'))
@shared_task(
verbose_name=_('Periodic delete terminal status'),
description=_("Unused")
)
@register_as_period_task(interval=3600)
@after_app_ready_start
def delete_terminal_status_period():
@ -36,7 +39,15 @@ def delete_terminal_status_period():
Status.objects.filter(date_created__lt=yesterday).delete()
@shared_task(verbose_name=_('Clean orphan session'))
@shared_task(
verbose_name=_('Clean orphan session'),
description=_(
"""
Check every 10 minutes for asset connection sessions that have been inactive for 3
minutes and mark these sessions as completed
"""
)
)
@register_as_period_task(interval=600)
@after_app_ready_start
@tmp_to_root_org()
@ -55,7 +66,15 @@ def clean_orphan_session():
session.save()
@shared_task(verbose_name=_('Upload session replay to external storage'))
@shared_task(
verbose_name=_('Upload session replay to external storage'),
description=_(
"""
If SERVER_REPLAY_STORAGE is configured in the config.txt, session commands and
recordings will be uploaded to external storage
"""
)
)
def upload_session_replay_to_external_storage(session_id):
logger.info(f'Start upload session to external storage: {session_id}')
session = Session.objects.filter(id=session_id).first()
@ -85,7 +104,13 @@ def upload_session_replay_to_external_storage(session_id):
@shared_task(
verbose_name=_('Run applet host deployment'),
activity_callback=lambda self, did, *args, **kwargs: ([did],)
activity_callback=lambda self, did, *args, **kwargs: ([did],),
description=_(
"""
When deploying from the remote application publisher details page, and the 'Deploy'
button is clicked, this task will be executed
"""
)
)
def run_applet_host_deployment(did, install_applets):
with tmp_to_builtin_org(system=1):
@ -95,7 +120,13 @@ def run_applet_host_deployment(did, install_applets):
@shared_task(
verbose_name=_('Install applet'),
activity_callback=lambda self, ids, applet_id, *args, **kwargs: (ids,)
activity_callback=lambda self, ids, applet_id, *args, **kwargs: (ids,),
description=_(
"""
When the 'Deploy' button is clicked in the 'Remote Application' section of the remote
application publisher details page, this task will be executed
"""
)
)
def run_applet_host_deployment_install_applet(ids, applet_id):
with tmp_to_builtin_org(system=1):
@ -106,7 +137,13 @@ def run_applet_host_deployment_install_applet(ids, applet_id):
@shared_task(
verbose_name=_('Uninstall applet'),
activity_callback=lambda self, ids, applet_id, *args, **kwargs: (ids,)
activity_callback=lambda self, ids, applet_id, *args, **kwargs: (ids,),
description=_(
"""
When the 'Uninstall' button is clicked in the 'Remote Application' section of the
remote application publisher details page, this task will be executed
"""
)
)
def run_applet_host_deployment_uninstall_applet(ids, applet_id):
with tmp_to_builtin_org(system=1):
@ -117,7 +154,13 @@ def run_applet_host_deployment_uninstall_applet(ids, applet_id):
@shared_task(
verbose_name=_('Generate applet host accounts'),
activity_callback=lambda self, host_id, *args, **kwargs: ([host_id],)
activity_callback=lambda self, host_id, *args, **kwargs: ([host_id],),
description=_(
"""
When a remote publishing server is created and an account needs to be created
automatically, this task will be executed
"""
)
)
def applet_host_generate_accounts(host_id):
applet_host = AppletHost.objects.filter(id=host_id).first()
@ -128,7 +171,16 @@ def applet_host_generate_accounts(host_id):
applet_host.generate_accounts()
@shared_task(verbose_name=_('Check command replay storage connectivity'))
@shared_task(
verbose_name=_('Check command replay storage connectivity'),
description=_(
"""
Check every day at midnight whether the external storage for commands and recordings
is accessible. If it is not accessible, send a notification to the recipients specified
in 'System Settings - Notifications - Subscription - Storage - Connectivity'
"""
)
)
@register_as_period_task(crontab='0 0 * * *')
@tmp_to_root_org()
def check_command_replay_storage_connectivity():

10
apps/users/signal_handlers.py

@ -190,7 +190,15 @@ def on_ldap_create_user(sender, user, ldap_user, **kwargs):
user.save()
@shared_task(verbose_name=_('Clean up expired user sessions'))
@shared_task(
verbose_name=_('Clean up expired user sessions'),
description=_(
"""
After logging in via the web, a user session record is created. At 2 a.m. every day,
the system cleans up inactive user devices
"""
)
)
@register_as_period_task(crontab=CRONTAB_AT_AM_TWO)
def clean_expired_user_session_period():
UserSession.clear_expired_sessions()

56
apps/users/tasks.py

@ -22,7 +22,15 @@ from .models import User
logger = get_logger(__file__)
@shared_task(verbose_name=_('Check password expired'))
@shared_task(
verbose_name=_('Check password expired'),
description=_(
"""
Check every day at 10 AM whether the passwords of users in the system are expired,
and send a notification 5 days in advance
"""
)
)
def check_password_expired():
users = User.get_nature_users().filter(source=User.Source.local)
for user in users:
@ -36,7 +44,16 @@ def check_password_expired():
PasswordExpirationReminderMsg(user).publish_async()
@shared_task(verbose_name=_('Periodic check password expired'))
@shared_task(
verbose_name=_('Periodic check password expired'),
description=_(
"""
With version iterations, new tasks may be added, or task names and execution times may
be modified. Therefore, upon system startup, it is necessary to register or update the
parameters of the task that checks if passwords have expired
"""
)
)
@after_app_ready_start
def check_password_expired_periodic():
tasks = {
@ -50,7 +67,15 @@ def check_password_expired_periodic():
create_or_update_celery_periodic_tasks(tasks)
@shared_task(verbose_name=_('Check user expired'))
@shared_task(
verbose_name=_('Check user expired'),
description=_(
"""
Check every day at 10 AM whether the users in the system are expired, and send a
notification 5 days in advance
"""
)
)
def check_user_expired():
date_expired_lt = timezone.now() + timezone.timedelta(days=User.DATE_EXPIRED_WARNING_DAYS)
users = User.get_nature_users() \
@ -67,7 +92,16 @@ def check_user_expired():
UserExpirationReminderMsg(user).publish_async()
@shared_task(verbose_name=_('Periodic check user expired'))
@shared_task(
verbose_name=_('Periodic check user expired'),
description=_(
"""
With version iterations, new tasks may be added, or task names and execution times may
be modified. Therefore, upon system startup, it is necessary to register or update the
parameters of the task that checks if users have expired
"""
)
)
@after_app_ready_start
def check_user_expired_periodic():
tasks = {
@ -81,7 +115,16 @@ def check_user_expired_periodic():
create_or_update_celery_periodic_tasks(tasks)
@shared_task(verbose_name=_('Check unused users'))
@shared_task(
verbose_name=_('Check unused users'),
description=_(
"""
At 2 a.m. every day, according to the configuration in "System Settings - Security -
Auth security - Auto disable threshold" users who have not logged in or whose API keys
have not been used for a long time will be disabled
"""
)
)
@register_as_period_task(crontab=CRONTAB_AT_PM_TWO)
@tmp_to_root_org()
def check_unused_users():
@ -96,7 +139,8 @@ def check_unused_users():
seconds_to_subtract = uncommon_users_ttl * 24 * 60 * 60
t = timezone.now() - timedelta(seconds=seconds_to_subtract)
last_login_q = Q(last_login__lte=t) | (Q(last_login__isnull=True) & Q(date_joined__lte=t))
api_key_q = Q(date_api_key_last_used__lte=t) | (Q(date_api_key_last_used__isnull=True) & Q(date_joined__lte=t))
api_key_q = Q(date_api_key_last_used__lte=t) | (
Q(date_api_key_last_used__isnull=True) & Q(date_joined__lte=t))
users = User.objects \
.filter(date_joined__lt=t) \

Loading…
Cancel
Save