Forums

web2py cron

How is web2py set up? Does the default setup enable the cron functionality of web2py? I have a job set up to run in web2py cron but it is not being executed.

I just did a bit of reading about the cron in web2py. It looks like there's a couple of ways to have cron-type functionality in web2py:

  • soft cron - Runs at the first request after the specified cron time. This should just work on PythonAnywhere. If it doesn't, that's a bug we need to look at.
  • hard cron - Runs a parallel thread in the built-in web server. This probably won't work on PA, and would be a bad idea, anyway.
  • external cron - You can set this up as a scheduled task on PA.

For PA, I would suggest external cron using a scheduled task or (if you have a task that takes a short time) soft cron

Thanks for looking into it. I'll set up a scheduled task then.

One more question. So which handler is used for web2py? Is it the CGI handler?

We use the WSGI handler. If you're curious, you can see the code that creates it at https://www.pythonanywhere.com/user/davidk01/files/var/www/wsgi.py?edit

Thanks. I didn't know I could do that.

For external cron, how would you run the scheduled task in a Web2py environment?

Normally adding code like this to the system crontab would do 0 9 * * 1-5 python /var/web2py/web2py.py -S app_name -M -J -R scripts/my_script.py. Here the script can access all the controllers and models in Web2py.

I'm not sure how I would implement this with Pythonanywhere's 'Schedule' tab. Anyone have any experience/success with this?

The schedule tab is not a complete implementation of cron. You can only run things hourly or daily and there is no way around that. So the example you have provided is impossible to mimic in the schedule tab. You'd have to incorporate some of the logic in the script itself. So for the particular example that you have given you'd need to set up a daily task and then check in the script itself that it is not saturday or sunday.

As for how to actually do it here's the template I have been using

python2.7 /home/davidk01/web2py/web2py.py -S test -M -N -R /home/davidk01/web2py/applications/test/modules/item_price_updater.py

and it has been working without problems. The above example will run the script following -R in the context of my test application, which means that the script has access to all the variables defined in the models and has access to all the global variables like request. I don't know where web2py is set up for you but if you have used the wizard provided by pythonanywhere then it will be in your home directory.

Thanks davidk01, the background process is running fine and it's loading inside the web2py app environment. I'll stick to hourly now, hopefully Pythonanywhere will roll out some more time options for the Scheduler.

"python2.7 /home/davidk01/web2py/web2py.py -S test -M -N -R /home/davidk01/web2py/applications/test/modules/item_price_updater.py"

Can someone explain the background syntax to this "-S test -M -N -R " -- Id rtfm, but I dont know which manual to rtf

That would be the web2py documentation relating to the web2py scheduler

I'm not seeing the flag syntax. Can you pull that out of the book? I'm sorry I'm being dumb

How about this?

Right on. Much appreciated.