Skip to content
This repository has been archived by the owner on Jun 5, 2023. It is now read-only.

Commit

Permalink
handle jobs queued by Ruby Resque with no module
Browse files Browse the repository at this point in the history
Ruby compatibility: Resque sends just a class name and not a module name
so if I use Resque to queue a ruby class called "Worker" then Pyres will
throw a "ValueError: Empty module name" exception.  To avoid that, if
there's no module name in the json then we'll use the class name as a
module name.
  • Loading branch information
toby cabot committed May 13, 2015
1 parent 89811a7 commit c100e3f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
9 changes: 9 additions & 0 deletions pyres/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ def safe_str_to_class(s):
klass = lst[-1]
mod_list = lst[:-1]
module = ".".join(mod_list)

# ruby compatibility kludge: resque sends just a class name and
# not a module name so if I use resque to queue a ruby class
# called "Worker" then pyres will throw a "ValueError: Empty
# module name" exception. To avoid that, if there's no module in
# the json then we'll use the classname as a module name.
if not module:
module = klass

mod = my_import(module)
if hasattr(mod, klass):
return getattr(mod, klass)
Expand Down
12 changes: 11 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
import os
from pyres import ResQ, str_to_class

class tests(object):
queue = 'basic'

@staticmethod
def perform(name):
s = "name:%s" % name
return s

class Basic(object):
queue = 'basic'

Expand Down Expand Up @@ -111,7 +119,9 @@ def test_safe_str_to_class(self):
assert safe_str_to_class('tests.Basic') == Basic
self.assertRaises(ImportError, safe_str_to_class, 'test.Mine')
self.assertRaises(ImportError, safe_str_to_class, 'tests.World')

# test that we'll use the class name as a module name if no
# module name is provided (for Ruby compatibility)
assert safe_str_to_class('tests') == tests

class PyResTests(unittest.TestCase):
def setUp(self):
Expand Down

0 comments on commit c100e3f

Please sign in to comment.