当前位置: 首页>>代码示例>>Python>>正文


Python TaskHistory.update_status_for方法代码示例

本文整理汇总了Python中notification.models.TaskHistory.update_status_for方法的典型用法代码示例。如果您正苦于以下问题:Python TaskHistory.update_status_for方法的具体用法?Python TaskHistory.update_status_for怎么用?Python TaskHistory.update_status_for使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在notification.models.TaskHistory的用法示例。


在下文中一共展示了TaskHistory.update_status_for方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Command

# 需要导入模块: from notification.models import TaskHistory [as 别名]
# 或者: from notification.models.TaskHistory import update_status_for [as 别名]
class Command(BaseCommand):
    help = "Check if all Tasks with status running are in celery"

    option_list = BaseCommand.option_list + (
        make_option(
            "-n",
            "--celery_hosts",
            dest="celery_hosts",
            help="Number of celery hosts",
            type="int",
        ),
    )

    def __init__(self):
        super(Command, self).__init__()

        self.task = TaskHistory()
        self.task.task_id = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
        self.task.task_name = "sync_celery_tasks"
        self.task.relevance = TaskHistory.RELEVANCE_WARNING
        self.task.task_status = TaskHistory.STATUS_RUNNING
        self.task.context = {'hostname': socket.gethostname()}
        self.task.save()
        self.task.add_detail('Syncing metadata tasks with celery tasks')
        self.unique_tasks = [{
            'name': 'backup.tasks.make_databases_backup',
            'unique_key': 'makedatabasebackupkey'
        }]
        self._redis_conn = None


    @property
    def redis_conn(self):
        if not self._redis_conn:
            self._redis_conn = Redis(
                host=settings.REDIS_HOST,
                port=settings.REDIS_PORT,
                password=settings.REDIS_PASSWORD
            )
        return self._redis_conn

    def handle(self, *args, **kwargs):
        self.task.arguments = {'args': args, 'kwargs': kwargs}
        if not kwargs['celery_hosts']:
            raise CommandError("Please specified the --celery_hosts count")

        try:
            tasks_with_problem = self.check_tasks(kwargs['celery_hosts'])
        except CeleryActivesNodeError as celery_error:
            self.task.update_status_for(
                TaskHistory.STATUS_WARNING,
                'Could not check celery tasks.\n{}{}'.format(
                    full_stack(), celery_error
                )
            )
            return
        except Exception as e:
            self.task.update_status_for(
                TaskHistory.STATUS_ERROR,
                'Could not execute task.\n{}{}'.format(full_stack(), e)
            )
            return

        problems = len(tasks_with_problem)
        status = TaskHistory.STATUS_SUCCESS
        if problems > 0:
            status = TaskHistory.STATUS_WARNING
        self.task.update_status_for(status, 'Problems: {}'.format(problems))

        self.check_unique_keys()

    def check_unique_keys(self):
        for unique_task in self.unique_tasks:
            task_running = TaskHistory.objects.filter(
                task_status='RUNNING',
                task_name=unique_task['name']
            )
            if not task_running:
                unique_key = unique_task['unique_key']
                if unique_key in self.redis_conn.keys():
                    self.redis_conn.delete(unique_key)

    def check_tasks(self, celery_hosts):
        tasks_running = TaskHistory.objects.filter(
            task_status=TaskHistory.STATUS_RUNNING
        ).exclude(
            id=self.task.id
        )
        self.task.add_detail(
            "\nTasks with status running: {}\n".format(len(tasks_running))
        )

        celery_tasks = self.get_celery_active_tasks(celery_hosts)
        self.task.add_detail("Celery running: {}\n".format(len(celery_tasks)))

        tasks_with_problem = []
        self.task.add_detail("Checking tasks status")
        for task in tasks_running:
            self.task.add_detail(
                "{} - {}".format(task.task_id, task.task_name), level=1
#.........这里部分代码省略.........
开发者ID:globocom,项目名称:database-as-a-service,代码行数:103,代码来源:sync_celery.py


注:本文中的notification.models.TaskHistory.update_status_for方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。