You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

check_upgradable_plugin_task.py 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import math
  2. import time
  3. import click
  4. import app
  5. from extensions.ext_database import db
  6. from models.account import TenantPluginAutoUpgradeStrategy
  7. from tasks import process_tenant_plugin_autoupgrade_check_task as check_task
  8. AUTO_UPGRADE_MINIMAL_CHECKING_INTERVAL = 15 * 60 # 15 minutes
  9. MAX_CONCURRENT_CHECK_TASKS = 20
  10. @app.celery.task(queue="plugin")
  11. def check_upgradable_plugin_task():
  12. click.echo(click.style("Start check upgradable plugin.", fg="green"))
  13. start_at = time.perf_counter()
  14. now_seconds_of_day = time.time() % 86400 - 30 # we assume the tz is UTC
  15. click.echo(click.style(f"Now seconds of day: {now_seconds_of_day}", fg="green"))
  16. strategies = (
  17. db.session.query(TenantPluginAutoUpgradeStrategy)
  18. .where(
  19. TenantPluginAutoUpgradeStrategy.upgrade_time_of_day >= now_seconds_of_day,
  20. TenantPluginAutoUpgradeStrategy.upgrade_time_of_day
  21. < now_seconds_of_day + AUTO_UPGRADE_MINIMAL_CHECKING_INTERVAL,
  22. TenantPluginAutoUpgradeStrategy.strategy_setting
  23. != TenantPluginAutoUpgradeStrategy.StrategySetting.DISABLED,
  24. )
  25. .all()
  26. )
  27. total_strategies = len(strategies)
  28. click.echo(click.style(f"Total strategies: {total_strategies}", fg="green"))
  29. batch_chunk_count = math.ceil(
  30. total_strategies / MAX_CONCURRENT_CHECK_TASKS
  31. ) # make sure all strategies are checked in this interval
  32. batch_interval_time = (AUTO_UPGRADE_MINIMAL_CHECKING_INTERVAL / batch_chunk_count) if batch_chunk_count > 0 else 0
  33. for i in range(0, total_strategies, MAX_CONCURRENT_CHECK_TASKS):
  34. batch_strategies = strategies[i : i + MAX_CONCURRENT_CHECK_TASKS]
  35. for strategy in batch_strategies:
  36. check_task.process_tenant_plugin_autoupgrade_check_task.delay(
  37. strategy.tenant_id,
  38. strategy.strategy_setting,
  39. strategy.upgrade_time_of_day,
  40. strategy.upgrade_mode,
  41. strategy.exclude_plugins,
  42. strategy.include_plugins,
  43. )
  44. if batch_interval_time > 0.0001: # if lower than 1ms, skip
  45. time.sleep(batch_interval_time)
  46. end_at = time.perf_counter()
  47. click.echo(
  48. click.style(
  49. f"Checked upgradable plugin success latency: {end_at - start_at}",
  50. fg="green",
  51. )
  52. )