Skip to content

Commit

Permalink
Cleaned up code slightly and implemented a bot restart command.
Browse files Browse the repository at this point in the history
  • Loading branch information
pachuco authored and pachuco committed Jul 15, 2016
1 parent 335a564 commit 3b7dcad
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
10 changes: 5 additions & 5 deletions sopel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def _version_info(version=__version__):
version_info = _version_info()


def run(config, pid_file, daemon=False):
def run(config, daemon=False):
import sopel.bot as bot
import sopel.logger
from sopel.tools import stderr
Expand Down Expand Up @@ -90,14 +90,14 @@ def signal_handler(sig, frame):
logfile.write(trace)
logfile.write('----------------------------------------\n\n')
logfile.close()
os.unlink(pid_file)
os._exit(1)
return 1

if not isinstance(delay, int):
break
if p.restart:
return -1
if p.hasquit:
break
stderr('Warning: Disconnected. Reconnecting in %s seconds...' % delay)
time.sleep(delay)
os.unlink(pid_file)
os._exit(0)
return 0
7 changes: 7 additions & 0 deletions sopel/irc.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def __init__(self, config):
self.ca_certs = ca_certs
self.enabled_capabilities = set()
self.hasquit = False
self.restart = False

self.sending = threading.RLock()
self.writing_lock = threading.Lock()
Expand Down Expand Up @@ -175,6 +176,12 @@ def initiate_connect(self, host, port):
print('KeyboardInterrupt')
self.quit('KeyboardInterrupt')

def restart(self, message):
"""Disconnect from IRC and restart the bot."""
self.write(['QUIT'], message)
self.restart = True
self.hasquit = True

def quit(self, message):
"""Disconnect from IRC and close the bot."""
self.write(['QUIT'], message)
Expand Down
11 changes: 11 additions & 0 deletions sopel/modules/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ def part(bot, trigger):
else:
bot.part(channel)

@sopel.module.require_privmsg
@sopel.module.require_owner
@sopel.module.commands('restart')
@sopel.module.priority('low')
def restart(bot, trigger):
"""Restart the bot. This is an owner-only command."""
quit_message = trigger.group(2)
if not quit_message:
quit_message = 'Restart on command from %s' % trigger.nick

bot.restart(quit_message)

@sopel.module.require_privmsg
@sopel.module.require_owner
Expand Down
11 changes: 9 additions & 2 deletions sopel/run_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,16 @@ def main(argv=None):
pid_file.write(str(os.getpid()))

# Step Five: Initialise And Run sopel
run(config_module, pid_file_path)
ret = run(config_module)
os.unlink(pid_file_path)
if ret == -1:
print(sys.argv)
os.execv(sys.executable, ['python'] + sys.argv)
else:
os._exit(ret)

except KeyboardInterrupt:
print("\n\nInterrupted")
os._exit(1)
if __name__ == '__main__':
main()
main(sys.argv)

0 comments on commit 3b7dcad

Please sign in to comment.