django-apscheduler is a third party library and this library supports admin mode.

Tutorials : https://pypi.org/project/django-apscheduler/

 

1. Installation (Develop)

(BlogVenv) C:\Work\Software\eclipse-jee-2019-12-R-win32-x86_64\workspace\blog.daonelab.com>pip list
Package              Version
-------------------- ----------
asgiref              3.2.3
certifi              2019.11.28
chardet              3.0.4
defusedxml           0.6.0
Django               3.0.6
django-allauth       0.41.0
django-debug-toolbar 2.2
django-disqus        0.5
django-js-asset      1.2.2
django-mptt          0.11.0
django-tagging       0.5.0
idna                 2.9
mock                 3.0.5
oauthlib             3.1.0
Pillow               7.0.0
pip                  22.2.2
psycopg2-binary      2.9.3
python3-openid       3.1.0
pytz                 2019.3
requests             2.23.0
requests-oauthlib    1.3.0
setuptools           56.0.0
six                  1.14.0
sqlparse             0.3.0
urllib3              1.25.8
wheel                0.36.2

(BlogVenv) C:\Work\Software\eclipse-jee-2019-12-R-win32-x86_64\workspace\blog.daonelab.com>pip install django-apscheduler
Collecting django-apscheduler
  Downloading django_apscheduler-0.6.2-py3-none-any.whl (24 kB)
Collecting django>=3.2
  Downloading Django-3.2.15-py3-none-any.whl (7.9 MB)
     ---------------------------------------- 7.9/7.9 MB 3.7 MB/s eta 0:00:00
Collecting apscheduler<4.0,>=3.2
  Using cached APScheduler-3.9.1-py2.py3-none-any.whl (59 kB)
Requirement already satisfied: six>=1.4.0 in c:\work\software\eclipse-jee-2019-12-r-win32-x86_64\workspace\blog.daonelab.com\blogvenv\lib\site-packages (from apscheduler<4.0,>=3.2->django-apscheduler) (1.14.0)
Requirement already satisfied: pytz in c:\work\software\eclipse-jee-2019-12-r-win32-x86_64\workspace\blog.daonelab.com\blogvenv\lib\site-packages (from apscheduler<4.0,>=3.2->django-apscheduler) (2019.3)
Collecting tzlocal!=3.*,>=2.0
  Using cached tzlocal-4.2-py3-none-any.whl (19 kB)
Requirement already satisfied: setuptools>=0.7 in c:\work\software\eclipse-jee-2019-12-r-win32-x86_64\workspace\blog.daonelab.com\blogvenv\lib\site-packages (from apscheduler<4.0,>=3.2->django-apscheduler) (56.0.0)
Collecting asgiref<4,>=3.3.2
  Using cached asgiref-3.5.2-py3-none-any.whl (22 kB)
Requirement already satisfied: sqlparse>=0.2.2 in c:\work\software\eclipse-jee-2019-12-r-win32-x86_64\workspace\blog.daonelab.com\blogvenv\lib\site-packages (from django>=3.2->django-apscheduler) (0.3.0)
Collecting typing-extensions
  Using cached typing_extensions-4.3.0-py3-none-any.whl (25 kB)
Collecting backports.zoneinfo
  Using cached backports.zoneinfo-0.2.1-cp37-cp37m-win_amd64.whl (38 kB)
Collecting pytz-deprecation-shim
  Using cached pytz_deprecation_shim-0.1.0.post0-py2.py3-none-any.whl (15 kB)
Collecting tzdata
  Using cached tzdata-2022.2-py2.py3-none-any.whl (336 kB)
Installing collected packages: tzdata, typing-extensions, backports.zoneinfo, pytz-deprecation-shim, asgiref, tzlocal, django, apscheduler, django-apscheduler
  Attempting uninstall: asgiref
    Found existing installation: asgiref 3.2.3
    Uninstalling asgiref-3.2.3:
      Successfully uninstalled asgiref-3.2.3
  Attempting uninstall: django
    Found existing installation: Django 3.0.6
    Uninstalling Django-3.0.6:
      Successfully uninstalled Django-3.0.6
Successfully installed apscheduler-3.9.1 asgiref-3.5.2 backports.zoneinfo-0.2.1 django-3.2.15 django-apscheduler-0.6.2 pytz-deprecation-shim-0.1.0.post0 typing-extensions-4.3.0 tzdata-2022.2 tzlocal-4.2

(BlogVenv) C:\Work\Software\eclipse-jee-2019-12-R-win32-x86_64\workspace\blog.daonelab.com>pip list
Package               Version
--------------------- -----------
APScheduler           3.9.1 # added
asgiref               3.5.2  # upgraded
backports.zoneinfo    0.2.1 # added
certifi               2019.11.28
chardet               3.0.4
defusedxml            0.6.0
Django                3.2.15 # upgraded
django-allauth        0.41.0
django-apscheduler    0.6.2 # added
django-debug-toolbar  2.2
django-disqus         0.5
django-js-asset       1.2.2
django-mptt           0.11.0
django-tagging        0.5.0
idna                  2.9
mock                  3.0.5
oauthlib              3.1.0
Pillow                7.0.0
pip                   22.2.2
psycopg2-binary       2.9.3
python3-openid        3.1.0
pytz                  2019.3
pytz-deprecation-shim 0.1.0.post0 # added
requests              2.23.0
requests-oauthlib     1.3.0
setuptools            56.0.0
six                   1.14.0
sqlparse              0.3.0
typing_extensions     4.3.0 # added
tzdata                2022.2 # added
tzlocal               4.2 # added
urllib3               1.25.8
wheel                 0.36.2

 

Product

(BlogVenv) [root@centOS7 blog.daonelab.com]# pip list
Package              Version
-------------------- ----------
asgiref              3.2.3
certifi              2019.11.28
chardet              3.0.4
defusedxml           0.6.0
Django               3.0.6
django-allauth       0.41.0
django-debug-toolbar 2.2
django-disqus        0.5
django-js-asset      1.2.2
django-mptt          0.11.0
django-tagging       0.5.0
idna                 2.9
mock                 3.0.5
oauthlib             3.1.0
Pillow               7.0.0
pip                  21.3.1
psycopg2-binary      2.9.3
python3-openid       3.1.0
pytz                 2019.3
requests             2.23.0
requests-oauthlib    1.3.0
setuptools           28.8.0
six                  1.14.0
sqlparse             0.3.0
urllib3              1.25.8
uWSGI                2.0.18
(BlogVenv) [root@centOS7 blog.daonelab.com]# pip install django-apscheduler
Collecting django-apscheduler
  Downloading django_apscheduler-0.6.2-py3-none-any.whl (24 kB)
Collecting apscheduler<4.0,>=3.2
  Using cached APScheduler-3.9.1-py2.py3-none-any.whl (59 kB)
Collecting django>=3.2
  Downloading Django-3.2.15-py3-none-any.whl (7.9 MB)
     |████████████████████████████████| 7.9 MB 10.5 MB/s
Requirement already satisfied: six>=1.4.0 in ./BlogVenv/lib/python3.6/site-packages (from apscheduler<4.0,>=3.2->django-apscheduler) (1.14.0)
Collecting tzlocal!=3.*,>=2.0
  Using cached tzlocal-4.2-py3-none-any.whl (19 kB)
Requirement already satisfied: setuptools>=0.7 in ./BlogVenv/lib/python3.6/site-packages (from apscheduler<4.0,>=3.2->django-apscheduler) (28.8.0)
Requirement already satisfied: pytz in ./BlogVenv/lib/python3.6/site-packages (from apscheduler<4.0,>=3.2->django-apscheduler) (2019.3)
Collecting asgiref<4,>=3.3.2
  Using cached asgiref-3.4.1-py3-none-any.whl (25 kB)
Requirement already satisfied: sqlparse>=0.2.2 in ./BlogVenv/lib/python3.6/site-packages (from django>=3.2->django-apscheduler) (0.3.0)
Collecting typing-extensions
  Using cached typing_extensions-4.1.1-py3-none-any.whl (26 kB)
Collecting pytz-deprecation-shim
  Using cached pytz_deprecation_shim-0.1.0.post0-py2.py3-none-any.whl (15 kB)
Collecting backports.zoneinfo
  Using cached backports.zoneinfo-0.2.1-cp36-cp36m-manylinux1_x86_64.whl (70 kB)
Collecting importlib-resources
  Using cached importlib_resources-5.4.0-py3-none-any.whl (28 kB)
Collecting tzdata
  Using cached tzdata-2022.2-py2.py3-none-any.whl (336 kB)
Collecting zipp>=3.1.0
  Using cached zipp-3.6.0-py3-none-any.whl (5.3 kB)
Installing collected packages: zipp, importlib-resources, tzdata, backports.zoneinfo, typing-extensions, pytz-deprecation-shim, tzlocal, asgiref, django, apscheduler, django-apscheduler
  Attempting uninstall: asgiref
    Found existing installation: asgiref 3.2.3
    Uninstalling asgiref-3.2.3:
      Successfully uninstalled asgiref-3.2.3
  Attempting uninstall: django
    Found existing installation: Django 3.0.6
    Uninstalling Django-3.0.6:
      Successfully uninstalled Django-3.0.6
Successfully installed apscheduler-3.9.1 asgiref-3.4.1 backports.zoneinfo-0.2.1 django-3.2.15 django-apscheduler-0.6.2 importlib-resources-5.4.0 pytz-deprecation-shim-0.1.0.post0 typing-extensions-4.1.1 tzdata-2022.2 tzlocal-4.2 zipp-3.6.0
(BlogVenv) [root@centOS7 blog.daonelab.com]# pip list
Package               Version
--------------------- -----------
APScheduler           3.9.1 # added
asgiref               3.4.1 # upgraded
backports.zoneinfo    0.2.1 # added
certifi               2019.11.28
chardet               3.0.4
defusedxml            0.6.0
Django                3.2.15 # upgraded
django-allauth        0.41.0
django-apscheduler    0.6.2 # added
django-debug-toolbar  2.2
django-disqus         0.5
django-js-asset       1.2.2
django-mptt           0.11.0
django-tagging        0.5.0
idna                  2.9
importlib-resources   5.4.0 # added
mock                  3.0.5
oauthlib              3.1.0
Pillow                7.0.0
pip                   21.3.1
psycopg2-binary       2.9.3
python3-openid        3.1.0
pytz                  2019.3
pytz-deprecation-shim 0.1.0.post0 # added
requests              2.23.0
requests-oauthlib     1.3.0
setuptools            28.8.0
six                   1.14.0
sqlparse              0.3.0
typing_extensions     4.1.1 # added
tzdata                2022.2 # added
tzlocal               4.2 # added
urllib3               1.25.8
uWSGI                 2.0.18
zipp                  3.6.0 # added

 

 

2. settings.py

INSTALLED_APPS = [
...
    # Scheduler
    'django_apscheduler',
...
]

# Django-Apscheduler
APSCHEDULER_DATETIME_FORMAT = "Y/m/d H:i:s"
APSCHEDULER_RUN_NOW_TIMEOUT = 25  # Seconds

 

 

3. migrate

(BlogVenv) C:\Work\Software\eclipse-jee-2019-12-R-win32-x86_64\workspace\blog.daonelab.com>python manage.py migrate --settings=Blog.settings.settings_dev

 

 

4. Create job App 

(BlogVenv) C:\Work\Software\eclipse-jee-2019-12-R-win32-x86_64\workspace\blog.daonelab.com>python manage.py startapp job

 

 

5. settings.py

INSTALLED_APPS = [
...
    'job.apps.JobConfig',
...
]

 

 

6. job/views.py

from datetime import datetime
import os

from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.cron import CronTrigger
from apscheduler.triggers.interval import IntervalTrigger
from django.conf import settings
from django_apscheduler import util
from django_apscheduler.jobstores import DjangoJobStore
from django_apscheduler.models import DjangoJobExecution


# Create your views here.
class Job():
    
    @staticmethod
    def start():
        import sys
        scheduler = BackgroundScheduler(timezone=settings.TIME_ZONE)
        scheduler.add_jobstore(DjangoJobStore(), "default")
        
        # https://apscheduler.readthedocs.io/en/latest/modules/schedulers/base.html#apscheduler.schedulers.base.BaseScheduler.add_job
        # https://apscheduler.readthedocs.io/en/3.x/modules/triggers/cron.html#module-apscheduler.triggers.cron
        
        scheduler.add_job(
          Task.delete_old_job_executions,
          trigger=CronTrigger(day="01", hour="00", minute="00"),
          args=(60 * 60 * 24 * 30,),    # 1 Month, The maximum length of time to retain historical job execution records
          id="delete_old_job_executions",
          max_instances=1,
          replace_existing=True,
        )
        
        if settings.DEBUG:
            scheduler.add_job(
              Task.test,
              trigger=IntervalTrigger(seconds=60),
              args=("작업!!",),
              id="test_job",
              max_instances=1,
              replace_existing=True,
            )
        
        scheduler.start()


class Task():
    
    @classmethod
    @util.close_old_connections
    def delete_old_job_executions(cls, max_age=604_800):
        DjangoJobExecution.objects.delete_old_job_executions(max_age)
  
    @classmethod
    def test(cls, msg):
        print(msg, "called Job!!")

 

 

 

7. job/apps.py

from django.apps import AppConfig


class JobConfig(AppConfig):
    name = "job"

    def ready(self):
        from job.views import Job
        Job.start()

 

 

8. Run server

..
작업!! called Job!!
작업!! called Job!!
작업!! called Job!!
작업!! called Job!!
작업!! called Job!!
...

 

 

9. Admin