当从命令行关闭bot时,我需要多次点击^ C,因为看起来我的事件监听器线程没有正确关闭。我是否需要更改停用功能?
class Icinga2bot(BotPlugin):
"""
Use errbot to talk to an Icinga2 monitoring server.
"""
def report_events(self):
'''Relay events from the Icinga2 API to a chat channel. Not interactive.
The event queues are selected in icinga2bot.ini'''
while not self.stop_thread.is_set():
queue = i2events()
room = self.query_room(self.bot_config.CHATROOM_PRESENCE[0])
debug(room)
for line in queue:
self.send(room,line)
debug(line)
def activate(self):
"""
Triggers on plugin activation
You should delete it if you're not using it to override any default behaviour
"""
self.stop_thread = threading.Event()
self.thread = threading.Thread(target = self.report_events)
self.thread.start()
super(Icinga2bot, self).activate()
def deactivate(self):
"""
Triggers on plugin deactivation
You should delete it if you're not using it to override any default behaviour
"""
debug('shutting down bot')
self.stop_thread.set()
debug('stop_thread.set() complete')
self.thread.join()
debug('thread.join() complete')
super(Icinga2bot, self).deactivate()
控制台窗口显示插件在停止标志设置和等待线程完成之间陷入困境。
14:56:28 INFO errbot.core Disconnect callback, deactivating all the plugins.
***DEBUG*** shutting down bot
***DEBUG*** stop_thread.set() complete
^C14:58:38 INFO errbot.core Disconnect callback, deactivating all the plugins.
***DEBUG*** shutting down bot
***DEBUG*** stop_thread.set() complete
^CTraceback (most recent call last):
File "/home/errbot/err3/lib/python3.4/site-packages/errbot/backends/xmpp.py", line 527, in serve_forever
self.conn.serve_forever()
File "/home/errbot/err3/lib/python3.4/site-packages/errbot/backends/xmpp.py", line 362, in serve_forever
self.client.process(block=True)
File "/home/errbot/err3/lib/python3.4/site-packages/sleekxmpp/basexmpp.py", line 246, in process
return XMLStream.process(self, *args, **kwargs)
File "/home/errbot/err3/lib/python3.4/site-packages/sleekxmpp/xmlstream/xmlstream.py", line 1454, in process
self._process()
File "/home/errbot/err3/lib/python3.4/site-packages/sleekxmpp/xmlstream/xmlstream.py", line 1513, in _process
self.disconnect()
File "/home/errbot/err3/lib/python3.4/site-packages/sleekxmpp/xmlstream/xmlstream.py", line 690, in disconnect
args=(reconnect, wait, send_close))
File "/home/errbot/err3/lib/python3.4/site-packages/sleekxmpp/thirdparty/statemachine.py", line 70, in transition
func=func, args=args, kwargs=kwargs)
File "/home/errbot/err3/lib/python3.4/site-packages/sleekxmpp/thirdparty/statemachine.py", line 111, in transition_any
return_val = func(*args,**kwargs) if func is not None else True
File "/home/errbot/err3/lib/python3.4/site-packages/sleekxmpp/xmlstream/xmlstream.py", line 738, in _disconnect
self.event('disconnected', direct=True)
File "/home/errbot/err3/lib/python3.4/site-packages/sleekxmpp/xmlstream/xmlstream.py", line 1173, in event
handler[0](out_data)
File "/home/errbot/err3/lib/python3.4/site-packages/errbot/backends/xmpp.py", line 504, in disconnected
self.disconnect_callback()
File "/home/errbot/err3/lib/python3.4/site-packages/errbot/core.py", line 646, in disconnect_callback
self.plugin_manager.deactivate_all_plugins()
File "/home/errbot/err3/lib/python3.4/site-packages/errbot/plugin_manager.py", line 409, in deactivate_all_plugins
self.deactivatePluginByName(name, BOTPLUGIN_TAG)
File "/home/errbot/err3/lib/python3.4/site-packages/yapsy/PluginManager.py", line 580, in deactivatePluginByName
plugin_to_deactivate.deactivate()
File "/home/errbot/plugins/icinga2bot.py", line 205, in deactivate
self.thread.join()
File "/usr/lib64/python3.4/threading.py", line 1060, in join
self._wait_for_tstate_lock()
File "/usr/lib64/python3.4/threading.py", line 1076, in _wait_for_tstate_lock
elif lock.acquire(block, timeout):
KeyboardInterrupt
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "./err3/bin/errbot", line 11, in <module>
sys.exit(main())
File "/home/errbot/err3/lib/python3.4/site-packages/errbot/cli.py", line 318, in main
bootstrap(backend, root_logger, config, restore)
File "/home/errbot/err3/lib/python3.4/site-packages/errbot/bootstrap.py", line 196, in bootstrap
bot.serve_forever()
File "/home/errbot/err3/lib/python3.4/site-packages/errbot/backends/xmpp.py", line 530, in serve_forever
self.disconnect_callback()
File "/home/errbot/err3/lib/python3.4/site-packages/errbot/core.py", line 646, in disconnect_callback
self.plugin_manager.deactivate_all_plugins()
File "/home/errbot/err3/lib/python3.4/site-packages/errbot/plugin_manager.py", line 409, in deactivate_all_plugins
self.deactivatePluginByName(name, BOTPLUGIN_TAG)
File "/home/errbot/err3/lib/python3.4/site-packages/yapsy/PluginManager.py", line 580, in deactivatePluginByName
plugin_to_deactivate.deactivate()
File "/home/errbot/plugins/icinga2bot.py", line 205, in deactivate
self.thread.join()
File "/usr/lib64/python3.4/threading.py", line 1060, in join
self._wait_for_tstate_lock()
File "/usr/lib64/python3.4/threading.py", line 1076, in _wait_for_tstate_lock
elif lock.acquire(block, timeout):
KeyboardInterrupt