Package: taskeduler
Taskeduler: schedule your tasks, easilyThis module is a tool written in python built to automate tasks.Write a python function and configure the task in a yaml file. Then it is going to be executed following the programed scheduler.How to schedule a taskThere are two main ways to use the Taskeduler: describing the tasks in a yaml file and running the module passing the file as an argument, or embeding the module in another python project, importing theTaskManagerand adding the tasks manually.There are examples of both usages in theexample file.Via YAML fileThere are 3 main elements that are reperented in the yaml file: thetask name, thetask entrypointand thetask schedule.my_task:# Here goes the entrypoint information.script:# The path to the file where the entrypoint function is defined.file:${SCRIPTS_DIR}/here/lies/the/entrypoint.py# The proper name of the entrypoint function.entrypoint:my_entrypoint_function# The entrypoint args and kwargs if wanted.args:-first_argument-...kwargs:kwarg_name:kwarg_value...# Here goes the schedule information.repeat:# How often the task will be repeated.frequency:['daily']# Some additional rules about when will the task be executed.execution_rules:week_day:["mon","tue","wed","thu","fri"]time:["10:00"]You may note that the parsing of the YAML file will resolve environment variables if enclosed into braces, preceded with the dollar sign:${ENV_VARIABLE}.All the fields shown above are mandatory except of
All the fields shown in the previous example are mandatory exceptargs,kwargsand the proper execution rules. To read more about the execution rules, go to thescheduler rules section.The frecuency and all the execution rules are ment to be iterables in the YAML definition, there will be created a task for each defined frecuency, named astask_name_frequency.Via Python codeTo build theTaskobject, first is needed to create aScheduler. Then, to execute the task is required to add the task to aTaskManager, and run the loop.>>>fromtaskeduler.schedulerimportExecutionRulesManager,Scheduler>>>fromtaskeduler.taskimportTask,TaskManager>>>>>># Define the entrypoint function>>>defmy_entrypoint_function(name:str)->None:...print(f"Hello{name}! What's up?")>>>>>># Create the Scheduler and the ExecutionRulesManager>>>scheduler=Scheduler(...frecuency="daily",...execution_rules_manager=ExecutionRulesManager(...week_days=["mon","tue","wed","thu","fri"],...time=["10:00"]...)...)>>>>>># Create the task>>>task=Task(...scheduler=scheduler,...task=greet,...args=("Peter",)...)>>>>>># Setup the TaskManager and run the loop>>>task_manager=TaskManager()>>>task_manager.add_task("test_taskeduler",task)>>>task_manager.loop.start()You can also load a YAML from the python code!>>>fromtaskedulerimportschedule>>>my_yaml="/path/to/my/file.yaml">>>task_manager=schedule(yaml_file=my_yaml)>>>task_manager.loop.start()Even with an already existingtask_manager:>>>fromtaskedulerimportTaskManager,schedule>>>task_manager=TaskManager()>>>my_yaml="/path/to/my/file.yaml">>>schedule(yaml_file=my_yaml,task_manager=task_manager)>>>task_manager.loop.start()Scheduler rulesThefrequenciesdetermine the period of wait between the task executions. Theexecution rulesdetermine some aditional"checks"that must be met, represented by a list of possible values.There are 6 schedulerfrequenciesand 4execution rulesavailable:Scheduler Frecuencies:minutely: Repeat every minute.i.e: 10:10 => 10:11hourly: Repeat every hour.i.e: 10:10 => 11:10daily: Repeat every day.i.e: 11 jul => 12 julweekly: Repeat every week (based on the week day).i.e: monday 1 => monday 8monthly: Repeat every month (based on the month day).i.e: January 27 => February 27; January 29 => February 29 (leap year)// March 1yearly: Repeat every year (based on the month day and month).i.e: 20 aug 2019 => 20 aug 2020Execution Rules:time: This restriction determines thehourandminutewhen the task can be executed in aHH:MMformat.month_days: This restriction determines thenumber of day in the monthwhen the task can be executed.week_days: This restriction determines theweek daywhen the task can be executed, in any of the following formats:full name: monday, tuesday, ...short name: mon, tue, ...number: 0, 1, ...month: This restriction determines themonthwhen the task can be executed, in any of the following formats:full name: january, february, ...short name: jan, feb, ...number: 1, 2, ...