Use @job decorator for class methods
AlexanderPashuk opened this issue · 2 comments
AlexanderPashuk commented
Hey,
Thanks for great module!
I have some problems with using @job
decorator with class methods. Check code below:
from flask import current_app
from flask_rq2 import RQ
rq = RQ(current_app)
class Test(object):
@rq.job
@classmethod
def hello(cls):
print('Hello world')
# add new task in queue
Test.hello.queue()
In worker logs I got:
AttributeError: 'module' object has no attribute 'hello'
Any ideas? I will be grateful for any tips.
AlexanderPashuk commented
Class Test
placed in test.py
file in models
directory. In RQ Dashboard I see that worker (possibly) tries to run function from path models.test.hello
that is obviously wrong...
AlexanderPashuk commented
Finished with something wrapping class method in additional function:
# file: tasks.py
from models.test import Test
@rq.job
def task_hello():
return Test.hello()
Actually, this makes the code more understandable and allows you to separate code with functionality and tasks (jobs) to run this code.