Skip to main content

A lightweight task runner for django with easy admin controls

Project description

Side Kick

Side Kick is a simple, lightweight scheduler for Django management commands. Anything can be a management command, whether it is doing database back ups or sending emails. Simply create the task, add the decorator and then in Django's admin you can set when it should run and also enable or disable it. It will also make sure that the task will only run if there is no other instance of the task already running to avoid unexpected issues if a task takes longer to complete than expected.

Installation:

pip install side-kick

Make sure you add sidekick to your installed apps.

Create a file somewhere within the project called tasks.txt. The file can be wherever you want, as long as it is inside the main project directory. This is where the tasks will be written to, which will be explained shortly.

Add the following to your settings file:

SIDEKICK = {
    'SIDEKICK_REGISTERED_APPS': [],
    'ENVIRONMENT': "",
    'DJANGO_PATH': "",
    'CRON_PATH': "",
    'LOCK_PATH': ""
}

SIDEKICK_REGISTERED_APPS is where you will need to define all the apps which have tasks that sidekick will handle. You just need to put the app name and then as soon as django loads it will import the tasks.py file of each app within this list. Any tasks that have the @sidekick_task decorator will create a new RegisteredTask instance if it does not already exist.

Each time you want to register a new task, don't forget to add it to this list. SIDEKICK_REGISTERED_APPS = ['stock', 'customers', 'pizzas',]

ENVIRONMENT is where you will define the user and the start-up file of that user for example:

ENVIRONMENT = "root . /root/.profile"

DJANGO_PATH is the path to your project directory and then the path to python, followed by the manage.py command which will be used to trigger the tasks. For example:

DJANGO_PATH = "/var/www/myproject && /var/www/virtualenv/bin/python manage.py"

CRON_PATH is the path to the tasks.txt file that you created earlier. This is where the cron files will be written, so you can check within this file once you have registered a task that it is working correctly.

CRON_PATH = "/var/www/myproject/sidekick_tasks/tasks.txt"

LOCK_FILE is the path to a directory where the lock files will be created when a task starts running and then deleted from when it is completed. The reason for this is to stop the same task running concurrently if the first instance of the task hasn't completed yet. This also can go anywhere but for ease of use I would suggest keeping it within the same directory you choose to create the tasks.txt file.

LOCK_PATH = "/var/www/myproject/sidekick_tasks/lock_files/"

These settings were designed so you can customise them and use different paths depending on the environment you are working in etc.

Once this is done, you will need to migrate to create the side kick models which you can then manage through the django admin.

The basic set up is now complete!

When it comes to creating and registering tasks, follow these simple steps:

Add the @sidekick_task decorator to any tasks you wish to register, make sure the task is in your tasks.py file of your app:

from sidekick.decorators import sidekick_task

@sidekick_task
def my_task():
    # Whatever task you wish to complete
    ...

Then add the name of the app to the SIDEKICK_REGISTERED_APPS list in your settings file.

Create a new directory within the app called management and then a subdirectory called commands. Add a __init__.py file and then a file with the name of the app eg. customers.py to the commands directory.

If you already have management commands in this file, that is fine, you can skip this step, but make sure to add the code for add_arguments() and also handle() in the next step.

File structure would be as follows:

myproject
|_ customers
   |_management
     |_commands
        |_ __init__.py
        |_ customers.py

Within customers.py (or whatever your app is) add the following:

import logging

from django.core.management.base import BaseCommand
from sidekick.services.helpers import get_task_name, get_app_name
from sidekick.services.crontab import CronTask

from sidekick.models import RegisteredTask

logger = logging.getLogger(__name__)
app_name = get_app_name(__name__)


class Command(BaseCommand):
    help = "Commands for the Stock app"

    def add_arguments(self, parser):
        """Defines the arguments """

        for task in RegisteredTask.objects.filter(task_name__startswith=app_name):
            task_name = task.task_name.split(' ')[1]
            parser.add_argument(
                task_name,
                action='store_true',
                dest=task_name[2:]
            )

    def handle(self, *args, **options):
        """Handle stock management commands.

        :param args:
        :param options: Arguments passed with command e.g. send_emails_to_customers, verbosity etc.
        """
        task_name = get_task_name(options)
        rt_task_name = "{} --{}".format(app_name, task_name)

        if RegisteredTask.objects.filter(task_name=rt_task_name):
            try:
                CronTask(task_name=task_name, app=app_name).run()
            except Exception as e:
                logger.error(msg=e)

You will need to have this same file structure in each app you want to have tasks registered to.

Once this is done, you need to create a file within the /etc/cron.d directory called anything you like, I would suggest something like sidekick_tasks, and then create a sym link between this file and the tasks.txt file you created earlier.

You can do this by connecting to your server and then:

cd /etc/cron.d
touch sidekick_tasks
ln -sf /var/www/myproject/path/to/tasks.txt /etc/cron.d/sidekick_tasks

Once this is done then you're all good to go, you can now register tasks with a simple decorator and easily manage them using django admin.

Side Kick Admin Example

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

side-kick-0.1.6.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

side_kick-0.1.6-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file side-kick-0.1.6.tar.gz.

File metadata

  • Download URL: side-kick-0.1.6.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/32.2.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.1

File hashes

Hashes for side-kick-0.1.6.tar.gz
Algorithm Hash digest
SHA256 88ed7e44b700afe9646f35370c12a966159d9c4ce51aa830492ce0989ba1d338
MD5 0054b1fb3a2d2f45ca21509b7a737b5f
BLAKE2b-256 d54b3a2637acb143333cb744fafc1c64dc53e448b11da9f843494a90ac497498

See more details on using hashes here.

File details

Details for the file side_kick-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: side_kick-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 13.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/32.2.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.1

File hashes

Hashes for side_kick-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 f116e8dee19889af73d85e420c0b3a1dd3964f172a9802bd2f8597f16240ee84
MD5 5299fed5b0d490ab0c04a0e8075d7652
BLAKE2b-256 03136739dfba43856737118e347b810c03872ba23d7d39ccd4b39de26bcd9b96

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page