mirror of https://github.com/rq/rq.git
Add beginnings of a rqworker script.
This commit is contained in:
parent
d780c929c0
commit
a029e5437b
|
@ -0,0 +1,34 @@
|
|||
#!/usr/bin/env python
|
||||
import optparse
|
||||
from rq import use_redis, Queue, Worker
|
||||
|
||||
def parse_args():
|
||||
parser = optparse.OptionParser()
|
||||
parser.add_option('-b', '--burst', dest='burst',
|
||||
action='store_true', default=False,
|
||||
help='Run in burst mode (quit after all work is done).')
|
||||
parser.add_option('-n', '--name', dest='name',
|
||||
action='store', type='string', default=None,
|
||||
help='Specify a different name.')
|
||||
opts, args = parser.parse_args()
|
||||
return (opts, args, parser)
|
||||
|
||||
def main():
|
||||
opts, args, parser = parse_args()
|
||||
|
||||
use_redis()
|
||||
|
||||
if len(args) == 0:
|
||||
# Use the default queue
|
||||
queues = [Queue()]
|
||||
else:
|
||||
queues = map(Queue, args)
|
||||
|
||||
w = Worker(queues, name=opts.name)
|
||||
if opts.burst:
|
||||
w.work_burst()
|
||||
else:
|
||||
w.work()
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
18
rq/worker.py
18
rq/worker.py
|
@ -3,6 +3,7 @@ import os
|
|||
import random
|
||||
import time
|
||||
import procname
|
||||
import socket
|
||||
from pickle import dumps
|
||||
try:
|
||||
from logbook import Logger
|
||||
|
@ -17,15 +18,17 @@ def iterable(x):
|
|||
return hasattr(x, '__iter__')
|
||||
|
||||
class Worker(object):
|
||||
def __init__(self, queues, rv_ttl=500):
|
||||
def __init__(self, queues, name=None, rv_ttl=500):
|
||||
if isinstance(queues, Queue):
|
||||
queues = [queues]
|
||||
self._name = name
|
||||
self.queues = queues
|
||||
self.validate_queues()
|
||||
self.rv_ttl = rv_ttl
|
||||
self._working = False
|
||||
self.log = Logger('worker')
|
||||
|
||||
|
||||
def validate_queues(self):
|
||||
if not iterable(self.queues):
|
||||
raise ValueError('Argument queues not iterable.')
|
||||
|
@ -40,12 +43,13 @@ class Worker(object):
|
|||
return map(lambda q: q.key, self.queues)
|
||||
|
||||
|
||||
def is_idle(self):
|
||||
return not self.is_working()
|
||||
|
||||
def is_working(self):
|
||||
return self._working
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
if self._name is None:
|
||||
hostname = socket.gethostname()
|
||||
shortname, _, _ = hostname.partition('.')
|
||||
self._name = '%s.%s' % (shortname, self.pid)
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def pid(self):
|
||||
|
|
Loading…
Reference in New Issue