src/pyams_utils/progress.py
changeset 89 b32bcb6a311e
parent 72 9049384a2bd4
child 90 aa78027158d6
equal deleted inserted replaced
88:2baf5c2ba8b7 89:b32bcb6a311e
    27 
    27 
    28 
    28 
    29 _local = local()
    29 _local = local()
    30 
    30 
    31 
    31 
       
    32 PROGRESS_CACHE_NAME = 'PyAMS::progress'
       
    33 PROGRESS_LOCK_NAME = 'PyAMS::progress::lock'
       
    34 PROGRESS_TASKS_CACHE_KEY = 'PyAMS::progress::running_tasks'
       
    35 PROGRESS_TASK_KEY = 'PyAMS::progress::task::{0}'
       
    36 
       
    37 
    32 def get_tasks_cache():
    38 def get_tasks_cache():
    33     """Get cache storing tasks list"""
    39     """Get cache storing tasks list"""
    34     try:
    40     try:
    35         tasks_cache = _local.running_tasks_cache
    41         tasks_cache = _local.running_tasks_cache
    36     except AttributeError:
    42     except AttributeError:
    37         manager = cache.CacheManager(**cache.cache_regions['persistent'])
    43         manager = cache.CacheManager(**cache.cache_regions['persistent'])
    38         tasks_cache = _local.running_tasks_cache = manager.get_cache('PyAMS::progress')
    44         tasks_cache = _local.running_tasks_cache = manager.get_cache(PROGRESS_CACHE_NAME)
    39     return tasks_cache
    45     return tasks_cache
    40 
    46 
    41 
    47 
    42 def get_progress_cache():
    48 def get_progress_cache():
    43     """Get cache storing tasks progress"""
    49     """Get cache storing tasks progress"""
    44     try:
    50     try:
    45         local_cache = _local.progress_cache
    51         local_cache = _local.progress_cache
    46     except AttributeError:
    52     except AttributeError:
    47         manager = cache.CacheManager(**cache.cache_regions['default'])
    53         manager = cache.CacheManager(**cache.cache_regions['default'])
    48         local_cache = _local.progress_cache = manager.get_cache('PyAMS::progress')
    54         local_cache = _local.progress_cache = manager.get_cache(PROGRESS_CACHE_NAME)
    49     return local_cache
    55     return local_cache
    50 
    56 
    51 
    57 
    52 def get_running_tasks():
    58 def get_running_tasks():
    53     """Get list of running tasks"""
    59     """Get list of running tasks"""
    54     tasks_cache = get_tasks_cache()
    60     tasks_cache = get_tasks_cache()
    55     return tasks_cache.get_value('PyAMS_utils::running_tasks', createfunc=set)
    61     return tasks_cache.get_value(PROGRESS_TASKS_CACHE_KEY, createfunc=set)
    56 
    62 
    57 
    63 
    58 def set_running_tasks(tasks):
    64 def set_running_tasks(tasks):
    59     """Update list of running tasks"""
    65     """Update list of running tasks"""
    60     tasks_cache = get_tasks_cache()
    66     tasks_cache = get_tasks_cache()
    61     tasks_cache.set_value('PyAMS_utils::running_tasks', tasks)
    67     tasks_cache.set_value(PROGRESS_TASKS_CACHE_KEY, tasks)
    62 
    68 
    63 
    69 
    64 @locked(name='progress_status')
    70 @locked(name=PROGRESS_LOCK_NAME)
    65 def init_progress_status(progress_id, owner, label, tags=None, length=None, current=None):
    71 def init_progress_status(progress_id, owner, label, tags=None, length=None, current=None):
    66     """Initialize progress status for given task ID"""
    72     """Initialize progress status for given task ID"""
    67     status = {'status': 'running',
    73     status = {'status': 'running',
    68               'owner': owner,
    74               'owner': owner,
    69               'label': label,
    75               'label': label,
    70               'tags': tags,
    76               'tags': tags,
    71               'length': length,
    77               'length': length,
    72               'current': current,
    78               'current': current,
    73               'started': datetime.utcnow()}
    79               'started': datetime.utcnow()}
    74     # Store task status
    80     # Store task status
    75     cache_key = 'PyAMS_task::{0}'.format(progress_id)
    81     cache_key = PROGRESS_TASK_KEY.format(progress_id)
    76     progress_cache = get_progress_cache()
    82     progress_cache = get_progress_cache()
    77     progress_cache.set_value(cache_key, status)
    83     progress_cache.set_value(cache_key, status)
    78     # Store task in running tasks list
    84     # Store task in running tasks list
    79     tasks = get_running_tasks()
    85     tasks = get_running_tasks()
    80     tasks.add(progress_id)
    86     tasks.add(progress_id)
    81     set_running_tasks(tasks)
    87     set_running_tasks(tasks)
    82 
    88 
    83 
    89 
    84 @locked(name='progress_status')
    90 @locked(name=PROGRESS_LOCK_NAME)
    85 def get_progress_status(progress_id):
    91 def get_progress_status(progress_id):
    86     """Get status of given task"""
    92     """Get status of given task"""
    87     progress_cache = get_progress_cache()
    93     progress_cache = get_progress_cache()
    88     cache_key = 'PyAMS_task::{0}'.format(progress_id)
    94     cache_key = PROGRESS_TASK_KEY.format(progress_id)
    89     try:
    95     try:
    90         status = progress_cache.get_value(cache_key)
    96         status = progress_cache.get_value(cache_key)
    91     except KeyError:
    97     except KeyError:
    92         status = {'status': 'unknown'}
    98         status = {'status': 'unknown'}
    93     else:
    99     else:
    97             if progress_id in tasks:
   103             if progress_id in tasks:
    98                 tasks.remove(progress_id)
   104                 tasks.remove(progress_id)
    99     return status
   105     return status
   100 
   106 
   101 
   107 
   102 @locked(name='progress_status')
   108 @locked(name=PROGRESS_LOCK_NAME)
   103 def set_progress_status(progress_id, status='running', message=None, length=None, current=None):
   109 def set_progress_status(progress_id, status='running', message=None, length=None, current=None):
   104     """Set status of given task"""
   110     """Set status of given task"""
   105     progress_cache = get_progress_cache()
   111     progress_cache = get_progress_cache()
   106     cache_key = 'PyAMS_task::{0}'.format(progress_id)
   112     cache_key = PROGRESS_TASK_KEY.format(progress_id)
   107     try:
   113     try:
   108         task_status = progress_cache.get_value(cache_key)
   114         task_status = progress_cache.get_value(cache_key)
   109     except KeyError:
   115     except KeyError:
   110         task_status = {'status': 'unknown'}
   116         task_status = {'status': 'unknown'}
   111     task_status.update({'status': status,
   117     task_status.update({'status': status,