debuggers.hg

view tools/python/xen/xend/XendTaskManager.py @ 0:7d21f7218375

Exact replica of unstable on 051908 + README-this
author Mukesh Rathor
date Mon May 19 15:34:57 2008 -0700 (2008-05-19)
parents
children
line source
1 #===========================================================================
2 # This library is free software; you can redistribute it and/or
3 # modify it under the terms of version 2.1 of the GNU Lesser General Public
4 # License as published by the Free Software Foundation.
5 #
6 # This library is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 # Lesser General Public License for more details.
10 #
11 # You should have received a copy of the GNU Lesser General Public
12 # License along with this library; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 #============================================================================
15 # Copyright (C) 2007 XenSource Ltd
16 #============================================================================
18 """
19 Task Manager for Xen API asynchronous tasks.
21 Stores all tasks in a simple dictionary in module's own local storage to
22 avoid the 'instance()' methods.
24 Tasks are indexed by UUID.
26 """
28 from xen.xend.XendTask import XendTask
29 from xen.xend import uuid
30 import threading
32 tasks = {}
33 tasks_lock = threading.Lock()
35 def create_task(func, args, func_name, return_type, label, session):
36 """Creates a new Task and registers it with the XendTaskManager.
38 @param func: callable object XMLRPC method
39 @type func: callable object
40 @param args: tuple or list of arguments
41 @type args: tuple or list
42 @param func_name: XMLRPC method name, so we can estimate the progress
43 @type func_name: string
45 @return: Task UUID
46 @rtype: string.
47 """
48 task_uuid = uuid.createString()
49 try:
50 tasks_lock.acquire()
51 task = XendTask(task_uuid, func, args, func_name, return_type, label,
52 '', session)
53 tasks[task_uuid] = task
54 finally:
55 tasks_lock.release()
57 task.start()
59 return task_uuid
61 def destroy_task(task_uuid):
62 """Destroys a task.
64 @param task_uuid: Task UUID
65 @type task_uuid: string.
66 """
67 try:
68 tasks_lock.acquire()
69 if task_uuid in tasks:
70 del tasks[task_uuid]
71 finally:
72 tasks_lock.release()
74 def get_all_tasks():
75 """ Returns all the UUID of tracked tasks, completed or pending.
77 @returns: list of UUIDs
78 @rtype: list of strings
79 """
80 try:
81 tasks_lock.acquire()
82 return tasks.keys()
83 finally:
84 tasks_lock.release()
86 def get_task(task_uuid):
87 """ Retrieves a task by UUID.
89 @rtype: XendTask or None
90 @return: Task denoted by UUID.
91 """
92 try:
93 tasks_lock.acquire()
94 return tasks.get(task_uuid)
95 finally:
96 tasks_lock.release()
98 def get_tasks_by_name(task_name):
99 """ Retrieves a task by UUID.
101 @rtype: XendTask or None
102 @return: Task denoted by UUID.
103 """
104 try:
105 tasks_lock.acquire()
106 return [t.uuid for t in tasks if t.name_label == name]
107 finally:
108 tasks_lock.release()