No project description provided
Project description
django-commands
Table of Contents
Installation
- pip install
pip install django-commands2
- 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>
]
- 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:
- it will create a django_commands.models.CommandLog instance
- 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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django_commands2-0.22.0.tar.gz
.
File metadata
- Download URL: django_commands2-0.22.0.tar.gz
- Upload date:
- Size: 11.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.27.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | de40fe07608d60c6ef4321c384a4eb8b00ca4676f672efe9432c05d825313b77 |
|
MD5 | 2e50354d7af1defacd1fdc5f926c90c2 |
|
BLAKE2b-256 | cb196416fe64ec3094df8be5908b896ca7787ca84e651e996ec3c421bce93e08 |
File details
Details for the file django_commands2-0.22.0-py3-none-any.whl
.
File metadata
- Download URL: django_commands2-0.22.0-py3-none-any.whl
- Upload date:
- Size: 17.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.27.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b37818927a9d5b1170dc871c1384fa21aed7b3b81334bc5c0a32920e5d2d29d9 |
|
MD5 | fa0d17953c70622ba0b5fa0cec963b13 |
|
BLAKE2b-256 | 7e1557007ef4d6993be7d40759987a93f4151571bebafc2d5ab905034788737e |