Skip to content

Commit

Permalink
Implemented task poller to notify users about task completion
Browse files Browse the repository at this point in the history
  • Loading branch information
br0ziliy committed Jun 3, 2016
1 parent 303fee0 commit 8125173
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
36 changes: 35 additions & 1 deletion ansible.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# -*- coding: utf8 -*-

from errbot import BotPlugin, arg_botcmd
from os import path
from lib import utils,tasks
Expand All @@ -7,6 +9,15 @@ class Ansible(BotPlugin):
Err plugin to run Ansible commands/playbooks
"""

def activate(self):
"""
Plugin "constructor", triggers on plugin activation
"""

# array of task UUIDs for tasks.task_poller() to watch
super(Ansible, self).activate()
self.start_poller(5, self.task_poller)

def get_configuration_template(self):
"""
Defines the configuration structure this plugin supports
Expand Down Expand Up @@ -92,4 +103,27 @@ def task_info(self, mess, uuid=None):

if not uuid:
return "Listing all jobs not implemented yet, please specify UUID of a job"
return tasks.get_task_info(uuid)
(result, status) = tasks.get_task_info(uuid)
if result:
return "Task {} status: {}\n\n{}".format(uuid, status, result)
else: return "Task {} status: {}".format(uuid, status)

def task_poller(self):
"""
Polls for in-progress tasks to notify users about task completion
"""

self.log.debug("Polling for completed tasks...")
if 'tasks' not in self:
self['tasks'] = []
self.log.debug("Task list: {}".format(self['tasks']))
tasklist = self['tasks']
for uuid in tasklist.keys():
author = tasklist[uuid]
(result, status) = tasks.get_task_info(uuid)
self.log.debug("Processing task: {}; status: {},\
result:\n{}".format(uuid, status, result))
if status in ['finished', 'failed']:
self.send(author, "Task {} status: {}\n\n{}".format(uuid, status, result))
del tasklist[uuid]
self['tasks'] = tasklist
13 changes: 8 additions & 5 deletions lib/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ def run_task(bot, cmd, _from):
bot will be blocked until a command returns.
"""

bot.log.info("Running {}".format(cmd))
bot.log.debug("Running {}".format(cmd))
async = True
try:
task = q.enqueue(check_output, cmd, stderr=STDOUT)
tasklist = bot['tasks']
tasklist[task.get_id()] = _from
bot['tasks'] = tasklist
bot.log.debug("Task list: {}".format(bot['tasks']))
return "Task enqueued: {}".format(task)
except ConnectionError:
bot.log.error("Error connecting to Redis, falling back to synchronous execution")
Expand All @@ -40,10 +44,7 @@ def get_task_info(uuid):
task = q.fetch_job(uuid)
res = task.result
status = task.status
if res:
return "Task {}: {}\n\n{}".format(uuid, status, res)
else:
return "Task {} is still running".format(uuid)
return (res, status)

def handle_task_exception(task, exc_type, exc_value, traceback):
"""
Expand All @@ -56,3 +57,5 @@ def handle_task_exception(task, exc_type, exc_value, traceback):
task_id = task.get_id()
r = task.connection
r.hset("rq:job:{}".format(task_id),'result',dumps(output))


0 comments on commit 8125173

Please sign in to comment.