Skip to content

Commit ad584f7

Browse files
author
aezell
committed
Cleanup of some docstrings. Added a Failures page.
Fixed some formatting and included failure classes in the docs.
1 parent c9559a6 commit ad584f7

File tree

10 files changed

+107
-40
lines changed

10 files changed

+107
-40
lines changed

docs/source/class.rst

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,25 @@ ResQ Classes
44
==========================================
55

66
.. autoclass:: pyres.ResQ
7-
:members:
8-
7+
:members:
98

109
Job Classes
1110
=================
1211

1312
.. autoclass:: pyres.job.Job
14-
:members:
13+
:members:
1514

1615
Worker Classes
1716
=================
1817

1918
.. autoclass:: pyres.worker.Worker
20-
:members:
19+
:members:
20+
21+
Failure Classes
22+
=================
23+
24+
.. autoclass:: pyres.failure.base.BaseBackend
25+
:members:
26+
27+
.. autoclass:: pyres.failure.RedisBackend
28+
:members:

docs/source/example.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ to do is add a :attr:`queue` attribute and define a :meth:`perform` method on th
3434

3535
To insert a job into the queue you need to do something like this::
3636

37-
from pyres import ResQ
38-
r = Resq()
39-
r.enqueue(Spam, 23) # Passing the comment id 23
37+
>>> from pyres import ResQ
38+
>>> r = Resq()
39+
>>> r.enqueue(Spam, 23) # Passing the comment id 23
4040

4141
This puts a job into the queue **Spam**. Now we need to fire off our workers.
4242
In the **scripts** folder there is an executable::

docs/source/index.rst

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ Welcome to pyres's documentation!
99
Contents:
1010

1111
.. toctree::
12-
:maxdepth: 2
12+
:maxdepth: 2
13+
14+
intro
15+
install
16+
example
17+
class
18+
tests
19+
failures
1320

14-
intro
15-
install
16-
example
17-
class
18-
tests
1921

2022
Indices and tables
2123
==================

pyres/__init__.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
import pyres.json_parser as json
55

66
def my_import(name):
7+
"""Helper function for walking import calls when searching for classes by string names."""
78
mod = __import__(name)
89
components = name.split('.')
910
for comp in components[1:]:
1011
mod = getattr(mod, comp)
1112
return mod
1213

1314
def safe_str_to_class(s):
15+
"""Helper function to map string class names to module classes."""
1416
lst = s.split(".")
1517
klass = lst[-1]
1618
mod_list = lst[:-1]
@@ -25,6 +27,7 @@ def safe_str_to_class(s):
2527
return None
2628

2729
def str_to_class(s):
30+
"""Alternate helper function to map string class names to module classes."""
2831
lst = s.split(".")
2932
klass = lst[-1]
3033
mod_list = lst[:-1]
@@ -39,8 +42,22 @@ def str_to_class(s):
3942
return None
4043

4144
class ResQ(object):
42-
"""ResQ class which defines the Queue object to enqueue jobs into various
43-
queues.
45+
"""The ResQ class defines the Redis server object to which we will
46+
enqueue jobs into various queues.
47+
48+
The ``__init__`` takes these keyword arguments:
49+
50+
``server`` -- IP address and port of the Redis server to which you want to connect. Default is `localhost:6379`.
51+
52+
``password`` -- The password, if required, of your Redis server. Default is "None".
53+
54+
``timeout`` -- The timeout keyword is in the signature, but is unused. Default is "None".
55+
56+
``retry_connection`` -- This keyword is in the signature but is deprecated. Default is "True".
57+
58+
59+
Both ``timeout`` and ``retry_connection`` will be removed as the python-redis client
60+
no longer uses them.
4461
4562
Example usage::
4663
@@ -52,8 +69,9 @@ class ResQ(object):
5269
5370
>>>> r.enqueue(SomeClass, args)
5471
55-
SomeClass can be any python class with *perform* method and a *queue*
72+
SomeClass can be any python class with a *perform* method and a *queue*
5673
attribute on it.
74+
5775
"""
5876
def __init__(self, server="localhost:6379", password=None,
5977
timeout=None, retry_connection=True):
@@ -110,9 +128,9 @@ def _set_redis(self, server):
110128
redis = property(_get_redis, _set_redis)
111129

112130
def enqueue(self, klass, *args):
113-
"""
114-
Enqueue a job into a specific queue. Make sure the class you are passing
131+
"""Enqueue a job into a specific queue. Make sure the class you are passing
115132
has **queue** attribute and a **perform** method on it.
133+
116134
"""
117135
queue = getattr(klass,'queue', None)
118136
#print cls._res
@@ -128,9 +146,9 @@ def queues(self):
128146
return self.redis.smembers("resque:queues") or []
129147

130148
def info(self):
131-
"""
132-
Returns a dictionary of the current status of the pending jobs,
149+
"""Returns a dictionary of the current status of the pending jobs,
133150
processed, no. of queues, no. of workers, no. of failed jobs.
151+
134152
"""
135153
pending = 0
136154
for q in self.queues():
@@ -170,8 +188,8 @@ def remove_queue(self, queue):
170188
del self.redis['resque:queue:%s' % queue]
171189

172190
def close(self):
173-
"""
174-
close the underlying redis connection
191+
"""Close the underlying redis connection.
192+
175193
"""
176194
self.redis.disconnect()
177195

@@ -198,8 +216,8 @@ def _enqueue(cls, klass, *args):
198216
#Job.create(queue, klass,*args)
199217

200218
class Stat(object):
201-
"""
202-
A Stat class which shows the current status of the queue.
219+
"""A Stat class which shows the current status of the queue.
220+
203221
"""
204222
def __init__(self, name, resq):
205223
self.name = name

pyres/extensions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from pyres.exceptions import NoQueueError
1010
from pyres.worker import Worker
11+
1112
class JuniorWorker(Worker):
1213
def work(self, interval=5):
1314
self.startup()

pyres/failure/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
from pyres import ResQ
33
import sys, traceback
44
from pyres.failure.redis import RedisBackend
5+
56
_backend = RedisBackend
7+
68
def create(*args, **kwargs):
79
return _backend(*args, **kwargs)
810

pyres/failure/base.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@
22
import traceback
33

44
class BaseBackend(object):
5+
"""Provides a base class that custom backends can subclass. Also provides basic
6+
traceback and message parsing.
7+
8+
The ``__init__`` takes these keyword arguments:
9+
10+
``exp`` -- The exception generated by your failure.
11+
12+
``queue`` -- The queue in which the ``Job`` was enqueued when it failed.
13+
14+
``payload`` -- The payload that was passed to the ``Job``.
15+
16+
``worker`` -- The worker that was processing the ``Job`` when it failed.
17+
18+
"""
519
def __init__(self, exp, queue, payload, worker=None):
620
excc, _, tb = sys.exc_info()
721

pyres/failure/redis.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import datetime
22
from base import BaseBackend
33
from pyres import ResQ
4+
45
class RedisBackend(BaseBackend):
6+
"""Extends the ``BaseBackend`` to provide a Redis backend for failed jobs."""
7+
58
def save(self, resq=None):
9+
"""Saves the failed Job into a "failed" Redis queue preserving all its original enqueud info."""
610
if not resq:
711
resq = ResQ()
812
data = {

pyres/job.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
from pyres import ResQ, str_to_class, safe_str_to_class
22
from pyres import failure
3+
34
class Job(object):
4-
"""
5-
Every job on the ResQ is a *Job* object which has queue and payload(all the
6-
args data and when its created etc).
5+
"""Every job on the ResQ is an instance of the *Job* class.
6+
7+
The ``__init__`` takes these keyword arguments:
8+
9+
``queue`` -- A string defining the queue to which this Job will be added.
10+
11+
``payload`` -- A dictionary which contains the string name of a class which extends this Job and
12+
a list of args which will be passed to that class.
13+
14+
``resq`` -- An instance of the ResQ class.
15+
16+
``worker`` -- The name of a specific worker if you'd like this Job to be done by that worker. Default is "None".
17+
718
"""
819
def __init__(self, queue, payload, resq, worker=None):
920
self._queue = queue
@@ -12,8 +23,9 @@ def __init__(self, queue, payload, resq, worker=None):
1223
self._worker = worker
1324

1425
def perform(self):
15-
"""This method converts payload into args and calls the **perform** method
26+
"""This method converts payload into args and calls the ``perform`` method
1627
on the payload class.
28+
1729
"""
1830
payload_class_str = self._payload["class"]
1931
payload_class = safe_str_to_class(payload_class_str)
@@ -25,15 +37,20 @@ def perform(self):
2537
return payload_class.perform()
2638

2739
def fail(self, exception):
28-
#Failure.create(exception)
40+
"""This method provides a way to fail a job and will use whatever failure backend
41+
you've provided. The default is the ``RedisBackend``.
42+
43+
"""
2944
fail = failure.create(exception, self._queue, self._payload, self._worker)
3045
fail.save(self.resq)
3146
return fail
3247

3348
@classmethod
3449
def reserve(cls, queue, res, worker=None):
35-
"""Reserve a job on the queue. In simple marking this job so that other worker
36-
will not pick it up"""
50+
"""Reserve a job on the queue. This marks this job so that other workers
51+
will not pick it up.
52+
53+
"""
3754
payload = res.pop(queue)
3855
if payload:
3956
return cls(queue, payload, res, worker)

pyres/worker.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
import json_parser as json
99

1010
class Worker(object):
11-
"""
12-
Defines a worker. The *pyres_worker* script instantiates this Worker class and
13-
pass a comma seperate list of queues to listen on.::
11+
"""Defines a worker. The ``pyres_worker`` script instantiates this Worker class and
12+
passes a comma-separated list of queues to listen on.::
1413
1514
>>> from pyres.worker import Worker
1615
>>> Worker.run([queue1, queue2], server="localhost:6379")
16+
1717
"""
1818
def __init__(self, queues=[], server="localhost:6379", password=None):
1919
self.queues = queues
@@ -30,7 +30,7 @@ def __init__(self, queues=[], server="localhost:6379", password=None):
3030

3131

3232
def validate_queues(self):
33-
"Checks if a worker is given atleast one queue to work on."
33+
"""Checks if a worker is given at least one queue to work on."""
3434
if not self.queues:
3535
raise NoQueueError("Please give each worker at least one queue.")
3636

@@ -90,16 +90,17 @@ def __str__(self):
9090
return '%s:%s:%s' % (hostname, self.pid, ','.join(self.queues))
9191

9292
def work(self, interval=5):
93-
"""Invoked by run() method. work() listens on a list of queues and sleeps
94-
for *interval* time.
93+
"""Invoked by ``run`` method. ``work`` listens on a list of queues and sleeps
94+
for ``interval`` time.
9595
96-
default -- 5 secs
96+
``interval`` -- Number of seconds the worker will wait until processing the next job. Default is "5".
9797
9898
Whenever a worker finds a job on the queue it first calls ``reserve`` on
99-
that job to make sure other worker won't run it, then *Forks* itself to
99+
that job to make sure another worker won't run it, then *forks* itself to
100100
work on that job.
101101
102-
Finally process() method actually processes the job.
102+
Finally, the ``process`` method actually processes the job by eventually calling the Job instance's ``perform`` method.
103+
103104
"""
104105
self.startup()
105106
while True:

0 commit comments

Comments
 (0)