summaryrefslogtreecommitdiff
path: root/doc/source/index.rst
blob: fc7e74e1c9c3a7ad5118ae9915c3be6bc939a7a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
Gear: Asynchronous Event-Driven Gearman Interface
=================================================
.. module:: gear
   :synopsis: Asynchronous Event-Driven Gearman Interface

This module implements an asynchronous event-driven interface to
Gearman.  It provides interfaces to build a client or worker, and
access to the administrative protocol.  The design approach is to keep
it simple, with a relatively thin abstration of the Gearman protocol
itself.  It should be easy to use to build a client or worker that
operates either synchronously or asynchronously.

The module also provides a simple Gearman server for use as a
convenience in unit tests.  The server is not designed for production
use under load.

Client Example
--------------

To use the client interface, instantiate a
:py:class:`Client`, and submit a :py:class:`Job`.  For example::

    import gear
    client = gear.Client()
    client.addServer('gearman.example.com')
    client.waitForServer()  # Wait for at least one server to be connected

    job = gear.Job("reverse", "test string")
    client.submitJob(job)

The waitForServer() call is only necessary when running in a
synchronous context.  When running asynchronously, it is probably more
desirable to omit that call and instead handle the
:py:class:`NoConnectedServersError` exception that submitJob may
raise if no servers are connected at the time.

When Gearman returns data to the client, the :py:class:`Job` object is
updated immediately.  Event handlers are called on the
:py:class:`Client` object so that subclasses have ample facilities for
reacting to events synchronously.

Worker Example
--------------

To use the worker interface, create a :py:class:`Worker`, register at
least one function that the worker supports, and then wait for a Job
to be dispatched to the worker.

An example of a Gearman worker::

    import gear
    worker = gear.Worker('reverser')
    worker.addServer('gearman.example.com')
    worker.registerFunction("reverse")

    while True:
        job = worker.getJob()
        job.sendWorkComplete(job.arguments[::-1])

Server Example
--------------

You can run the Gearman server by executing the `geard` command.  For help
execute `geard --help`

.. include:: geard.rst

SSL Connections
---------------

For versions of Gearman supporting SSL connections, specify the
files containing the SSL private key, public certificate, and
CA certificate in the addServer() call. For example::

     ssl_key = '/path/to/key.pem'
     ssl_cert = '/path/to/cert.pem'
     ssl_ca = '/path/to/ca.pem'
     client.addServer('gearman.example.com', 4730, ssl_key, ssl_cert, ssl_ca)

All three files must be specified for SSL to be used.

API Reference
=============

The following sections document the module's public API.  It is
divided into sections focusing on implementing a client, a worker,
using the administrative protocol, and then the classes that are
common to all usages of the module.

Client Usage
------------

The classes in this section should be all that are needed in order to
implement a Gearman client.

Client Objects
^^^^^^^^^^^^^^
.. autoclass:: gear.Client
  :members:
  :inherited-members:

Job Objects
^^^^^^^^^^^
.. autoclass:: gear.Job
  :members:
  :inherited-members:


Worker Usage
------------

The classes in this section should be all that are needed in order to
implement a Gearman worker.

Worker Objects
^^^^^^^^^^^^^^
.. autoclass:: gear.Worker
  :members:
  :inherited-members:

FunctionRecord Objects
^^^^^^^^^^^^^^^^^^^^^^
.. autoclass:: gear.FunctionRecord
  :members:
  :inherited-members:

WorkerJob Objects
^^^^^^^^^^^^^^^^^
.. autoclass:: gear.WorkerJob
  :members:
  :inherited-members:

Administrative Protocol
-----------------------

Gearman provides an administrative protocol that is multiplexed on the
same connection as the normal binary protocol for jobs.  The classes
in this section are useful for working with that protocol.  They need
to be used with an existing :py:class:`Connection` object; either one
obtained via a :py:class:`Client` or :py:class:`Worker`, or via direct
instantiation of :py:class:`Connection` to a Gearman server.

AdminRequest Objects
^^^^^^^^^^^^^^^^^^^^
.. autoclass:: gear.AdminRequest
  :members:
  :inherited-members:

.. autoclass:: gear.StatusAdminRequest
  :inherited-members:

.. autoclass:: gear.ShowJobsAdminRequest
  :inherited-members:

.. autoclass:: gear.ShowUniqueJobsAdminRequest
  :inherited-members:

.. autoclass:: gear.CancelJobAdminRequest
  :inherited-members:

.. autoclass:: gear.VersionAdminRequest
  :inherited-members:


Server Usage
------------

Logging
^^^^^^^

To enable Gearman server logging you can setup a log configuration file and
pass it to `geard` (i.e. geard --log-config=logging.config)

Example logging.config::

  [loggers]
  keys=root,gear

  [handlers]
  keys=console,debug,info

  [formatters]
  keys=simple

  [logger_root]
  level=WARNING
  handlers=console

  [logger_gear]
  level=INFO
  handlers=debug,info
  qualname=gear

  [handler_console]
  level=WARNING
  class=StreamHandler
  formatter=simple
  args=(sys.stdout,)

  [handler_debug]
  level=DEBUG
  class=logging.handlers.TimedRotatingFileHandler
  formatter=simple
  args=('/var/log/gear/debug.log', 'midnight', 1, 30,)

  [handler_info]
  level=INFO
  class=logging.handlers.TimedRotatingFileHandler
  formatter=simple
  args=('/var/log/gear/info.log', 'midnight', 1, 30,)

  [formatter_simple]
  format=%(asctime)s %(levelname)s %(name)s: %(message)s
  datefmt=

ACL
^^^

The syntax of the optional ACL file consists of a number of sections
identified by the SSL certificate Common Name Subject, and the
arguments to the :py:class:`ACLEntry` constructor as key-value pairs::

  [<subject>]
  register=<regex>
  invoke=<regex>
  grant=<boolean>

For example::

  [my_worker]
  register=.*

  [my_client]
  invoke=.*

  [my_node_manager]
  grant=True

Server Objects
^^^^^^^^^^^^^^
.. autoclass:: gear.Server
  :members:
  :inherited-members:


Access Control
--------------

The gear server supports authorization via access control lists.  When
an :py:class:`ACL` object is supplied to the server (or a file on the
command line), gear changes from the normal Gearman mode of
allow-by-default to deny-by-default and only clients with ACL entries
will be able to perform actions such as registering functions or
submitting jobs.  Authorization is based on the SSL certificate Common
Name Subject associated with the connection.  An :py:class:`ACL`
object may be modified programatically at run-time.

The administrative protocol supports modifying ACLs with the following
commands:

**acl list**
  List the current acls::

    acl list
    client	register=None	invoke=.*	grant=True
    worker	register=.*	invoke=None	grant=True
    .

**acl grant <verb> <subject> <pattern>**
  Grant the `<verb>` action for functions matching `<pattern>` to
  `<subject>`.  Verbs can be one of ``register``, ``invoke``, or
  ``grant``.  This requires the current connection have the grant
  permission.  Example::

    acl grant register worker .*
    OK

**acl revoke <verb> <subject>**
  Revoke the `<verb>` action from `<subject>`.  Verbs can be one of
  ``register``, ``invoke``, ``grant``, or ``all`` to indicate all
  permissions for the subject should be revoked.  This requires the
  grant permission, except that a subject may always revoke its own
  permissions.  Example::

    acl revoke register worker
    OK

**acl self-revoke <verb>**
  Revoke the `<verb>` action from connections associted with the
  current certificate subject.  Verbs can be one of ``register``,
  ``invoke``, ``grant``, or ``all`` to indicate all permissions for
  the subject should be revoked.  This is similar to ``acl revoke``
  but is a convenience method so that a subject does not need to know
  its own common name.  A subject always has permission to revoke its
  own permissions.  Example::

    acl self-revoke register
    OK

ACL Objects
^^^^^^^^^^^
.. autoclass:: gear.ACL
  :members:
  :inherited-members:

ACLEntry Objects
^^^^^^^^^^^^^^^^
.. autoclass:: gear.ACLEntry
  :members:
  :inherited-members:


Common
------

These classes do not normally need to be directly instatiated to use
the gear API, but they may be returned or otherwise be accessible from
other classes in this module.  They generally operate at a lower
level, but still form part of the public API.

Connection Objects
^^^^^^^^^^^^^^^^^^
.. autoclass:: gear.Connection
  :members:
  :inherited-members:

Packet Objects
^^^^^^^^^^^^^^
.. autoclass:: gear.Packet
  :members:
  :inherited-members:

Exceptions
^^^^^^^^^^
.. autoexception:: gear.ConnectionError
.. autoexception:: gear.InvalidDataError
.. autoexception:: gear.ConfigurationError
.. autoexception:: gear.NoConnectedServersError
.. autoexception:: gear.UnknownJobError
.. autoexception:: gear.InterruptedError


Constants
---------

These constants are used by public API classes.

.. py:data:: PRECEDENCE_NORMAL

   Normal job precedence.

.. py:data:: PRECEDENCE_LOW

   Low job precedence.

.. py:data:: PRECEDENCE_HIGH

   High job precedence.

.. automodule:: gear.constants
  :members:


Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`