Skip to content

Commit d972d30

Browse files
committed
more documentation
1 parent d09e3f3 commit d972d30

File tree

7 files changed

+68
-11
lines changed

7 files changed

+68
-11
lines changed

docs/source/class.rst

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

66
.. autoclass:: pyres.ResQ
7+
:members:
8+
9+
10+
Job Classes
11+
=================
12+
13+
.. autoclass:: pyres.job.Job
14+
:members:
15+
16+
Worker Classes
17+
=================
18+
19+
.. autoclass:: pyres.worker.Worker
20+
:members:

docs/source/example.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
Let's take a real wold example of a blog where comments need to be checked for
66
spam check. When the comment is saved in the database, we create a job in the
7-
queue with comment data. Let's take a django model in this case.::
7+
queue with comment data. Let's take a django model in this case.
8+
9+
.. code-block:: python
10+
:linenos:
811
912
class Comment(models.Model):
1013
name = Model.CharField()

docs/source/install.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ Installation
33

44
Requirements:
55
--------------
6-
simplejson>=2.0.9
7-
itty>=0.6.2
8-
redis>=0.6.0
9-
pystache>=0.1.0
6+
simplejson>=2.0.9
7+
itty>=0.6.2
8+
redis>=0.6.0
9+
pystache>=0.1.0
1010

1111
Make sure you install these requirements before proceeding.
1212

docs/source/intro.rst

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ message queue. Read_ the blog post from github about how they use resque in
66
production.
77

88
:synopsis: Any job which takes a little while to run can be put on a message
9-
queue. Read our :doc:`Example </example>` implementation of how a PyRes can be used to spam
10-
check comments.
11-
12-
13-
9+
queue. Read our :doc:`Example </example>` implementation of how a PyRes can be used to spam check comments.
1410

1511

1612
.. _resque: http://github.com/defunkt/resque#readme

pyres/__init__.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ class ResQ(object):
5050
>>> r = ResQ(server="192.168.1.10:6379", password="some_pwd")
5151
# Assuming redis is running on default port with no password
5252
53-
53+
**r** is a resque object on which we can enqueue tasks.::
54+
55+
>>>> r.enqueue(SomeClass, args)
56+
57+
SomeClass can be any python class with *perform* method and a *queue*
58+
attribute on it.
5459
"""
5560
def __init__(self, server="localhost:6379", password=None,
5661
timeout=None, retry_connection=True):
@@ -109,6 +114,10 @@ def _set_redis(self, server):
109114
redis = property(_get_redis, _set_redis)
110115

111116
def enqueue(self, klass, *args):
117+
"""
118+
Enqueue a job into a specific queue. Make sure the class you are passing
119+
has **queue** attribute and a **perform** method on it.
120+
"""
112121
queue = getattr(klass,'queue', None)
113122
#print cls._res
114123
if queue:
@@ -123,6 +132,10 @@ def queues(self):
123132
return self.redis.smembers("resque:queues")
124133

125134
def info(self):
135+
"""
136+
Returns a dictionary of the current status of the pending jobs,
137+
processed, no. of queues, no. of workers, no. of failed jobs.
138+
"""
126139
pending = 0
127140
for q in self.queues():
128141
pending += self.size(q)
@@ -183,6 +196,9 @@ def _enqueue(cls, klass, *args):
183196
#Job.create(queue, klass,*args)
184197

185198
class Stat(object):
199+
"""
200+
A Stat class which shows the current status of the queue.
201+
"""
186202
def __init__(self, name, resq):
187203
self.name = name
188204
self.key = "resque:stat:%s" % self.name

pyres/job.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
from pyres import ResQ, str_to_class, safe_str_to_class
22
from pyres import failure
33
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).
7+
"""
48
def __init__(self, queue, payload, resq, worker=None):
59
self._queue = queue
610
self._payload = payload
711
self.resq = resq
812
self._worker = worker
913

1014
def perform(self):
15+
"""This method converts payload into args and calls the **perform** method
16+
on the payload class.
17+
"""
1118
payload_class_str = self._payload["class"]
1219
payload_class = safe_str_to_class(payload_class_str)
1320
args = self._payload.get("args", None)
@@ -24,6 +31,8 @@ def fail(self, exception):
2431

2532
@classmethod
2633
def reserve(cls, queue, res, worker=None):
34+
"""Reserve a job on the queue. In simple marking this job so that other worker
35+
will not pick it up"""
2736
payload = res.pop(queue)
2837
if payload:
2938
return cls(queue, payload, res, worker)

pyres/worker.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
import simplejson
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.::
14+
15+
>>> from pyres.worker import Worker
16+
>>> Worker.run([queue1, queue2], server="localhost:6379")
17+
"""
1118
def __init__(self, queues=[], server="localhost:6379", password=None):
1219
self.queues = queues
1320
self.validate_queues()
@@ -23,6 +30,7 @@ def __init__(self, queues=[], server="localhost:6379", password=None):
2330

2431

2532
def validate_queues(self):
33+
"Checks if a worker is given atleast one queue to work on."
2634
if not self.queues:
2735
raise NoQueueError("Please give each worker at least one queue.")
2836

@@ -82,6 +90,17 @@ def __str__(self):
8290
return '%s:%s:%s' % (hostname, self.pid, ','.join(self.queues))
8391

8492
def work(self, interval=5):
93+
"""Invoked by run() method. work() listens on a list of queues and sleeps
94+
for *interval* time.
95+
96+
default -- 5 secs
97+
98+
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
100+
work on that job.
101+
102+
Finally process() method actually processes the job.
103+
"""
85104
self.startup()
86105
while True:
87106
if self._shutdown:

0 commit comments

Comments
 (0)