APSchedulerServer 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
File without changes
@@ -0,0 +1,37 @@
1
+ from apscheduler.jobstores.memory import MemoryJobStore
2
+ from apscheduler.schedulers.blocking import BlockingScheduler
3
+
4
+ # 执行器配置
5
+ SCHEDULER_EXECUTORS = {"default": {"type": "threadpool", "max_workers": 20},
6
+ "cpu": {"type": "processpool", "max_workers": 8}} # 高并发环境 # CPU核心数
7
+
8
+ # 任务默认参数
9
+ SCHEDULER_JOB_DEFAULTS = {"coalesce": True, "max_instances": 5,
10
+ "misfire_grace_time": 300} # 合并错过执行,避免堆积 # 允许一定并发 # 5分钟宽限期
11
+
12
+ # APScheduler 配置
13
+ APSCHEDULER_DATETIME_FORMAT = "N j, Y, f:s a" # 日期时间格式
14
+ APSCHEDULER_RUN_NOW_TIMEOUT = 25 # 最大允许运行时间(秒)
15
+ APSCHEDULER_DELETE_OLD_JOB_EXECUTIONS = 180 # 每次有新任务执行时,系统会自动删除超过设定天数的旧记录(保留最近180天数据)
16
+ TIME_ZONE = "Asia/Shanghai"
17
+
18
+
19
+
20
+
21
+ class BaseConfig(object):
22
+
23
+ def set_job_stores(self):
24
+ return MemoryJobStore()
25
+
26
+ def set_scheduler(self):
27
+ scheduler = BlockingScheduler(
28
+ executors=SCHEDULER_EXECUTORS,
29
+ job_defaults=SCHEDULER_JOB_DEFAULTS,
30
+ timezone=TIME_ZONE
31
+ )
32
+
33
+ # 添加自定义配置属性
34
+ scheduler.run_now_timeout = APSCHEDULER_RUN_NOW_TIMEOUT
35
+ scheduler.datetime_format = APSCHEDULER_DATETIME_FORMAT
36
+ scheduler.cleanup_days = APSCHEDULER_DELETE_OLD_JOB_EXECUTIONS
37
+ return scheduler
@@ -0,0 +1,21 @@
1
+ import importlib
2
+ import importlib.util
3
+
4
+ class Loader(object):
5
+ # 默认配置类名
6
+ DEFAULT_CONFIG_CLASS = "APSchedulerConfig"
7
+
8
+ def __init__(self):
9
+ try:
10
+ config = importlib.import_module("APSchedulerConfig.config")
11
+ self.config = config.ApsConfig()
12
+ except Exception as e:
13
+ print(e)
14
+ self.config = None
15
+
16
+ try:
17
+ registry = importlib.import_module("APSchedulerConfig.registry_tasks")
18
+ self.set_registry_tasks = registry.set_registry_tasks
19
+ except Exception as e:
20
+ print(e)
21
+ self.set_registry_tasks = None
@@ -0,0 +1,61 @@
1
+ import sys
2
+ import signal
3
+ import logging
4
+ import platform
5
+ from .loader import Loader
6
+
7
+ logger = logging.getLogger(__name__)
8
+
9
+
10
+ def main():
11
+ logging.basicConfig(
12
+ level=logging.INFO,
13
+ format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
14
+ )
15
+
16
+ loader = Loader()
17
+ if loader.config is None:
18
+ logger.error("配置加载失败,请检查 APSchedulerConfig 模块")
19
+ sys.exit(1)
20
+
21
+ scheduler = loader.config.set_scheduler()
22
+ jobStore = loader.config.set_job_stores()
23
+ scheduler.add_jobstore(jobStore, "default")
24
+
25
+ if loader.set_registry_tasks:
26
+ loader.set_registry_tasks(scheduler)
27
+ else:
28
+ logger.warning("未找到任务注册函数")
29
+
30
+ def graceful_shutdown(signum, frame):
31
+ logger.info("正在关闭调度器...")
32
+ scheduler.shutdown()
33
+ sys.exit(0)
34
+
35
+ signal.signal(signal.SIGINT, graceful_shutdown)
36
+ signal.signal(signal.SIGTERM, graceful_shutdown)
37
+
38
+ try:
39
+ scheduler.start()
40
+ logger.info("调度器已启动")
41
+
42
+ if platform.system() == 'Windows':
43
+ import time
44
+ while scheduler.running:
45
+ time.sleep(1)
46
+ else:
47
+ signal.pause()
48
+
49
+ except (KeyboardInterrupt, SystemExit):
50
+ logger.info("收到退出信号")
51
+ except Exception as e:
52
+ logger.exception(f"调度器运行错误: {str(e)}")
53
+ try:
54
+ scheduler.shutdown(wait=False)
55
+ except Exception:
56
+ logger.exception("关闭调度器时出错")
57
+ sys.exit(1)
58
+
59
+
60
+ if __name__ == "__main__":
61
+ main()
@@ -0,0 +1,442 @@
1
+ Metadata-Version: 2.4
2
+ Name: APSchedulerServer
3
+ Version: 0.1.0
4
+ Summary: 基于 APScheduler 的任务调度服务器,支持多种 JobStore 后端
5
+ Author: fly
6
+ Author-email: fly <fzkf117@163.com>
7
+ License: MIT
8
+ Keywords: apscheduler,scheduler,task,job,cron,redis,mysql,django
9
+ Classifier: Development Status :: 4 - Beta
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Operating System :: OS Independent
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.6
15
+ Classifier: Programming Language :: Python :: 3.7
16
+ Classifier: Programming Language :: Python :: 3.8
17
+ Classifier: Programming Language :: Python :: 3.9
18
+ Classifier: Programming Language :: Python :: 3.10
19
+ Classifier: Programming Language :: Python :: 3.11
20
+ Classifier: Programming Language :: Python :: 3.12
21
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
22
+ Classifier: Topic :: System :: Distributed Computing
23
+ Requires-Python: >=3.6
24
+ Description-Content-Type: text/markdown
25
+ License-File: LICENSE
26
+ Requires-Dist: APScheduler>=3.10.0
27
+ Provides-Extra: redis
28
+ Requires-Dist: redis>=4.0.0; extra == "redis"
29
+ Provides-Extra: mysql
30
+ Requires-Dist: mysql-connector-python>=8.0.0; extra == "mysql"
31
+ Provides-Extra: django
32
+ Requires-Dist: django>=3.2.0; extra == "django"
33
+ Provides-Extra: sqlalchemy
34
+ Requires-Dist: sqlalchemy>=1.4.0; extra == "sqlalchemy"
35
+ Provides-Extra: all
36
+ Requires-Dist: APSchedulerServer[django,mysql,redis,sqlalchemy]; extra == "all"
37
+ Dynamic: license-file
38
+
39
+ # APSchedulerServer 使用教程
40
+
41
+ ## 项目简介
42
+
43
+ APSchedulerServer 是一个基于 APScheduler 的任务调度服务器,支持多种 JobStore 后端(Redis、MySQL、Django 等),提供了灵活的任务配置和注册机制。
44
+
45
+ ## 项目结构
46
+
47
+ ```
48
+ APSchedulerServer/
49
+ ├── src/ # 源代码目录
50
+ │ └── APSchedulerServer/ # 核心代码包
51
+ │ ├── __init__.py
52
+ │ ├── base_config.py # 基础配置类
53
+ │ ├── loader.py # 配置加载器
54
+ │ └── main.py # 主程序入口
55
+ ├── APSchedulerConfig/ # 配置目录(用户需要创建)
56
+ │ ├── config.py # 调度器配置
57
+ │ └── registry_tasks.py # 任务注册
58
+ ├── pyproject.toml # 项目配置文件
59
+ ├── setup.py # 安装脚本
60
+ └── README.md # 项目文档
61
+ ```
62
+
63
+ ## 安装
64
+
65
+ ### 基础安装
66
+
67
+ ```bash
68
+ pip install APSchedulerServer
69
+ ```
70
+
71
+ ### 安装可选依赖
72
+
73
+ 根据所选的 JobStore,安装额外的依赖:
74
+
75
+ ```bash
76
+ # Redis JobStore
77
+ pip install APSchedulerServer[redis]
78
+
79
+ # MySQL JobStore
80
+ pip install APSchedulerServer[mysql]
81
+
82
+ # Django JobStore (需要在 Django 项目中使用)
83
+ pip install APSchedulerServer[django]
84
+
85
+ # SQLAlchemy JobStore
86
+ pip install APSchedulerServer[sqlalchemy]
87
+
88
+ # 安装所有可选依赖
89
+ pip install APSchedulerServer[all]
90
+ ```
91
+
92
+ ### 从源码安装
93
+
94
+ ```bash
95
+ git clone https://github.com/yourusername/APSchedulerServer.git
96
+ cd APSchedulerServer
97
+ pip install -e .
98
+ ```
99
+
100
+ ## 快速开始
101
+
102
+ ### 1. 基础配置
103
+
104
+ 编辑 `APSchedulerConfig/config.py`,配置调度器和 JobStore:
105
+
106
+ ```python
107
+ from APSchedulerServer.base_config import BaseConfig
108
+ from apscheduler.jobstores.redis import RedisJobStore
109
+ from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore
110
+ from apscheduler.jobstores.django import DjangoJobStore
111
+
112
+
113
+ class ApsConfig(BaseConfig):
114
+
115
+ def __init__(self):
116
+ print("配置加载成功")
117
+
118
+ def set_job_stores(self):
119
+ """配置 JobStore"""
120
+ return {
121
+ # Redis JobStore
122
+ 'redis': RedisJobStore(
123
+ jobs_key='apscheduler.jobs',
124
+ run_times_key='apscheduler.run_times',
125
+ host='localhost',
126
+ port=6379,
127
+ db=0
128
+ ),
129
+ # MySQL JobStore
130
+ 'mysql': SQLAlchemyJobStore(
131
+ url='mysql+mysqlconnector://user:password@localhost/scheduler_db'
132
+ ),
133
+ # Django JobStore
134
+ 'django': DjangoJobStore(),
135
+ # Memory JobStore (默认)
136
+ 'default': MemoryJobStore()
137
+ }
138
+
139
+ def set_scheduler(self):
140
+ """配置调度器"""
141
+ scheduler = super().set_scheduler()
142
+ # 可以在这里添加自定义配置
143
+ return scheduler
144
+ ```
145
+
146
+ ### 2. 任务注册
147
+
148
+ 编辑 `APSchedulerConfig/registry_tasks.py`,注册你的定时任务:
149
+
150
+ ```python
151
+ from apscheduler.triggers.cron import CronTrigger
152
+ from apscheduler.triggers.interval import IntervalTrigger
153
+ from apscheduler.triggers.date import DateTrigger
154
+ import datetime
155
+
156
+ def set_registry_tasks(scheduler):
157
+ """注册所有定时任务"""
158
+
159
+ # 示例1: 使用 Cron 表达式(每天 8:30 执行)
160
+ scheduler.add_job(
161
+ func=daily_task,
162
+ trigger=CronTrigger(hour=8, minute=30),
163
+ id='daily_task',
164
+ name='每日任务',
165
+ replace_existing=True
166
+ )
167
+
168
+ # 示例2: 使用间隔触发器(每 5 分钟执行一次)
169
+ scheduler.add_job(
170
+ func=interval_task,
171
+ trigger=IntervalTrigger(minutes=5),
172
+ id='interval_task',
173
+ name='间隔任务',
174
+ replace_existing=True
175
+ )
176
+
177
+ # 示例3: 使用日期触发器(在指定时间执行一次)
178
+ scheduler.add_job(
179
+ func=one_time_task,
180
+ trigger=DateTrigger(run_date=datetime.datetime(2024, 1, 1, 12, 0)),
181
+ id='one_time_task',
182
+ name='一次性任务',
183
+ replace_existing=True
184
+ )
185
+
186
+ print("所有任务注册完成")
187
+
188
+ # 定义任务函数
189
+ def daily_task():
190
+ """每日任务"""
191
+ print("执行每日任务")
192
+ # 在这里添加你的业务逻辑
193
+
194
+ def interval_task():
195
+ """间隔任务"""
196
+ print("执行间隔任务")
197
+ # 在这里添加你的业务逻辑
198
+
199
+ def one_time_task():
200
+ """一次性任务"""
201
+ print("执行一次性任务")
202
+ # 在这里添加你的业务逻辑
203
+ ```
204
+
205
+ ### 3. 运行调度器
206
+
207
+ #### 方法1:使用命令行工具(推荐)
208
+
209
+ ```bash
210
+ apscheduler-server
211
+ ```
212
+
213
+ #### 方法2:使用 Python 模块方式
214
+
215
+ 在项目根目录下执行:
216
+
217
+ ```bash
218
+ python -m APSchedulerServer.main
219
+ ```
220
+
221
+ #### 方法3:直接运行
222
+
223
+ ```python
224
+ from APSchedulerServer.main import main
225
+
226
+ if __name__ == '__main__':
227
+ main()
228
+ ```
229
+
230
+ ## JobStore 配置详解
231
+
232
+ ### Redis JobStore
233
+
234
+ ```python
235
+ from apscheduler.jobstores.redis import RedisJobStore
236
+
237
+ def set_job_stores(self):
238
+ return {
239
+ 'redis': RedisJobStore(
240
+ jobs_key='apscheduler.jobs', # 存储任务的键名
241
+ run_times_key='apscheduler.run_times', # 存储运行时间的键名
242
+ host='localhost', # Redis 主机
243
+ port=6379, # Redis 端口
244
+ db=0, # Redis 数据库
245
+ password='your_password' # Redis 密码(可选)
246
+ )
247
+ }
248
+ ```
249
+
250
+ ### MySQL JobStore
251
+
252
+ ```python
253
+ from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore
254
+
255
+ def set_job_stores(self):
256
+ return {
257
+ 'mysql': SQLAlchemyJobStore(
258
+ url='mysql+mysqlconnector://user:password@localhost/scheduler_db'
259
+ )
260
+ }
261
+ ```
262
+
263
+ 支持的数据库 URL 格式:
264
+ - MySQL: `mysql+mysqlconnector://user:password@host/dbname`
265
+ - PostgreSQL: `postgresql://user:password@host/dbname`
266
+ - SQLite: `sqlite:///jobs.sqlite`
267
+
268
+ ### Django JobStore
269
+
270
+ ```python
271
+ from apscheduler.jobstores.django import DjangoJobStore
272
+
273
+ def set_job_stores(self):
274
+ return {
275
+ 'django': DjangoJobStore()
276
+ }
277
+ ```
278
+
279
+ 注意:使用 Django JobStore 需要将 `apscheduler` 添加到 Django 的 `INSTALLED_APPS` 中。
280
+
281
+ ## 高级配置
282
+
283
+ ### 修改执行器配置
284
+
285
+ 编辑 `APSchedulerServer/base_config.py` 中的 `SCHEDULER_EXECUTORS`:
286
+
287
+ ```python
288
+ SCHEDULER_EXECUTORS = {
289
+ "default": {"type": "threadpool", "max_workers": 20},
290
+ "cpu": {"type": "processpool", "max_workers": 8}
291
+ }
292
+ ```
293
+
294
+ ### 修改任务默认参数
295
+
296
+ 编辑 `APSchedulerServer/base_config.py` 中的 `SCHEDULER_JOB_DEFAULTS`:
297
+
298
+ ```python
299
+ SCHEDULER_JOB_DEFAULTS = {
300
+ "coalesce": True, # 合并错过的执行
301
+ "max_instances": 5, # 最大并发实例数
302
+ "misfire_grace_time": 300 # 错过执行的宽限期(秒)
303
+ }
304
+ ```
305
+
306
+ ### 时区配置
307
+
308
+ 编辑 `APSchedulerServer/base_config.py` 中的 `TIME_ZONE`:
309
+
310
+ ```python
311
+ TIME_ZONE = "Asia/Shanghai"
312
+ ```
313
+
314
+ ## 常见问题
315
+
316
+ ### 1. 找不到模块错误
317
+
318
+ 确保在项目根目录下运行:
319
+
320
+ ```bash
321
+ python -m APSchedulerServer.main
322
+ ```
323
+
324
+ ### 2. Redis 连接失败
325
+
326
+ 检查 Redis 服务是否启动,以及连接配置是否正确。
327
+
328
+ ### 3. MySQL 连接失败
329
+
330
+ 确保 MySQL 服务已启动,并且数据库已创建。
331
+
332
+ ### 4. 任务没有执行
333
+
334
+ 检查任务的触发器配置是否正确,以及调度器是否正常运行。
335
+
336
+ ## 示例项目
337
+
338
+ ### 示例 1: 使用 Redis JobStore
339
+
340
+ ```python
341
+ # APSchedulerConfig/config.py
342
+ from APSchedulerServer.base_config import BaseConfig
343
+ from apscheduler.jobstores.redis import RedisJobStore
344
+
345
+
346
+ class ApsConfig(BaseConfig):
347
+ def __init__(self):
348
+ print("Redis JobStore 配置加载成功")
349
+
350
+ def set_job_stores(self):
351
+ return {
352
+ 'default': RedisJobStore(
353
+ host='localhost',
354
+ port=6379,
355
+ db=0
356
+ )
357
+ }
358
+ ```
359
+
360
+ ### 示例 2: 使用 MySQL JobStore
361
+
362
+ ```python
363
+ # APSchedulerConfig/config.py
364
+ from APSchedulerServer.base_config import BaseConfig
365
+ from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore
366
+
367
+
368
+ class ApsConfig(BaseConfig):
369
+ def __init__(self):
370
+ print("MySQL JobStore 配置加载成功")
371
+
372
+ def set_job_stores(self):
373
+ return {
374
+ 'default': SQLAlchemyJobStore(
375
+ url='mysql+mysqlconnector://user:password@localhost/scheduler_db'
376
+ )
377
+ }
378
+ ```
379
+
380
+ ### 示例 3: 使用 Django JobStore
381
+
382
+ ```python
383
+ # APSchedulerConfig/config.py
384
+ from APSchedulerServer.base_config import BaseConfig
385
+ from apscheduler.jobstores.django import DjangoJobStore
386
+
387
+
388
+ class ApsConfig(BaseConfig):
389
+ def __init__(self):
390
+ print("Django JobStore 配置加载成功")
391
+
392
+ def set_job_stores(self):
393
+ return {
394
+ 'default': DjangoJobStore()
395
+ }
396
+ ```
397
+
398
+ ### 示例 4: 使用 SQLAlchemy JobStore
399
+
400
+ ```python
401
+ # APSchedulerConfig/config.py
402
+ from APSchedulerServer.base_config import BaseConfig
403
+ from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore
404
+
405
+
406
+ class ApsConfig(BaseConfig):
407
+ def __init__(self):
408
+ print("SQLAlchemy JobStore 配置加载成功")
409
+
410
+ def set_job_stores(self):
411
+ return {
412
+ 'default': SQLAlchemyJobStore(
413
+ url='postgresql://user:password@localhost/scheduler_db'
414
+ )
415
+ }
416
+ ```
417
+
418
+ ## 最佳实践
419
+
420
+ 1. **选择合适的 JobStore**:
421
+ - 开发环境:使用 MemoryJobStore
422
+ - 生产环境:使用 Redis 或 SQLAlchemy JobStore
423
+ - Django 项目:使用 Django JobStore
424
+ - 需要跨平台支持:使用 SQLAlchemy JobStore(支持 PostgreSQL、MySQL、SQLite 等)
425
+
426
+ 2. **任务设计**:
427
+ - 任务函数应该是幂等的(可以安全地多次执行)
428
+ - 避免长时间运行的任务
429
+ - 合理设置任务的并发实例数
430
+
431
+ 3. **监控和日志**:
432
+ - 添加任务执行日志
433
+ - 监控任务执行状态
434
+ - 设置任务失败告警
435
+
436
+ 4. **错误处理**:
437
+ - 在任务函数中添加适当的异常处理
438
+ - 设置合理的重试策略
439
+
440
+ ## 许可证
441
+
442
+ 本项目采用 MIT 许可证。
@@ -0,0 +1,10 @@
1
+ APSchedulerServer/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ APSchedulerServer/base_config.py,sha256=6P9B_qU-AGO1FFKYFSZdQYR3PzKv0ikckC8_An3QIhw,1490
3
+ APSchedulerServer/loader.py,sha256=SEvhmwdz5kvWOd0mEJ1KTBwnkmVTL59vW2dCaNXplFo,650
4
+ APSchedulerServer/main.py,sha256=Yvlm1VRO30B27Ohc84NZ1vUCCA2ofmu4RGjdStPqxCI,1635
5
+ apschedulerserver-0.1.0.dist-info/licenses/LICENSE,sha256=W4H6dAH8qOQT5sMYw3si792b44Bosyn0nISPSFi-l8w,1060
6
+ apschedulerserver-0.1.0.dist-info/METADATA,sha256=e2cdAahhAbXnTBl4k66O-tUQxNy2yN1-hOgsr_WQitY,11725
7
+ apschedulerserver-0.1.0.dist-info/WHEEL,sha256=YCfwYGOYMi5Jhw2fU4yNgwErybb2IX5PEwBKV4ZbdBo,91
8
+ apschedulerserver-0.1.0.dist-info/entry_points.txt,sha256=p5k8V6JA9IxNK_aYRcx6tMlyWcCpeRbBmK3kCd1jAEI,67
9
+ apschedulerserver-0.1.0.dist-info/top_level.txt,sha256=vtLCm0oboxNsNZoTjq1pHzBPuPavt58lqIvlTysw28c,18
10
+ apschedulerserver-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (82.0.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ apscheduler-server = APSchedulerServer.main:main
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 fly
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ APSchedulerServer