| 1 |
Task management |
| 2 |
=============== |
| 3 |
|
| 4 |
.. highlight:: python |
| 5 |
|
| 6 |
.. _class-Task: |
| 7 |
|
| 8 |
Structure of Task |
| 9 |
----------------- |
| 10 |
|
| 11 |
A ClusterShell *Task* and its underlying *Engine* class are the fundamental |
| 12 |
infrastructure associated with a thread. An *Engine* implements an event |
| 13 |
processing loop that you use to schedule work and coordinate the receipt of |
| 14 |
incoming events. The purpose of this run loop is to keep your thread busy when |
| 15 |
there is work to do and put your thread to sleep when there is none. When |
| 16 |
calling the :meth:`.Task.resume()` or :meth:`.Task.run()` methods, your thread |
| 17 |
enters the Task Engine run loop and calls installed event handlers in response |
| 18 |
to incoming events. |
| 19 |
|
| 20 |
Using Task objects |
| 21 |
------------------ |
| 22 |
|
| 23 |
A *Task* object provides the main interface for adding shell commands, files |
| 24 |
to copy or timer and then running it. Every thread has a single *Task* object |
| 25 |
(and underlying *Engine* object) associated with it. The *Task* object is an |
| 26 |
instance of the :class:`.Task` class. |
| 27 |
|
| 28 |
|
| 29 |
Getting a Task object |
| 30 |
^^^^^^^^^^^^^^^^^^^^^ |
| 31 |
|
| 32 |
To get the *Task* object bound to the **current thread**, you use one of the following: |
| 33 |
|
| 34 |
* Use the :func:`.Task.task_self()` function available at the root of the Task |
| 35 |
module |
| 36 |
* or use ``task = Task()``; Task objects are only instantiated when needed. |
| 37 |
|
| 38 |
Example of getting the current task object:: |
| 39 |
|
| 40 |
>>> from ClusterShell.Task import task_self |
| 41 |
>>> task = task_self() |
| 42 |
|
| 43 |
So for a single-threaded application, a Task is a simple singleton (which |
| 44 |
instance is also available through :func:`.Task.task_self()`). |
| 45 |
|
| 46 |
To get the *Task* object associated to a specific thread *thr* (a |
| 47 |
``threading.Thread`` object), you use the following:: |
| 48 |
|
| 49 |
>>> from ClusterShell.Task import Task |
| 50 |
>>> task = Task(thread=thr) |
| 51 |
|
| 52 |
|
| 53 |
.. _class-Task-configure: |
| 54 |
|
| 55 |
Configuring the Task object |
| 56 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 57 |
|
| 58 |
Each *Task* provides an info dictionary that shares both internal |
| 59 |
*Task*-specific parameters and user-defined (key, value) parameters. Use the |
| 60 |
following :class:`.Task` class methods to get or set parameters: |
| 61 |
|
| 62 |
* :meth:`.Task.info` |
| 63 |
* :meth:`.Task.set_info` |
| 64 |
|
| 65 |
|
| 66 |
For example, to configure the task debugging behavior:: |
| 67 |
|
| 68 |
>>> task.set_info('debug', True) |
| 69 |
>>> task.info('debug') |
| 70 |
True |
| 71 |
|
| 72 |
You can also use the *Task* info dictionary to set your own *Task*-specific |
| 73 |
key, value pairs. You may use any free keys but only keys starting with |
| 74 |
*USER_* are guaranteed not to be used by ClusterShell in the future. |
| 75 |
|
| 76 |
Task info keys and their default values: |
| 77 |
|
| 78 |
+-----------------+----------------+------------------------------------+ |
| 79 |
| Info key string | Default value | Comment | |
| 80 |
+=================+================+====================================+ |
| 81 |
| debug | False | Enable debugging support (boolean) | |
| 82 |
+-----------------+----------------+------------------------------------+ |
| 83 |
| print_debug | internal using | Default is to print debug lines to | |
| 84 |
| | *print* | stdout using *print*. To override | |
| 85 |
| | | this behavior, set a function that | |
| 86 |
| | | takes two arguments (the task | |
| 87 |
| | | object and a string) as the value. | |
| 88 |
+-----------------+----------------+------------------------------------+ |
| 89 |
| fanout | 64 | Ssh *fanout* window (integer) | |
| 90 |
+-----------------+----------------+------------------------------------+ |
| 91 |
| connect_timeout | 10 | Value passed to ssh or pdsh | |
| 92 |
| | | (float) | |
| 93 |
+-----------------+----------------+------------------------------------+ |
| 94 |
| command_timeout | 0 (no timeout) | Value passed to ssh or pdsh | |
| 95 |
| | | (float) | |
| 96 |
+-----------------+----------------+------------------------------------+ |
| 97 |
|
| 98 |
Below is an example of ``print_debug`` override. As you can see, we set the |
| 99 |
function ``print_csdebug(task, s)`` as the value. When debugging is enabled, |
| 100 |
this function will be called for any debug text line. For example, this |
| 101 |
function searches for any known patterns and prints a modified debug line to |
| 102 |
stdout when found:: |
| 103 |
|
| 104 |
import re |
| 105 |
|
| 106 |
def print_csdebug(task, s): |
| 107 |
m = re.search(r"(\w+): SHINE:\d:(\w+):", s) |
| 108 |
if m: |
| 109 |
print("%s<pickle>" % m.group(0)) |
| 110 |
else: |
| 111 |
print(s) |
| 112 |
|
| 113 |
# Install the new debug printing function |
| 114 |
task_self().set_info("print_debug", print_csdebug) |
| 115 |
|
| 116 |
|
| 117 |
.. _taskshell: |
| 118 |
|
| 119 |
Submitting a shell command |
| 120 |
^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 121 |
|
| 122 |
You can submit a set of commands for local or distant execution in parallel |
| 123 |
with :meth:`.Task.shell`. |
| 124 |
|
| 125 |
Local usage:: |
| 126 |
|
| 127 |
task.shell(command [, key=key] [, handler=handler] [, timeout=secs]) |
| 128 |
|
| 129 |
Distant usage:: |
| 130 |
|
| 131 |
task.shell(command, nodes=nodeset [, handler=handler] [, timeout=secs]) |
| 132 |
|
| 133 |
This method makes use of the default local or distant worker. ClusterShell |
| 134 |
uses a default Worker based on the Python *subprocess* standard module to |
| 135 |
execute local commands, and a Worker based on *ssh* (Secure SHell) for distant |
| 136 |
commands. |
| 137 |
|
| 138 |
If the Task is not running, the command is scheduled for later execution. If |
| 139 |
the Task is currently running, the command is executed as soon as possible |
| 140 |
(depending on the current *fanout*). |
| 141 |
|
| 142 |
To set a per-worker (e.g. per-command) timeout value, just use the timeout |
| 143 |
parameter (in seconds), for example:: |
| 144 |
|
| 145 |
task.shell("uname -r", nodes=remote_nodes, handler=ehandler, timeout=5) |
| 146 |
|
| 147 |
This is the preferred way to specify a command timeout. When a worker times |
| 148 |
out, the :meth:`.EventHandler.ev_close` event is generated with its |
| 149 |
``timedout`` argument set to ``True`` to indicate that some nodes have timed |
| 150 |
out. You may then retrieve the nodes with |
| 151 |
:meth:`.DistantWorker.iter_keys_timeout()`. |
| 152 |
|
| 153 |
Submitting a file copy action |
| 154 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 155 |
|
| 156 |
Local file copy to distant nodes is supported. You can submit a copy action |
| 157 |
with :meth:`.Task.copy`:: |
| 158 |
|
| 159 |
task.copy(source, dest, nodes=nodeset [, handler=handler] [, timeout=secs]) |
| 160 |
|
| 161 |
This method makes use of the default distant copy worker which is based on scp |
| 162 |
(Secure CoPy) which comes with OpenSSH. |
| 163 |
|
| 164 |
If the Task is not running, the copy is scheduled for later execution. If the |
| 165 |
Task is currently running, the copy is started as soon as possible (depending |
| 166 |
on the current *fanout*). |
| 167 |
|
| 168 |
Starting the Task |
| 169 |
^^^^^^^^^^^^^^^^^ |
| 170 |
|
| 171 |
Before you run a Task, you must add at least one worker (shell command, file |
| 172 |
copy) or timer to it. If a Task does not have any worker to execute and |
| 173 |
monitor, it exits immediately when you try to run it with:: |
| 174 |
|
| 175 |
task.resume() |
| 176 |
|
| 177 |
At this time, all previously submitted commands will start in the associated |
| 178 |
Task thread. From a library user point of view, the task thread is blocked |
| 179 |
until the end of the command executions. |
| 180 |
|
| 181 |
Please note that the special method :meth:`.Task.run` does a |
| 182 |
:meth:`.Task.shell` and a :meth:`.Task.resume` in one call. |
| 183 |
|
| 184 |
To set a Task execution timeout, use the optional *timeout* parameter to set |
| 185 |
the timeout value in seconds. Once this time is elapsed when the Task is still |
| 186 |
running, the running Task raises ``TimeoutError`` exception, cleaning up all |
| 187 |
scheduled workers and timers in the process. Using such a timeout ensures that the |
| 188 |
Task will not exceed a given time for all its scheduled works. You can also |
| 189 |
configure per-worker timeout that generates an |
| 190 |
:meth:`.EventHandler.ev_close` event (with ``timedout`` set to ``True``) but |
| 191 |
will not raise an exception, allowing the Task to continue. Indeed, using a |
| 192 |
per-worker timeout is the preferred way for most applications. |
| 193 |
|
| 194 |
|
| 195 |
Getting Task results |
| 196 |
^^^^^^^^^^^^^^^^^^^^ |
| 197 |
|
| 198 |
After the task is finished (after :meth:`.Task.resume` or :meth:`.Task.run`) |
| 199 |
or after a worker is completed when you have previously defined an event |
| 200 |
handler (at :meth:`.EventHandler.ev_close`), you can use Task result getters: |
| 201 |
|
| 202 |
* :meth:`.Task.iter_buffers` |
| 203 |
* :meth:`.Task.iter_errors` |
| 204 |
* :meth:`.Task.node_buffer` |
| 205 |
* :meth:`.Task.node_error` |
| 206 |
* :meth:`.Task.max_retcode` |
| 207 |
* :meth:`.Task.num_timeout` |
| 208 |
* :meth:`.Task.iter_keys_timeout` |
| 209 |
|
| 210 |
Note: *buffer* refers to standard output, *error* to standard error. |
| 211 |
|
| 212 |
Please see some examples in :ref:`prog-examples`. |
| 213 |
|
| 214 |
|
| 215 |
Exiting the Task |
| 216 |
^^^^^^^^^^^^^^^^ |
| 217 |
|
| 218 |
If a Task does not have anymore scheduled worker or timer (for example, if you |
| 219 |
run one shell command and then it closes), it exits automatically from |
| 220 |
:meth:`.Task.resume`. Still, except from a signal handler, you can always call |
| 221 |
the following method to abort the Task execution: |
| 222 |
|
| 223 |
* :meth:`.Task.abort` |
| 224 |
|
| 225 |
For example, it is safe to call this method from an event handler within the |
| 226 |
task itself. On abort, all scheduled workers (shell command, file copy) and |
| 227 |
timers are cleaned and :meth:`.Task.resume` returns, unblocking the Task |
| 228 |
thread from a library user point of view. Please note that commands being |
| 229 |
executed remotely are not necessarily stopped (this is due to *ssh(1)* |
| 230 |
behavior). |
| 231 |
|
| 232 |
|
| 233 |
.. _configuring-a-timer: |
| 234 |
|
| 235 |
Configuring a Timer |
| 236 |
^^^^^^^^^^^^^^^^^^^ |
| 237 |
|
| 238 |
A timer is bound to a Task (and its underlying Engine) and fires at a preset |
| 239 |
time in the future. Timers can fire either only once or repeatedly at fixed |
| 240 |
time intervals. Repeating timers can also have their next firing time manually |
| 241 |
adjusted (see :meth:`.Task.timer`). |
| 242 |
|
| 243 |
A timer is not a real-time mechanism; it fires when the Task's underlying |
| 244 |
Engine to which the timer has been added is running and able to check if the |
| 245 |
timer firing time has passed. |
| 246 |
|
| 247 |
When a timer fires, the method :meth:`.EventHandler.ev_timer` of the |
| 248 |
associated EventHandler is called. |
| 249 |
|
| 250 |
To configure a timer, use the following (secs in seconds with floating point |
| 251 |
precision):: |
| 252 |
|
| 253 |
task.timer(fire=secs, handler=handler [, interval=secs]) |
| 254 |
|
| 255 |
|
| 256 |
.. _task-default-worker: |
| 257 |
|
| 258 |
Changing default worker |
| 259 |
^^^^^^^^^^^^^^^^^^^^^^^ |
| 260 |
|
| 261 |
When calling :meth:`.Task.shell` or :meth:`.Task.copy` the Task object creates |
| 262 |
a worker instance for each call. When the *nodes* argument is defined, the |
| 263 |
worker used for these calls is based on Task default *distant_workername* |
| 264 |
(default: ``ssh``). Change this value to use another worker, for example |
| 265 |
**rsh**:: |
| 266 |
|
| 267 |
from ClusterShell.Task import task_self |
| 268 |
|
| 269 |
task_self().set_default('distant_workername', 'rsh') |
| 270 |
|
| 271 |
|
| 272 |
Thread safety and Task objects |
| 273 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 274 |
|
| 275 |
|
| 276 |
ClusterShell is an event-based library and one of its advantages is to avoid |
| 277 |
the use of threads (and their safety issues), so it's mainly not thread-safe. |
| 278 |
When possible, avoid the use of threads with ClusterShell. However, it's |
| 279 |
sometimes not so easy, first because another library you want to use in some |
| 280 |
event handler is not event-based and may block the current thread (that's |
| 281 |
enough to break the deal). Also, in some cases, it could be useful for you to |
| 282 |
run several Tasks at the same time. Since version 1.1, ClusterShell provides |
| 283 |
support for launching a Task in another thread and some experimental support |
| 284 |
for multiple Tasks, but: |
| 285 |
|
| 286 |
* you should ensure that a Task is configured and accessed from one thread at |
| 287 |
a time before it's running (there is no API lock/mutex protection), |
| 288 |
* once the Task is running, you should modify it only from the same thread |
| 289 |
that owns that Task (for example, you cannot call :meth:`.Task.abort` from |
| 290 |
another thread). |
| 291 |
|
| 292 |
The library provides two thread-safe methods and a function for basic Task |
| 293 |
interactions: :meth:`.Task.wait`, :meth:`.Task.join` and |
| 294 |
:func:`.Task.task_wait` (function defined at the root of the Task module). |
| 295 |
Please refer to the API documentation. |
| 296 |
|
| 297 |
Configuring explicit Shell Worker objects |
| 298 |
----------------------------------------- |
| 299 |
|
| 300 |
We have seen in :ref:`taskshell` how to easily submit shell commands to the |
| 301 |
Task. The :meth:`.Task.shell` method returns an already scheduled Worker |
| 302 |
object. It is possible to instantiate the Worker object explicitly, for |
| 303 |
example:: |
| 304 |
|
| 305 |
from ClusterShell.Worker.Ssh import WorkerSsh |
| 306 |
|
| 307 |
worker = WorkerSsh('node3', handler=None, command="/bin/echo alright") |
| 308 |
|
| 309 |
To be used in a Task, add the worker to it with:: |
| 310 |
|
| 311 |
task.schedule(worker) |
| 312 |
|
| 313 |
If you have pdsh installed, you can use it by easily switching to the Pdsh |
| 314 |
worker, which should behave the same manner as the Ssh worker:: |
| 315 |
|
| 316 |
from ClusterShell.Worker.Pdsh import WorkerPdsh |
| 317 |
|
| 318 |
worker = WorkerPdsh('node3', handler=None, command="/bin/echo alright") |