From 4541dd0ebb83ceb90c4bcb6ac382c88a94aecb54 Mon Sep 17 00:00:00 2001 From: Thorsten Date: Wed, 22 Jun 2016 22:13:51 +0200 Subject: [PATCH] systemd support and separate message queues per room --- deploy/deploy.yml | 80 +++++++++++++++++++++++++++++++--- deploy/requirements-deploy.yml | 2 + deploy/urlbug@.service | 12 +++++ plugins/commands.py | 10 ++--- plugins/translate.py | 2 + urlbot.py | 11 ++--- 6 files changed, 101 insertions(+), 16 deletions(-) create mode 100644 deploy/requirements-deploy.yml create mode 100644 deploy/urlbug@.service diff --git a/deploy/deploy.yml b/deploy/deploy.yml index 712ed3c..4aa719b 100644 --- a/deploy/deploy.yml +++ b/deploy/deploy.yml @@ -20,6 +20,7 @@ vars: - botrepo: http://aero2k.de/t/repos/urlbot-native.git - pypi_mirror: http://pypi.fcio.net/simple/ + - systemd: true tasks: - include_vars: credentials.yml tags: [render_config] @@ -27,6 +28,7 @@ shell: virtualenv -p python3 --system-site-packages ~/botenv creates=~/botenv/bin/activate - name: virtualenv for supervisord shell: virtualenv -p python2 ~/svenv creates=~/svenv/bin/activate + when: not systemd - name: clone repository git: repo="{{botrepo}}" dest=~/urlbot force=yes update=yes register: source_code @@ -34,6 +36,7 @@ pip: requirements="~/urlbot/requirements.txt" virtualenv=~/botenv extra_args="-i {{pypi_mirror}}" - name: install supervisor pip: name=supervisor virtualenv=~/svenv extra_args="-i {{pypi_mirror}}" + when: not systemd - name: set configuration lineinfile: dest=~/urlbot/local_config.ini create=yes line="{{item.key}} = {{item.value}}" regexp="^{{item.key}}.=" @@ -60,29 +63,94 @@ - name: create supervisor config copy: src=supervisord.conf dest=~/supervisord.conf register: supervisord + when: not systemd + + - name: create directory for systemd unit file + shell: mkdir -p ~/.config/systemd/user/ creates=~/.config/systemd/user/ + when: systemd + + - name: create unitfile + copy: src=urlbug@.service dest=~/.config/systemd/user/urlbug@.service + when: systemd + register: unitfile + + # crapshit does not work + - name: reload unitfiles + become: true + shell: systemctl daemon-reload + when: unitfile.changed + ignore_errors: true + + - name: enable services + shell: "systemctl --user enable urlbug@{{item}}.service" + with_items: + - idlebot + - urlbot + when: systemd - name: verify supervisor running shell: nc -z 127.0.0.1 9004; echo $? executable=/bin/bash register: supervisor_running changed_when: false + when: not systemd - name: start supervisord shell: source ~/svenv/bin/activate && supervisord executable=/bin/bash register: start_supervisor - when: supervisord.changed or supervisor_running.stdout == "1" + when: + - not systemd + - supervisord.changed or supervisor_running.stdout == "1" #changed_when: "'already listening' not in start_supervisor.stdout" - name: activate supervisord changes - when: supervisord.changed shell: source ~/svenv/bin/activate && supervisorctl reload executable=/bin/bash + when: + - not systemd + - supervisord.changed - name: idlebot started supervisorctl: name=idlebot state=restarted supervisorctl_path=~/svenv/bin/supervisorctl - when: (source_code.changed or urlbot_config.changed) and not supervisord.changed + when: + - not systemd + - (source_code.changed or urlbot_config.changed) and not supervisord.changed - - pause: seconds=30 - when: (source_code.changed or urlbot_config.changed) and not supervisord.changed + # following tasks are workaround for missing ansible systemd-user support + - name: get systemd unit status + shell: systemctl --user status urlbug.slice + register: systemd_unit_status + + - debug: var=systemd_unit_status + - debug: msg="{{'{{item}}.service' not in systemd_unit_status.stdout}}" + with_items: + - idlebot + - urlbot + + - name: bots started + shell: "systemctl --user start urlbug@{{item}}.service && sleep 20" + with_items: + - idlebot + - urlbot + when: systemd and '{{item}}.service' not in systemd_unit_status.stdout + register: started_bots + + - debug: var=started_bots + + - name: bots restarted + shell: "systemctl --user restart urlbug@{{item}}.service && sleep 10" + with_items: + - idlebot + - urlbot + when: + - systemd + - source_code.changed or urlbot_config.changed + + - pause: seconds=20 + when: + - not systemd + - (source_code.changed or urlbot_config.changed) and not supervisord.changed - name: urlbot started supervisorctl: name=bot state=restarted supervisorctl_path=~/svenv/bin/supervisorctl - when: (source_code.changed or urlbot_config.changed) and not supervisord.changed + when: + - not systemd + - (source_code.changed or urlbot_config.changed) and not supervisord.changed diff --git a/deploy/requirements-deploy.yml b/deploy/requirements-deploy.yml new file mode 100644 index 0000000..7547399 --- /dev/null +++ b/deploy/requirements-deploy.yml @@ -0,0 +1,2 @@ +ansible +markupsafe diff --git a/deploy/urlbug@.service b/deploy/urlbug@.service new file mode 100644 index 0000000..7c8f923 --- /dev/null +++ b/deploy/urlbug@.service @@ -0,0 +1,12 @@ +[Unit] +Description=jabber bot entertaining and supporting activity on jabber MUCs + +[Service] +ExecStart=/home/jabberbot/botenv/bin/python3 /home/jabberbot/urlbot/%i.py +WorkingDirectory=/home/jabberbot/urlbot/ +StandardOutput=journal+console +StandardError=journal+console +Restart=always + +[Install] +WantedBy=multi-user.target diff --git a/plugins/commands.py b/plugins/commands.py index 5d9c34f..898cf06 100644 --- a/plugins/commands.py +++ b/plugins/commands.py @@ -801,12 +801,12 @@ def raise_an_error(argv, **args): raise RuntimeError("Exception for debugging") -@pluginfunction('repeat', 'repeat the last message', ptypes.COMMAND, enabled=False) +@pluginfunction('repeat', 'repeat the last message', ptypes.COMMAND) def repeat_message(argv, **args): - return {'msg': 'disabled until channel separation'} - return { - 'msg': args['stack'][-1]['body'] - } + if args['stack']: + return { + 'msg': args['stack'][-1]['body'] + } @pluginfunction('isdown', 'check if a website is reachable', ptypes.COMMAND) diff --git a/plugins/translate.py b/plugins/translate.py index 701faea..dcdbda4 100644 --- a/plugins/translate.py +++ b/plugins/translate.py @@ -22,6 +22,8 @@ def translate(argv, **args): if not api_key: return message_stack = args['stack'] + if not message_stack[-1]: + return last_message = message_stack[-1]['body'] data = { 'q': last_message, diff --git a/urlbot.py b/urlbot.py index a1f3a43..96ec0d3 100755 --- a/urlbot.py +++ b/urlbot.py @@ -7,6 +7,8 @@ import re import shlex import sys import time +from collections import deque + from lxml import etree import requests @@ -34,7 +36,7 @@ class UrlBot(IdleBot): self.hist_ts = {p: [] for p in rate_limit_classes} self.hist_flag = {p: True for p in rate_limit_classes} - self.message_stack = [] + self.message_stack = {str(room): deque(maxlen=5) for room in self.rooms} self.add_event_handler('message', self.message) self.priority = 100 @@ -196,9 +198,8 @@ class UrlBot(IdleBot): except Exception as e: self.logger.exception(e) finally: - if len(self.message_stack) > 4: - self.message_stack.pop(0) - self.message_stack.append(msg_obj) + if msg_obj['from'].bare in self.rooms: + self.message_stack[msg_obj['from'].bare].append(msg_obj) def handle_muc_online(self, msg_obj): """ @@ -268,7 +269,7 @@ class UrlBot(IdleBot): reply_user=reply_user, msg_obj=msg_obj, argv=words[2:] if len(words) > 1 else [], - stack=self.message_stack + stack=self.message_stack.get(msg_obj['from'].bare, []) ) if ret: