Permalink
Browse files

BeanstalkClient.call() will use settings.BEANSTALK_JOB_NAME pattern t…

…o derive the function name if appname and jobname parameters are passed
  • Loading branch information...
1 parent e101d85 commit c853072dc15f3c1094c0ff0953737ef743ad80ef Cole Krumbholz committed Sep 15, 2011
Showing with 32 additions and 1 deletion.
  1. +21 −0 README.md
  2. +11 −1 django_beanstalkd/__init__.py
View
@@ -42,6 +42,14 @@ functions:
# %(job)s : job name
BEANSTALK_JOB_NAME = '%(app)s.%(job)s'
+For example, if you have separate staging and production servers running
+beanstalk jobs with the same name, you can create a separate namespace for each:
+
+ # in the settings.py for the staging server
+ BEANSTALK_JOB_NAME = 'staging.%(app)s.%(job)s'
+
+ # in the settings.py for the production server
+ BEANSTALK_JOB_NAME = 'production.%(app)s.%(job)s'
Workers
-------
@@ -79,13 +87,26 @@ function with it:
client = BeanstalkClient()
client.call('beanstalk_example.background_counting', '5')
+Alternatively, to use the settings.BEANSTALK\_JOB\_NAME pattern, `call` the
+function with explicit appname and jobname parameters:
+
+ client.call(appname='beanstalk_example', jobname='background_counting', arg='5')
+
For a live example look at the `beanstalk_example` app, in the
`management/commands/beanstalk_example_client.py` file. Arguments to `call` are
+ func: the function name following the pattern `appname.jobname` or
+ the pattern specified by settings.BEANSTALK_JOB_NAME
+ arg: the argument to pass to the beanstalk job
priority: an integer number that specifies the priority. Jobs with a
smaller priority get executed first
delay: how many seconds to wait before the job can be reserved
ttr: how many seconds a worker has to process the job before it gets requeued
+ appname: if present with jobname, will be applied to the pattern specified
+ by settings.BEANSTALK_JOB_NAME to specify the function name. The
+ func argument will be ignored
+ jobname: if present with appname, used to specify the function name
+
Example App
@@ -29,7 +29,7 @@ class BeanstalkError(Exception):
class BeanstalkClient(object):
"""beanstalk client, automatically connecting to server"""
- def call(self, func, arg='', priority=DEFAULT_PRIORITY, delay=0, ttr=DEFAULT_TTR):
+ def call(self, func=None, arg='', priority=DEFAULT_PRIORITY, delay=0, ttr=DEFAULT_TTR, appname=None, jobname=None):
"""
Calls the specified function (in beanstalk terms: put the specified arg
in tube func)
@@ -39,6 +39,16 @@ def call(self, func, arg='', priority=DEFAULT_PRIORITY, delay=0, ttr=DEFAULT_TTR
delay: how many seconds to wait before the job can be reserved
ttr: how many seconds a worker has to process the job before it gets requeued
"""
+
+ if appname and jobname:
+ try:
+ func = settings.BEANSTALK_JOB_NAME % {
+ 'app': appname,
+ 'job': jobname,
+ }
+ except AttributeError:
+ func = '%s.%s' % (appname, jobname)
+
self._beanstalk.use(func)
self._beanstalk.put(str(arg), priority=priority, delay=delay, ttr=ttr)
def close(self):

0 comments on commit c853072

Please sign in to comment.