Forums

Can't get a scheduled task to run with Web2Py app.

I can't get a scheduled task to run with Web2Py.

I have a web app, call it webapp1.

I want to do some maintenance activites on the database once a day with a function, call it cleandb().

I've tried to follow the articles on Scheduled Tasks but I'm not seeing my function run.

I can see in the Scheduled Task Log, that the script runs, but apparently the function cleandb() never runs.

This is what I've done:

Created a model file: scheduler.py with lines of code similar to this:

def cleandb():

    #Do stuff here.
    return

from gluon.scheduler import Scheduler

scheduler = Scheduler(db)

Created a file under my home directory called: start_task.py

It has code:

#/usr/bin/env python

import os

import socket

import subprocess

import sys

filename = os.path.abspath(__file__)  # we use this to generate a unique socket name

\# we use a local socket as a lock.

\# it can only be bound once, and will be released if the process dies

\# we want to keep the socket open until the very end of

\# our script so we use a global variable to avoid going

\# out of scope and being garbage-collected

lock_socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)

try:

    lock_socket.bind('\0' + filename)

except socket.error:

    print("Failed to acquire lock, task must already be running.")

    sys.exit()

subprocess.call(["python", "web2py/web2py.py", "-K", "webapp1"])

Then I created a Scheduled Task to run start_task.py

I can see in the Scheduled Task Log, that the script runs, but apparently the function cleandb() never runs.

How do I get it to run?

I thought that's what all of this was supposed to accomplish.

[edit by admin: formatting]

It looks like you're using two different schedulers there -- the scheduled task feature of PythonAnywhere and the gluon scheduler.

What happens if you just run

python web2py/web2py.py -K webapp1

...in a Bash console? I don't see anything in there that would specifically run your scheduler.py, though that may simply be me not understanding part of web2py.

Oh I see. Everything I read in the help forum mentioned using the Scheduled Tasks, etc. When I develop Web2Py apps locally, I can start and stop the scheduler from the launcher panel. There's no launcher panel on PythonAnywhere, so I thought I had to use these work-arounds they mention using the Scheduled Tasks, etc.

Let me look at this again...

OK. I think I understand this now.

I now have a Web2Py app running a task periodically.

For whoever else might need this, to run a periodic task using Web2Py's scheduler mechanism there are only 2 things to do:

.1. Create a model file, say scheduler.py

In it, put the definition of the function you want to run periodically and the two lines of boiler-plate code:

from gluon.scheduler import Scheduler

scheduler = Scheduler(db)

.2. Create a new record in the database table: scheduler_task

Enter the name of the function to run and the desired values for the task schedule.

--That's it.

Great! Glad you got it working.