Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 捕获哨兵的后空位作业异常_Python_Python 3.x_Sentry_Apscheduler - Fatal编程技术网

Python 捕获哨兵的后空位作业异常

Python 捕获哨兵的后空位作业异常,python,python-3.x,sentry,apscheduler,Python,Python 3.x,Sentry,Apscheduler,我正在使用apscheduler在后台处理一些事情 我想捕获并向哨兵报告可能的异常情况。我的代码如下所示: sentry = Client(dsn=SENTRY_DSN) def sample_method(): # some processing.. raise ConnectionError def listen_to_exceptions(event): if event.exception: # I was hoping raven wi

我正在使用apscheduler在后台处理一些事情

我想捕获并向哨兵报告可能的异常情况。我的代码如下所示:

sentry = Client(dsn=SENTRY_DSN)

def sample_method():
     # some processing..

     raise ConnectionError

def listen_to_exceptions(event):
    if event.exception:
        # I was hoping raven will capture the exception using  sys.exc_info(), but it's not
        sentry.captureException()


scheduler = BlockingScheduler()

scheduler.add_listener(listen_to_exceptions, EVENT_JOB_EXECUTED | EVENT_JOB_ERROR)

scheduler.add_job(sample_method, 'interval', minutes=5, max_instances=1)

# run forever!!!
scheduler.start()
但它不是捕获异常,而是生成更多异常,试图将其报告给Sentry

ConnectionError
Error notifying listener
Traceback (most recent call last):
  File "/.../venv/lib/python3.6/site-packages/apscheduler/schedulers/base.py", line 825, in _dispatch_event
    cb(event)
  File "app.py", line 114, in listen_to_exceptions
    sentry.captureException(event.exception)
  File "/.../venv/lib/python3.6/site-packages/raven/base.py", line 814, in captureException
    'raven.events.Exception', exc_info=exc_info, **kwargs)
  File "/.../venv/lib/python3.6/site-packages/raven/base.py", line 623, in capture
    if self.skip_error_for_logging(exc_info):
  File "/.../venv/lib/python3.6/site-packages/raven/base.py", line 358, in skip_error_for_logging
    key = self._get_exception_key(exc_info)
  File "/.../venv/lib/python3.6/site-packages/raven/base.py", line 345, in _get_exception_key
    code_id = id(exc_info[2] and exc_info[2].tb_frame.f_code)
TypeError: 'ConnectionError' object is not subscriptable
我正试图使用事件侦听器,根据是否有其他方法捕获已执行作业中的异常?


当然,我可以向每个作业函数添加try-except块。我只是想知道是否有办法用apscedular来实现这一点,因为我有20多个作业,并且添加了
sentry.captureException()
,每个地方都像是重复。

你只需要捕获
事件\作业\错误
。另外,
sentry.captureException()
需要exc\u信息元组作为其参数,而不是异常对象。以下内容适用于Python 3:

def listen_to_exceptions(event):
    exc_info = type(event.exception), event.exception, event.exception.__traceback__
    sentry.captureException(exc_info)

scheduler.add_listener(listen_to_exceptions, EVENT_JOB_ERROR)

您只需捕获
事件\作业\错误
。另外,
sentry.captureException()
需要exc\u信息元组作为其参数,而不是异常对象。以下内容适用于Python 3:

def listen_to_exceptions(event):
    exc_info = type(event.exception), event.exception, event.exception.__traceback__
    sentry.captureException(exc_info)

scheduler.add_listener(listen_to_exceptions, EVENT_JOB_ERROR)

该文件已提交。因此,您必须按照以下方式进行操作:

from sentry_sdk import capture_exception
....
def sentry_listener(event):
    if event.exception:
        capture_exception(event.exception)

scheduler.add_listener(sentry_listener, EVENT_JOB_ERROR)

该文件已提交。因此,您必须按照以下方式进行操作:

from sentry_sdk import capture_exception
....
def sentry_listener(event):
    if event.exception:
        capture_exception(event.exception)

scheduler.add_listener(sentry_listener, EVENT_JOB_ERROR)

您的示例代码与回溯不匹配。在回溯中,您调用的是
sentry.captureException(event.exception)
,这不是sentry文档要求您执行的操作。您的示例代码与回溯不匹配。在回溯中,您正在调用
sentry.captureException(event.exception)
,这不是sentry文档所说的您应该做的。这解决了问题。谢谢。我从未想过从异常中构建exc_信息。是否可以从异常中捕获局部变量()?Sentry应该自动为您执行此操作。
EVENT_JOB_ERROR
可以从
apscheduler导入。events
解决了此问题。谢谢。我从未想过从异常生成exc_信息。是否可以从异常捕获局部变量()?Sentry应该自动为您执行此操作。
EVENT_JOB_ERROR
可以从
apscheduler.events