How to know if the scheduler is running?

489 views
Skip to first unread message

Alfonso Serra

unread,
Mar 8, 2016, 4:59:34 PM3/8/16
to web...@googlegroups.com
In order to run a scheduler task, the scheduler must be running using
web2py.py -K myapp

Its all good but sometimes servers are stopped for mantainance or they kill processes if they are running for too long.

So if i have a controller that assigns a task, how do i detect if the scheduler is running or not?

Most hosting services doesnt let you run shell commands, so is there a way to run it within a controller? using the module subproccess maybe?

Thank you very much.


Anthony

unread,
Mar 8, 2016, 5:48:38 PM3/8/16
to web2py-users
You can create a cron job that starts the scheduler on restart of the OS.

Anthony

Nico de Groot

unread,
Mar 9, 2016, 4:46:55 AM3/9/16
to web2py-users
Or use something like Monit (on Linux) to check if scheduler is still running, restarting it if it's not.

Nico de Groot

Alfonso Serra

unread,
Mar 9, 2016, 5:42:25 AM3/9/16
to web2py-users
Thanks Nico ill give it a try.

Niphlod

unread,
Mar 9, 2016, 6:06:50 AM3/9/16
to web2py-users
BTW: exactly as a webapp is suppossed to be kept alive from the os, the same applies to the scheduler. That being said the controller that queue tasks can see if any scheduler is alive inspecting the scheduler_worker table.

Niphlod

unread,
Mar 9, 2016, 6:09:54 AM3/9/16
to web2py-users
there's a method for it, get_workers(). according to your heartbeat interval, you can inspect for any worker that has a last_heartbeat too much in the past to see if it's running or not.

Alfonso Serra

unread,
Mar 9, 2016, 6:24:33 AM3/9/16
to web2py-users
Thanks Niphlod.

So i can inspect for workers with a last_heartbeat less than 4 seconds ago (default interval is 3 i think). Is that right? It might work, thanks.


Alfonso Serra

unread,
Mar 9, 2016, 6:58:56 AM3/9/16
to web2py-users
This would be the funcion:

from gluon.scheduler import Scheduler
sched
= Scheduler(db)

def sched_running():
   
from datetime import datetime
    workers
= sched.get_workers()
 

   
for key, worker in workers.items():
       
last = (datetime.now() - worker.last_heartbeat).seconds
       
if last < 3:
           
return True
   
return False


Niphlod

unread,
Mar 9, 2016, 8:09:23 AM3/9/16
to web2py-users
I'd check for < 30 to be sure that the worker is not sleeping but the general idea is that.

BTW: don't ever ever do 
from datetime import datetime

given web2py's unique execution model, anything past that will have the import screwed . use plain

import datetime

if in need. 
BTW2: you don't need datetime at all sometimes ^_^ 

Alfonso Serra

unread,
Mar 9, 2016, 4:33:22 PM3/9/16
to web2py-users
Ok this should do it.

def sched_running():

   
    workers
= sched.get_workers()
   
   
for key, worker in workers.items():

       
last = (request.now - worker.last_heartbeat).seconds
       
if last < 4:
           
return True
   
return False

One last thing, any idea on how to start the scheduler from within a controller?

Im trying os.system("web2py -K myapp") but the scheduler runs on top of the server process and stops working.

Thanks
 

Niphlod

unread,
Mar 9, 2016, 4:45:21 PM3/9/16
to web2py-users
simply said, you can't reliably. 
The whole reason you offload tasks to a worker vs a really simpler ajax call is because it's a totally external process that is NOT managed by the webserver. webservers do kill long-running processes (usually after 60 seconds) spawned by themselves to avoid zombie stale resources.

Alfonso Serra

unread,
Mar 9, 2016, 5:08:06 PM3/9/16
to web2py-users
This looks like it works.

import subprocess
ret
= subprocess.Popen(["python","D:/web2py/web2py.py", "-K", "myapp"], shell=True)
print ret.pid

I have the stdout in the same console as the server but both, the sched and the server, are working.

This whole scheduler thing looks very messy but it looks like the only way to run very long tasks.

Thanks again for the tricks to workaround these situations.

Niphlod

unread,
Mar 9, 2016, 5:16:05 PM3/9/16
to web2py-users
as soon as you acknowledge that:
- what you're trying to do is highly discouraged
- lots os safer/documented/supported/simpler ways exist  

feel free to experiment .

Alfonso Serra

unread,
Mar 9, 2016, 6:29:22 PM3/9/16
to web2py-users
Yes you are right, the connection resets often and even if it works its not worthy.



Alfonso Serra

unread,
Mar 11, 2016, 9:20:09 AM3/11/16
to web2py-users
Excuse me Niphlod, i can't find any docs about running the scheduler programatically.

Do you have any advise on how to do it?

Thanks.

Dave S

unread,
Mar 11, 2016, 1:44:26 PM3/11/16
to web2py-users


On Friday, March 11, 2016 at 6:20:09 AM UTC-8, Alfonso Serra wrote:
Excuse me Niphlod, i can't find any docs about running the scheduler programatically.

Do you have any advise on how to do it?



Niphlod

unread,
Mar 13, 2016, 7:05:13 AM3/13/16
to web2py-users
that's to queue programmatically "once and forget". to start it, you NEED to look around your OS's facilities and start the scheduler process as a service or under a supervisor. you can't start the scheduler from within the web application for the limitations explained earlier.

Reply all
Reply to author
Forward
0 new messages