Classes in this module

CronTab

Manages all cron jobs.

CronTask

Individual cron task job, can be used to control the cron task.

CronTab (yombo.lib.crontab)

Note

Cron like library that can be used to perform scheduled actions. Can be used by modules to call a function at set times.

Idea and partial code from: http://stackoverflow.com/questions/373335/suggestions-for-a-cron-like-scheduler-in-python

You can create a new CronTask by specifing which function to call, specify the second, minute, hour, day, month, day of week (dow), along with args and kwargs to send to function. Not all items need to specified. If time elements are not set, then it’s assumed to be * (all).

Examples:

#  M H D M DOW
#  * * * * *  # call every minute, every hour, every day, every month
self._CronTab.new(self.myFunction)

# */2 * * * *  # call every other minute)
myArgs=("arg1", "arg2")
self._CronTab.new(self.myFunction, mins=range(0, 59, 2), args=myArgs)  # use range and specify a step
# The range just creates a list of minutes. You can also just pass a list of numbers.

# 0 0,6,12,18 * * *  # at midnight, 6am, 12pm, 6pm
# myKwargs={"argument1" : "value1", "argument2" : "value2"}
self._CronTab.new(self.myFunction, mins=0, hours=(0,6,12,18), kwargs=myKwargs)  # Notice the list of hours to run.

# 0 12 * 0 0 # at 12:00pm on sunday
self._CronTab.new(self.myFunction, mins=0, hours=12, dow=0 )  # use range and specify a step

# 0 12 * 0 0 # at 12:00pm on sunday
self._CronTab.new(self.myFunction, mins=0, hours=12, dow=0 )  # use range and specify a step

Usage example

self.MyCron = self._CronTab.new(self.myFunction, mins=0, hours=12, dow=0 )

#want to disable for a while..
self.MyCron.disable()

#re-enable it
self.MyCron.enable()

#label it
self.MyCron.label = "modules.myModule.Lunchtime on Sundays"

Module author: Mitch Schwenk <mitch-gw@yombo.net>

copyright

Copyright 2013-2020 by Yombo.

license

LICENSE for details.

view-source

View Source Code

CronTab Library Class

class yombo.lib.crontab.CronTab(parent, *args, **kwargs)[source]

Manages all cron jobs.

All modules already have a predefined reference to this library as self._CronTab. All documentation will reference this use case.

crontabs: ClassVar[dict] = {}
check_cron_tabs_loop = None
_storage_attribute_name: ClassVar[str] = 'crontabs'
_storage_primary_field_name: ClassVar[str] = 'cron_id'
_storage_fields: ClassVar[list] = ['label', 'cron_id', 'mins_orig', 'hours_orig', 'days_orig', 'months_orig', 'dow_orig', '', 'args', 'kwargs']
_storage_pickled_fields: ClassVar[Dict[str, str]] = {'args': 'json', 'kwargs': 'json'}
_storage_search_fields: ClassVar[List[str]] = ['cron_id', 'label']
_storage_attribute_sort_key: ClassVar[str] = 'machine_label'
__contains__(cron_task_requested)[source]

Note

The cron task must be enabled to be found using this method.

Checks to if a provided cron task id, label, or machine_label exists.

>>> if "129da137ab9318" in self._CronTab:

or:

>>> if "module.mymodule.mycron" in self._CronTab:
Raises

YomboWarning – Raised when request is malformed.

Parameters

cron_task_requested (str) – The cron task ID, label, or machine_label to search for.

Returns

Returns true if exists, otherwise false.

Return type

bool

__getitem__(cron_task_requested)[source]

Note

The cron task must be enabled to be found using this method. An alternative, but equal function is: get()

Attempts to find the device requested using a couple of methods.

>>> off_cmd = self._CronTab["129da137ab9318"]  #by id

or:

>>> off_cmd = self._CronTab["module.mymodule.mycron"]  #by label & machine_label
Raises
  • YomboWarning – Raised when request is malformed.

  • KeyError – Raised when request is not found.

Parameters

cron_task_requested (str) – The cron task ID, label, or machine_label to search for.

Returns

A pointer to the cron task instance.

Return type

instance

__setitem__(cron_task_requested, value)[source]

Sets are not allowed. Raises exception.

Raises

Exception – Always raised.

__delitem__(cron_task_requested)[source]

Deletes are not allowed. Raises exception.

Raises

Exception – Always raised.

__iter__()[source]

iter cron tasks.

__len__()[source]

Returns an int of the number of cron tasks configured.

Returns

The number of cron tasks configured.

Return type

int

keys()[source]

Returns the keys (cron task ID’s) that are configured.

Returns

A list of cron task IDs.

Return type

list

items()[source]

Gets a list of tuples representing the cron tasks configured.

Returns

A list of tuples.

Return type

list

values()[source]
_init_(**kwargs)[source]

Setups up the basic framework.

Parameters

loader (Instance of Loader) – A pointer to the Loader library.

_start_(**kwargs)[source]

Start the looping call to check for cron every minute.

_stop_(**kwargs)[source]

Simply stop the cron tab from running.

start_cron_loop()[source]

Start the cron task loop. This was called from _start_ in an attempt to run the loop at the top of every second.

check_cron_tabs()[source]

Checks to see if cron needs to run anything.

new(crontab_callback, mins=None, hours=None, days=None, months=None, dow=None, label='', enabled=True, args=None, kwargs={}, cron_id=None, load_source=None)[source]

Add a new CronTask.

Parameters
  • crontab_callback (Reference to function) – Function to call

  • mins ("*", int, or list of ints) – (optional) Minute to perform crontab_callback

  • hours ("*", int, or list of ints) – (optional) Hour to perform crontab_callback

  • days ("*", int, or list of ints) – (optional) Day to perform crontab_callback

  • months ("*", int, or list of ints) – (optional) Month to perform crontab_callback

  • dow ("*", int, or list of ints) – (optional) Day of week to perform crontab_callback

  • label (string) – (optional) Label for cron job.

  • enabled (bool) – (optional, default=True) If CronTask should be enabled.

  • args (List of arguments) – (optional) Arguments to pass to “crontab_callback”

  • kwargs (Dict of arguments) – (optional) Keyword arguments to pass to “crontab_callback”

  • cron_id – A label for the cron task, used to find it again later.

remove(cron_task_requested)[source]

Removes a CronTask. Accepts either cron id or cron name.

To remove a cron (note, it”s a method not a dictionary):

>>> self._CronTab.remove("7s453hhxl3")  #by cron id

or:

>>> self._CronTab.remove("module.YomboBot.MyCron")  #by label
Raises

YomboCronTabError – Raised when cron job cannot be found.

Parameters

cron_task_requested (string) – The cron task id or cron label

enable(cron_task_requested)[source]

Enable a CronTask. Accepts either cron id or cron name.

To enable a cron (note, it”s a method not a dictionary):
>>> self._CronTab.enable("7s453hhxl3")  #by cron id
or::
>>> self._CronTab.enable("module.YomboBot.MyCron")  #by label
Raises

YomboCronTabError – Raised when cron job cannot be found.

Parameters

cron_task_requested (string) – The cron task id or label

disable(cron_task_requested)[source]

Disable a CronTask. Accepts either cron id or cron name.

To disable a cron (note, it’s a method not a dictionary):
>>> self._CronTab.disable("7s453hhxl3")  #by cron id
or::
>>> self._CronTab.disable("module.YomboBot.MyCron")  #by label
Parameters

cron_task_requested (string) – The cron task id or label

status(cron_task_requested)[source]

Get the status of a cron task. Accepts either cron id or cron name.

To disable a cron (note, it’s a method not a dictionary):
>>> self._CronTab.disable("7s453hhxl3")  #by cron id
or::
>>> self._CronTab.disable("module.YomboBot.MyCron")  #by name
Parameters

cron_task_requested (string) – The cron task id or label

run_now(cron_task_requested)[source]

Runs a CronTask now. Accepts either cron id or cron name.

To run a cron (note, it”s a method not a dictionary):
>>> self._CronTab.run_now("7s453hhxl3")  #by cron id
or::
>>> self._CronTab.run_now("module.YomboBot.MyCron")  #by name
Parameters

cron_task_requested (string) – The cron task id or label

set_label(cron_task_requested, label)[source]

Set job label. Accepts either cron id or cron name.

To set a label for a cron job:
>>> self._CronTab.set_label("7s453hhxl3", "modules.mymodule.mycrontask")  #by cron label
Raises

YomboCronTabError – Raised when cron job cannot be found.

Parameters
  • cron_task_requested (string) – The cron task id

  • label (string) – New label for cron job.

run_at(crontab_callback, timestring, label='', args=(), kwargs={})[source]

Helper function for CronTab.new(), should not be called externally.

Acceptable format for “timestring” value.

  • “HH:MM” (24 hour). EG: 21:10 (9:10pm)

  • “h:mAM” EG: 1:14pm, 6:30am

  • “h:m AM” EG: 1:14 pm, 6:30 am

Parameters
  • crontab_callback (Reference to function) – Function to call

  • timestring (string) – String to parse to get hour:minute from

  • label (string) – (optional) Label for cron job.

  • enabled (bool) – (optional, default=True) If CronTask should be enabled.

  • args (List of arguments) – (optional) Arguments to pass to “crontab_callback”

  • kwargs (Dict of arguments) – (optional) Keyword arguments to pass to “crontab_callback”

__annotations__ = {'_storage_attribute_name': typing.ClassVar[str], '_storage_attribute_sort_key': typing.ClassVar[str], '_storage_fields': typing.ClassVar[list], '_storage_pickled_fields': typing.ClassVar[typing.Dict[str, str]], '_storage_primary_field_name': typing.ClassVar[str], '_storage_search_fields': typing.ClassVar[typing.List[str]], 'crontabs': typing.ClassVar[dict]}
__module__ = 'yombo.lib.crontab'

CronTask class

class yombo.lib.crontab.CronTask(parent, crontab_callback, mins=None, hours=None, days=None, months=None, dow=None, label='', enabled=True, args=None, kwargs={}, cron_id=None, load_source=None)[source]

Individual cron task job, can be used to control the cron task.

_Entity_type: ClassVar[str] = 'CronTask'
_Entity_label_attribute: ClassVar[str] = 'cron_id'
__init__(parent, crontab_callback, mins=None, hours=None, days=None, months=None, dow=None, label='', enabled=True, args=None, kwargs={}, cron_id=None, load_source=None)[source]

Setup the cron event.

__del__()[source]

About to delete myself. Going to disable myself and tell crontab about this if it’s linked.

enable()[source]

Enable this CronTask.

disable()[source]

Disable this CronTask.

status()[source]

Returns the status of the cron task. If enabled, return True, otherwise returns False.

Returns

Status of cron task.

Return type

bool

match_time(t)[source]

Return True if this event should trigger at the specified datetime

check(t)[source]

” Called by the parent to see if the cron needs to run. This function should not be called externally.

Parameters

t – Current time in a format for the cron task.

Returns

None

__annotations__ = {'_Entity_label_attribute': typing.ClassVar[str], '_Entity_type': typing.ClassVar[str]}
__module__ = 'yombo.lib.crontab'
run_now()[source]

Run the cron task now.

Returns

None

Last updated: Jun 17, 2020