简体   繁体   English

APScheduler:修复对重构、重命名或移动模块中已更改方法的引用

[英]APScheduler: Fix references to changed methods in Refactor, renamed or moved modules

I've hada project in python3.7 for a while with a PostgreSQL database and APScheduler 3.X.我已经在 python3.7 中使用 PostgreSQL 数据库和 APScheduler 3.X 进行了一段时间的项目。 I'm using a SQLAlchemyJobStore and have several types of jobs configured, both as cron/interval and as single time events.我正在使用SQLAlchemyJobStore并配置了几种类型的作业,包括 cron/interval 和单次事件。

I had to refactor the code in order to improve the structure and dockerize in a simpler way, and as a result, some folders were renamed.我不得不重构代码以改进结构并以更简单的方式进行 dockerize,结果,一些文件夹被重命名。 As a result, APScheduler cannot find the methods, as they were stored using the earlier file references and imports.因此,APScheduler 无法找到这些方法,因为它们是使用较早的文件引用和导入存储的。 This is the resulting error when it tries to run a job:这是它尝试运行作业时产生的错误:

Traceback (most recent call last):
2023-01-27 11:44:43   File "/usr/local/lib/python3.7/site-packages/apscheduler/jobstores/sqlalchemy.py", line 141, in _get_jobs
2023-01-27 11:44:43     jobs.append(self._reconstitute_job(row.job_state))
2023-01-27 11:44:43   File "/usr/local/lib/python3.7/site-packages/apscheduler/jobstores/sqlalchemy.py", line 128, in _reconstitute_job
2023-01-27 11:44:43     job.__setstate__(job_state)
2023-01-27 11:44:43   File "/usr/local/lib/python3.7/site-packages/apscheduler/job.py", line 272, in __setstate__
2023-01-27 11:44:43     self.func = ref_to_obj(self.func_ref)
2023-01-27 11:44:43   File "/usr/local/lib/python3.7/site-packages/apscheduler/util.py", line 305, in ref_to_obj
2023-01-27 11:44:43     raise LookupError('Error resolving reference %s: could not import module' % ref)
2023-01-27 11:44:43 LookupError: Error resolving reference app.scheduler.daily_dump:execDailyDumpEntryPoint: could not import module
2023-01-27 11:44:43 Unable to restore job "7e62c8b0b1144edfbace7e898af5c557" -- removing it
2023-01-27 11:44:43 Traceback (most recent call last):
2023-01-27 11:44:43   File "/usr/local/lib/python3.7/site-packages/apscheduler/util.py", line 303, in ref_to_obj
2023-01-27 11:44:43     obj = __import__(modulename, fromlist=[rest])
2023-01-27 11:44:43 ModuleNotFoundError: No module named 'app'
2023-01-27 11:44:43 
2023-01-27 11:44:43 During handling of the above exception, another exception occurred:
2023-01-27 11:44:43 
2023-01-27 11:44:43 Traceback (most recent call last):
2023-01-27 11:44:43   File "/usr/local/lib/python3.7/site-packages/apscheduler/jobstores/sqlalchemy.py", line 141, in _get_jobs
2023-01-27 11:44:43     jobs.append(self._reconstitute_job(row.job_state))
2023-01-27 11:44:43   File "/usr/local/lib/python3.7/site-packages/apscheduler/jobstores/sqlalchemy.py", line 128, in _reconstitute_job
2023-01-27 11:44:43     job.__setstate__(job_state)
2023-01-27 11:44:43   File "/usr/local/lib/python3.7/site-packages/apscheduler/job.py", line 272, in __setstate__
2023-01-27 11:44:43     self.func = ref_to_obj(self.func_ref)
2023-01-27 11:44:43   File "/usr/local/lib/python3.7/site-packages/apscheduler/util.py", line 305, in ref_to_obj
2023-01-27 11:44:43     raise LookupError('Error resolving reference %s: could not import module' % ref)
2023-01-27 11:44:43 LookupError: Error resolving reference app.scheduler.daily_dump:execDailyDumpEntryPoint: could not import module

and here's my scheduler setup:这是我的调度程序设置:

jobstores = {
    'default': SQLAlchemyJobStore(engine=engine,
                                  tablename="scheduled_jobs")
}
executors = {
    'default': ThreadPoolExecutor(10),
    'processpool': ProcessPoolExecutor(5)
}
job_defaults = {
    'coalesce': False,
    'max_instances': 30,
    'misfire_grace_time': 1200
}
scheduler = BackgroundScheduler(jobstores=jobstores, executors=executors,
                                job_defaults=job_defaults, timezone="America/Santiago",
                            )

I've noticed APScheduler hashes it's job configuration although I don't know if I can manually access and update the jobs with the new references in any way.我注意到 APScheduler 对其作业配置进行了哈希处理,尽管我不知道我是否可以以任何方式使用新引用手动访问和更新作业。 Is there a simple way to update all modules referenced in the database to the new structure?有没有一种简单的方法可以将数据库中引用的所有模块更新为新结构?

APScheduler (3.x) does not hash its job configurations, but it does serialize them with pickle . APScheduler (3.x) 没有 hash 它的作业配置,但它确实用pickle序列化它们。 If you want to preserve the jobs but change the target function, you need to do two things:如果要保留作业但更改目标 function,则需要做两件事:

  1. Create a module with the old name (containing the target functions) so that the jobs can be loaded使用旧名称(包含目标函数)创建一个模块,以便可以加载作业
  2. Call sched.modify_job() for each job, setting the func parameter to the corresponding new function object为每个job调用sched.modify_job() ,将func参数设置为对应的new function object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM