(SOLVED) APScheduler info and how to terminate a running way

This is the place for queries that don't fit in any of the other categories.

(SOLVED) APScheduler info and how to terminate a running way

Postby Grinch » Thu Oct 03, 2013 7:40 am

Dear All,
I'm trying to use this class to add some scheduling functionality to my application.
At now I'm working around a script configurator utility, see the attached image with name image1.jpg.
With the aid of the script configurator I should be able to choose the day of the week, the month and the starting time in a set spanning from 00 to 23 (24h format) with the freedom to choose the starting minutes that will be the same for every hour selected (if is possible select also a custom minute for every hour this should be the best choice but at this time I don't know if this is possible).
Suppose that you like to start a job every January and February on Monday and Thursday at this time 00:30, 12:30, and 23:30 In my thinking I've to set the form as shown in image2.jpg.
Now I like to know how to compose the command string that I've to pass to the scheduler in order to setup this job.

From the web page http://pythonhosted.org/APScheduler/cronschedule.html I think that the configuration string should be this one:

sched.add_cron_job(job_function, month='1-2', day='mon-thu', hour='0-12-23', minute=30)

but I'm not sure if this is correct, someone have better knowledge on the subject and can give me a hint?

Also to choose a arbitray minutes is possible to setup the scheduler with the minutes specified inside the hour tag like:

hour = '0:15-12:10-23:30'

to fire event at 00:15, 12:10 and 23:30?

Thanks in advance and

best regards
Grinch
Attachments
image2.jpg
image2.jpg
image2.jpg (87.12 KiB) Viewed 262 times
image1.jpg
image1.jpg
image1.jpg (86.8 KiB) Viewed 262 times
Last edited by Grinch on Mon Oct 07, 2013 7:45 am, edited 2 times in total.
Grinch
 
Posts: 10
Joined: Wed Sep 04, 2013 10:00 am

Re: APScheduler info

Postby Grinch » Mon Oct 07, 2013 7:20 am

Dear All,
problem solved, the correct syntax is reported below:

sched.add_cron_job(job_function, month='1-2', day='mon-thu', hour='0,12,23', minute=30)

actually is not possible to set a different minute starting time, if you need this feature the only way is define and add new separate scheduled job, then the minute parameter affect in a same way all the hour specified times.

Best regards
Powermos
Grinch
 
Posts: 10
Joined: Wed Sep 04, 2013 10:00 am

Re: APScheduler info example as to terminate a scheduled tas

Postby Grinch » Mon Oct 07, 2013 7:44 am

Dear All,
here below an example how is possible to exit from a running task without shutting down the task in a forcibly way. To achieve this you can use a global flag variable and use it to exit the task in a clean way.
Into the following code there are two flag:

- TerminateJob
- JobIsTerminated

the TerminateJob flag is used to signal to the task that is time to quit, hence the task perform all the needed operation to exit in a clean way respect the whole application. Meantime that the task is performing the closing operations, somewhere into the main code, we are waiting for the task ending by continuously checking the JobIsTerminated flag. Just before the task exit as a last instruction we have simply to set the JobIsTerminated flag, in this way, from the main code, we are able to detect that the task have quit all the operations and then we can proceed to remove the job from the scheduler in a clean way.

Additional notes
Inside of the reported example is not handled the possible condition of a crashing into the task code. In that sense, in order to prevent any code stucking due to the waiting loop for the JobIsTerminated flag setting, you shall provide a simple timeout mechanism in order to know if the task have problem in quitting and then perform the necessary actions.

Bye
Powermos

Code: Select all
# ----------------------------------------------------------
# Powermos 03/10/2013
# Example how to kill a thread from scheduler in a clean way
# in responce to this post:
# https://groups.google.com/d/msg/apscheduler/WB_9Z0FllJk/Uatm2nse8xYJ
# https://groups.google.com/d/msg/apscheduler/D0mn505ZAYc/Sbe9mrA73E4J
# ----------------------------------------------------------

from apscheduler.scheduler import Scheduler
import time
import os
import sys
from datetime import datetime
import logging

# Global flag definition
TerminateProgram = False
TerminateJob = False
JobIsTerminated = False

# Define the function that is to be executed
# it will be executed in a thread by the scheduler
def myjob(arg1, arg2):
      global TerminateJob
      global JobIsTerminated
      JobIsTerminated = False
      print("Starting job "+arg1+" at "+str(datetime.now()))
      print("Iteration number: "+str(arg2))
      # Code start of your routine ....
      c=0
      while c < arg2:
            print("work")
            time.sleep(1)
            c=c+1
            if TerminateJob:
                  break
      # Job finished
      print("Job "+arg1+" finished with counter = "+str(c))
      JobIsTerminated = True

# ----------------------------------------------------------
# MAIN
# ----------------------------------------------------------
if __name__ == "__main__":

      # For the scheduler
      logging.basicConfig()

      # Start the scheduler
      sched=Scheduler()
      job = sched.add_cron_job(myjob, args=["myjob", 20], month = '*', day_of_week='*', hour='*', minute='*', second=5)
      sched.start()
     
      os.system("clear")
                               
      while not TerminateProgram:
            print("Menu")
            print("1  Kill Current Job")
            print("2  Print Current Schedules")
            print("3  Terminate Program")
            menu=int(raw_input('Enter a number:'))
            if menu == 1:
                  print("Killing Current Job!")
                  TerminateJob = True
                  # Wait until myjob task exit
                  while not JobIsTerminated:
                        pass
                  # Remove the job from the scheduler
                  sched.unschedule_job(job)
                  # Force the scheduler termination
                  sched.shutdown(wait=False)
                  print("Scheduler shutdown Complete")
            elif menu == 2:
                sched.print_jobs()
            elif menu == 3:
                print "Force main program exit"
                TerminateProgram = True
Attachments
schedexample.txt
APScheduler: example how to quit a scheduled task in a clean way
(2.6 KiB) Downloaded 29 times
Grinch
 
Posts: 10
Joined: Wed Sep 04, 2013 10:00 am


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 2 guests

cron