【问题标题】:Unable to restore job - apscheduler, sqlalchemy无法恢复作业 - apscheduler、sqlalchemy
【发布时间】:2019-04-26 13:53:30
【问题描述】:

我正在尝试编写自己的小型 python flask 应用程序来监控我的服务器的硬盘驱动器。

但是从现在开始,我在使用 apscheduler 的 sqljobstore 时遇到了麻烦。 当服务器运行时,一切都很好。但重启后,我无法访问网络界面并获得以下输出:

Unable to restore job "refresh_disks" -- removing it
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/apscheduler/util.py", line 289, in ref_to_obj
    obj = getattr(obj, name)
AttributeError: module 'dirkules.tasks' has no attribute 'refresh_disks'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/apscheduler/jobstores/sqlalchemy.py", line 141, in _get_jobs
    jobs.append(self._reconstitute_job(row.job_state))
  File "/usr/local/lib/python3.6/dist-packages/apscheduler/jobstores/sqlalchemy.py", line 128, in _reconstitute_job
    job.__setstate__(job_state)
  File "/usr/local/lib/python3.6/dist-packages/apscheduler/job.py", line 272, in __setstate__
    self.func = ref_to_obj(self.func_ref)
  File "/usr/local/lib/python3.6/dist-packages/apscheduler/util.py", line 292, in ref_to_obj
    raise LookupError('Error resolving reference %s: error looking up object' % ref)
LookupError: Error resolving reference dirkules.tasks:refresh_disks: error looking up object
[2019-04-26 15:46:39 +0200] [13296] [INFO] Shutting down: Master

这是我的 config.py:

import os
from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore
#from apscheduler.jobstores.memory import MemoryJobStore

baseDir = os.path.abspath(os.path.dirname(__file__))

SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(baseDir, 'dirkules.db')
SQLALCHEMY_TRACK_MODIFICATIONS = False

# The SCHEDULER_JOB_DEFAULTS configuration is per job, that means each job can execute at most 3 threads at the same time.
# The SCHEDULER_EXECUTORS is a global configuration, in this case, only 1 thread will be used for all the jobs.
# I believe the best way for you is to use max_workers: 1 when running locally

SCHEDULER_JOBSTORES = {'default': SQLAlchemyJobStore(url='sqlite:///' + os.path.join(baseDir, 'dirkules.db'))}
#SCHEDULER_JOBSTORES = {'default': MemoryJobStore()}

SCHEDULER_EXECUTORS = {'default': {'type': 'threadpool', 'max_workers': 3}}

SCHEDULER_JOB_DEFAULTS = {'coalesce': False, 'max_instances': 1}

SCHEDULER_API_ENABLED = True

init.py:

import dirkules.config as config

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_apscheduler import APScheduler

app = Flask(__name__)
app.config.from_object(config)
db = SQLAlchemy(app)

import dirkules.models
db.create_all()
scheduler = APScheduler()
scheduler.init_app(app)
scheduler.start()


#@app.before_first_request
from dirkules import tasks


# from dirkules.models import Time
# from sqlalchemy.orm.exc import NoResultFound
#
# try:
#     Time.query.one()
# except NoResultFound:
#     db.session.add(Time("Drives"))
#     db.session.commit()

import dirkules.views

和tasks.py:

from dirkules import scheduler
import datetime
import dirkules.driveManagement.driveController as drico

@scheduler.task('interval', id='refresh_disks', seconds=10)
def refresh_disks():
    #drives = drico.getAllDrives()
    print("Drives refreshed")

希望你能帮助我!

【问题讨论】:

    标签: python-3.x flask flask-sqlalchemy apscheduler


    【解决方案1】:

    作为导入模块的副作用启动调度程序被认为是不好的做法,也是属性查找失败的可能原因。但是,我必须看到一个简化的、更完整的示例来重现该问题。

    【讨论】:

    • 取决于应用程序的结构,但我建议将其作为应用程序入口点的一部分启动(例如,如果您在某处有 __main__ 块)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-21
    • 2018-03-09
    • 2017-05-16
    相关资源
    最近更新 更多