Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
October 14, 2022 03:46 pm GMT

Django Tasks Manager - Free PyPi Library

Hello Coders!

This article presents an open-source sample that explains step-by-step how to use Django Tasks Manager library in a new project. The commits are made to highlight each step of the implementation starting from an empty directory up to the final phase when the project manages the tasks through UI controls. Thanks for reading!

What's in the box

The library used by the sample is powered by Django and Celery, a popular task queue manager, and provides a simple way to execute background tasks in full control: start/stop, view output, and runtime logs. Here is the full list of library features:

  • Create/Revoke Celery Tasks
  • View LOGS & Output
  • Minimal Configuration
  • Installation via PyPi
  • Available TASKS (provided as starting samples)
  • Task users_in_db() - List all registered users
  • Task execute_script() - let users execute system scripts

In the end, this UI exposed by the tool is similar to this:

Django Celery Integration - Tasks Board

Django Celery Integration - Task Runtime Log

In case this library sounds useful, the integration steps for any Django project are presented below.

An important aspect is the Redis service dependency required by Celery used to manage and tasks state tracking in real-time.

Step 1 - Install django-tasks-manager via PIP

The recommended way to do this is to use a virtual environment that isolates the installation of the library.

$ pip install django-tasks-manager 

Once the installation is finished the next step is to create two directories used by the library:

  • celery_logs - used to log the runtime execution of the tasks
  • celery_scripts - the location where the library search for scrips to be executed

The package ships a few simple scripts as a proof of concept available for download from the public repository.

Step 2 - Update app routing

# core/urls.pyfrom django.urls import path, include     # <-- UPDATE: Add 'include' HELPERurlpatterns = [    ...    path("", include("django_tm.urls")),  # <-- New Routes    ...]

Step 3 - Update Configuration, include the new APPS

# App Settings, truncated contentINSTALLED_APPS = [    ...                      'django_tm',              # Django Tasks Manager   # <-- NEW    'django_celery_results',  # Django Celery Results  # <-- NEW]

Step 4 - Update Configuration, include page templates

# App Settings, truncated contentTEMPLATE_DIR_TASKS = os.path.join(BASE_DIR, "django_tm/templates") # <-- NEWTEMPLATES = [    {        'BACKEND': 'django.template.backends.django.DjangoTemplates',           'DIRS': [TEMPLATE_DIR_TASKS],                              # <-- NEW        'APP_DIRS': True,    },]

Step 5 - Update Configuration, New CELERY_ Section

############################################################## Celery configurations# BASE_DIR points to the ROOT of the project# Note: make sure you have 'os' object importedBASE_DIR = os.path.dirname(os.path.dirname(__file__))# Working Directories required write permissionCELERY_SCRIPTS_DIR        = os.path.join(BASE_DIR, "celery_scripts" )CELERY_LOGS_DIR           = os.path.join(BASE_DIR, "celery_logs"    )CELERY_BROKER_URL         = os.environ.get("CELERY_BROKER", "redis://localhost:6379")CELERY_RESULT_BACKEND     = os.environ.get("CELERY_BROKER", "redis://localhost:6379")CELERY_TASK_TRACK_STARTED = TrueCELERY_TASK_TIME_LIMIT    = 30 * 60CELERY_CACHE_BACKEND      = "django-cache"CELERY_RESULT_BACKEND     = "django-db"CELERY_RESULT_EXTENDED    = TrueCELERY_RESULT_EXPIRES     = 60*60*24*30 # Results expire after 1 monthCELERY_ACCEPT_CONTENT     = ["json"]CELERY_TASK_SERIALIZER    = 'json'CELERY_RESULT_SERIALIZER  = 'json'#############################################################

Step 6 - Migrate the database & start the app

$ python manage.py makemigrations$$ python manage.py migrate$ $ python manage.py runserver

Step 7 - Start the Celery manager (using another terminal)

$ celery --app=django_tm.celery.app worker --loglevel=info 

The superusers now can connect to the UI and orchestrate the tasks visually at this address: http://127.0.0.1:8000/tasks

Django Celery Integration - Visualize Finished Tasks

Thanks for reading! For more resources, feel free to access:

PROMO (contains affiliate links)

In case you're a junior developer or know one, this [PROMO Bundle crafted, and Discounted with 85% by Creative-Tim might be useful. The package includes a rock-solid collection of premium assets (Kits & Dashboards) that can be used to build eye-catching portfolios and web apps in no time.

Junior PROMO Bundle - 24 PREMIUM Kits & Designer Files

Promo Bundle for Junior Developers - By Creative-Tim.


Original Link: https://dev.to/sm0ke/django-tasks-manager-open-source-pypi-library-1b8

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To