django-simpletask2


Namedjango-simpletask2 JSON
Version 0.1.12 PyPI version JSON
download
home_pageNone
SummaryA simple asynchronous task manager based on the Django framework. All business logic written in a custom task data model. Tasks are triggered by a Redis queue.
upload_time2024-06-18 15:25:57
maintainerChang TianQun
docs_urlNone
authorChang TianQun
requires_pythonNone
licenseMIT
keywords django-simpletask2
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # django-simpletask2

A simple asynchronous task manager based on the Django framework. All business logic written in a custom task data model. Tasks are triggered by a Redis queue.

## Install

```
pip install django-simpletask2
```

## Usage

**pro/settings.py**

```
INSTALLED_APPS = [
    ...
    'django_simpletask2',
    ...
]

# requires redis cache backend, see django-redis for detail
CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://:xxx@xxx:6379/0?decode_responses=True",
    }
}

DJANGO_SIMPLETASK2_TASK_PULL_TIMEOUT = 5 # optional, default to 5
DJANGO_SIMPLETASK2_REDIS_NAME = "default" # optional, default to "default"
DJANGO_SIMPLETASK2_ACLKEY = "Bqud27SzhUymXDuBfvYNHJWQm0i4FdUB" # optional, default to settings.SECRET_KEY
```

**pro/urls.py**

```
from django.urls import path
from django.urls import include

urlpatterns = [
    ...
    path('django-simpletask2/', include("django_simpletask2.urls")),
    ....
]
```

**app/models.py**

```
import base64
from django.db import models
from django_simpletask2.models import SimpleTask


class HelloTask(SimpleTask):
    name = models.CharField(max_length=64)

    def do_task_main(self, payload):
        return "Hello, {}. Nice to meet you!".format(self.name)

class WorldTask(SimpleTask):
    url = models.CharField(max_length=128)
    content = models.TextField(null=True, blank=True)

    is_multi_steps = True
    final_step = 2

    def do_task_main_step1(self, payload=None):
        return {
            "proxy": {
                "method": "GET",
                "url": self.url,
            }
        }
    
    def do_task_main_step2(self, payload=None):
        payload = payload or {}
        base64content = payload.get("proxied_content", None)
        if base64content:
            try:
                content = base64.decodebytes(base64content.encode()).decode("utf-8")
                success = True
            except UnicodeDecodeError:
                try:
                    content = base64.decodebytes(base64content.encode()).decode("gb18030")
                    success = True
                except UnicodeDecodeError:
                    content = "failed to decode proxied_content: {0}".format(base64content)
                    success = False
        else:
            content = payload.get("proxied_error", None)
            success = False

        self.content = content
        self.save()

        if success:
            return True
        else:
            raise RuntimeError("WorldTask.do_task_main_step2 failed....")
```

**etc/django-simpletask2-server-config.yml**

```
redis: "redis://:xxx@xxx:6379/0?decode_responses=True"
channels: default
server: https://localhost:80000/django-simpletask2/
aclkey: xxxx
task-pull-engine: redis
threads: 1
idle-sleep: 5
error-sleep: 5
auto-reset-task-interval: 60
do-auto-reset-task: true
```

**Start django-simpletask2-server to trigger tasks**

```
django-simpletask2-server --config etc/django-simpletask2-server-config.yml start
```

## Error Codes

| Code | Message |
| --- | --- |
| 2910000 | system error. |
| 2910001 | please send request parameters in PAYLOAD format. |
| 2910002 | `aclkey` field is required. |
| 2910003 | aclkey is wrong and access denied. |
| 2910004 | got an already deleted task {task_info}, you may ignore this and continue. |
| 2910005 | bad formatted task_info: {task_info}. |
| 2910006 | Simple task model {task_class_name} not exists. |
| 2910007 | task handler is not implemented, task={app_label}.{model_name}, handler={}. |
| 2910008 | task status is not READY but {status}, you can not start it. |
| 2910009 | calling {handler_name} failed with error message: {error}. |
| 2910010 | save task done status failed with error message: {error}. |
| 2910011 | `task_info` field is required for a multi-steps task. payload={payload}. |
| 2910012 | got NO task in channels: {channels}. |
| 2910013 | task {task_info} locked by another worker. |
| 2910014 | task {app_label}.{model_name}:{task_id} failed to save status with error message: {error}. |
| 2910015 | `task_info` field is required. |
| 2910016 | get task instance {task_info} failed with error: {error}. |


## Releases

### v0.0.1

- First release.

### v0.0.4

- Fixed the problem that the task duplicately executed for the task queue is too long.

### v0.0.5

- Fix do_task problem in actions.
- Fix ugettext_lazy problem.
- Fix problems in django 4.x.

### v0.0.6

- Doc update.

### v0.0.7

- Fix error message formatting problem.

### v0.0.8

- Push to mq after instance-create-transaction committed.

### v0.1.9

- Add SimpleTaskQueue and SimpleTaskChannel management.

### v0.1.10

- Add Update Channel's Queue Size button for SimpleTaskChannelAdmin.

### v0.1.12

- Do remove_duplicate_items_for_all_channels and update_channel_queue_size_for_all_channels at the end of do_auto_reset.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "django-simpletask2",
    "maintainer": "Chang TianQun",
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "django-simpletask2",
    "author": "Chang TianQun",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/e7/f3/0270d8eef9095a007558ae84129f3409d96e04551810933812a5148bef92/django-simpletask2-0.1.12.tar.gz",
    "platform": null,
    "description": "# django-simpletask2\n\nA simple asynchronous task manager based on the Django framework. All business logic written in a custom task data model. Tasks are triggered by a Redis queue.\n\n## Install\n\n```\npip install django-simpletask2\n```\n\n## Usage\n\n**pro/settings.py**\n\n```\nINSTALLED_APPS = [\n    ...\n    'django_simpletask2',\n    ...\n]\n\n# requires redis cache backend, see django-redis for detail\nCACHES = {\n    \"default\": {\n        \"BACKEND\": \"django_redis.cache.RedisCache\",\n        \"LOCATION\": \"redis://:xxx@xxx:6379/0?decode_responses=True\",\n    }\n}\n\nDJANGO_SIMPLETASK2_TASK_PULL_TIMEOUT = 5 # optional, default to 5\nDJANGO_SIMPLETASK2_REDIS_NAME = \"default\" # optional, default to \"default\"\nDJANGO_SIMPLETASK2_ACLKEY = \"Bqud27SzhUymXDuBfvYNHJWQm0i4FdUB\" # optional, default to settings.SECRET_KEY\n```\n\n**pro/urls.py**\n\n```\nfrom django.urls import path\nfrom django.urls import include\n\nurlpatterns = [\n    ...\n    path('django-simpletask2/', include(\"django_simpletask2.urls\")),\n    ....\n]\n```\n\n**app/models.py**\n\n```\nimport base64\nfrom django.db import models\nfrom django_simpletask2.models import SimpleTask\n\n\nclass HelloTask(SimpleTask):\n    name = models.CharField(max_length=64)\n\n    def do_task_main(self, payload):\n        return \"Hello, {}. Nice to meet you!\".format(self.name)\n\nclass WorldTask(SimpleTask):\n    url = models.CharField(max_length=128)\n    content = models.TextField(null=True, blank=True)\n\n    is_multi_steps = True\n    final_step = 2\n\n    def do_task_main_step1(self, payload=None):\n        return {\n            \"proxy\": {\n                \"method\": \"GET\",\n                \"url\": self.url,\n            }\n        }\n    \n    def do_task_main_step2(self, payload=None):\n        payload = payload or {}\n        base64content = payload.get(\"proxied_content\", None)\n        if base64content:\n            try:\n                content = base64.decodebytes(base64content.encode()).decode(\"utf-8\")\n                success = True\n            except UnicodeDecodeError:\n                try:\n                    content = base64.decodebytes(base64content.encode()).decode(\"gb18030\")\n                    success = True\n                except UnicodeDecodeError:\n                    content = \"failed to decode proxied_content: {0}\".format(base64content)\n                    success = False\n        else:\n            content = payload.get(\"proxied_error\", None)\n            success = False\n\n        self.content = content\n        self.save()\n\n        if success:\n            return True\n        else:\n            raise RuntimeError(\"WorldTask.do_task_main_step2 failed....\")\n```\n\n**etc/django-simpletask2-server-config.yml**\n\n```\nredis: \"redis://:xxx@xxx:6379/0?decode_responses=True\"\nchannels: default\nserver: https://localhost:80000/django-simpletask2/\naclkey: xxxx\ntask-pull-engine: redis\nthreads: 1\nidle-sleep: 5\nerror-sleep: 5\nauto-reset-task-interval: 60\ndo-auto-reset-task: true\n```\n\n**Start django-simpletask2-server to trigger tasks**\n\n```\ndjango-simpletask2-server --config etc/django-simpletask2-server-config.yml start\n```\n\n## Error Codes\n\n| Code | Message |\n| --- | --- |\n| 2910000 | system error. |\n| 2910001 | please send request parameters in PAYLOAD format. |\n| 2910002 | `aclkey` field is required. |\n| 2910003 | aclkey is wrong and access denied. |\n| 2910004 | got an already deleted task {task_info}, you may ignore this and continue. |\n| 2910005 | bad formatted task_info: {task_info}. |\n| 2910006 | Simple task model {task_class_name} not exists. |\n| 2910007 | task handler is not implemented, task={app_label}.{model_name}, handler={}. |\n| 2910008 | task status is not READY but {status}, you can not start it. |\n| 2910009 | calling {handler_name} failed with error message: {error}. |\n| 2910010 | save task done status failed with error message: {error}. |\n| 2910011 | `task_info` field is required for a multi-steps task. payload={payload}. |\n| 2910012 | got NO task in channels: {channels}. |\n| 2910013 | task {task_info} locked by another worker. |\n| 2910014 | task {app_label}.{model_name}:{task_id} failed to save status with error message: {error}. |\n| 2910015 | `task_info` field is required. |\n| 2910016 | get task instance {task_info} failed with error: {error}. |\n\n\n## Releases\n\n### v0.0.1\n\n- First release.\n\n### v0.0.4\n\n- Fixed the problem that the task duplicately executed for the task queue is too long.\n\n### v0.0.5\n\n- Fix do_task problem in actions.\n- Fix ugettext_lazy problem.\n- Fix problems in django 4.x.\n\n### v0.0.6\n\n- Doc update.\n\n### v0.0.7\n\n- Fix error message formatting problem.\n\n### v0.0.8\n\n- Push to mq after instance-create-transaction committed.\n\n### v0.1.9\n\n- Add SimpleTaskQueue and SimpleTaskChannel management.\n\n### v0.1.10\n\n- Add Update Channel's Queue Size button for SimpleTaskChannelAdmin.\n\n### v0.1.12\n\n- Do remove_duplicate_items_for_all_channels and update_channel_queue_size_for_all_channels at the end of do_auto_reset.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A simple asynchronous task manager based on the Django framework. All business logic written in a custom task data model. Tasks are triggered by a Redis queue.",
    "version": "0.1.12",
    "project_urls": null,
    "split_keywords": [
        "django-simpletask2"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "82ac4768b84317ef398f4f0238cc78ced063ef64b50d6426bd32a1110ba765ad",
                "md5": "a088116b758fa9d31684e7dcda341449",
                "sha256": "cd506910d9996ec0fca727fd3e2c2db0fe77c8b3856cc10a36c2bb6b2c3f145e"
            },
            "downloads": -1,
            "filename": "django_simpletask2-0.1.12-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "a088116b758fa9d31684e7dcda341449",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 27700,
            "upload_time": "2024-06-18T15:25:50",
            "upload_time_iso_8601": "2024-06-18T15:25:50.111028Z",
            "url": "https://files.pythonhosted.org/packages/82/ac/4768b84317ef398f4f0238cc78ced063ef64b50d6426bd32a1110ba765ad/django_simpletask2-0.1.12-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e7f30270d8eef9095a007558ae84129f3409d96e04551810933812a5148bef92",
                "md5": "b2a4e7f426ac54c298aa6e0a243e854c",
                "sha256": "0a7a6190834a450ca1ea434f53d9a235d06725ce440597ee09c07393f86652ed"
            },
            "downloads": -1,
            "filename": "django-simpletask2-0.1.12.tar.gz",
            "has_sig": false,
            "md5_digest": "b2a4e7f426ac54c298aa6e0a243e854c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 22312,
            "upload_time": "2024-06-18T15:25:57",
            "upload_time_iso_8601": "2024-06-18T15:25:57.152110Z",
            "url": "https://files.pythonhosted.org/packages/e7/f3/0270d8eef9095a007558ae84129f3409d96e04551810933812a5148bef92/django-simpletask2-0.1.12.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-06-18 15:25:57",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "django-simpletask2"
}
        
Elapsed time: 0.95722s