APScheduler 随机关闭

Lit*_*ait 5 python apscheduler

调度程序在生产中运行良好,然后突然关闭。显然,DB 可能已经离线了一段时间(网络应用程序从未错过任何一个节拍,因此它是短暂的)。

日志报告...

[2019-11-25 07:59:14,907: INFO/ercscheduler] Scheduler has been shut down
[2019-11-25 07:59:14,908: DEBUG/ercscheduler] Looking for jobs to run
[2019-11-25 07:59:14,909: WARNING/ercscheduler] Error getting due jobs from job store 'default': (psycopg2.OperationalError) could not connect to server: Network is unreachable
        Is the server running on host "localhost" (127.0.0.1) and accepting
        TCP/IP connections on port 6432?

(Background on this error at: http://sqlalche.me/e/e3q8)
[2019-11-25 07:59:14,909: DEBUG/ercscheduler] Next wakeup is due at 2019-11-25 13:59:24.908318+00:00 (in 10.000000 seconds)
[2019-11-25 07:59:14,909: INFO/ercscheduler] listener closed
[2019-11-25 07:59:14,909: INFO/ercscheduler] server has terminated
[2019-11-25 08:00:10,747: INFO/ercscheduler] Adding job tentatively -- it will be properly scheduled when the scheduler starts
[2019-11-25 08:00:10,797: INFO/ercscheduler] Adding job tentatively -- it will be properly scheduled when the scheduler starts
[2019-11-26 15:27:48,392: INFO/ercscheduler] Adding job tentatively -- it will be properly scheduled when the scheduler starts
[2019-11-26 15:27:48,392: INFO/ercscheduler] Adding job tentatively -- it will be properly scheduled when the scheduler starts
Run Code Online (Sandbox Code Playgroud)

如何让调度器更容错?我必须再次重新启动守护程序才能让它运行。

小智 6

我在 APScheduler Github 存储库上发现了与您的问题非常相似的内容。 https://github.com/agronholm/apscheduler/issues/109

这个问题在这里似乎得到了缓解并合并到version 3.3.

您所要做的就是至少升级到3.3. 如果您想更改默认的 10 秒间隔,则必须jobstore_retry_interval在创建调度程序实例时进行设置。

如果你无法升级,那么我会尝试猴子修补APScheduler中的相应功能。

def monkey_patched_process_jobs(self):

     # You have alter the way job processing done in this function.

     pass

# replacing the function with the patched one
BackgroundScheduler._process_jobs = monkey_patched_process_jobs

scheduler = BackgroundScheduler()
Run Code Online (Sandbox Code Playgroud)

请记住,这并不理想,如果由于重大更改而无法升级,我只会进行猴子修补。


此功能的幕后工作原理

这是来自 APScheduler Git 存储库的片段

try:
    due_jobs = jobstore.get_due_jobs(now)
except Exception as e:
    # Schedule a wakeup at least in jobstore_retry_interval seconds
    self._logger.warning('Error getting due jobs from job store %r: %s',
                         jobstore_alias, e)
    retry_wakeup_time = now + timedelta(seconds=self.jobstore_retry_interval)
    if not next_wakeup_time or next_wakeup_time > retry_wakeup_time:
        next_wakeup_time = retry_wakeup_time

    continue
Run Code Online (Sandbox Code Playgroud)

self.jobstore_retry_interval按以下方式设置:

self.jobstore_retry_interval = float(config.pop('jobstore_retry_interval', 10))
Run Code Online (Sandbox Code Playgroud)