|
1 # |
|
2 # Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net> |
|
3 # All Rights Reserved. |
|
4 # |
|
5 # This software is subject to the provisions of the Zope Public License, |
|
6 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. |
|
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED |
|
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
|
10 # FOR A PARTICULAR PURPOSE. |
|
11 # |
|
12 |
|
13 __docformat__ = 'restructuredtext' |
|
14 |
|
15 |
|
16 # import standard library |
|
17 from datetime import datetime |
|
18 from threading import local |
|
19 |
|
20 # import interfaces |
|
21 |
|
22 # import packages |
|
23 from beaker import cache |
|
24 from pyams_utils.lock import locked |
|
25 from pyramid.httpexceptions import HTTPBadRequest |
|
26 from pyramid.view import view_config |
|
27 |
|
28 |
|
29 _local = local() |
|
30 |
|
31 |
|
32 def get_tasks_cache(): |
|
33 """Get cache storing tasks list""" |
|
34 try: |
|
35 tasks_cache = _local.running_tasks_cache |
|
36 except AttributeError: |
|
37 manager = cache.CacheManager(**cache.cache_regions['persistent']) |
|
38 tasks_cache = _local.running_tasks_cache = manager.get_cache('PyAMS::progress') |
|
39 return tasks_cache |
|
40 |
|
41 |
|
42 def get_progress_cache(): |
|
43 """Get cache storing tasks progress""" |
|
44 try: |
|
45 local_cache = _local.progress_cache |
|
46 except AttributeError: |
|
47 manager = cache.CacheManager(**cache.cache_regions['default']) |
|
48 local_cache = _local.progress_cache = manager.get_cache('PyAMS::progress') |
|
49 return local_cache |
|
50 |
|
51 |
|
52 def get_running_tasks(): |
|
53 """Get list of running tasks""" |
|
54 tasks_cache = get_tasks_cache() |
|
55 return tasks_cache.get_value('PyAMS_utils::running_tasks', createfunc=set) |
|
56 |
|
57 |
|
58 def set_running_tasks(tasks): |
|
59 """Update list of running tasks""" |
|
60 tasks_cache = get_tasks_cache() |
|
61 tasks_cache.set_value('PyAMS_utils::running_tasks', tasks) |
|
62 |
|
63 |
|
64 @locked(name='progress_status') |
|
65 def init_progress_status(progress_id, owner, label, tags=None, length=None, current=None): |
|
66 """Initialize progress status for given task ID""" |
|
67 status = {'status': 'running', |
|
68 'owner': owner, |
|
69 'label': label, |
|
70 'tags': tags, |
|
71 'length': length, |
|
72 'current': current, |
|
73 'started': datetime.utcnow()} |
|
74 # Store task status |
|
75 cache_key = 'PyAMS_task::{0}'.format(progress_id) |
|
76 progress_cache = get_progress_cache() |
|
77 progress_cache.set_value(cache_key, status) |
|
78 # Store task in running tasks list |
|
79 tasks = get_running_tasks() |
|
80 tasks.add(progress_id) |
|
81 set_running_tasks(tasks) |
|
82 |
|
83 |
|
84 @locked(name='progress_status') |
|
85 def get_progress_status(progress_id): |
|
86 """Get status of given task""" |
|
87 progress_cache = get_progress_cache() |
|
88 cache_key = 'PyAMS_task::{0}'.format(progress_id) |
|
89 try: |
|
90 status = progress_cache.get_value(cache_key) |
|
91 except KeyError: |
|
92 status = {'status': 'unknown'} |
|
93 else: |
|
94 if status.get('status') == 'finished': |
|
95 progress_cache.remove_value(cache_key) |
|
96 tasks = get_running_tasks() |
|
97 if progress_id in tasks: |
|
98 tasks.remove(progress_id) |
|
99 return status |
|
100 |
|
101 |
|
102 @locked(name='progress_status') |
|
103 def set_progress_status(progress_id, status='running', message=None, length=None, current=None): |
|
104 """Set status of given task""" |
|
105 progress_cache = get_progress_cache() |
|
106 cache_key = 'PyAMS_task::{0}'.format(progress_id) |
|
107 try: |
|
108 task_status = progress_cache.get_value(cache_key) |
|
109 except KeyError: |
|
110 task_status = {'status': 'unknown'} |
|
111 task_status.update({'status': status, |
|
112 'message': message, |
|
113 'length': length, |
|
114 'current': current}) |
|
115 progress_cache.set_value(cache_key, task_status) |
|
116 |
|
117 |
|
118 @view_config(name='get_progress_status.json', renderer='JSON', xhr=True) |
|
119 def get_progress_status_view(request): |
|
120 """Get progress status""" |
|
121 if 'progress_id' not in request.params: |
|
122 raise HTTPBadRequest("Missing argument") |
|
123 return get_progress_status(request.params['progress_id']) |