Skip to content

Commit 7382c36

Browse files
authored
Merge pull request CloudBotIRC#127 from linuxdaemon/gonzobot+periodic-hook-fix
Properly cancel the periodic hooks
2 parents c4b8123 + 3774118 commit 7382c36

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

cloudbot/bot.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ def run(self):
130130
self.loop.run_until_complete(self._init_routine())
131131
# Wait till the bot stops. The stopped_future will be set to True to restart, False otherwise
132132
restart = self.loop.run_until_complete(self.stopped_future)
133+
self.loop.run_until_complete(self.plugin_manager.unload_all())
133134
self.loop.close()
134135
return restart
135136

cloudbot/plugin.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ def load_all(self, plugin_dir):
112112
# Load plugins asynchronously :O
113113
yield from asyncio.gather(*[self.load_plugin(path) for path in path_list], loop=self.bot.loop)
114114

115+
@asyncio.coroutine
116+
def unload_all(self):
117+
yield from asyncio.gather(
118+
*[self.unload_plugin(path) for path in self.plugins.keys()], loop=self.bot.loop
119+
)
120+
115121
@asyncio.coroutine
116122
def load_plugin(self, path):
117123
"""
@@ -173,10 +179,10 @@ def load_plugin(self, path):
173179
self.plugins[plugin.file_name] = plugin
174180

175181
for periodic_hook in plugin.periodic:
176-
asyncio.async(self._start_periodic(periodic_hook))
182+
task = asyncio.async(self._start_periodic(periodic_hook))
183+
plugin.tasks.append(task)
177184
self._log_hook(periodic_hook)
178185

179-
180186
# register commands
181187
for command_hook in plugin.commands:
182188
for alias in command_hook.aliases:
@@ -249,6 +255,9 @@ def unload_plugin(self, path):
249255
# get the loaded plugin
250256
plugin = self.plugins[file_name]
251257

258+
for task in plugin.tasks:
259+
task.cancel()
260+
252261
# unregister commands
253262
for command_hook in plugin.commands:
254263
for alias in command_hook.aliases:
@@ -502,6 +511,7 @@ def __init__(self, filepath, filename, title, code):
502511
:type filename: str
503512
:type code: object
504513
"""
514+
self.tasks = []
505515
self.file_path = filepath
506516
self.file_name = filename
507517
self.title = title

0 commit comments

Comments
 (0)