core.tasks

Module Contents

Classes

JsonKeyEncoder

Add support for Keys in deferred tasks

CallableTaskBase

Base class for user-callable tasks.

TaskHandler

Task Handler.

MetaQueryIter

This is the meta class for QueryIters.

QueryIter

BaseClass to run a database Query and process each entry matched.

DeleteEntitiesIter

Simple Query-Iter to delete all entities encountered.

Functions

jsonDecodeObjectHook(obj)

Inverse to JsonKeyEncoder: Check if the object matches a keymarker and parse it’s key

removePeriodicTask(task: Callable) → None

Removes a periodic task from the queue. Useful to unqueue an task

noRetry(f)

Prevents a deferred Function from being called a second time

callDeferred(func)

This is a decorator, which always calls the function deferred.

PeriodicTask(interval=0, cronName='default')

Decorator to call a function periodic during maintenance.

CallableTask(fn)

Marks a Class as representing a user-callable Task.

StartupTask(fn)

Functions decorated with this are called shortly at instance startup.

runStartupTasks()

Runs all queued startupTasks.

class core.tasks.JsonKeyEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)

Bases: json.JSONEncoder

Add support for Keys in deferred tasks

default(self, o: Any)Any

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this:

def default(self, o):
    try:
        iterable = iter(o)
    except TypeError:
        pass
    else:
        return list(iterable)
    # Let the base class default method raise the TypeError
    return JSONEncoder.default(self, o)
core.tasks.jsonDecodeObjectHook(obj)

Inverse to JsonKeyEncoder: Check if the object matches a keymarker and parse it’s key

core.tasks._gaeApp
core.tasks.regionMap
core.tasks.queueRegion
core.tasks.regionPrefix
core.tasks.taskClient
core.tasks._periodicTasks :Dict[str, Dict[Callable, int]]
core.tasks._callableTasks
core.tasks._deferedTasks
core.tasks._startupTasks = []
core.tasks._appengineServiceIPs
exception core.tasks.PermanentTaskFailure

Bases: Exception

Indicates that a task failed, and will never succeed.

core.tasks.removePeriodicTask(task: Callable)None

Removes a periodic task from the queue. Useful to unqueue an task that has been inherited from an overridden module.

class core.tasks.CallableTaskBase

Base class for user-callable tasks. Must be subclassed.

key
name
descr
kindName = server-task
canCall(self)

Checks wherever the current user can execute this task :returns: bool

dataSkel(self)

If additional data is needed, return a skeleton-instance here. These values are then passed to execute.

execute(self)

The actual code that should be run goes here.

class core.tasks.TaskHandler(moduleName, modulePath)

Task Handler. Handles calling of Tasks (queued and periodic), and performs updatececks Do not Modify. Do not Subclass.

adminInfo
retryCountWarningThreshold = 25
exposed = True
exposed = True
exposed = True
exposed = True
exposed = True
findBoundTask(self, task, obj=None, depth=0)

Tries to locate the instance, this function belongs to. If it succeeds in finding it, it returns the function and its instance (-> its “self”). Otherwise, None is returned. :param task: A callable decorated with @PeriodicTask :type task: callable :param obj: Object, which will be scanned in the current iteration. None means start at conf[“viur.mainApp”]. :type obj: object :param depth: Current iteration depth. :type depth: int

queryIter(self, *args, **kwargs)

This processes one chunk of a queryIter (see below).

deferred(self, *args, **kwargs)

This catches one deferred call and routes it to its destination

cron(self, cronName='default', *args, **kwargs)
list(self, *args, **kwargs)

Lists all user-callable tasks which are callable by this user

execute(self, taskID, *args, **kwargs)

Queues a specific task for the next maintenance run

core.tasks.admin = True
core.tasks.vi = True
core.tasks.html = True
core.tasks.noRetry(f)

Prevents a deferred Function from being called a second time

core.tasks.callDeferred(func)

This is a decorator, which always calls the function deferred. Unlike Googles implementation, this one works (with bound functions)

core.tasks.PeriodicTask(interval=0, cronName='default')

Decorator to call a function periodic during maintenance. Interval defines a lower bound for the call-frequency for this task; it will not be called faster than each interval minutes. (Note that the actual delay between two sequent might be much larger) :param interval: Call at most every interval minutes. 0 means call as often as possible. :type interval: int

core.tasks.CallableTask(fn)

Marks a Class as representing a user-callable Task. It should extend CallableTaskBase and must provide its API

core.tasks.StartupTask(fn)

Functions decorated with this are called shortly at instance startup. It’s not guaranteed that they actually run on the instance that just started up! Wrapped functions must not take any arguments.

core.tasks.runStartupTasks()

Runs all queued startupTasks. Do not call directly!

class core.tasks.MetaQueryIter(cls, name, bases, dct)

Bases: type

This is the meta class for QueryIters. Used only to keep track of all subclasses of QueryIter so we can emit the callbacks on the correct class.

_classCache
class core.tasks.QueryIter

Bases: object

BaseClass to run a database Query and process each entry matched. This will run each step deferred, so it is possible to process an arbitrary number of entries without being limited by time or memory.

To use this class create a subclass, override the classmethods handleEntry and handleFinish and then call startIterOnQuery with an instance of a database Query (and possible some custom data to pass along)

queueName = default
classmethod startIterOnQuery(cls, query: viur.core.db.Query, customData: Any = None)None

Starts iterating the given query on this class. Will return immediately, the first batch will already run deferred.

Warning: Any custom data must be json-serializable and must be passed in customData. You cannot store any data on this class as each chunk may run on a different instance!

classmethod _requeueStep(cls, qryDict: Dict[str, Any])None

Internal use only. Pushes a new step defined in qryDict to either the taskqueue or append it to the current request if we are on the local development server.

classmethod _qryStep(cls, qryDict: Dict[str, Any])None

Internal use only. Processes one block of five entries from the query defined in qryDict and reschedules the next block.

classmethod handleEntry(cls, entry, customData)

Overridable hook to process one entry. “entry” will be either an db.Entity or an SkeletonInstance (if that query has been created by skel.all())

Warning: If your query has an sortOrder other than __key__ and you modify that property here it is possible to encounter that object later one again (as it may jump behind the current cursor).

classmethod handleFinish(cls, totalCount: int, customData)

Overridable hook that indicates the current run has been finished.

classmethod handleError(cls, entry, customData, exception)bool

Handle a error occurred in handleEntry. If this function returns True, the queryIter continues, otherwise it breaks and prints the current cursor.

class core.tasks.DeleteEntitiesIter

Bases: core.tasks.QueryIter

Simple Query-Iter to delete all entities encountered.

..Warning: Do not use this iter on skeletons. It only works on the low-level db API and would not clear

relations, locks etc.

classmethod handleEntry(cls, entry, customData)

Overridable hook to process one entry. “entry” will be either an db.Entity or an SkeletonInstance (if that query has been created by skel.all())

Warning: If your query has an sortOrder other than __key__ and you modify that property here it is possible to encounter that object later one again (as it may jump behind the current cursor).