django-commands2


Namedjango-commands2 JSON
Version 0.19.0 PyPI version JSON
download
home_pageNone
SummaryNone
upload_time2024-10-03 08:36:22
maintainerNone
docs_urlNone
authorNone
requires_python>=3.7
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # django-commands

[documentation](https://django-commands.readthedocs.io/zh-cn/latest/)  

[![PyPI - Version](https://img.shields.io/pypi/v/django-commands.svg)](https://pypi.org/project/django-commands)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/django-commands.svg)](https://pypi.org/project/django-commands)

-----

**Table of Contents**

- [Installation](#installation)
- [Usage](#Usage)
- [License](#license)

## Installation
1. pip install
```console
pip install django-commands2
```

2. add django_commands to INSTALLED_APPS, and logging
```python
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>
]
```

3. add url config like if you want to enable call command by request:

```python
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.

```python
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
```python
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

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "django-commands2",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": null,
    "keywords": null,
    "author": null,
    "author_email": "Xiang Wang <ramwin@qq.com>",
    "download_url": "https://files.pythonhosted.org/packages/39/30/08f36940e0998ad5b180569cc65165cc68bf270da90a22daedd4d3faa546/django_commands2-0.19.0.tar.gz",
    "platform": null,
    "description": "# django-commands\n\n[documentation](https://django-commands.readthedocs.io/zh-cn/latest/)  \n\n[![PyPI - Version](https://img.shields.io/pypi/v/django-commands.svg)](https://pypi.org/project/django-commands)\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/django-commands.svg)](https://pypi.org/project/django-commands)\n\n-----\n\n**Table of Contents**\n\n- [Installation](#installation)\n- [Usage](#Usage)\n- [License](#license)\n\n## Installation\n1. pip install\n```console\npip install django-commands2\n```\n\n2. add django_commands to INSTALLED_APPS, and logging\n```python\nINSTALLED_APPS = [\n    ...\n    'django_commands',\n]\nLOGGING = {\n    \"loggers\": {\n        \"django_commands\": {\n            ...your custom level, handles config...\n        }\n    }\n}\nDJANGO_COMMANDS_ALLOW_REMOTE_CALL = [\n    \"slow_command\",  # add slow_command if you want to run unittest\n    <your command>\n]\n```\n\n3. add url config like if you want to enable call command by request:\n\n```python\npath('api/django-commands/', include(\"django_commands.urls\")),\n```\n\n## Call Command from url\n```\nimport requests\nrequests.post(\"/api/django-commands/call-command\", {\"command\": \"slow_command\"})\n```\n\n\n## Usage\n### AutoLogCommands\nany exception will be logged in the autologcommand\n```\n<yourapp/management/commands/command_name.py>\nfrom django_commands import AutoLogCommand\n\n\nclass Command(AutoLogCommand):\n\n    def handle(self):\n        <write your code, any exception will be logged>\n```\n\n\n\n### MultiTimesCommand\nMultiTimesCommand will run multi times according to `INTERVAL` and `MAX_TIMES`. You can easily use this command to realize a crontab job every 1 second.\n\n```python\nclass Command(MultiTimesCommand):\n    INTERVAL = 1  # default 1\n    MAX_TIMES = 60  # default 60\n\n    def handle(self):\n        <this handle function will run 60 times>\n```\n*This command does not consider the running time of your code. It will just run 60 times, and during each execute, wait 1 second*\n\n### DurationCommand(AutoLogCommand):\nDurationCommand will run your commands over and over again until the running time exceed the configuration\n```python\nimport datetime\n\nclass Commmand(DurationCommand):\n    INTERVAL = 1\n    DURATION = datetime.timedelta(minutes=1)\n\n    def handle(self, *args, **kwargs):\n        <your code>\n```\n\n### UniqueCommand\nUniqueCommand can assert that only one command instance is running\n\nHow it works?\nEvery time the command executes:\n\n1. it will create a django_commands.models.CommandLog instance\n2. it will check if there is another Command Instance pending with the same UNIQUE_NAME\n\n```\nimport time\nfrom django_commands.commands import UniqueCommand\n\nclass Command(UniqueCommand):\n    def handle(self):\n        print(\"I'm running. In the next 5 seconds, you cannot execute this command. It will exist directly\")\n        time.sleep(5)\n        print(\"I'm running\")\n        raise Exception(\"even error occurs, this task will be set finished\")\n```\n\n## License\n\n`django-commands` is distributed under the terms of the ONLY USE NO PRIVATE CHANGE LICENSE license.\n\nAnyone can use `pip install django-commands` to use this project by any meaning as long as you keep the source code unchanged.  \nYou are not allowed to change the source code without publishing your change.  \nHere publishing means you:\n    fork this project from github and keep your change available to public on the github\n",
    "bugtrack_url": null,
    "license": null,
    "summary": null,
    "version": "0.19.0",
    "project_urls": {
        "Documentation": "https://github.com/ramwin/django-commands#readme",
        "Issues": "https://github.com/ramwin/django-commands/issues",
        "Source": "https://github.com/ramwin/django-commands"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "c83b2686a150456ef6c501d445434e074683f020a85b612ff6e1dfc1224161f6",
                "md5": "9753fd0631613958b09314e0e855e914",
                "sha256": "1aea41b2321d3ad38c9edd20a1e2e462c26e965e7f6f825ef69b99fd9c8eea6a"
            },
            "downloads": -1,
            "filename": "django_commands2-0.19.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "9753fd0631613958b09314e0e855e914",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 15884,
            "upload_time": "2024-10-03T08:36:24",
            "upload_time_iso_8601": "2024-10-03T08:36:24.050783Z",
            "url": "https://files.pythonhosted.org/packages/c8/3b/2686a150456ef6c501d445434e074683f020a85b612ff6e1dfc1224161f6/django_commands2-0.19.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "393008f36940e0998ad5b180569cc65165cc68bf270da90a22daedd4d3faa546",
                "md5": "a81d1126e0f5b0615b6d9eb5e2e32c86",
                "sha256": "aba69bea11a7037ef18ce98641e96fe1f0a520aa055081a7963912fb43490611"
            },
            "downloads": -1,
            "filename": "django_commands2-0.19.0.tar.gz",
            "has_sig": false,
            "md5_digest": "a81d1126e0f5b0615b6d9eb5e2e32c86",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 10171,
            "upload_time": "2024-10-03T08:36:22",
            "upload_time_iso_8601": "2024-10-03T08:36:22.362301Z",
            "url": "https://files.pythonhosted.org/packages/39/30/08f36940e0998ad5b180569cc65165cc68bf270da90a22daedd4d3faa546/django_commands2-0.19.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-03 08:36:22",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "ramwin",
    "github_project": "django-commands#readme",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "django-commands2"
}
        
Elapsed time: 2.78824s