Skip to main content

django-commands

documentation

PyPI - Version PyPI - Python Version


Table of Contents

Installation

  1. pip install
pip install django-commands2
  1. add django_commands to INSTALLED_APPS, and logging
INSTALLED_APPS = [
    ...
    'django_commands',
]
LOGGING = {
    "loggers": {
        "django_commands": {
            ...your custom level, handles config...
        }
    }
}
DJANGO_COMMANDS_ALLOW_REMOTE_CALL = [
    "slow_command",  # add slow_command if you want to run unittest
    <your command>
]
  1. add url config like if you want to enable call command by request:
path('api/django-commands/', include("django_commands.urls")),

Call Command from url

import requests
requests.post("/api/django-commands/call-command", {"command": "slow_command"})

Usage

AutoLogCommands

any exception will be logged in the autologcommand

<yourapp/management/commands/command_name.py>
from django_commands import AutoLogCommand


class Command(AutoLogCommand):

    def handle(self):
        <write your code, any exception will be logged>

MultiTimesCommand

MultiTimesCommand will run multi times according to INTERVAL and MAX_TIMES. You can easily use this command to realize a crontab job every 1 second.

class Command(MultiTimesCommand):
    INTERVAL = 1  # default 1
    MAX_TIMES = 60  # default 60

    def handle(self):
        <this handle function will run 60 times>

This command does not consider the running time of your code. It will just run 60 times, and during each execute, wait 1 second

DurationCommand(AutoLogCommand):

DurationCommand will run your commands over and over again until the running time exceed the configuration

import datetime

class Commmand(DurationCommand):
    INTERVAL = 1
    DURATION = datetime.timedelta(minutes=1)

    def handle(self, *args, **kwargs):
        <your code>

UniqueCommand

UniqueCommand can assert that only one command instance is running

How it works? Every time the command executes:

  1. it will create a django_commands.models.CommandLog instance
  2. it will check if there is another Command Instance pending with the same UNIQUE_NAME
import time
from django_commands.commands import UniqueCommand

class Command(UniqueCommand):
    def handle(self):
        print("I'm running. In the next 5 seconds, you cannot execute this command. It will exist directly")
        time.sleep(5)
        print("I'm running")
        raise Exception("even error occurs, this task will be set finished")

License

django-commands is distributed under the terms of the ONLY USE NO PRIVATE CHANGE LICENSE license.

Anyone can use pip install django-commands to use this project by any meaning as long as you keep the source code unchanged.
You are not allowed to change the source code without publishing your change.
Here publishing means you: fork this project from github and keep your change available to public on the github

Metadata

Release files for django-commands2 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-commands2 1.0.0
File Size Uploaded
django_commands2-1.0.0.tar.gz 11.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-commands2 1.0.0
File Interpreter ABI Platform
django_commands2-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 29.6 kB

Release files / django_commands2-1.0.0.tar.gz

Download URL django_commands2-1.0.0.tar.gz
Size 11.3 kB
Tags Source
SHA-256 checksum
How to use checksums
48b457d24c853d479bd38a64ac4f2ea509a2e2c872ca183f02b3d2a401d9308e
BLAKE2b-256 checksum
How to use checksums
c6d897d07f79e05a56615db40fb7083c44e72bcce932dc81be8a0ea42a77f418
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Hatch/1.16.5 cpython/3.14.3 HTTPX/0.28.1

Release files / django_commands2-1.0.0-py3-none-any.whl

Download URL django_commands2-1.0.0-py3-none-any.whl
Size 18.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f33ce16b838dbbef1d5eddb512083bc5d484f176106ccc8b941653930387eac3
BLAKE2b-256 checksum
How to use checksums
b10f316d11d02d18cee56b2e44a6409841dea2b1452aa9e9ef9fb9abbc047364
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Hatch/1.16.5 cpython/3.14.3 HTTPX/0.28.1

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.26.0

2 release files

0.24.2

2 release files

0.24.1

2 release files

0.23.1

2 release files

0.23.0

2 release files

0.22.0

2 release files

0.21.0

2 release files

0.18.0

2 release files

0.16.1

2 release files

0.15.1

2 release files

0.15.0

2 release files

0.14.0

2 release files

0.10.6

2 release files

0.10.5

2 release files

0.10.4

2 release files

0.10.1

2 release files

0.10.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.7

2 release files

0.3.6

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page