I am using uwsgi decorators(specifically the cron decorator) to do things at specific times. I have the following code:
import cherrypy
import uwsgidecorators
class TestObject(object):
@cherrypy.expose
def index(self):
launchapp = self.launchapp(-1,-1,-1,-1,-1,"foobar")
return "This is a test"
@uwsgidecorators.cron(minute,hour,day,month,dayweek)
def launchapp(self,target):
print "the target is %s" %target
return
However I get the error:
@uwsgidecorators.cron(minute,hour,day,month,dayweek)
NameError: name 'minute' is not defined
I am basically trying to specify the timing parameters for the cron decorator in the index function. Anyone know what I am doing wrong?
Why your code fails
The error has nothing to do with the fact that you’re passing parameters, it’s just that the
minutevariable is undefined.You would get the exact same error doing:
What you probably want to do is:
Please have a look at the uwsgi documentation for a more comprehensive example.
— EDIT below.
How you could fix it
It seems that you’re trying to add a task to your crontab after index is hit.
That’s not the use case for the decorator, the decorator is only meant to say that a function should run on a cron at function definition.
Basically, when the decorator is executed (and the function defined, not called), it gets added to the crontab.
In your case, you want to add a function to the crontab; so you should use something like
uwsgi.add_cron. You can have a look at the decorator code to see how you could use it.You shouldn’t be using a method but a function, though.