Scheduled task service based on APScheduler, You can dynamically add scheduled tasks.
schedule_server
: 核心功能是定时触发HTTP请求。fontend
: 通过前端UI管理定时任务。you server
: 你也可以通过调接口的方式管理定时任务。
安装依赖
> cd schedule_server
> pip install -r requirements.txt
开发运行
> uvicorn main:app --reload
INFO: Will watch for changes in these directories: ['/.../schedule-server/schedule_server']
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [21905] using StatReload
INFO: Started server process [21907]
INFO: Waiting for application startup.
部署运行
指定IP和端口
> uvicorn main:app --workers 1 --host 127.0.0.1 --port 8004 &
nginx配置
location /scheduler/ {
proxy_pass http://127.0.0.1:8004;
proxy_pass_request_headers on;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
查看API
访问url: http://127.0.0.1:8000/docs
- install
> npm install
- running
> npm run dev
> wiremock-ui@0.0.1 dev
> vite
VITE v4.3.9 ready in 3426 ms
➜ Local: http://localhost:5173/
➜ Network: use --host to expose
➜ press h to show help
查看UI
访问url: http://localhost:5173/
date类型定时任务
data类型比较简单,适合固定的日期时间
触发定时任务。
-
调用接口
- URL:http://127.0.0.1:8000/scheduler/date/add_job
- Method:
POST
- Type:
JSON
{
"job_id": "date_job_111",
"url": "https://httpbin.org/get?id=111",
"year": 2022,
"month": 11,
"day": 18,
"hour": 7,
"minute": 0,
"second": 0
}
- 前端配置
- job_id: 设置一个唯一的
job_id
,后面可以通过job_id
删除/暂停/恢复 定时任务。- url: 定时任务触发的url。
- datatime: 设置
2022-11-18 07:00:00
触发一次。这里用的是UTC时间,所以,北京时间你需要手动加8小时。
interval类型定时任务
interval适合间隔时间重复执行
的定时任务。
-
调用接口
- URL:http://127.0.0.1:8000/scheduler/interval/add_job
- Method:
POST
- Type:
JSON
{
"job_id": "interval_job_222",
"url": "https://httpbin.org/get?id=222",
"hours": 0,
"minutes": 0,
"seconds": 10
}
- 前端配置
crontab类型定时任务
conn使一种复杂的定时任务,能够支持所有的定时任务需求。
-
调用接口
- URL:http://127.0.0.1:8000/scheduler/cron/add_job
- Method:
POST
- Type:
JSON
{
"job_id": "cron_job_333",
"url": "https://httpbin.org/get?id=333",
"second": "0",
"minute": "*/3",
"hour": "*",
"day": "*",
"month": "*",
"day_of_week": "*"
}
可以参考这个网站学习:https://tooltt.com/crontab-parse/
- 前端配置
其他接口
-
删除定时任务:
- URL:
http://127.0.0.1:8000/scheduler/remove_job?job_id=interval_job_222
- Method:
DELETE
- URL:
-
暂停定时任务:
http://127.0.0.1:8000/scheduler/pause_job?job_id=interval_job_222
- Method:
PUT
-
恢复定时任务:
- URL:
http://127.0.0.1:8000/scheduler/resume_job?job_id=interval_job_222
- Method:
PUT
- URL:
-
查询定时任务:
- URL:
http://127.0.0.1:8000/scheduler/get_job?job_id=interval_job_222
- Method:
GET
- URL:
-
查询所有定时任务:
- URL:
http://127.0.0.1:8000/scheduler/get_jobs
- Method:
GET
- URL: