Helpers

In addition to the fully-functional built-in tasks provided by jogger, it also includes some helpers to assist in writing custom tasks.

DjangoTask

A replacement for the default Task parent for a class-based task, DjangoTask is designed for tasks that require a configured Django environment in order to run. For example, tasks that need to import and query Django models.

In order to configure the Django environment, DjangoTask needs to know the path to the Django settings module. This can be provided using the django_settings_module class attribute, which should be set to the dotted path of the settings module, relative to the project directory, e.g. 'my_project.settings'.

DjangoTask will automatically import the configured settings module and make it available as the django_settings instance attribute. This avoids the need to manually import the settings module, as a standard module-level import will not work. Note that any additional imports from the Django project should be made within the task’s methods, not at the module level.

from jogger.tasks import DjangoTask


class MyTask(DjangoTask):

    django_settings_module = 'myproject.settings'

    def handle(self, *args, **options):

        from myproject.myapp.models import MyModel

        if not self.django_settings.DEBUG:
            raise TaskError('Can only be run in DEBUG mode.')

        # Do something with MyModel

configure_django

A function that can be called by tasks that require a configured Django environment in order to run. For example, tasks that need to import and query Django models. While DjangoTask handles this requirement for class-based tasks, configure_django() can be used by function-based tasks or any other scripts.

In order to configure the Django environment, configure_django() needs to know the path to both the project directory and the Django settings module, which it takes as arguments. It returns the configured Django settings object, so that Django settings can be accessed without needing to subsequently import django.conf.settings.

Note that any imports from the Django project should be made after calling this function, not at the module level.

import os

from jogger.tasks import configure_django


def my_task(settings, stdout, stderr):

    project_dir = os.path.dirname(os.path.abspath(__file__))
    django_settings = configure_django(project_dir, 'myproject.settings')

    from myproject.myapp.models import MyModel

    if not django_settings.DEBUG:
        raise TaskError('Can only be run in DEBUG mode.')

    # Do something with MyModel