Djano+apscheduler realizes timing operation of interface automation platform

Use apscheduler to implement timing tasks in a Django project

In django, you can directly use the apscheduler library to configure the timing program, but because the apscheduler library loads the job into the memory when running a scheduled job (job), when the django project restarts, the task work will also be cleared synchronously (if you want to work Plan to persist and need to be packaged in the database by yourself), so the library used here is

django_apscheduler

1. Install the module

pip install django_apscheduler
pip install apscheduler

(Note: After installing the module, you need to use the python manage.py makemigrations command to migrate the database. After the migration is completed, two libraries, django_apscheduler_djangojob and django_apscheduler_djangojobexecution, will be generated)
where django_apscheduler_djangojob stores the remaining work (job) plan in the current database

Insert picture description here


next_run_time It is the estimated time when the job will run next time;
django_apscheduler_djangojobexecution is to store the history record of the job that has been run or is running

Insert picture description here

2. Use Module-Initialize

The module needs to be added to apps in the setting

Insert picture description here

To use the apscheduler library in django, you need to write the initialization work in the url or in the view file. The code can be loaded when the django service is running,
so here I write the code in the views under a certain APP

Insert picture description here
# 导入模块
from apscheduler.schedulers.background import BackgroundScheduler
from django_apscheduler.jobstores import DjangoJobStore, register_events, register_job
from django_apscheduler.models import DjangoJob
#初始化模块
#将已经过时的工作都删除 __lt小于
DjangoJob.objects.filter(next_run_time__lt=datetime.now()).delete()  # 将数据库中已经过时的工作删除
# 初始化定时器:BackgroundScheduler为在后台运行
scheduler = BackgroundScheduler()
scheduler.add_jobstore(DjangoJobStore(), 'default')
register_events(scheduler)
scheduler.start() # 此处直接在django服务运行加载时,直接启动定时任务,之后在代码中动态的加入工作计划(job)

3. Dynamically build tasks in django code

Here is to add a get method to the views view to get the data input by the user from the front end, and build the timing task. The
main build code is scheduler.add_job(run, 'cron', hour=hour,minute=minute,id=str(task.id),args=[Case_id, case.name,steps ])
in cron and date format. When the user enters the specific time format , I would like to add a run method to the timing task ; the main function of the
run method is to run a test case. Execute the test report in the test case (the specific code of the run method is not described here)
. The parameters are transmitted in the format of args=[ ]; the
id is the id of the custom task library. When the user wants to modify a timed task, You need to delete this task in the django_apscheduler_djangojob library, and then re-use the add_job method to build and enter to achieve the effect of modifying the timing task;

Insert picture description here


well, the above is all the content of django using apscheduler timing tasks to achieve interface automation timing.
Summary:
write all the code In the views, import the module at the top and complete the corresponding initialization work. In the specific views view, use the add_job method to complete the construction of the timed task according to the value input by the user;

Others-the current error:
1. Job identifier (test_jobs) conflicts with an existing job
This error is caused by the job id being consistent with the existing job id in the database, you need to modify the id name to something else;

Insert picture description here


2: django.db.utils.OperationalError: database is locked
preliminary estimate is due to too many open jobs, resulting in The connection pool is not enough. After changing a way to instantiate the scheduler, it can be solved, and add a misfire_grace_time attribute value to set this value larger, and increase the fault tolerance mechanism

Insert picture description here


3:

When using cron and data methods for timing can not run the problem:

Because the timezone='MST' parameter was added when the scheduler was instantiated, the zone time should be set incorrectly, just delete it

Insert picture description here