Setting a max duration for a job

1,532 views
Skip to first unread message

Guy Blavin

unread,
Nov 15, 2015, 1:39:42 AM11/15/15
to APScheduler
Hi all,

I have been using apscheduler for 2 years, in a client-server scheduler application (clients connect to a server, the server gives them their jobs to schedule).

I have a problem with a specific job, which consists of executing an external command every minute. I set max_instances=1 and hence the job always waits for the previous instance to finish before executing the new instance. 
But sometimes (rarely, given the number of times the command is executed) the command hangs indefinitely. 

I wonder if there is a way to tell apscheduler that a given job instance shouldn't be allowed to run for longer than X seconds. Like a max_duration setting.

Also, I would welcome any insight on the use of logging with apscheduler, as I will probably need to add more logging to find out what is going on. Currently, each job uses the python logging module, but the scheduler itself doesn't log anything about what he is doing. I would welcome any feedback from people experienced in this.

Thx,
Guy

Alex Grönholm

unread,
Nov 15, 2015, 1:55:16 AM11/15/15
to apsch...@googlegroups.com
I just had a conversation on this very topic. Limiting the maximum run time of a job is possible only if the job is run in a dedicated process. Threads cannot be forcibly terminated. Unfortunately even with processes, APScheduler currently has no mechanism to assign timeouts to jobs or to terminate job executions early. This would require a finer degree of control than what the concurrent.futures package offers. This is something I will probably have to implement in the next APScheduler version. In the meantime, your only option is to implement your own executor, possibly based on the multiprocessing module. Even then you will probably need to augment the Job class to add the timeout parameter.
--
You received this message because you are subscribed to the Google Groups "APScheduler" group.
To unsubscribe from this group and stop receiving emails from it, send an email to apscheduler...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Guy Blavin

unread,
Nov 18, 2015, 3:56:30 PM11/18/15
to APScheduler
Thx for the reply, Alex.

Is there any way I could check the running time of a job and decide to shut it down ?
I don't find anything about the running state while looking at help(sched.get_jobs()[0]) after creating a sample job

Alex Grönholm

unread,
Nov 18, 2015, 4:04:28 PM11/18/15
to apsch...@googlegroups.com
How would you shut it down? In your scheduled callable, you can store the current timestamp and later check it against the current time then. That's how you get the run time. If the run time exceeds your timeout, you can then exit from the callable. That's the only way I see it could work.

Guy Blavin

unread,
Feb 10, 2016, 8:26:12 AM2/10/16
to APScheduler
Hello Alex,

Actually, all the time when a job runs for a long time, it is when the job launches an external executable and that executable gets stuck.

What I realize is that I put a 120s timeout when calling the external process using this idea:

So, I don't really understand why the job gets stuck as the timeout feature should take care of stuck processes.

I am not expecting you to help me on this issue as it doesn't seem to be related to APScheduler, I just wanted to tell you where I was on this subject.

Guy
Reply all
Reply to author
Forward
0 new messages